资讯专栏INFORMATION COLUMN

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

mj / 2377人阅读

摘要:还有连锁反应会出现这种问题所有的支持库版本号要一致,也是避免运行时崩溃的问题。该依赖的版本被箭头所指的版本代替。强制指定强制指定依赖的版本。

一、问题的产生 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

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命令也不行。 如果有知道的希望可以交流一下。 谢谢。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/14338.html

相关文章

  • Android 集成React Native 到现有项目踩坑记录

    摘要:运行新应用的采坑记录指定和的版本。这个软件很好用设置支持的库架构把原生项目拷贝到项目的目录下上面步骤可以运行一个的简单项目,接下来时怎么集成到现有的项目项目结构如下我们做的话,是用打开这个目录的。 集成步骤 官方文档:https://facebook.github.io/react-native/docs/0.54/integration-with-existing-apps借鉴博客:...

    cnio 评论0 收藏0
  • 关于 OkHttp 依赖冲突问题解决过程

    摘要:由于使用了的远程依赖形式,故直接删除冲突的内容无效,须转为使用本地依赖的形式。根据官方文档的指示,删除的仓库依赖,使用本地包形式的依赖,去除对的包的引用,即可顺利解决问题。 「博客搬家」 原地址: CSDN 原发表时间: 2016-11-18 OkHttp 是一个流行的开源网络请求库。许多第三方库的底层都是使用 OkHttp 实现网络请求,所以 OkHttp 相关的依赖冲突问题就...

    myeveryheart 评论0 收藏0
  • 深入浅出Gradle

    摘要:提供一个的不同版本有时,您会想要在谷歌市场中列出您的的多个版本,例如,您想要提供您的的免费和版本。确保只有被选中,并接受默认最低设置。控制台显示任务执行的输出,包括任何错误消息或警告。 原文:https://code.tutsplus.com/zh-...原作:Jessica Thornsby翻译:Stypstive 作为第一个专为Android打造的IDE(集成开发环境),由Goo...

    dingda 评论0 收藏0
  • Android Studio3.0新特性及安装详解

    摘要:许多新的棉绒检查。如果程序在断点上暂停,则应用程序重新启动。新的中的新型提供了对应用程序活动的实时统一视图。要打开,请按照下列步骤操作单击视图工具您也可以在工具栏中单击。从工具栏中选择要配置的设备和应用程序进程。这样基本上就顺利安装了。 简介: Android Studio是Android的官方IDE。它是专为Android而打造,可以加快您的开发速度,帮助您为每款Android设备构...

    DDreach 评论0 收藏0
  • Android Studio3.0新特性及安装详解

    摘要:许多新的棉绒检查。如果程序在断点上暂停,则应用程序重新启动。新的中的新型提供了对应用程序活动的实时统一视图。要打开,请按照下列步骤操作单击视图工具您也可以在工具栏中单击。从工具栏中选择要配置的设备和应用程序进程。这样基本上就顺利安装了。 简介: Android Studio是Android的官方IDE。它是专为Android而打造,可以加快您的开发速度,帮助您为每款Android设备构...

    sherlock221 评论0 收藏0

发表评论

0条评论

mj

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<