解决依赖库版本不一致导致的问题

我们在写代码时候总会借鉴一些第三方库,有的是别人写的比较优秀的,有的则是google官方提供的,但是我们在依赖之后编译运行时候总会出各种各样的问题:

1、Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.

> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex  

2、“Error:Execution failed for task ':app:preDebugBuild'.

> Android dependency 'com.android.support:appcompat-v7' has different version for the compile (27.0.2) and runtime (27.1.0) classpath. You should manually set the same version via DependencyResolution”,

针对上面的support:appcompat库我们可以像下面一样子来指定自己所依赖的版本:

在build.gradle的dependencies中添加如下代码即可

configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
        //循环一个个的依赖库
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            //获取当前循环到的依赖库
            def requested = details.requested
            //如果这个依赖库群组的名字是com.android.support
            if (requested.group == 'com.android.support') {
                //且其名字不是以multidex开头的
                if (!requested.name.startsWith("multidex")) {
                    //这里指定需要统一的依赖版本
                    details.useVersion '27.1.0'
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/JustinNick/article/details/80898494