无法合并dex

324

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

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

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

但我不知道该如何解决这个错误。我已经谷歌搜索了几个小时,但是没有成功。

我的项目gradle:

    // 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'
}

在没有导入第二个模块(parser)时,应用程序不会在dex上崩溃,但当未导入该模块时,应用程序无法工作。:D :D

____________找到答案____________


6
如果有人在使用Android Studio 3.0稳定版本时遇到同样的问题,我强烈建议您查看 https://dev59.com/f1YN5IYBdhLWcg3wwKdZ#46971548 上的答案,并看看它是否能够帮助您。 - Bhavesh Patadiya
1
我曾经遇到同样的问题,我按照本页上提到的每种方法都尝试了,但都没有帮助到我,所以我去了Gradle并在每个库上按住光标检查它们是否有新版本可用。那些有新版本的我就更新了,同步 Gradle 并运行。这样就解决了问题。 - Shahzad Afridi
63个回答

2
我遇到了同样的问题,并找到了我情况下的真正原因。 之前,我也尝试了所有以前的答案,但都没有解决问题。 我的wear应用程序项目中有两个模块,build.gradle如下:
wear模块的build.gradle:
implementation project(':common')
implementation files('libs/farmer-motion-1.0.jar')

通用模块的 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;

在farmer-motion-1.0.jar包中的SportSummary类,在阅读了官方迁移指南之后,我将我的build.gradle更改为以下内容:
wear模块的build.gradle:
implementation project(':common')
// delete dependency implementation files('libs/farmer-motion-1.0.jar')

build.gradle文件是指common module的构建文件。
api files('libs/farmer-motion-1.0.jar') // change implementation to api

现在,wear模块将依赖于由common模块导出的farmer-motion-1.0.jar。如果在运行时没有jar包依赖关系,则可以将jar包的“implementation”依赖项更改为“compileOnly”。

2

这可能不是您的问题,但当我意外地在项目的dependencies{}部分包含了两个相同但名称不同的库时,我遇到了这个错误。


2

在我们的项目中,我们不小心添加了同一个jar包两次,但它们的名称不同。删除其中一个就解决了问题。


这解决了我的问题。我在构建一个内部的Android库项目,在它的libs文件夹下,有两个jar文件,其中一个只是另一个备份但使用不同的文件名。 - hopia

2

我也遇到了这个问题。

我通过将compileSdkVersiontargetSdkVersion更改为最新版本来解决它。


1

如果前面的答案对您没有用,那么您的问题可能是您有多个依赖项依赖于同一个库。

以下是一些调试提示。在此示例代码中,com.google.code.findbugs:jsr305:3.0.0 是有问题的库。

每次修改都要清理并重新构建以检查解决方案!

  1. Build with the --stacktrace flag on for more detail. It will complain about a class, Google that class to find the library. Here's how you can set up Android studio to always run gradle with the --stacktrace flag.

  2. Take a glance at the Gradle Console in Android Studio View > Tool Windows > Gradle Console after a build

  3. Check for repeated dependences by running ./gradlew -q app:dependencies. You can re-run this each time you modify the your build.gradle.

  4. In 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'
                }
            }
    }
    
  5. In 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'
        }
    }
    
  6. In build.gradle,

    android {
        ...
        configurations.all {
            resolutionStrategy {
                // Completely exclude the library. Works for transitive
                // dependencies.
                exclude group: 'com.google.code.findbugs'
            }
        }
    }
    
  7. If some of your dependencies are in jar files, open up the jar files and see if there are any conflicting class names. If they are, you will probably have to re-build the jars with new class names or look into shading.

一些更多的背景阅读:

1
可能的根本原因之一是: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

对我来说,需要在app\build.gradle中更新Firebase消息传递。

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

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

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

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

2
你能详细说明一下吗? - sg7
我也曾经遇到过同样的问题,但是我意识到解决了当时的问题,但在未来的更新中,情况可能会发生变化。 - Eben Watts

1
在我的情况下,一个库引起了这个问题,虽然库已经成功添加到项目中,但是当我运行我的应用程序时,它显示了这个错误。因此,如果你也遇到了这个问题,你可以去github查看问题或提出新的问题。如果你找不到有关该库的解决方案,我建议你替换它。

1
我发现了我的项目出现问题的原因。在build.gradle中,我添加了一个依赖两次。一次是通过添加依赖,另一次是通过添加jar依赖:
compile 'org.achartengine:achartengine:1.2.0'
...
implementation files('../achartengine-1.2.0.jar')

删除第一行后,问题得到解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接