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

本文介绍了解决Android开发中因重复包引用导致的错误提示方法。通过定位并删除重复包,或调整依赖版本,可有效避免DexArchiveMergerException问题。

 

错误提示:

Error:Execution failed for task ':ZMU.B:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

原因:

根据相关文档提示,工程中有重复包被使用;

1、此时你需要去找一下所属工程中是否有重复引用包,有删除多余留一个就,重新编译就可以;

2、如果使用的引用包过多不好查找,那么有一个一劳永逸的方法:

按照上图设置;

此时编译一下工程;

此时如上图提示,你可以很快定位到那个包被重复引用,删除即可;

3、如果依照方法2,都删除所有错误提示的包,才没有该错误,但所依赖该包的方法缺报undefined错误,那么非常有可能是你引用的包本身有问题;可以尝试降版本或升版本

将如下这行:
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.2'
替换成: 
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
也就是将版本1.5.2退回到1.5,就可以了

这个错误通常是由于dex方法数超出限制引起的。你可以尝试以下方法解决: 1.app的build.gradle文件中添加下面的代码: ```groovy android { defaultConfig { ... multiDexEnabled true } ... } dependencies { implementation 'com.android.support:multidex:1.0.3' } ``` 然后在你的Application类中添加下面的代码: ```java public class MyApplication extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } ... } ``` 2. 如果你使用了很多第三方库,可以尝试使用ProGuard来优化代码。在app的build.gradle文件中添加下面的代码: ```groovy android { ... buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } ``` 然后在app的根目录下创建一个proguard-rules.pro文件,添加下面的代码: ``` -dontwarn okio.** -dontwarn javax.annotation.** -dontwarn com.squareup.okhttp.** -dontwarn retrofit2.Platform$Java8 -dontwarn retrofit2.Platform$Java8$.* -dontwarn retrofit2.Platform$Java9 -dontwarn retrofit2.Platform$Java9$.* -keep class com.google.gson.** { *; } -keep class org.apache.commons.io.** { *; } -keep class retrofit2.** { *; } -keepattributes Signature -keepattributes Exceptions -keepattributes InnerClasses ``` 注意:ProGuard可能会导致某些库出现问题,因此需要进行适当的测试和调整。 3. 如果以上方法都无法解决问题,可以尝试升级Gradle版本或者在项目中使用AndroidX。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值