How can I force Gradle to set the same version for

2019-01-13 00:53发布

I use the following two dependencies:

compile 'com.google.guava:guava:14.0.1'
compile 'com.google.guava:guava-gwt:14.0.1'

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'.
+--- com.google.guava:guava:14.0.1 -> 17.0
+--- com.google.guava:guava-gwt:14.0.1
|    +--- com.google.code.findbugs:jsr305:1.3.9
|    \--- com.google.guava:guava:14.0.1 -> 17.0 

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

7条回答
The star\"
2楼-- · 2019-01-13 01:06

I had a similar situation where one of the dependencies used spring-web 4.2.4 which was broken. You have to force specific library version you want. As mentioned in another comment, it might cause compatibility issues but sometimes is necessary.

Least intrusive way of forcing a library version I found was instead of using

compile "org.springframework:spring-web:4.2.3.RELEASE"

specifying dependency configuration as forced:

compile("org.springframework:spring-web:4.2.3.RELEASE"){
    force = true
}

I used it when I needed to downgrade Spring version temporarily (until next release).

查看更多
小情绪 Triste *
3楼-- · 2019-01-13 01:06

I would suggest against setting transitive = false, as you from this moment you would have to resolve the depency tree yourself manually.

You could either force the desired guava version via configurations.all, or add the depency explicitely and set it forced = true.

Examples here: http://www.devsbedevin.com/android-understanding-gradle-dependencies-and-resolving-conflicts/

查看更多
爷的心禁止访问
4楼-- · 2019-01-13 01:08

If it's OK to just use the newer version for both dependencies, the simplest way to fix your problem is to update your dependencies:

compile 'com.google.guava:guava:17.0'
compile 'com.google.guava:guava-gwt:17.0'

That will make sure both of them are on 17.0. It's simpler than trying to force both of them on the older version and as an added bonus you get a newer version, which (probably) comes with bug fixes and new features.

To be fair @Klunk mentions this in his answer, by asking "Can you not just use the 17.0 version as your dependency?", but it's just in passing and easy to miss, so I thought it made sense to post as a separate answer.

查看更多
小情绪 Triste *
5楼-- · 2019-01-13 01:09
configurations.all {
  resolutionStrategy {  
    eachDependency { DependencyResolveDetails details ->
      if (details.requested.group == 'com.google.guava') {
        details.useVersion "14.0.1"
      }
    }
  }
}

dependencies {
  compile 'com.google.guava:guava'
  compile 'com.google.guava:guava-gwt'
}
查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-13 01:10

Alternatively you can use dependencySets (or mavenBom when BOM POM is available) support in spring-dependency-management Gradle plugin. Note that this plugin is also automatically applied with spring-boot Gradle plugin. For more details see here.

plugins {
  id 'io.spring.dependency-management' version '1.0.1.RELEASE'
}

dependencyManagement {
  dependencies {
    dependencySet(group: 'com.google.guava', version: '14.0.1') {
      entry 'guava'
      entry 'guava-gwt'
    }
  }
}

dependencies {
  compile 'com.google.guava:guava'
  compile 'com.google.guava:guava-gwt'
}
查看更多
Explosion°爆炸
7楼-- · 2019-01-13 01:11
configurations.all {
    resolutionStrategy { 
        force 'com.google.guava:guava:14.0.1'
        force 'com.google.guava:guava-gwt:14.0.1'
    }
}
查看更多
登录 后发表回答