confile confile - 1 year ago 127
Java Question

How can I force Gradle to set the same version for two dependencies?

I use the following two dependencies:

compile ''
compile ''

Both must be the same version to work correctly. Since my other dependencies use a higher version, Gradle uses different versions for each dependency.

I found this by running
gradle dependencies

compile - Compile classpath for source set 'main'.
+--- -> 17.0
| +---
| \--- -> 17.0

How can I force Gradle to set the same version for these two dependencies?

Answer Source

One of your dependencies is forcing the guava version to update. Use gradle --dependencies to locate which library is evicting your version.

The problem you have is that if you force it to use 14.0.1 another library may not work properly. Can you not just use the 17.0 version as your dependency?

Rather than maintain individual version numbers in the build.gradle I use a dependencies.gradle file which will have a mapping of version numbers and pull that into the build.gradle. That way I only need to maintain the single guava version. So your example will be:


ext {
    ver = [
        guava: '14.0.1'

and then in the build.gradle file you can have:

apply from: "dependencies.gradle"

dependencies {
    compile group: '', module: 'guava', version: ver.guava
    compile group: '', module: 'guava-gwt', version: ver.guava

then when I want to move to 17.0 I only need to change the dependencies.gradle.

I am also a definite fan of setting transitive dependencies to false with

configurations.compile { transitive = false }

this way you do not have some dependencies evicted at compile time, although you may have a problem at run time if the evicting library is not fully backward compatible. Lets face it if you are writing the code you should know what libraries you use and you should be explicit about your dependencies. It protects you from one of your dependencies upgrading and messing you up.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download