2014-03-07 62 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' 
} 
相關問題