问题描述
假设我已在我的应用程序中添加了Facebook
和Twitter
依赖项.
Let say I have added Facebook
and Twitter
dependencies in my app.
com.twitter.sdk.android:twitter:2.1.0
com.twitter.sdk.android:twitter:2.1.0
当我看着Gradle树时,他们提出了许多其他可传递依赖项.
When i look at Gradle tree, They come up with bunch of other transitive dependencies.
现在,如果Facebook使用com.android.support:support-annotations:24.1.1
,而twitter使用com.android.support:support-annotations:25.0.3
Now If Facebook uses com.android.support:support-annotations:24.1.1
and twitter uses com.android.support:support-annotations:25.0.3
然后gradle将使用哪个版本.
Then which version gradle will use.
在gradle树中,它在较早版本的依赖项前面显示->
.我了解到,这意味着gradle将使用较新的版本,而不会使用较旧的版本.
In gradle tree, It shows ->
in front of older version of dependency. I learnt that this means gradle will use the newer version, and will not use the older version.
但这可能是一个问题,因为某些库是要在特定版本上运行的,而我已经遇到了这个问题.
But this can be a problem, because some libraries are meant to run on the specific versions, and i have faced this problem.
在文章之一中,我发现了npm
如何管理这些内容依赖项冲突,但是我仍然不确定gradle如何在同一个库的不同版本之间进行管理.
In one of article i found out how npm
manages these dependencies conflicts, but i am still unsure how gradle will manage between different version of same library.
推荐答案
在apk中,同一库不能有不同版本.
You can't have different versions of the same library inside an apk.
正如您提到的,默认情况下,Gradle将最新版本放入构建.如果要指定项目中应使用的库的具体版本,则可以添加直接的compile
(对于Android Gradle Plugin v3 +,则为implementation
/api
)语句,以及所需的版本以获取一个
As you mentioned, by default, Gradle puts the newest one to the build. If you want to specify a concrete version of the library that should be used in your project, you can add a direct compile
(or implementation
/ api
for Android Gradle Plugin v3+) statement with a required version to get one.
此外,您可以使用特殊语法强制版本,但以后可能会导致一些问题.您可以在此帖子中找到有关版本冲突解决的更多信息
Also, you can force version using a special syntax, but it can lead to some problems later. You can find more information about version conflicts resolution in this post
这篇关于Gradle如何在Gradle树中的同一库的多个版本之间进行选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!