如何强制 Gradle 为两个依赖项设置相同的版本? [英] How can I force Gradle to set the same version for two dependencies?

查看:81
本文介绍了如何强制 Gradle 为两个依赖项设置相同的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下两个依赖项:

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 dependencies 发现了这个:

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 

如何强制 Gradle 为这两个依赖项设置相同的版本?

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

推荐答案

您的依赖项之一是强制更新番石榴版本.使用 gradle dependencies 来定位哪个库正在驱逐你的版本.

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 中维护单独的版本号,而是使用了一个 dependencies.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 时,我只需要更改dependencies.gradle.

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

我也非常喜欢将传递依赖设置为 false

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 为两个依赖项设置相同的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆