others-how to exclude gradle dependencies globally in build.gradle ?

1. Purpose

In this post, I will show you how to exclude specific dependency globally in your build.gradle.



2. Solution

2.1 Exclude transitive jar dependency

Excluding a dependency in a dependency declaration:

dependencies {
    implementation('com.google.apis:google-api-services-gmail:v1-rev110-1.25.0',{
        exclude group: 'org.apache.httpcomponents'
    })
}

Excluding transitive dependency for a particular configuration:

configurations {
    implementation {
        exclude group: 'commons-collections', module: 'commons-collections'
    }
}

dependencies {
    implementation 'commons-beanutils:commons-beanutils:1.9.4'
    implementation 'com.opencsv:opencsv:4.6'
}

2.2 Exclude dependency globally in all your dependencies

Exclude in specific environment:

configurations {
    runtime.exclude group: "org.slf4j", module: "slf4j-log4j12"
    compile.exclude group: "org.slf4j", module: "slf4j-log4j12"
}

Or exclude in all environment:

configurations.all {
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}



3. Summary

In this post, I demonstrated how to exclude gradle dependency for environment or globally. That’s it, thanks for your reading.