Gradle won't exclude a module as requested

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'
}

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.