How to exclude multiple groups when using dependencies with gradle

Viewed 9338

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?

3 Answers

Using this solution with Groovy combinations (Cartesian product) to exclude some list from all configurations:

/**
 * These artifacts must be excluded from all configurations and dependencies.
 */
final excludedModules = [
    'module-to-exclude-1', 'module-to-exclude-2'
]


/**
 * Exclude dependencies from all configurations.
 * This configuration solves an issue 
 * when the same transitive dependency is included by different libraries.
 */
configurations {
    [all, excludedModules].combinations { config, moduleToExclude ->
        config.exclude module: moduleToExclude
    }
}

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)}"
    }
}
Related