Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude .jar from compile in Android Studio with Gradle

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?

like image 518
Joakim Engstrom Avatar asked Nov 19 '13 12:11

Joakim Engstrom


People also ask

How do I exclude in dependencies build Gradle?

Option 1) per-dependency exclude rules. When you specify a dependency in your build script, you can provide an exclude rule at the same time telling Gradle not to pull in the specified transitive dependency. For example, say we have a Gradle project that depends on Google's Guava library, or more specifically com.

How do I exclude a particular class in Gradle?

Just specify the group , the module and the version , like you do for dependencies. If you just want to exclude one class for a dependency jar, take a look at the jar jar links tool and its Gradle plugin. It allows you to alter included jars, e.g. to change packages or remove classes.

How do you resolve transitive dependencies in Gradle?

A variant of a component can have dependencies on other modules to work properly, so-called transitive dependencies. Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically.


1 Answers

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'
}
like image 172
Sergii Pechenizkyi Avatar answered Nov 03 '22 23:11

Sergii Pechenizkyi