解决 Android Gradle 依赖的各种版本问题

发布时间:2019-06-25 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了解决 Android Gradle 依赖的各种版本问题脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

一、问题的产生

1.1 引入的支持库版本和编译版本不一致

相信大家在build.gradle中引入各种依赖的时候,或多或少会见过一些红线,gradle会提示你,当前的编译版本和你依赖的这个支持库的版本号不同,应该使用相同的支持库版本,来比避免编译不通过问题,类似于这种。

This support library should not use a different version (27) than the compileSdkVersion (26)

解决 Android Gradle 依赖的各种版本问题

还有连锁反应会出现这种问题:所有的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 Gradle 依赖的各种版本问题

上面的问题,也可以在Android studio左侧的PRoject栏的External Libraries中查看,可以看到 由于引入了和当前编译版本号不同的支持库所产生的问题:

解决 Android Gradle 依赖的各种版本问题

1.2 第三方库中的子依赖和当前项目的编译版本不一致。

当引入一个第三方库,该库中也依赖了Android支持库,且支持库的版本,和当前版本不一致而导致的版本冲突:

二、如何解决

解决冲突的方式包括:强制指定,排除。

2.1 查看依赖树

Gradle 默认开启了 依赖传递 意思就是 项目依赖了A,A又依赖了B和C,这时候,我们只需要写一行代码:implementation A就行了,由传递依赖导致的冲突,默认是以最高版本的依赖为准,要想查看整个项目的依赖传递关系,使用命令:

./gradlew app:dependencies --configuration releaseRuntimeClasspath

app是具体的module
releaseRuntimeClasspath是具体的VARiants类型。

+--- 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 可以搭配 groupmodule使用,将会排除所有被匹配的依赖。

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,请注明来意。