构建Android项目时出现:app:mergeExtDexDebug任务执行失败
错误详情
任务':app:mergeExtDexDebug'执行失败。
无法解析':app:debugRuntimeClasspath'配置的所有文件。
无法转换play-services-ads-11.0.1.aar(com.google.android.gms:play-services-ads:11.0.1)以匹配属性{artifactType=android-dex, asm-transformed-variant=NONE, dexing-enable-desugaring=true, dexing-is-debuggable=true, dexing-min-sdk=21, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}。DexingWithClasspathTransform执行失败:/Users/pavelkiruhin/.gradle/caches/transforms-3/bc9f963676e4faed6b0c0af40d264d92/transformed/jetified-play-services-ads-11.0.1-runtime.jar。
转译Dex时出错。
已尝试的解决方法
- 清理项目并重新构建
- 失效缓存并重启Android Studio
- 删除本地
.gradle文件夹 - 重新安装Android Studio
项目配置
build.gradle (Module)
plugins { id 'com.android.application' id 'com.google.gms.google-services' id 'com.google.firebase.crashlytics' id 'kotlin-android' } android { compileSdkVersion 32 buildToolsVersion "30.0.3" defaultConfig { applicationId 'com.pavloffmedev.dcn' minSdkVersion 21 targetSdkVersion 32 versionCode 90 versionName "3.10 beta 4" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } buildFeatures { viewBinding true compose true } composeOptions { kotlinCompilerExtensionVersion = "$compose_version" } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } kotlinOptions { jvmTarget = '1.8' } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } } dependencies { implementation 'androidx.core:core-ktx:1.8.0' implementation 'androidx.appcompat:appcompat:1.4.2' implementation 'com.google.android.material:material:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.google.firebase:firebase-messaging:23.0.7' implementation 'com.google.firebase:firebase-crashlytics-ktx:18.2.12' implementation 'com.google.firebase:firebase-analytics-ktx:21.1.0' implementation 'com.google.firebase:firebase-messaging-ktx:23.0.7' implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' implementation 'androidx.activity:activity-compose:1.5.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation 'com.google.android.play:core:1.10.3' implementation 'com.android.volley:volley:1.2.1' implementation 'com.google.android.gms:play-services-auth:20.2.0' androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4' implementation 'com.adcolony:sdk:4.8.0' implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1' implementation 'com.pollfish:pollfish-googleplay:6.2.5' implementation 'com.google.code.gson:gson:2.9.1' implementation 'com.squareup.picasso:picasso:2.71828' implementation 'com.adscendmedia.sdk:adscendmedia:2.3.9' implementation 'androidx.core:core-ktx:1.8.0' implementation 'io.getstream:avatarview-coil:1.0.6' implementation 'com.github.PsssUm:upmob-api:0.2.6' implementation 'com.gitlab.adgate-media:androidsdk:2.1.1' implementation 'com.airbnb.android:lottie:5.2.0' implementation 'com.facebook.shimmer:shimmer:0.5.0' implementation files('../libs/unity-ads.aar') implementation "androidx.compose.compiler:compiler:$compose_version" implementation "androidx.compose.foundation:foundation:$compose_version" implementation "androidx.compose.material:material:$compose_version" implementation "androidx.compose.runtime:runtime:$compose_version" implementation "androidx.compose.ui:ui:$compose_version" debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version" implementation "androidx.compose.material3:material3:1.0.0-alpha15" } repositories { mavenCentral() }
build.gradle (Project)
buildscript { ext.kotlin_version = '1.7.0' ext.compose_version = "1.2.0" repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.2.2' classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0' classpath 'com.google.gms:google-services:4.3.13' classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() mavenCentral() maven { url "https://bitbucket.org/adscend/androidsdk/raw/master/" } maven { url 'https://jitpack.io' } } } task clean(type: Delete) { delete rootProject.buildDir }
解决方案
1. 修复Google Play服务版本冲突
错误中的play-services-ads:11.0.1是老旧版本,和项目中play-services-auth:20.2.0、play-services-ads-identifier:18.0.1版本严重不兼容:
- 执行
./gradlew app:dependencies查看依赖树,定位引入旧版play-services-ads的依赖。 - 在Module的
build.gradle的android块中添加版本强制统一规则:
configurations.all { resolutionStrategy { force 'com.google.android.gms:play-services-ads:20.6.0' // 选择与其他服务匹配的版本 } }
或者直接在dependencies块中显式添加兼容版本的play-services-ads依赖。
2. 修正依赖与插件位置错误
Module的dependencies块中混入了插件应用代码,需将以下代码移至文件顶部(与现有plugins块合并或放在文件最开头):
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions'
同时删除重复的androidx.core:core-ktx:1.8.0依赖。
3. 匹配Build Tools与Compile SDK版本
当前buildToolsVersion "30.0.3"与compileSdkVersion 32不匹配,升级为对应版本:
buildToolsVersion "32.0.0"
4. 启用D8 Dex编译器
在项目根目录的gradle.properties中添加(无此文件则创建):
android.enableD8=true
完成以上修改后,重新清理项目并执行构建。
内容的提问来源于stack exchange,提问作者paulme

