java.lang.RuntimeException:java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:无法合并dex

我的项目是在Android Studio 3.0.1上的Kotlin,这是我的设计gradle

buildscript { ext.kotlin_version = '1.2.0' ext.anko_version='0.10.3' repositories { jcenter() mavenCentral() google() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath 'com.google.gms:google-services:3.1.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() jcenter() mavenCentral() } } task clean(type: Delete) { delete rootProject.buildDir } 

在这个应用程序中,我创建了一个库,在这个库中我有以下渐变文件。

 apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 buildToolsVersion '26.0.2' defaultConfig { minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" multiDexEnabled true } buildTypes { release { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation('com.google.api-client:google-api-client-android:1.22.0') { exclude group: 'org.apache.httpcomponents' exclude group: 'com.google.code.findbugs' } implementation('com.google.apis:google-api-services-gmail:v1-rev66-1.22.0') { exclude group: 'org.apache.httpcomponents' } implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:design:26.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' testImplementation 'com.greghaskins:spectrum:1.2.0' implementation 'com.squareup.retrofit2:retrofit:2.3.0' implementation 'com.squareup.retrofit2:converter-gson:2.3.0' implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' implementation 'com.google.code.gson:gson:2.8.0' implementation 'io.reactivex.rxjava2:rxjava:2.0.2' implementation 'io.reactivex.rxjava2:rxandroid:2.0.1' implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation "org.jetbrains.anko:anko-commons:$anko_version" implementation 'com.android.support:multidex:1.0.2' implementation 'com.google.android.gms:play-services-location:11.6.2' implementation 'com.google.android.gms:play-services-gcm:11.6.2' implementation 'com.google.android.gms:play-services-auth:11.6.2' } apply plugin: 'com.google.gms.google-services' 

到目前为止,项目编译正常,但是当我去建立,通过手机上的USB运行应用程序发生以下错误,我仍然无法解决。

错误:任务’:app:transformDexArchiveWithExternalLibsDexMergerForDebug’的执行失败。

java.lang.RuntimeException:java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:无法合并dex

预先感谢您的帮助。

当发生这种情况时,我注释掉multiDexEnabled true然后执行clean / build / run。

如果你的应用程序没有达到65k的方法限制,你不需要启用multidex

一个有同样的问题,我解决了两个窍门如果你已经启用multiDex,尝试从kotlin排除注释包

 implementation ("org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"){ exclude group: 'org.jetbrains', module: 'annotations' } 

这是解决方案帮助我