7

I currently have something like this in the build.gradle file.

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

    }
}

A problem arises since both jUnit and hamcrest-core are present in the com.xxx:xxx maven repository, creating an error like this:

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 is there a way to actually exclude the jar that is: hamcrest-core-1.3.jar Or exclude all .txt files, or exclude jUnit all together from the maven repository (it's not used).

Any other ideas that could be helpful?

Joakim Engstrom
  • 6,243
  • 12
  • 48
  • 67

1 Answers1

15

Yes, you can exclude transitive dependencies:

In your case this would 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'
}
Sergii Pechenizkyi
  • 22,227
  • 7
  • 60
  • 71
  • This worked for me, with a slight change. I had to leave out the group. So my exclude line looked like this: `exclude module: 'support-v4'` – Eric Engel May 19 '15 at 01:41
  • What version of gradle and the android gradle wrapper provides the "exclude" function? – Daniel Baughman Jun 03 '15 at 18:11
  • @DanielBaughman I believe the issue you are facing is this: http://stackoverflow.com/questions/23172458/unsupported-gradle-dsl-method-found-exclude – Abraham Philip Jun 06 '15 at 11:35
  • @AbrahamPhilip I believe you are correct. I think in my case I need to use the latter syntax and I kept trying to use the former. – Daniel Baughman Jun 10 '15 at 19:12
  • is there any right solution.i am getting same problem java.util.zip.ZipException: duplicate entry: com/google/gson/annotations/Expose.class please help me. i put like this configurations{ all*.exclude module: 'gson-2.1' } – RamBabu Pudari Jun 24 '15 at 11:01
  • The right solution is to understand why that file is duplicated. Which dependencies have it and how to include only one of it. Typically this is done by searching if there is a known solution or build dependency graph manually yourself and inspect if any of dependency has fatjar which results in sources included twice. – Sergii Pechenizkyi Jun 24 '15 at 11:58