Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude multiple groups when using dependencies with gradle

Tags:

gradle

groovy

Just like this code:

dependencies {
    compile ('com.wdullaer:materialdatetimepicker:3.2.2') {
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'design'
    }
}

In an android app build.gradle file, when I want to dependency a remote library, how to use exclude group syntax to exclude multiple groups?

Although the code above is a right way, but it`s a little bit complicated.Is there a simpler way?

like image 845
YiFeng Avatar asked Jun 04 '17 06:06

YiFeng


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 tasks in Gradle?

Using Command Line Flags Task :compileTestJava > Task :processTestResources NO-SOURCE > Task :testClasses > Task :test > ... To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build.

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.

What is CompileOnly in Gradle?

compileOnly dependencies are available while compiling but not when running them. This is equivalent to the provided scope in maven. It means that everyone who wants to execute it needs to supply a library with all classes of the CompileOnly library.


2 Answers

Well basically 'exclude' is just a method belongs to 'ModuleDependency' class which accepts a 'Map' of 'group' and 'module' and there's no way to pass more.

However you can use 'Groovy' power in this case and for each 'group' from list to call method 'exclude' on 'ModuleDependency' and to pass current 'group'. Take a look at approximate code below.

compile() { dep ->
    [group1, group2].each{ group -> dep.exclude group: group }
}
like image 134
webdizz Avatar answered Oct 02 '22 15:10

webdizz


Example is here:

compile group: 'org.jitsi', name: 'libjitsi', version: '1.0-9-g4b85531', {
    [new Tuple('org.opentelecoms.sdp', 'sdp-api'),
     new Tuple('junit', 'junit')].each {
        exclude group: "${it.get(0)}", module: "${it.get(1)}"
    }
}
like image 23
Alexander Ryabov Avatar answered Oct 02 '22 16:10

Alexander Ryabov