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

编程入门 行业动态 更新时间:2024-10-11 09:24:43
本文介绍了如何强制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依赖关系:

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

configurationspile { 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为两个依赖关系设置相同的版本?

本文发布于:2023-07-14 10:35:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1104125.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   版本   关系   Gradle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!