Android Studio + Gradle: java.lang.IllegalArgumentException

I have a project with Gradle and declare my build.gradle dependencies:

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' 

}

Building with Gradle works fine, but when I launch my project at compile time, an error occurs:

 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) 

I am using Gradle 1.8.

+1
source share
2 answers

It appears that several libraries include core library files; I get a slightly different exception when I make an example, but that is the same reason. If I open the External Libraries tab to see which banks they use, I see spring-android-core and spring -core , and if I open them to see which classes in them, I see that both contain org.springframework.core.ErrorCoded (which is a duplicate class in my test case).

Project view showing added Spring libraries

You do not include spring -core directly; it is transitively dependent on the spring -android-auth library (if I include only these two libraries and omit spring -android-rest-template I still get the error). I tried to break through the definitions of the pom file in Maven Central to try to prove why this happens, but I'm not sure I can give you an explanation that did not have many holes, so I will be silent on this front ;-) But I will not lack understanding to try to fix the problem. If you specify the spring -android-auth dependency to exclude spring -core , this does the trick:

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

I also ran into this error:

 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 

so I had to follow the instructions in Android Gradle plugin 0.7.0: “duplicate files during APK packaging” to exclude some META-INF files / files from packaging, and I added:

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

Scott Bartha's answer is exactly correct. Here is another way to globally exclude specific Spring modules in the build.gradle file. Like any solution that has global effects, it should be used with caution.

 configurations.compile { exclude module: 'spring-core' exclude module: 'spring-web' exclude module: 'commons-logging' } 
0
source

Source: https://habr.com/ru/post/973733/


All Articles