Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle won't exclude a module as requested

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?

like image 415
aez Avatar asked Sep 23 '14 11:09

aez


People also ask

What is exclude in 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 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.

How do I force a dependency in Gradle?

If the project requires a specific version of a dependency on a configuration-level then it can be achieved by calling the method ResolutionStrategy. force(java. lang. Object[]).

How do I skip a task in Gradle?

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. As a result, the test sources aren't compiled, and therefore, aren't executed.


2 Answers

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'
}
like image 162
aez Avatar answered Oct 17 '22 07:10

aez


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.

like image 22
yvolk Avatar answered Oct 17 '22 06:10

yvolk