2014-03-07 64 views
1

我有我的摇篮项目申报上的build.gradle我的依赖关系:Android的工作室+摇篮:java.lang.IllegalArgumentException异常

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' 

}

与摇篮建立正常工作,但是当运行我的项目出现以下错误是编译阶段:

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) 

我使用Gradle 1.8。

回答

5

看起来像多个库包含核心库文件;当我做一个示例时,我会看到一个稍微不同的例外,但这是同样的原因。如果我打开外部库选项卡来查看它使用的是什么罐子,我看到spring-android-corespring-core,如果我打开它们以查看它们中的类是什么,我会看到都包含org.springframework.core.ErrorCoded(这是我的测试用例中的重复类)。

Project view showing added Spring libraries

你不包括直接弹簧核心;它作为传递依赖从spring-android-auth库(如果我只包含这两个库并省略spring-android-rest-template我仍然收到错误)。我试着通过Maven Central中的pom文件定义来试图证明它为什么会发生,但我不确定我可以给你一个没有很多漏洞的解释,所以我会保持安静那个前锋;-)但是我不会缺乏理解,试图解决这个问题。如果你告诉弹簧的Android-auth的依赖排除弹簧核心,它的伎俩:

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' 
} 

我也遇到了这个错误:

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 

,所以我不得不请按照Android Gradle plugin 0.7.0: "duplicate files during packaging of APK"的说明从包装中排除一些META-INF /文件,并且我添加了:

android { 
    ... 
    packagingOptions { 
     exclude 'META-INF/notice.txt' 
     exclude 'META-INF/license.txt' 
    } 
} 
+1

非常感谢你,@scott,很好的答案和很好的解释。 – Joseph

+0

'spring-android-core'是'spring-core'的一个分支子集,它已经针对Android进行了构建和测试。相比之下,Spring框架模块本身并没有在开发或测试Android时考虑到。对于单独的休息模板使用,一切运作顺利。然而,当使用Spring Social(以及Spring Security Crypto)时,我们需要确保排除任何支持'spring-android-core'和'spring-android-rest-template'的Spring框架依赖。 –

+0

Spring for Android GitHub README现在包含一个[示例生成配置](https://github.com/spring-projects/spring-android#example-build-configuration)。希望这会帮助其他人看到这个线程。 –

0

Scott Barta的回答完全正确。这是全局排除build.gradle中某些Spring模块的另一种方法。与任何具有全局影响的解决方案一样,应谨慎使用。

configurations.compile { 
    exclude module: 'spring-core' 
    exclude module: 'spring-web' 
    exclude module: 'commons-logging' 
} 
相关问题