问题描述
我使用以下两个依赖关系:
I use the following two dependencies:
compile 'com.google.guava:guava:14.0.1'
compile 'com.google.guava:guava-gwt:14.0.1'
相同的版本正常工作。由于我的其他依赖使用更高版本,因此Gradle对每个依赖使用不同的版本。
Both must be the same version to work correctly. Since my other dependencies use a higher version, Gradle uses different versions for each dependency.
我发现这是通过运行 gradle依赖关系
:
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
如何强制Gradle为这两个依赖关系设置相同的版本?
How can I force Gradle to set the same version for these two dependencies?
推荐答案
您的依赖关系之一是强制更新番石榴版本。使用 gradle依赖关系
来定位哪个库正在驱逐您的版本。
One of your dependencies is forcing the guava version to update. Use gradle dependencies
to locate which library is evicting your version.
您遇到的问题是,如果强制使用14.0.1另一个库可能无法正常工作。你不能只使用17.0版本作为依赖吗?
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?
而不是在build.gradle中维护个别版本号,我使用一个依赖关系的的版本号并将其拉入build.gradle。这样我只需要维护单个番石榴版本。所以你的例子将是:
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:
dependencies.gradle
dependencies.gradle
ext {
ver = [
guava: '14.0.1'
]
}
,然后在build.gradle文件中,您可以:
and then in the build.gradle file you can have:
apply from: "dependencies.gradle"
dependencies {
compile group: 'com.google.guava', module: 'guava', version: ver.guava
compile group: 'com.google.guava', module: 'guava-gwt', version: ver.guava
}
然后当我想移动到17.0时,我只需要更改依赖关系。
then when I want to move to 17.0 I only need to change the dependencies.gradle.
我也是将传递依赖关系设置为false的一个确定的粉丝。 b
$ b
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.
这篇关于如何强制Gradle为两个依赖关系设置相同的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!