【问题标题】:Unable to Merge Dex - Android Studio 3.0无法合并 Dex - Android Studio 3.0
【发布时间】:2018-04-07 13:36:29
【问题描述】:

当我在稳定通道中将我的 Android Studio 更新到 3.0 并运行项目时,我开始收到以下错误。

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

我尝试清理和重建项目,但没有成功。任何帮助将不胜感激。

项目级 build.gradle

buildscript {
repositories {
    jcenter()
    google()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0'
    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
}

应用级 build.gradle

apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
    applicationId "com.med.app"
    minSdkVersion 21
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    resConfigs "auto"
    multiDexEnabled true

}
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.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'

//appcompat libraries
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:design:26.1.0'


//butterknife
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

//picasso
compile 'com.squareup.picasso:picasso:2.5.2'

//material edittext
compile 'com.rengwuxian.materialedittext:library:2.1.4'

// Retrofit & OkHttp & and OkHttpInterceptor & gson
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'

// FirebaseUI for Firebase Auth
compile 'com.firebaseui:firebase-ui-auth:3.1.0'
}
apply plugin: 'com.google.gms.google-services'

我已经尝试了所有给出的答案,但我无法解决这个错误。请帮忙。

【问题讨论】:

  • @IntelliJAmiya 我也尝试了您链接的问题的解决方案。它们都不起作用。
  • 你还没有启用multidex,你需要
  • @NirajSanghani 我也尝试启用 multidex。它没有帮助。
  • 你可以尝试将 google() 存储库放在 buildscript 存储库中

标签: android android-gradle-plugin android-studio-3.0


【解决方案1】:

play-services-auth 添加显式依赖以及您的firebase-ui-auth 依赖:

// FirebaseUI for Firebase Auth
    compile 'com.firebaseui:firebase-ui-auth:3.1.0'
    compile 'com.google.android.gms:play-services-auth:11.4.2'

这是因为firebase-ui-auth 具有对play-services-auth 的传递依赖,并且必须与play-services-auth 的对应版本一起使用。请参阅this explanation

firebase-ui-auth
|--- com.google.firebase:firebase-auth
|--- com.google.android.gms:play-services-auth

Gradle 构建工具的早期版本不包含传递依赖项,因此现在版本可能与其他 play-services 版本发生冲突。

我的问题得到解释和回答(以防有人想知道)

当您升级到 Android Studio 3.0 并将 gradle 构建工具版本更新到 3.0.0 时,编译依赖项的方式现在与早期版本不同。

我最近遇到了同样的问题。我正在使用这些依赖项,这些依赖项在 Gradle 2.3.3 版中运行良好:

implementation 'org.apache.httpcomponents:httpmime:4.3.6'
implementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'

升级到 gradle-build-version 3.0.0 后,我得到了同样的错误。仔细一看,我发现httpmime的传递依赖与文件httpclient-android冲突是包含的。

说明

让我详细解释一下。早些时候,在使用 gradle-tool-version 2.3.3 时,我使用 httpclient-android 来获取并使用名为 org.apache.http.entity.ContentType.java 的类 扩展org.apache.httpcomponents:httpmime:4.3.6 的传递依赖关系表明它具有org.apache.httpcomponents:httpcore:4.3.6,这与我想使用的包相同。但是在编译或同步构建时,它不包括org.apache.http.entity.ContentType.java,所以我需要添加这个依赖项,其中包括ContentType.java

implementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'

之后一切正常。

我将 gradle-build-version 升级到 3.0.0 后,情况发生了变化。它现在包括所有传递依赖。因此,在使用带有 gradle-build-tool 版本 3.0.0 的最新 Android Studio 进行编译时,我的 ContentType.java 被编译了两次。一次来自org.apache.httpcomponents:httpcore:4.3.6(这是httpmime 的隐式传递依赖),再次来自我之前使用的org.apache.httpcomponents:httpclient-android:4.3.5.1

要解决此问题,我必须删除现有的 org.apache.httpcomponents:httpclient-android:4.3.5.1 依赖项,因为 httpmime 本身会获取我的应用程序所需的相关类。

我的情况的解决方案:只使用所需的依赖项并删除httpclient-android

implementation 'org.apache.httpcomponents:httpmime:4.3.6'

请注意,这只是我的情况。您需要深入了解自己的依赖项并相应地应用解决方案。

【讨论】:

  • 没错,我也遇到了类似的问题。我的项目依赖项之一及其传递依赖库依赖于“com.android.volley”。所以这个新的 gradle 在合并 dex 文件时遇到了问题。我通过在我的 gradle 文件中添加“排除组:'com.android.volley' 来修复它。
  • @Bhavesh Patadiya,请在这里帮助我 (stackoverflow.com/questions/46976279/…)。我只是卡住了,无法解决。
  • 幸运的是,您的解释恰好与 org.apache.httpcomponents:httpclient-android 和 org.apache.httpcomponents:httpmime 相关。我快疯了,想知道这件事。谢谢!
  • you need to dig into the classes your are having an issue with 但是您是如何找出您遇到问题的课程的?您是否运行了./gradlew dependencies 并遍历了所有传递依赖项(在输出中标记为xxx -> yyy)?
【解决方案2】:

首先我按照之前的 cmets 中的建议启用了 multidex。

然后,如果错误仍然存​​在,请打开 Gradle 控制台(单击“消息”部分左侧的“显示控制台输出”图标)并单击链接以使用 Debug/Info/Stack 选项重新编译。 这将显示有关错误的更多详细信息。

在我的情况下,错误“无法合并 dex”是由重复条目引起的 在“com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0”中。

我从我的项目中手动删除了冲突的库并执行了“重建项目”(强制重新加载库)。这解决了问题。

【讨论】:

  • 您在哪里看到重复条目?在 build.gradle 文件中?文件夹中的实际罐子?谢谢
  • 我收到了com.android.dex.DexIndexOverflowException,尽管事实上我已经启用了 multidex 并且我的项目在更新到 Android Gradle Plugin 3.0 之前构建良好。
  • 我按照上面提到的做了,得到了以下结果: 原因:com.android.dex.DexException: Multiple dex files defined Lcom/google/firebase/iid/zzd;我尝试删除 FirebaseUI 库,重建项目,然后再次复制依赖项。但它不起作用。
  • Eres una pistola
  • 花了很长时间来查明和消除重复的依赖项(我觉得 gradle 应该自动完成),但是使用堆栈信息重新编译(告诉我哪个类被重复)是我需要的工具完全可以修复它。
【解决方案3】:

我遇到了这个错误:

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

为了解决这个问题,我最终改回了我的 gradle。

app\build.gradle

android {
compileSdkVersion 25
//buildToolsVersion '26.0.2'
buildToolsVersion '25.0.3'//<< Changed back to old version before my studio 3.0 update
defaultConfig { ....

.\build.gradle

buildscript {
repositories {
    jcenter()
    google()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3' //<< Changed back to old version before my studio 3.0 update
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

这并不理想,因为它是回溯日期,但它对我有用,应该可以让我到达那里,直到可能的补丁发布。

【讨论】:

    【解决方案4】:

    检查您的 build.gradle(应用程序)中的依赖项 如果您使用 2 个(或更多)具有相同名称和不同版本的库。 例如(以我为例):

    implementation files('src/main/libs/support-v4-24.1.1.jar')
    implementation 'com.android.support:support-v4:27.0.2'
    

    删除一个,然后清理并重建。另请注意,dependenciesbuildscript 之外。

    【讨论】:

      【解决方案5】:
      android {
          defaultConfig {
             multiDexEnabled true
          }
      }
      

      将此行添加到:gradle 文件中

      【讨论】:

      • 虽然此代码可以回答问题,但提供有关 如何为什么 解决问题的附加上下文将提高​​答案的长期价值。
      【解决方案6】:

      有时会发生此错误,当“libs”文件夹中存在相同的 .jar 库文件时,同时我们试图通过在应用程序 gradle 文件中添加“compile”行来获取源代码。

      其中任何一个,如果我们删除,那么我们可以克服这个错误。

      希望这可能会有所帮助。

      【讨论】:

        【解决方案7】:

        我有同样的问题 我解决了它:

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

        在构建脚本中->依赖项

        build.gradle

        在我的文件中:

        buildscript {
            repositories {
                jcenter()
                google()
            }
            dependencies {
                classpath 'com.android.tools.build:gradle:3.0.0'
                classpath 'com.google.gms:google-services:3.0.0'
            }
        }
        

        【讨论】:

          【解决方案8】:

          只需将您的类路径更改为:

          类路径'com.android.tools.build:gradle:2.3.3'

          并同步你的 Gradle。

          希望这会有所帮助。

          【讨论】:

          • 虽然这可能会回答这个问题,但最好解释一下答案的基本部分,以及 OPs 代码可能存在什么问题。
          【解决方案9】:

          “所有 gsm 库” 谢谢,它帮助解决了我的问题,但不仅是 gsm 库,而且所有谷歌库都必须具有相同的版本。 我有这个 dexing 错误,因为 com.android.support:recyclerview-v7 的版本与 com.android.support:appcompat-v7 不同

          Android Studio 在 build.gradle 文件中用红色下划线显示这些行。

          【讨论】:

            【解决方案10】:

            我完全按照下面屏幕截图中的提示进行操作,将 11.0.4 更改为 11.8.0

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

            效果很好

            【讨论】:

              【解决方案11】:

              似乎这个错误有很多情况。在我的情况下是在build.gradle(应用程序)中编译的 1.8 java:

              compileOptions {
                  targetCompatibility 1.8
                  sourceCompatibility 1.8
              }
              

              我删除了,错误消失了

              【讨论】:

                【解决方案12】:

                我将以下内容从 11.6.0 更改为 11.8.0 并且它有效。

                compile 'com.google.android.gms:play-services-ads:11.6.0'
                
                implementation 'com.google.android.gms:play-services-ads:11.8.0'
                

                【讨论】:

                  【解决方案13】:

                  我在使用 Firebase UI 数据库时遇到了同样的错误。即使按照其他答案中的建议启用了 multiDex,我仍然收到错误消息。然后我知道 Firebase UI 和 Firebase 数据库需要与 Firebase UI GitHub 存储库中给出的相同版本。

                  Firebase UI GitHub

                  【讨论】:

                    【解决方案14】:

                    将此添加到您的 gradle: implementation 'com.android.support:multidex:1.0.0'

                    清理项目,然后重新构建。这可行

                    【讨论】:

                      【解决方案15】:

                      这也可能为时已晚,但我想我也有答案。根据我最近的试验,在编译应用程序时,请确保在同一个项目中没有同一个包的jar文件和'implementation'('compile' for 3.0.1 &gt; gradle)。就我而言,我在同一个项目中有implementation 'org.jsoup:jsoup:1.11.2'Jsoup jar。菜鸟的错误,但是,我学会了。

                      【讨论】:

                        【解决方案16】:

                        对于那些最近仍在为此苦苦挣扎并添加了组件的人。对我来说是什么原因添加了:

                        编译'android.arch.lifecycle:extensions:1.0.0' annotationProcessor 'android.arch.lifecycle:compiler:1.0.0'

                        什么修复了它正在更新它

                        编译'android.arch.lifecycle:extensions:1.1.1' annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'

                        希望对您有所帮助。

                        【讨论】:

                          【解决方案17】:

                          我认为这是因为不同的库依赖于同一个子库但版本不同所以排除一个库的依赖,例如:

                          api (rootProject.ext.dependencies["bindingRecyclerView"]) {
                              exclude group: 'com.android.support'
                          }
                          

                          【讨论】:

                            【解决方案18】:

                            就我而言,我必须做三件事:

                            1. 当我使用 firebase 时,请确保 firebase 和 google play 服务的版本相同。最初 play 服务的版本较低。 主要是12.0.1版本帮助

                            2. 在应用程序的级别 build.gradle 中设置此项

                              android {  
                                  multiDexEnabled true   
                              }
                              
                            3. 再次在 app 的 build.gradle 中添加

                              compileOptions{
                                  sourceCompatibility 1.8
                                  targetCompatibility 1.8
                              }
                              

                            【讨论】:

                              【解决方案19】:

                              通过在 build.gradle(app module) 中添加以下代码为我工作

                              android {
                                    defaultConfig {
                                        multiDexEnabled true
                                    }
                              }
                              
                              dependencies {
                                  androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
                                  implementation 'com.android.support:support-v4:26.1.0'
                              }
                              

                              【讨论】:

                                【解决方案20】:

                                这个link 为我解决了这个问题。

                                首先我将 pubspec.yaml 中的依赖项设置为

                                dependencies:
                                  flutter:
                                    sdk: flutter
                                  cloud_firestore: ^0.8.2 
                                

                                并在我的 IDE 终端中运行 flutter packages get

                                我还必须更改最低目标 SDK 版本:

                                1. 打开android/app/build.gradle,然后找到上面写着的那一行 minSdkVersion 16.
                                2. 将该行更改为 minSdkVersion 21。
                                3. 保存文件。

                                另外,我必须打开android/app/build.gradle,然后将以下行添加为文件的最后一行: apply plugin: 'com.google.gms.google-services'

                                接下来,我必须打开android/build.gradle,然后在 buildscript 标签内,添加一个新的依赖项:

                                buildscript {
                                   repositories {
                                       // ...
                                   }
                                
                                   dependencies {
                                       // ...
                                       classpath 'com.google.gms:google-services:3.2.1'   // new
                                   }
                                }
                                

                                在此之后,我的应用程序终于在 android 模拟器上运行了。

                                如果您遇到困难,link 有更完整的演练。

                                【讨论】:

                                  【解决方案21】:

                                  奇怪的情况,但是multiDexEnabled 并没有帮助我,但是在删除 Gradle 目录并重建应用程序后问题解决了。

                                  【讨论】:

                                    【解决方案22】:

                                    这是我的 gradle 项目

                                    机器人{ compileSdkVersion 26 buildToolsVersion "27.0.3"

                                    defaultConfig {
                                        applicationId "net.mobilegts.candyjump"
                                        minSdkVersion 14
                                        targetSdkVersion 23
                                    
                                        ndk {
                                            moduleName "player_shared"
                                        }
                                    }
                                    sourceSets {
                                        main {
                                            jni.srcDirs = []
                                        }
                                    }
                                    
                                    buildTypes {
                                        release {
                                            minifyEnabled false
                                            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
                                        }
                                    }
                                    

                                    }

                                    【讨论】:

                                    • 这是依赖项
                                    • 依赖{ compile 'com.google.android.gms:play-services:12.0.0' 编译文件('libs/dagger-1.2.2.jar') 编译文件('libs/javax .inject-1.jar') 编译文件('libs/nineoldandroids-2.4.0.jar') 编译文件('libs/support-v4-19.0.1.jar') }
                                    • 我认为你最好再次编辑你的答案,以便其他人清楚地理解你的想法
                                    猜你喜欢
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 2018-04-15
                                    • 2018-05-04
                                    • 2018-04-24
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 2018-04-09
                                    • 1970-01-01
                                    相关资源
                                    最近更新 更多