Unable to merge dex

今天在引用一个库UCrop时,报错:

1
2
3
4
5
6
Error:Execution failed for task ':cameraphotoframe:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

翻译器:
错误:执行失败的任务':应用程序:转换Dex存档与外部库Dex合并调试'
> java.lang.Runtime异常:java.lang.Runtime异常:com.android.builder.dexing.Dex归档合并异常:无法合并dex

原因是引入了重复的lib导致的:
开始翻这个库的源码:

1
2
3
4
dependencies {
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.squareup.okhttp3:okhttp:3.8.1'
}

我的module

1
2
compile 'com.github.yalantis:ucrop:2.2.2'
implementation 'com.android.support:appcompat-v7:26.1.0'

然后删掉自己的的appcompat,重新编译,成功了!

当你遇到这样的错误时,一定要先检查你的build.gradle是不是有重复的包

参考:https://stackoverflow.com/questions/46267621/unable-to-merge-dex?page=1&tab=votes#tab-top

第二种情况

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: animated-vector-drawable:27.1.0 and com. Android. Support: design:26.1.0 more… (8 F1)

This support library should not use a different version (27) than the compileSdkVersion (26) more.. (8 F1)

compileSdkVersion 与com.android.support:design:$supportLibVersion” 的版本不一致。

Fork me on GitHub