首页 文章

无法合并dex

提问于
浏览
275

我有Android Studio Beta . 我创建了一个新项目,编译我的旧模块,但是当我尝试启动应用程序时,它没有启动消息:

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

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

但我不知道如何解决这个错误 . 我用谷歌搜索了几个小时但没有成功 .

我的项目是:

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

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta6'
        classpath "io.realm:realm-gradle-plugin:3.7.1"
        classpath 'com.google.gms:google-services:3.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

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

我的应用程序gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "parad0x.sk.onlyforyou"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
        }
    }
    compileOptions {
        targetCompatibility 1.7
        sourceCompatibility 1.7
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }
    lintOptions {
        checkReleaseBuilds false
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    //noinspection GradleCompatible
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile project(path: ':loginregisterview')


}

我的模块是gradle:

apply plugin: 'com.android.library'
apply plugin: 'realm-android'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.0.2'
    compile 'com.android.support:support-v4:26.1.0'
    compile 'com.github.bumptech.glide:glide:4.0.0'
    testCompile 'junit:junit:4.12'
    compile project(path: ':parser')

}

我的第二个模块:

apply plugin: 'com.android.library'
apply plugin: 'realm-android'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    realm {
        syncEnabled = true
    }
    useLibrary 'org.apache.http.legacy'

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile 'junit:junit:4.12'
    //  compile 'com.android.support:appcompat-v7:23.1.0'

    //   compile 'com.fasterxml.jackson.core:jackson-core:2.9.0'
 //   compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.0'
 //   compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0'
    compile 'com.google.code.gson:gson:2.6.2'
}

___finding

当我没有导入第二个模块(解析器)时,应用程序没有在dex上崩溃,但是当模块未导入时,应用程序无效 . :D:D

30 回答

  • 10

    添加以下命令:

    android {
    ...
    
        dexOptions {
    
            jumboMode true
            javaMaxHeapSize "4g"
    
        }
    }
    
  • 5

    With Android Studio 3.0 stable build以下步骤对我有用:

    • 获得SdkManager - > Android Sdk - > Sdk工具和更新谷歌播放服务到最新版本到46 .

    • 清理项目并重建项目 .

  • 13

    安装Google Play服务(最新版)包括

    android {
        defaultConfig {
            multiDexEnabled true
            }
    }
    

    在build.gradle中为我解决了问题,请确保清理并重建项目!

  • 4

    当我从 com.google.android.gms:play-services:11.2.2 更新到 com.google.android.gms:play-services:11.4.0 时,我遇到了同样的问题 . 这解决了我:

    • 干净

    • 重建

  • 2

    我尝试了以上所有内容并且没有一个帮助,最后,我发现这对我有用:

    app/build.gradle:

    android {
        defaultConfig {
           multiDexEnabled true
        }
    }
    
  • 4

    注意警告!

    有时您只需要消除 warningserror 将自动消失 . 见下面的特例:


    我在模块级 build.gradle 文件中有这两个依赖项:

    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support:recyclerview-v7:27.0.2'
    

    和Studio had warned (除了dex合并问题):

    所有com.android.support库必须使用完全相同的版本规范(混合版本可能导致运行时崩溃) . 找到的版本27.0.2,21.0.3 . 示例包括com.android.support:animated-vector-drawable:27.0.2和com.android.support:support-v4:21.0.3

    So I explicitly determined the version of com.android.support:support-v4 (有关详细信息,请参阅hereand both problems (警告和与dex合并相关的警告) solved

    implementation 'com.android.support:support-v4:27.0.2'  // Added this line (according to above warning message)
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support:recyclerview-v7:27.0.2'
    

    其他类似情况见下面的评论 .

  • 2

    [无法合并DEX求助]经过数小时的堆栈溢出后,我解决了“UNABLE TO MERGE DEX ERROR”

    • 将gradle中的所有com.android.support行更新为v27.1.0

    原因 - Android已将其支持库更新到v27.1.0,因此您必须将gradle文件中的所有android支持行从26.1.0更改为27.1.0

    allprojects {repositories {jcenter()maven {url“https://maven.google.com”}}}

    原因: - Android无法更新SDK管理器中的支持库,现在它使用maven.google.com进行更新,因此您必须包含此内容才能使用27.1.0支持库

    After Change Version: 1.清理项目2.重建项目

  • 1

    就我而言,不幸的是,Michel和Suragch的解决方案都不适合我 .

    所以我通过执行以下操作解决了这个问题:

    gradle:3.0 中, compile 配置现已弃用,应替换为 implementationapi . 有关更多信息,您可以read here您可以在Gradle Build Tool阅读官方文档

    编译配置仍然存在但不应使用,因为它不提供api和实现配置提供的保证 .

    最好使用 implementationapi 而不是 compile

    只需将 compile 替换为 implementation ,将 debugCompile 替换为 debugImplementation ,将 testCompile 替换为 testImplementation ,将 androidtestcompile 替换为 androidTestImplementation

    例如:而不是这个

    compile 'com.android.support:appcompat-v7:26.0.2'
    compile 'com.android.support:support-v4:26.1.0'
    compile 'com.github.bumptech.glide:glide:4.0.0'
    

    像这样用

    implementation 'com.android.support:appcompat-v7:26.0.2'
    implementation 'com.android.support:support-v4:26.1.0'
    implementation 'com.github.bumptech.glide:glide:4.0.0'
    

    之后

    • 删除项目中的.gradle文件夹(注意,为了查看.gradle,您需要切换到左上角导航器中的"Project"视图)

    • 删除所有 build folders 和gradle缓存 .

    • 在Build菜单中,按 Clean Project 按钮 .

    • 任务完成后,按“构建”菜单中的 Rebuild Project 按钮 .

    希望它会有所帮助!

  • 3
    • 删除 .gradle 目录 .

    • 再次运行您的应用 .

    Notes

    • .gradle 目录位于项目的根文件夹中 . (您可能必须先显示隐藏文件 . )

    • 每次使用Android 3.0更新依赖模块时都必须这样做 . (Android Studio 3的最新版本似乎解决了这个问题 . )

  • 275

    按照Suragch的建议删除 .gradle 对我来说还不够 . 另外,我不得不执行 Build > Clean Project .

    请注意,要查看 .gradle ,您需要切换到左上角导航器中的"Project"视图:

    Switch to project view

  • 253

    我尝试了所有其他解决方案,但没有人为我工作 . 最后,我通过编辑 build.gradle 使用相同的依赖版本解决了它 . 我认为在将库添加到使用不同依赖版本的支持或谷歌库的gradle时会出现此问题 .

    将以下代码添加到构建gradle文件中 . 然后 cleanrebuild 项目 .

    ps:这对我来说是旧解决方案所以你应该使用以下库的更新版本 .

    configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '26.1.0'
            }
        } else if (requested.group == "com.google.android.gms") {
            details.useVersion '11.8.0'
            } else if (requested.group == "com.google.firebase") {
                details.useVersion '11.8.0'
              }
          }
    }
    
  • 4

    在我的情况下问题是因为房间库:

    compile 'android.arch.persistence.room:runtime:1.0.0-alpha1'
    

    将其更改为:

    compile 'android.arch.persistence.room:runtime:1.0.0'
    

    工作 .

  • 1

    嗨,我有同样的问题几乎尝试了一切 . 所以,最后我通过逐行调试一切经过长达6小时的努力解决了 .

    classpath 'com.google.gms:google-services:3.0.0'
    

    Google-services 3.0 不支持带有playServiceVersion的Studio 3.0的firebase:11.6.0或更低版本 .

    implementation "com.google.firebase:firebase-messaging:$rootProject.ext.playServiceVersion"
    implementation "com.google.firebase:firebase-core:$rootProject.ext.playServiceVersion"
    implementation "com.firebase:firebase-jobdispatcher-with-gcm-dep:$rootProject.ext.jobdispatcherVersion"
    

    Solution :

    我已经改变了 google services

    classpath 'com.google.gms:google-services:3.1.1'
    

    它支持firebase服务 .

    希望有人能节省他/她的时间 .

  • 11

    只是添加到上面解决方案:

    确保在多个位置(甚至在同一文件中)没有指向不同版本的重复依赖项 .

  • 2

    if(1 . 尝试清理和重建工作)那么好

    否则如果(2.尝试删除gradle工作)那么好

    else-> 3.尝试添加grade.properties

    android.enableD8 = false
    

    else-> 4.将 multiDexEnabled true添加到build.gradle

    android {
        compileSdkVersion 26
        defaultConfig {
          ...
            minSdkVersion 15
            targetSdkVersion 26
            multiDexEnabled true
         ...
        }
    }
    

    并添加依赖项

    dependencies {
        compile 'com.android.support:multidex:1.0.1'}
    

    它可能是第一个适用于你的等等,但它实际上取决于你的问题的性质,例如

    添加此库后,我收到错误消息

    implementation 'com.jjoe64:graphview:4.2.2'
    

    后来我发现我必须检查一下,我必须添加相同版本的支持库 . 所以我必须尝试另一个版本

    compile 'com.jjoe64:graphview:4.2.1'
    

    它解决了这个问题 . 所以要注意这一点 .

  • 1
    • 启用

    defaultConfig

    • 如果步骤1不起作用

    转到项目结构并找出使用不同版本的外部库 . 双击它并删除其jar文件 . 关闭项目并再次打开android studio将重建项目 . 问题应该消失了 .

  • 25

    如果在包含kotlin支持后出现此错误,并且其他解决方案都不起作用,请尝试将app模块的 build.gradle 的kotlin依赖关系更改为:

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

    这适用于Android Studio 3.0 Beta 6.请参阅this答案以获得进一步说明 .

  • 37

    其中一种可能性是: the presence of the same library but with different versions in the dependencies .

    我在gradle文件中有以下几行出现此问题:

    • compile fileTree(include: ['*.jar'], dir: 'libs')

    • compile 'com.google.code.gson:gson:2.8.2'

    gson库位于我的libs目录中,但具有以前的版本 . 我从libs目录中删除了 gson-2.3.1.jar ,一切都恢复正常 .

  • 21

    我同意Chris-Jr . 如果您使用Firebase嵌入AdMob广告(或者即使您不是),则即使您不将其添加为依赖项,播放服务分析也会包含播放服务广告 . 谷歌显然在他们的11.4.0推出中犯了一个错误,因为分析包括版本10.0.1的广告,而不是11.4.0(鼠标在gradle中的提示显示了这一点) .

    我在顶部手动添加了编译'com.google.android.gms:play-services-ads:11.4.0',但只有在我禁用了Instant Run之后:http://stackoverflow.com/a/35169716/530047

    因此,它要么回归到10.0.1,要么添加广告并禁用即时运行 . 这就是我发现的,如果有帮助的话 .

  • 22

    在我的情况下它是 gson-2.8.1.jar 我已添加到项目的 libs 文件夹中 . 但SDK的引用已经存在 . 因此,不必将 gson-2.8.1.jar 添加到libs文件夹中 .

    当我 took it out th gson-2.8.1.jar 项目编译没有这个错误的错误 .

    所以尝试修改 libs 文件夹和 dependencies .

  • 54

    我遇到了同样的问题,并找到了我案件的真正原因 . 以前,我也再次尝试了所有以前的答案,但它没有解决问题 . 我的Wear app项目中有两个模块,build.gradle如下:

    wear module的build.gradle:

    implementation project(':common')
    implementation files('libs/farmer-motion-1.0.jar')
    

    common module的build.gradle:

    implementation files('libs/farmer-motion-1.0.jar')
    

    在升级到gradle 3.x之前,'implementation'都是'compile' .

    我使用--stacktrace选项运行gradlew来获取堆栈跟踪,当出现此问题时,您可以在gradle控制台窗口中单击此选项 . 并发现对jar包的依赖重复:

    Caused by: com.android.dex.DexException: Multiple dex files define Lcom/farmer/motion/common/data/pojo/SportSummary$2;
    

    Class.SportSummary中farmer-motion-1.0.jar包,在读完official migration guide之后,我将build.gradle改为:

    wear module的build.gradle:

    implementation project(':common')
    // delete dependency implementation files('libs/farmer-motion-1.0.jar')
    

    common module的build.gradle:

    api files('libs/farmer-motion-1.0.jar') // change implementation to api
    

    现在磨损模块将通过普通模块输出农民动作1.0.jar的依赖 . 如果 has no dependency on jar package during runtime ,' implementation '依赖jar包 can also be change to 'compileOnly' .

  • 9

    我也有问题 .

    我能够通过将 compileSdkVersiontargetSdkVersion 更改为最新版本来解决 .

  • 5

    对于我们的项目,我们意外地添加了两次不同名称的jar . 删除其中一个解决了这个问题 .

  • 1

    对我来说,它是在 app\build.gradle: 更新firebase消息

    compile 'com.google.firebase:firebase-messaging:10.0.1'
    

    compile 'com.google.firebase:firebase-messaging:11.4.2'
    
  • 2

    可能的根本原因之一:Android Studio导入多模块项目未正确处理的重复瞬态依赖性 . 检查列表并将其删除 . 对我来说,解决方案实际上是这样的:

    --- a/project/module/build.gradle
    +++ b/project/module/build.gradle
    @@ -21,5 +21,4 @@ android {
     dependencies {
         implementation project(':upstream-dependency-project')
         implementation 'com.android.support:support-v4:18.0.0'
    -    implementation files('libs/slf4j-android-1.6.1-RC1.jar')
     }
    
  • 1

    我找到了我的项目出现这个问题的原因 . I was added one dependency twice in build.gradle . 一次通过添加依赖关系添加依赖关系和一次再添加jar依赖关系:

    compile 'org.achartengine:achartengine:1.2.0' ... implementation files('../achartengine-1.2.0.jar')

    删除后第一行问题解决了 .

  • 1

    如果最佳答案对您不起作用,则您的问题可能是您有多个依赖于同一个库的依赖项 .

    这是一些调试技巧 . 在此示例代码中, com.google.code.findbugs:jsr305:3.0.0 是违规库 .

    Always clean and rebuild every time you modify to check your solution!

    • 使用 --stacktrace 标志构建以获取更多详细信息 . 它会抱怨一个类,谷歌那个类找到了库 . Here's how you can set up Android studio to always run gradle with the --stacktrace flag .

    • 构建完成后,在Android Studio View > Tool Windows > Gradle Console 中查看Gradle控制台

    • 通过运行 ./gradlew -q app:dependencies 检查重复的依赖性 . 每次修改build.gradle时都可以重新运行它 .

    • 在build.gradle中,

    android {
            ...
            configurations.all {
                resolutionStrategy {
                    // Force a particular version of the library 
                    // across all dependencies that have that dependency
                    force 'com.google.code.findbugs:jsr305:3.0.0'
                }
            }
    }
    
    • 在build.gradle中,
    dependencies {
        ...
        implementation('com.google.auth:google-auth-library-oauth2-http:0.6.0') {
            // Exclude the library for this particular import
            exclude group: 'com.google.code.findbugs'
        }
    }
    
    • 在build.gradle中,
    android {
        ...
        configurations.all {
            resolutionStrategy {
                // Completely exclude the library. Works for transitive
                // dependencies.
                exclude group: 'com.google.code.findbugs'
            }
        }
    }
    
    • 如果您的某些依赖项位于jar文件中,请打开jar文件并查看是否存在任何冲突的类名 . 如果是,您可能需要使用新的类名重新构建jar或查看shading .

    更多背景阅读:

  • 5
    android {
        defaultConfig {
            ...
            minSdkVersion 15 
            targetSdkVersion 26
            multiDexEnabled true
        }
        ...
    }
    
    dependencies {
      compile 'com.android.support:multidex:1.0.1'
    }
    
  • 10

    这可能不是你的问题,但是当我意外地在项目的依赖项{}部分中包含两个相同(但名称不同)的库时,我收到了此错误 .

  • 1

    在我的情况下,库会出现此问题,库已成功添加到项目中,但是当我运行我的应用程序时,它会向我显示此错误 . 因此,如果您也遇到这种情况,您可以访问github并检查问题或提出新问题 . 如果您没有找到有关该库的任何解决方案,我建议您更换它 .

相关问题