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

1、问题的产生

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

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

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

clipboard.png

还有连锁反应会出现这种问题:全部的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.0git

clipboard.png

上面的问题,也能够在 Android studio左侧的 Project栏的 External Libraries中查看,能够看到 因为引入了和当前编译版本号不一样的支持库所产生的问题:

clipboard.png

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

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

2、如何解决

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

2.1 查看依赖树

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

./gradlew app:dependencies --configuration releaseRuntimeClasspathgithub

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

+--- 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 (*)

符号的含义:ide

  • 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"
}

执行结果:(部分)gradle

\--- 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 排除了。ui

  • 排除指定:

implementation ('com.github.bumptech.glide:glide:4.7.1'){
        exclude module:"support-fragment"
    }

执行结果:(部分)google

+--- 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 不影响其余依赖。spa

exclude 能够搭配 groupmodule使用,将会排除全部被匹配的依赖。

2.3 Force 强制指定

强制指定依赖的版本。
configurations.all {
   resolutionStrategy {
       force 'com.android.support:support-fragment:26.1.0'
   }
}

执行结果(部分):code

+--- 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命令也不行。 若是有知道的但愿能够交流一下。 谢谢。

相关文章
相关标签/搜索