gradle依赖包重复冲突解决

时间:2024-03-13 17:41:27

android项目是通过gradle来进行依赖包管理的,在引入依赖包的时候,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版本,重新编译一遍可以发现错误消失了:
gradle依赖包重复冲突解决