gradle依赖包冲突解决

android项目是通过gradle来进行依赖包管理的,在引入依赖包的时候,gradle会连带着将该依赖包所依赖的包全部引入,这种情况下就很有可能会出现依赖包重复引入冲突的情况,如下图所示:


依赖包冲突截图

报错信息如下:

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 28.0.0, 27.1.1, 22.2.1. Examples include com.android.support:animated-vector-drawable:28.0.0 and com.android.support:support-media-compat:27.1.1 less... (Ctrl+F1) 
There are some combinations of libraries, or tools and libraries, that are incompatible, or can lead to bugs. One such incompatibility is compiling with a version of the Android support libraries that is not the latest version (or in particular, a version lower than your targetSdkVersion).

由上方的报错信息可以看出,com.android.support依赖库被引入了多个版本,出现版本冲突了;
在这里我们可以通过强制规定特定的依赖库的版本来解决这个问题,在项目Module 的build.gradle添加代码如下所示:

//解决冲突 同一版本
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '28.0.0'
            }
        }
    }
}

上面的代码强制规定了,group为“com.android.support”并且名字不是“multidex”包统一使用28.0.0版本,重新编译一遍可以发现错误消失了:


转载地址:https://www.jianshu.com/p/a806c1aba24f

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值