无法合并dex

问题描述 投票:279回答:59

我有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

android dex android-studio-3.0
59个回答
5
投票

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

  1. 得到SdkManager - > Android Sdk - > Sdk工具和更新谷歌播放服务到最新版本到46。
  2. 清理项目并重建项目。

5
投票

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

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

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

  1. 确保存储库部分包含带有“https://maven.google.com”端点的maven部分。例如: allprojects {repositories {jcenter()maven {url“https://maven.google.com”}}}

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

更改版本后:1。清理项目2.重建项目


5
投票

其中一种可能性是:存在相同的库但在依赖项中具有不同的版本。

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

  • compile fileTree(include: ['*.jar'], dir: 'libs')
  • compile 'com.google.code.gson:gson:2.8.2'

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


4
投票

添加以下命令:

android {
...

    dexOptions {

        jumboMode true
        javaMaxHeapSize "4g"

    }
}

4
投票

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

android {
    defaultConfig {
        multiDexEnabled true
        }
}

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


3
投票

我同意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,要么添加广告并禁用即时运行。这就是我发现的,如果有帮助的话。


2
投票

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

当我拿出它时,gson-2.8.1.jar项目编译没有这个奇怪的错误。

因此,尝试修改libs文件夹和依赖项。


2
投票

我遇到了同样的问题,并找到了我案件的真正原因。以前,我也再次尝试了所有以前的答案,但它没有解决问题。我的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;

在farm-motion-1.0.jar包中的类SportSummary,在阅读了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的依赖。如果在运行时没有依赖jar包,jar包的'implementation'依赖也可以改为'compileOnly'。


2
投票

我也有问题。

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


2
投票

对于我们的项目,我们意外地添加了两次不同名称的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 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
投票

我找到了我的项目出现这个问题的原因。我在build.gradle中添加了两次依赖项。一次通过添加依赖关系添加依赖关系和一次再添加jar依赖关系:

编译'org.achartengine:achartengine:1.2.0' ... 实现文件('../ achartengine-1.2.0.jar')

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


1
投票

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

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

每次修改时都要清洁并重建以检查解决方案!

  1. 使用--stacktrace旗帜构建更多细节。它会抱怨一个类,谷歌那个类找到了库。 Here's how you can set up Android studio to always run gradle with the --stacktrace flag
  2. 构建完成后,在Android Studio View > Tool Windows > Gradle Console中查看Gradle控制台
  3. 通过运行./gradlew -q app:dependencies检查重复的依赖性。每次修改build.gradle时都可以重新运行它。
  4. 在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. 在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. 在build.gradle中, android { ... configurations.all { resolutionStrategy { // Completely exclude the library. Works for transitive // dependencies. exclude group: 'com.google.code.findbugs' } } }
  7. 如果您的某些依赖项位于jar文件中,请打开jar文件并查看是否存在任何冲突的类名。如果是,您可能需要使用新的类名重新构建jar或查看shading

更多背景阅读:


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

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

1
投票

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


1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.