36

I can't exclude a guava module in a build.gradle file using "exclude".

With this dependency block:

dependencies {
    ...
    compile(group: 'com.google.api-client', name: 'google-api-client', version: '1.19.0') {
        exclude(group: 'com.google.guava', module: 'guava-jdk5') // !!! Doesn't work!!!
       ...
    }
...
}

I get the dependency tree below. Note that guava-jdk5 is not excluded.

+--- com.google.api-client:google-api-client:1.19.0
|    +--- com.google.oauth-client:google-oauth-client:1.19.0
|    |    +--- com.google.http-client:google-http-client:1.19.0
|    |    |    ...
|    |    \--- com.google.code.findbugs:jsr305:1.3.9
|    ...
|    \--- com.google.guava:guava-jdk5:13.0

...

Notice that the last line still includes the guava module, it has not been excluded. Why? How to exclude it?

aez
  • 2,406
  • 2
  • 26
  • 46
  • Does this answer your question? [Gradle Transitive dependency exclusion is not working as expected. (How do I get rid of com.google.guava:guava-jdk5:13.0 ?)](https://stackoverflow.com/questions/25792398/gradle-transitive-dependency-exclusion-is-not-working-as-expected-how-do-i-get) – Ilya Serbis Aug 04 '20 at 14:06

2 Answers2

34

The problem was that another compile object was also dependent on the first level dependency that had the guava-jdk5 as a transitive dependency, so this other object was bringing in the unwanted module.

I was able to finally see this using

./gradlew -q :app:dependencyInsight --dependency guava --configuration compile

and could exclude it using

configurations {
    compile.exclude module: 'guava-jdk5'
}
aez
  • 2,406
  • 2
  • 26
  • 46
  • 1
    how can we exclude particular class from package – Ajay Oct 19 '16 at 13:26
  • @Ajay you can't do that in Gradle. You can only exclude transitive dependencies, not specific classes or packages from those dependencies or transitive dependencies. – sversch Nov 17 '17 at 06:54
23

An answer by the question's author didn't help me, because it's too narrow / concrete. This is a modified approach, which works for exclusion of any module:

  1. Check currently recognized dependencies using this command in the "Terminal":

    gradlew app:dependencies
    

The command is issued from the root of your project and "app" here should be replaced with a name of your application's "module".

You may also cd inside your application's folder and launch this:

..\gradlew -q dependencies

Note: Use ../gradle on Linux.

You will see that several configurations exist, not "compile" only (e.g. "_debugCompile" etc.). Each "configuration" has its own dependencies! This is why e.g. excluding something in "compile" configuration may be not enough.

  1. Now apply exclusion to all "configurations" this way: Add this block to your application's ("module") gradle.build file (with your list of modules to exclude, of course :-) ):

    configurations {
        all {
            exclude module: 'httpclient'
            exclude module: 'httpcore'
        }
    }
    

    Tip: Advances exclusion cases are discussed e.g. here: https://discuss.gradle.org/t/how-do-i-exclude-specific-transitive-dependencies-of-something-i-depend-on/17991/7

  2. Repeat steps 1. and 2. until you don't see modules, which you want to exclude, in a list of dependencies of any configuration.

Tip: Here, inside "all" block, you may decide to put:

transitive = false

And you will get rid of all transitive dependencies.

yvolk
  • 2,391
  • 3
  • 21
  • 26
  • 1
    Shouldn't you also use `group: 'com.some.group'`? – IgorGanapolsky Aug 08 '16 at 18:12
  • 1
    @IgorGanapolsky If module doesn't fully identify an artifact to exclude, you may use group also, e.g.: exclude group: 'org.apache.httpcomponents', module: 'httpclient' – yvolk Jan 27 '19 at 07:02