Android Studio + Gradle:java.lang.IllegalArgumentException

Jos*_*eph 1 android gradle android-studio spring-android

我有我的Gradle项目,并在build.gradle上声明我的依赖项:

dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
compile 'org.springframework.android:spring-android-auth:1.0.1.RELEASE'
compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
compile 'org.roboguice:roboguice:2.0'
Run Code Online (Sandbox Code Playgroud)

}

使用Gradle构建工作正常,但是当运行我的项目时,在编译阶段抛出以下错误:

Gradle: UNEXPECTED TOP-LEVEL EXCEPTION:
Gradle: java.lang.IllegalArgumentException: already added: Lorg/springframework/util/FileCopyUtils;
Gradle: at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:122)
Gradle: at com.android.dx.dex.file.DexFile.add(DexFile.java:161)
Run Code Online (Sandbox Code Playgroud)

我使用Gradle 1.8.

Sco*_*rta 5

看起来多个库包含核心库文件; 当我制作一个示例案例时,我得到了一个稍微不同的异常,但这是相同的原因.如果我打开外部库选项卡来查看它正在使用的jar,我看到spring-android-corespring-core,如果我打开它们以查看它们中的类,我看到它们都包含org.springframework.core.ErrorCoded(这是我的测试用例中的重复类).

项目视图显示添加的Spring库

你不是直接包括spring-core ; 它是来自spring-android-auth库的传递依赖(如果我只包含那两个库并省略spring-android-rest-template我仍然会得到错误).我尝试挖掘Maven Central中的pom文件定义以试图证明它为什么会发生,但我不确定我能给你一个没有很多漏洞的解释,所以我会保持安静前面;-)但我不会缺乏理解妨碍尝试解决问题.如果你告诉spring-android-auth依赖项排除spring-core,那就可以了:

dependencies {
    ...
    compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
    compile('org.springframework.android:spring-android-auth:1.0.1.RELEASE') {
        exclude module: 'spring-core'
    }
    compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
}
Run Code Online (Sandbox Code Playgroud)

我也遇到了这个错误:

Execution failed for task ':app:packageDebug'.
> Duplicate files copied in APK META-INF/notice.txt
File 1: /Users/sbarta/.gradle/caches/modules-2/files-2.1/org.springframework.android/spring-android-auth/1.0.1.RELEASE/f43faebbf90aef324979a81a4f5eee1e3b95191f/spring-android-auth-1.0.1.RELEASE.jar
File 2: /Users/sbarta/.gradle/caches/modules-2/files-2.1/org.springframework.android/spring-android-auth/1.0.1.RELEASE/f43faebbf90aef324979a81a4f5eee1e3b95191f/spring-android-auth-1.0.1.RELEASE.jar
Run Code Online (Sandbox Code Playgroud)

所以我不得不遵循Android Gradle插件0.7.0中的说明:"在打包APK期间重复文件"以从包装中排除一些META-INF /文件,我补充说:

android {
    ...
    packagingOptions {
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
    }
}
Run Code Online (Sandbox Code Playgroud)