因项目需要引入了第三方包,发现报错
Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
很头大,各种方式都尝试了。
虽然知道本质上是jar包冲突,但是需要找到具体哪个。
解决方式
1
在app: build.gradle中修改配置
android {
defaultConfig {
...
multiDexEnabled true
}
dexOptions {
incremental true
}
}
dependencies {
...
compile 'com.android.support:multidex:1.0.3'
}
继承MultiDexApplication 或者在Application的attachBaseContext方法中加入
MultiDex.install(this);
2
在app: build.gradle中最下面添加
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex") && !requested.name.startsWith("multidex-instrumentation")) {
details.useVersion '你自己的版本' //
}
}
}
}
3
查看自己引入的第三方包使用的sdk版本,是否和自己冲突。
如果第三方是AndroidX. 你需要修改为
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
// implementation 'com.android.support:appcompat-v7:28.0.0' //去除自己的引用包,同时修改自己的Activity等引用
}
我的情况是第三种方式解决的。