Exclude .jar from compilation in Android Studio using Gradle

I currently have something similar in the build.gradle file.

dependencies { compile 'com.android.support:support-v4:13.0.+' compile ('com.xxx:xxx-commons:1.+') { } } 

There is a problem because jUnit and hamcrest-core are present in the com.xxx:xxx maven repository, creating this error:

 Gradle: Origin 1: /Users/yyy/.gradle/caches/artifacts-26/filestore/junit/junit/4.11/jar/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar Gradle: Origin 2: /Users/yyy/.gradle/caches/artifacts-26/filestore/org.hamcrest/hamcrest-core/1.3/jar/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar Gradle: Execution failed for task ':android:packageDebug'. > Duplicate files copied in APK LICENSE.txt File 1: /Users/yyy/.gradle/caches/artifacts-26/filestore/junit/junit/4.11/jar/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar File 2: /Users/yyy/.gradle/caches/artifacts-26/filestore/junit/junit/4.11/jar/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar 

Since jUnit actually includes the hamcrest library these days, there is a way to actually exclude the jar, which is: hamcrest-core-1.3.jar Or exclude all .txt files or exclude jUnit together from the maven repository (it is not used).

Any other ideas that might be helpful?

+7
android android-studio android-gradle maven gradle
source share
1 answer

Yes, you can exclude transitive dependencies :

In your case, it will be:

 dependencies { compile 'com.android.support:support-v4:13.0.+' compile ("com.xxx:xxx-commons:1.+") { exclude group: 'junit', module: 'junit' } } 

or

 configurations { all*.exclude group: 'junit', module: 'junit' } 
+14
source share

All Articles