脚本宝典收集整理的这篇文章主要介绍了解决 Android Gradle 依赖的各种版本问题,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
一、问题的产生
1.1 引入的支持库版本和编译版本不一致
相信大家在build.gradle
中引入各种依赖的时候,或多或少会见过一些红线,gradle
会提示你,当前的编译版本和你依赖的这个支持库的版本号不同,应该使用相同的支持库版本,来比避免编译不通过问题,类似于这种。
This support library should not use a different version (27) than the compileSdkVersion (26)
还有连锁反应会出现这种问题:所有的AndROId支持库support
版本号要一致,也是避免运行时崩溃的问题。
All com.android.support libraries must use the exact same version sPEcification (mixing versions can lead to runtime crashes). Found versions 27.1.0, 26.1.0. Examples include com.android.support:recyclerview-v7:27.1.0 and com.android.support:aniMATEd-vector-drawable:26.1.0
上面的问题,也可以在Android studio
左侧的PRoject
栏的External Libraries
中查看,可以看到 由于引入了和当前编译版本号不同的支持库所产生的问题:
1.2 第三方库中的子依赖和当前项目的编译版本不一致。
当引入一个第三方库,该库中也依赖了Android
支持库,且支持库的版本,和当前版本不一致而导致的版本冲突:
二、如何解决
解决冲突的方式包括:强制指定,排除。
2.1 查看依赖树
Gradle 默认开启了 依赖传递 意思就是 项目依赖了A,A又依赖了B和C,这时候,我们只需要写一行代码:implementation A
就行了,由传递依赖导致的冲突,默认是以最高版本的依赖为准,要想查看整个项目的依赖传递关系,使用命令:./gradlew app:dependencies --configuration releaseRuntimeClasspath
+--- com.android.support.constraint:constraint-layout:1.1.2
| --- com.android.support.constraint:constraint-layout-solver:1.1.2
+--- com.android.support:appcompat-v7:26.1.0
| +--- com.android.support:support-annotations:26.1.0 -> 27.1.1
| +--- com.android.support:support-v4:26.1.0
| | +--- com.android.support:support-compat:26.1.0 -> 27.1.1
| | | +--- com.android.support:support-annotations:27.1.1
| | | --- android.arch.lifecycle:runtime:1.1.0
| | | +--- android.arch.lifecycle:common:1.1.0
| | | --- android.arch.core:common:1.1.0
| | +--- com.android.support:support-media-compat:26.1.0
| | | +--- com.android.support:support-annotations:26.1.0 -> 27.1.1
| | | --- com.android.support:support-compat:26.1.0 -> 27.1.1 (*)
| | +--- com.android.support:support-core-utils:26.1.0 -> 27.1.1
| | | +--- com.android.support:support-annotations:27.1.1
| | | --- com.android.support:support-compat:27.1.1 (*)
| | +--- com.android.support:support-core-ui:26.1.0 -> 27.1.1
| | | +--- com.android.support:support-annotations:27.1.1
| | | +--- com.android.support:support-compat:27.1.1 (*)
| | | --- com.android.support:support-core-utils:27.1.1 (*)
| | --- com.android.support:support-fragment:26.1.0 -> 27.1.1
| | +--- com.android.support:support-compat:27.1.1 (*)
| | +--- com.android.support:support-core-ui:27.1.1 (*)
| | +--- com.android.support:support-core-utils:27.1.1 (*)
| | +--- com.android.support:support-annotations:27.1.1
| | +--- android.arch.lifecycle:livedata-core:1.1.0
| | | +--- android.arch.lifecycle:common:1.1.0
| | | +--- android.arch.core:common:1.1.0
| | | --- android.arch.core:runtime:1.1.0
| | | --- android.arch.core:common:1.1.0
| | --- android.arch.lifecycle:viewmodel:1.1.0
| +--- com.android.support:support-vector-drawable:26.1.0
| | +--- com.android.support:support-annotations:26.1.0 -> 27.1.1
| | --- com.android.support:support-compat:26.1.0 -> 27.1.1 (*)
| --- com.android.support:animated-vector-drawable:26.1.0
| +--- com.android.support:support-vector-drawable:26.1.0 (*)
| --- com.android.support:support-core-ui:26.1.0 -> 27.1.1 (*)
+--- com.android.support:recyclerview-v7:26.1.0
| +--- com.android.support:support-annotations:26.1.0 -> 27.1.1
| +--- com.android.support:support-compat:26.1.0 -> 27.1.1 (*)
| --- com.android.support:support-core-ui:26.1.0 -> 27.1.1 (*)
--- com.github.bumptech.glide:glide:4.7.1
+--- com.github.bumptech.glide:gifdecoder:4.7.1
| --- com.android.support:support-annotations:27.1.1
+--- com.github.bumptech.glide:disklrucache:4.7.1
+--- com.github.bumptech.glide:annotations:4.7.1
--- com.android.support:support-fragment:27.1.1 (*)
符号的含义:
-
x.x.x (*)
该依赖已经有了,将不再重复依赖。
-
x.x.x -> x.x.x
该依赖的版本被箭头所指的版本代替。
-
x.x.x -> x.x.x(*)
该依赖的版本被箭头所指的版本代替,并且该依赖已经有了,不再重复依赖。
2.2 Exclude 排除
排除所有:
// 在build.gradle 中添加下面节点
configuration{
all*.exclude module: "support-fragment"
}
执行结果:(部分)
--- com.github.bumptech.glide:glide:4.7.1
+--- com.github.bumptech.glide:gifdecoder:4.7.1
| --- com.android.support:support-annotations:27.1.1
+--- com.github.bumptech.glide:disklrucache:4.7.1
--- com.github.bumptech.glide:annotations:4.7.1
可以看到相比最开始的执行结果,已经将 glide
的子依赖 com.android.support:support-fragment
排除了。
排除指定:
implementation ('com.github.bumptech.glide:glide:4.7.1'){
exclude module:"support-fragment"
}
执行结果:(部分)
+--- com.android.support:appcompat-v7:26.1.0
| +--- com.android.support:support-annotations:26.1.0 -> 27.1.1
| +--- com.android.support:support-v4:26.1.0
| | +--- com.android.support:support-compat:26.1.0
| | | ...
| | +--- com.android.support:support-media-compat:26.1.0
| | | ...
| | +--- com.android.support:support-core-utils:26.1.0
| | | ...
| | +--- com.android.support:support-core-ui:26.1.0
| | | //不影响该子依赖
| | --- com.android.support:support-fragment:26.1.0
| | +--- com.android.support:support-compat:26.1.0 (*)
| | +--- com.android.support:support-core-ui:26.1.0 (*)
| | --- com.android.support:support-core-utils:26.1.0 (*)
| +--- com.android.support:support-vector-drawable:26.1.0
| | ...
| --- com.android.support:animated-vector-drawable:26.1.0
| ...
+--- com.android.support:recyclerview-v7:26.1.0
| //该依赖的子依赖被排除
--- com.github.bumptech.glide:glide:4.7.1
+--- com.github.bumptech.glide:gifdecoder:4.7.1
| --- com.android.support:support-annotations:27.1.1
+--- com.github.bumptech.glide:disklrucache:4.7.1
--- com.github.bumptech.glide:annotations:4.7.1
可以看到指定排除glide
依赖的子依赖 com.android.support:support-fragment
不影响其他依赖。
exclude 可以搭配 group
和module
使用,将会排除所有被匹配的依赖。
2.3 Force 强制指定
强制指定依赖的版本。
configurations.all {
resolutionStrategy {
force 'com.android.support:support-fragment:26.1.0'
}
}
执行结果(部分):
+--- com.android.support:appcompat-v7:26.1.0
| ...
| +--- com.android.support:support-v4:26.1.0
| | +--- com.android.support:support-compat:26.1.0
| | | ...
| | +--- com.android.support:support-media-compat:26.1.0
| | | ...
| | +--- com.android.support:support-core-utils:26.1.0
| | | ...
| | +--- com.android.support:support-core-ui:26.1.0
| | | //该依赖被强制指定了版本号
| | --- com.android.support:support-fragment:26.1.0
| | +--- com.android.support:support-compat:26.1.0 (*)
| | +--- com.android.support:support-core-ui:26.1.0 (*)
| | --- com.android.support:support-core-utils:26.1.0 (*)
| +--- com.android.support:support-vector-drawable:26.1.0
| | ...
| --- com.android.support:animated-vector-drawable:26.1.0
| ...
+--- com.android.support:recyclerview-v7:26.1.0
| ...
--- com.github.bumptech.glide:glide:4.7.1
+--- com.github.bumptech.glide:gifdecoder:4.7.1
| --- com.android.support:support-annotations:27.1.1
+--- com.github.bumptech.glide:disklrucache:4.7.1
+--- com.github.bumptech.glide:annotations:4.7.1
//强制指定了版本号
--- com.android.support:support-fragment:27.1.1 -> 26.1.0 (*)
写的匆忙,如有纰漏,还望指正,如果不小心绑到了你,那真是极好的,今天顺便GOOGLE了一下,如何查看具体的某一个依赖的情况,但是一直没有头绪,使用了dependencyInsight
命令也不行。 如果有知道的希望可以交流一下。 谢谢。
以上是脚本宝典为你收集整理的解决 Android Gradle 依赖的各种版本问题全部内容,希望文章能够帮你解决解决 Android Gradle 依赖的各种版本问题所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。