【问题标题】:Adding kSOAP dependency to Gradle project将 kSOAP 依赖项添加到 Gradle 项目
【发布时间】:2013-09-12 21:07:27
【问题描述】:

我正在尝试使用 Gradle 让 kSOAP 在我的 Android 项目中工作。

这是我项目的 build.gradle 文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()

    maven {
        url 'http://ksoap2-android.googlecode.com/svn/m2-repo'
    }
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 18
    }
}

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile 'ch.acra:acra:4.5.0'
    compile 'com.google.guava:guava:12.0'
    compile 'com.google.code.ksoap2-android:ksoap2-android:3.0.0'
}

该库似乎包含在项目中并且编译确实可以工作,但是当我尝试导入一个类(即 SoapObject)时,它似乎甚至不存在命名空间。有趣的是,其他库(例如 ACRA 或 Guava)运行良好。我该如何解决这个问题?

【问题讨论】:

  • 这可能是 android studio 的问题(我假设你使用一个)。您是否尝试编写代码(使用 kSOAP)然后从命令行构建项目?
  • 我做了,但它根本没有编译。它在导入时告诉我“错误:包 org.ksoap2.serialization 不存在”,在第一个 SoapObject 变量定义时告诉我“错误:找不到符号”。

标签: android maven gradle ksoap2


【解决方案1】:

我正在使用@amitav13 的解决方案,但存储库的 URL 已更改:

repositories {
        maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases' }
    }

目前的版本是 3.6.0

compile 'com.google.code.ksoap2-android:ksoap2-android:3.6.0'

这确实对我有用。

【讨论】:

    【解决方案2】:

    这也花了我一些时间才弄清楚,但我终于让它工作了。我一直在研究一个为 KSoap 解析的 WSDL 解析器,最后让它工作只是为了通过 Gradle 与 ksoap 的导入进行斗争。无论如何,这就是你的做法。

    apply plugin: 'android-library'
    
    buildscript {
        repositories {
            mavenCentral()
            maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.8.+'
            classpath 'com.google.code.ksoap2-android:ksoap2-android:3.1.1'
        }
    }
    
    repositories {
        maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
    }
    
    
    android {
        compileSdkVersion 19
        buildToolsVersion "19.0.1"
    
        defaultConfig {
            minSdkVersion 15
            targetSdkVersion 19
            versionCode 1
            versionName "1.0"
        }
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    
    dependencies {
    
        compile 'com.google.code.ksoap2-android:ksoap2-android:3.1.1'
    
    }
    

    当然我的是一个服务库,所以你可能想使用应用插件:'android'。 希望这可以帮助并节省一些时间。

    【讨论】:

    • 谢谢!最后我让它与 Gradle 一起工作。我花了一些时间来修复它,但现在它可以工作了。
    • 不适合我,它可以编译但不会在我的设备上运行'ClassNotFoundException',所以我返回了 jar 文件 ksoap2-android-assembly-3.3.0-jar-with-dependencies。 jar,我不知道是不是因为依赖关系(不要这么认为,因为这是 maven 的力量)......无论如何感谢分享这个;)
    • 实际编译 'com.google.code.ksoap2-android:ksoap2-android:3.1.1' 不行!
    【解决方案3】:

    以下是 Sam 建议的适用于我的 build.gradle 的更精简版本:

    apply plugin: 'com.android.application'
    
    android {
    compileSdkVersion 21
    buildToolsVersion "20.0.0"
    
    defaultConfig {
        applicationId "com.example.test"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        repositories {
            maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases' }
        }
    }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.google.code.ksoap2-android:ksoap2-android:3.6.2'
    }
    

    注意这是app的build.gradle,我没有对项目build.gradle做任何修改。

    【讨论】:

    • @amitav13,您的示例还需要 .jar 文件吗?如果不是,你可能不使用'compile fileTree(...) 对吗?
    • @Brabbeldas 不,我没有。我把它留在那里,因为这是默认生成的构建文件,加上让 kSoap 工作的代码。
    • @amitav13 谢谢。经过几个小时的努力并尝试了其他所有答案,这就是有效的答案!但是存储库地址已更改。你应该更新那个:)
    【解决方案4】:
    repositories {
        maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
    }
    

    内部依赖

    compile 'com.google.code.ksoap2-android:ksoap2-android:3.6.1'
    

    【讨论】:

      【解决方案5】:

      我也遇到了同样的情况。我放弃了从 mavencentral 添加那个 jar。

      我将原始 jar 添加为库。

      1. 从以下位置下载最新(或任何版本)的 ksoap2-android this url
      2. 在 build.gradle 中调用它,见下文

      代码如下:

      dependencies {
         compile files('libs/ksoap2-android.jar')
      }
      

      我希望它会有所帮助。

      【讨论】:

      • 我完全放弃了 Gradle。对于小型项目,这是一个矫枉过正!
      • @AxelGeNuS,是的。我也有同样的情况。但即使对于小型项目,它也非常有效。当你有空闲时间时,你应该再试一次。 :)
      【解决方案6】:

      这些答案已经很老了,没有一个对我有用。这是我使用 Android Studio 3.1.4 和 ksoap 3.6.4 的解决方案

      安卓毕业:

      // Top-level build file where you can add configuration options common to all sub-projects/modules.
      
      buildscript {
      
          repositories {
              google()
              jcenter()
          }
          dependencies {
              classpath 'com.android.tools.build:gradle:3.1.4'
      
              // NOTE: Do not place your application dependencies here; they belong
              // in the individual module build.gradle files
          }
      }
      
      repositories {
          maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
      }
      
      allprojects {
          repositories {
              google()
              jcenter()
          }
      }
      
      task clean(type: Delete) {
          delete rootProject.buildDir
      }
      

      应用分级:

      apply plugin: 'com.android.application'
      
      android {
          compileSdkVersion 28
          defaultConfig {
              applicationId "com.myapp.aspservicetest"
              minSdkVersion 24
              targetSdkVersion 28
              versionCode 1
              versionName "1.0"
              testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
          }
          buildTypes {
              release {
                  minifyEnabled false
                  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
              }
      
              repositories {
                  maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases' }
              }
          }
      }
      
      dependencies {
          implementation fileTree(dir: 'libs', include: ['*.jar'])
          implementation 'com.android.support:appcompat-v7:28.0.0'
          implementation 'com.android.support.constraint:constraint-layout:1.1.3'
          testImplementation 'junit:junit:4.12'
          androidTestImplementation 'com.android.support.test:runner:1.0.2'
          androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
          implementation 'com.google.code.ksoap2-android:ksoap2-android:3.6.4'
      }
      

      【讨论】:

      • 终于!谢谢!
      • 一旦我从 /libs/ 目录中删除了 ksoap jar 文件的预先存在的副本,这对我有用。
      【解决方案7】:

      更简单的解决方案是从http://www.bvbcode.com/code/ed5zc936-1814251-down 下载 .jar 并将这个 .jar 文件添加到您的 libs 文件夹中。

      终于在你的 build.gradle 中:

      dependencies {
          compile fileTree(include: ['*.jar'], dir: 'libs') 
      }
      

      这对我来说很好。

      【讨论】:

        【解决方案8】:

        我知道已经有使用 jar 文件的答案。我想添加一个带有最新更新链接的链接:

        Download ksoap2-android-assembly-3.6.4-jar-with-dependencies.jar 并将其添加到文件夹 libs。

        在 build.grade 中:

        android {
        ...
            configurations {
                all {
                    exclude group: 'com.squareup.okhttp3', module: 'okhttp'
                }
            }
        }
        ...
        dependencies {
        ...
         implementation fileTree(include: '*.jar', dir: 'libs')
        ...
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-06-03
          • 2020-08-22
          • 1970-01-01
          • 2015-03-17
          • 2015-07-27
          • 2014-10-06
          • 2021-07-07
          相关资源
          最近更新 更多