【问题标题】:android - Running app fails with error 'com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex'android - 运行应用程序失败并出现错误“com.android.builder.dexing.DexArchiveMergerException:无法合并 dex”
【发布时间】:2017-10-01 14:14:35
【问题描述】:

我正在尝试在手机上运行我的应用程序,但在 build 时间它失败并出现以下错误:

Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.

java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

到目前为止,我尝试了什么,但无济于事: -> 清理并重建(重建失败) -> 删除./gradle 文件和所有项目构建和缓存文件,然后缓存失效

我的项目 gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.51'
    repositories {
        jcenter()
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta6'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        jcenter()
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的应用 Gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

def daggerVersion = '2.11'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.connectus.connectus"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/INDEX.LIST'
    }
}

kapt {
    generateStubs = true
}

dependencies {
    /**
     * Kotlin
     */
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile 'org.jetbrains.anko:anko-support-v4:0.10.1'

    /**
     * Unit testing
     */
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.powermock:powermock-module-junit4:1.6.2'
    testCompile 'org.powermock:powermock-api-mockito:1.6.2'

    /**
     * Android views and widgets
     */
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'

    /**
     * Retrofit
     */
    compile 'com.squareup.retrofit2:retrofit:2.+'
    compile 'com.squareup.retrofit2:converter-gson:2.+'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.+'
    compile 'io.reactivex:rxjava:1.0.4'
    compile 'io.reactivex:rxandroid:0.24.0'

    /**
     * Glide
     */
    compile 'com.github.bumptech.glide:glide:3.7.0'

    /**
     * Dagger 2
     */
    kapt "com.google.dagger:dagger-compiler:$daggerVersion"
    compile "com.google.dagger:dagger:$daggerVersion"
    provided 'org.glassfish:javax.annotation:10.0-b28'

    /**
     * Butterknife for view injection
     */
    compile 'com.jakewharton:butterknife:8.8.1'
    kapt 'com.jakewharton:butterknife-compiler:8.8.1'

    /**
     * GSON
     */
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'org.powermock:powermock-api-mockito:1.6.2'

    /**
     * Facebook SDK
     */
    compile 'com.facebook.android:facebook-login:[4,5)'

    /**
     * UI Testing
     */
    androidTestCompile 'com.android.support:support-annotations:25.3.1'
    androidTestCompile 'junit:junit:4.12'

    testCompile 'com.google.dexmaker:dexmaker:1.2'
    testCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
        exclude module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') {
        exclude module: 'support-annotations'
        exclude module: 'appcompat-v7'
        exclude module: 'support-v4'
        exclude module: 'support-v13'
        exclude module: 'recyclerview-v7'
        exclude module: 'design'
    }
    androidTestCompile('com.android.support.test:runner:0.3') {
        exclude module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test:rules:0.3') {
        exclude module: 'support-annotations'
    }
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
}

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
repositories {
    mavenCentral()
}

我正在使用 Kotlin 和 Android Studio 3.0.0 beta 6。有什么问题?

【问题讨论】:

  • 我会使缓存失效并重新启动,然后重试。如果问题没有得到解决,那么您必须检查您的依赖关系。有时 AAR 依赖项具有具有相同包名的内部 JAR 库,当 Android Studio 想要将这些 JAR 文件合并在一起时,会遇到合并异常的错误。我会搜索一个库依赖,它里面有一个 JAR ;)
  • 我有与 OP 相同的错误,但代码略有不同,我将 compileSdkVersion 设置为 26 并且 com.android.support 库仍在 25.3.1 上。将这些更新到 26.0.2 为我修复了 dex 合并错误。

标签: android gradle kotlin


【解决方案1】:

虽然我不完全确定造成这种情况的原因,但在执行some digging 之后,kotlin 插件中的 jetbrains 包含的注释库与其他地方似乎存在版本冲突。您可以通过运行来验证这一点

gradle :app:assembleDebug --stacktrace

最后给出,

Caused by: com.android.dex.DexException: Multiple dex files define Lorg/intellij/lang/annotations/JdkConstants$PatternFlags;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:661)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:616)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:598)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:198)
    at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:61)
    ... 1 more

我通过从 kotlin 依赖项中排除注释库来解决这个问题。 (之后可能需要清理和重建)

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

这对我有用,但我不确定这个解决方案有多好。

【讨论】:

  • 您好,升级到 beta 7 后我遇到了这个问题,请您提供您的 build.gradle 文件
  • @SenzoMalinga 只需将您的 kotlin 依赖项替换为我在答案中指定的依赖项即可。确保清理并重建。
  • 嗨,谢谢,最后运行“./gradlew app:dependencies”来告诉我什么lib正在使用什么,然后我排除了每个旧的和重复的传递依赖项,我不需要
【解决方案2】:

添加这个

defaultConfig {
    multiDexEnabled true
}

像这样:

android {
compileSdkVersion 25
buildToolsVersion "26.0.2"
defaultConfig {
    applicationId "com.connectus.connectus"
    minSdkVersion 16
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

defaultConfig {
   multiDexEnabled true
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/INDEX.LIST'
}

}

【讨论】:

    【解决方案3】:

    我设法通过仅包含基本播放服务而不是完整服务来修复它:

    -    compile 'com.google.android.gms:play-services:11.6.0'
    +    compile 'com.google.android.gms:play-services-base:11.6.0'
    

    并删除了我的 Activity 中的 appindexing 依赖项:

    -import com.google.android.gms.appindexing.AppIndex;
    -import com.google.android.gms.common.api.GoogleApiClient;
    

    (尽管这可能是我升级到 API 27 后的遗留问题)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      相关资源
      最近更新 更多