无法合并dex

Unable to merge dex

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

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

com.android.builder.dexing.DexArchiveMergerException: Unable to merge 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'
}

____________finding_________

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

  1. 删除.gradle目录。

  2. 运行 又是你的应用。

备注

  • .gradle 目录位于项目的根文件夹中。 (您可能必须先显示隐藏文件。)
  • 每次使用 Android 3.0 更新依赖模块时,我都必须这样做。 (最近发布的 Android Studio 3 似乎已经解决了这个问题。)

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

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

不幸的是,Michel 和 Suragch 的解决方案都不适合我。

我最终要做的只是将我的 com.google.firebase:firebase-database 回滚到版本 10.0.1,因为 11.4.0 在我的应用程序 gradle 文件中导致依赖项不一致警告。

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

  1. 清洁
  2. 重建

我同意小克里斯的观点。如果您使用 Firebase 嵌入您的 AdMob 广告(或者即使您没有),play-services-analytics 会包含 play-services-ads,即使您没有将其添加为依赖项。 Google 显然在推出 11.4.0 时犯了一个错误,因为分析包括广告版本 10.0.1,而不是 11.4.0(gradle 中的鼠标悬停提示显示了这一点)。

我在顶部手动添加了 compile 'com.google.android.gms:play-services-ads:11.4.0',但只有在我禁用 Instant 运行 之后:

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

如果在包含 kotlin 支持后出现此错误,并且 none 其他解决方案有效,请尝试将应用模块 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 答案以获取更多说明。

我尝试了以上所有方法,其中 none 有帮助。最后,我找到了适合我的作品:

app/build.gradle:

android {
    defaultConfig {
       multiDexEnabled true
    }
}

如果您正在使用

compile ("org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:1.0.1")

你应该把它改成

compile ("org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:1.0.1"){
    exclude group:'org.apache.oltu.oauth2' , module: 'org.apache.oltu.oauth2.common'
}

由于客户端库已经包含公共库,因此它创建了一个重复的 class 条目并给出错误:

Unable to merge dex

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

  1. 进入SdkManager-->Android Sdk --> Sdk Tools and Update Google play services to latest version to 46.
  2. 清理项目并重建项目。

在下面添加命令:

android {
...

    dexOptions {

        jumboMode true
        javaMaxHeapSize "4g"

    }
}

对我来说,它正在更新 app\build 中的 firebase 消息传递。gradle:

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

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

在我的例子中,我导入了一个 'android_support' 库两次,所以我在 'libs' 目录下删除了一个并重建。

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

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

gradle:3.0 中,compile 配置现已弃用,应由 implementation 代替api。如需更多信息,您可以 You can read the official docs at Gradle Build Tool

The compile configuration still exists but should not be used as it will not offer the guarantees that the api and implementation configurations provide.

最好使用实现api而不是编译

只需将 compile 替换为 implementation,将 debugCompile 替换为 debugImplementationtestCompiletestImplementationandroidtestcompileandroidTestImplementation

例如:而不是这个

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" 视图左 )
  • 删除所有 构建文件夹 和 gradle 缓存。
  • 在构建菜单中,按 清理项目 按钮。
  • 任务完成后,按“生成”菜单中的重建项目按钮。

希望对您有所帮助!

您好,我几乎尝试了所有方法都遇到了同样的问题。所以,经过 6 小时的长期斗争,我终于通过逐行调试所有内容解决了问题。

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

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

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"

解决方案:

我已将 google 服务 更改为

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

并且它支持 firebase 服务。

希望有人能节省 his/her 时间。

可能的根本原因之一: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')
 }

仅添加到上述解决方案中:

确保没有重复的依赖项指向 它们的不同版本,在多个地方(甚至在同一个文件中)。

在我的案例中,问题是因为房间图书馆:

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

将其更改为:

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

成功了。

这样试试,对我有用。

在app/build.gradle:

dependencies {
    ....

    compile project(':grow-notification')

    /** Firebase Start **/
    compile 'com.google.firebase:firebase-messaging:11.4.0'
    compile 'com.firebase:firebase-jobdispatcher:0.6.0'
    /** Firebase End **/
}

apply plugin: 'com.google.gms.google-services'

在模块 grow-notification/build 中时。gradle:

dependencies {
    ....

    /** Firebase Start **/
    provided 'com.google.firebase:firebase-messaging:11.4.0'
    provided 'com.firebase:firebase-jobdispatcher:0.6.0'
    /** Firebase End **/
}

在我的例子中,问题是我在我的 build.gradle 文件中实现了同一个库两次。删除重复条目对我有用。甚至不需要清理、重建项目。

简单的解决方案对我有用:

  1. 不要compile/implement 相同依赖项的两个版本。
  2. 如果直接从 gradle 编译它,请不要使用项目的 libs 文件夹中的 .jar 文件。以上一点也适用于此。

然后清理重建项目。

我遇到了同样的问题。我通过执行清理然后从 Android Studio 3.0 菜单构建项目来解决它:

  1. 构建 -> 清理项目
  2. 构建->重建项目

在我的例子中,它是 gson-2.8.1.jar,我已将其添加到项目的 libs 文件夹中。但是 SDK 已经提供了参考。 所以没有必要添加 gson-2.8.1.jar 到 libs 文件夹。

当我取出 gson-2.8 时。1.jar 项目编译时没有这个奇怪的错误。

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

在大多数情况下,这是可行的。 这个对我有用 。 如果它也适合你,祝你好运。

步骤 1:In build.gradle(模块:应用程序) 添加这个: 默认配置{ multiDexEnabled 真 }

第 2 步:执行此操作:构建 => 清理项目

第 3 步:执行此操作:构建 => 重建项目

./gradlew :app:assembleStubLiveDebug -debug -stacktrace

或类似的(从 Android Studio 获取任务名称 (:app:assembleStubLiveDebug))。

当您添加同一库的两个版本时,这是一个常见问题..无论是在依赖项中还是在库中...这是我的情况,几乎每次都解决了这个问题..

正在安装 Google 播放服务(最新版本)+ 包括

android {
    defaultConfig {
        multiDexEnabled true
        }
}

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

在我的项目中,我有两个以上的模块和 sdk,我尝试了上面列出的所有建议或答案,例如

  • 在默认配置中添加 multiDexEnabled true
  • 清理并重建项目
  • 用实现替换编译
  • 更新所有依赖项

所有这些都暂时有效,但是当我打开项目结构(cntrl + altr + shift+ s)时我发现,在我的项目中 属性 什么都不会选择 - - 编译SDK版本,构建sdk版本 - 口味相同

我更新了所有这些并执行清理和重建项目,它对我有用。

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

我在 gradle 文件中的以下行遇到了这个问题:

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

gson 库在我的 libs 目录中,但使用的是以前的版本。 我从libs目录中删除了gson-2.3.1.jar,一切恢复正常。

    android {
        defaultConfig {
            multiDexEnabled true
            }
    }

   compile 'com.google.android.gms:play-services:11.0.4'

   compile 'com.google.android.gms:play-services-location:11.0.4'

构建清理并构建重建

注意警告!

有时您只需消除警告错误就会自动消失。请参阅以下特殊情况:


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

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

和Studio 已经警告除了dex合并问题):

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.0.2, 21.0.3. Examples include com.android.support:animated-vector-drawable:27.0.2 and com.android.support:support-v4:21.0.3

所以我明确的确定了com.android.support:support-v4的版本(详见和两个问题 (警告和与 dex 合并相关的警告)已解决:

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'

其他类似情况见下方评论。

  1. 启用

    默认配置{ multiDexEnabled 真 }

  2. 如果第 1 步不起作用,那么

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

我遇到了同样的问题,并找到了我的案例的真正原因。 之前我也把之前的所有答案都试了一遍,还是没有解决问题。 我的wear app项目中有两个模块,build.gradle如下:

磨损模块的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'.

I 运行 gradlew 使用 --stacktrace 选项获取堆栈跟踪,当出现此问题时,您可以在 gradle 控制台 window 上单击它。并且发现对jar包的依赖重复了:

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

Class farmer-motion-1.0.jar 包中的 SportSummary,阅读 official migration guide 后,我将 build.gradle 更改为:

磨损模块的build.gradle:

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

通用模块的build.gradle:

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

现在 wear 模块将具有 farmer-motion-1 的依赖。0.jar 由普通模块导出。 如果在运行期间没有对jar包的依赖,'实现'对jar包的依赖也可以改为 'compileOnly'.

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

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

删除第一行后问题解决。

在关注依赖关系之前尝试了所有这些。

下面是杀手所在:

//implementation 'org.apache.httpcomponents:httpcore:4.3.1'
//implementation 'org.apache.httpcomponents:httpmime:4.3.1'

现在正在按预期工作。

升级一些依赖后我找到了解决方案。我们应该使用最新的播放服务版本。在build.gradle[app]依赖中。

compile 'com.android.support:multidex:1.0.2'
compile 'com.google.android.gms:play-services:11.8.0'
compile 'com.google.firebase:firebase-core:11.8.0'

build.gradle[项目]中,我们应该使用最新的Google插件。

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

我也分享下面的代码以便更好地理解。

    android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.***.user"
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 26
        versionName "1.0.20"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true

        aaptOptions {
            cruncherEnabled = false
        }

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

对我来说,问题是我包含在项目中的 2 个罐子中有相同的 类。

对于这些罐子中的 1 个,我需要排除 类:

jar {
    exclude('com/files/to/exclude/**')
}

在我的例子中,错误是导入依赖项:

实施'com.firebaseui:firebase-ui:2.3.0'

我没有使用它,当我删除它清理项目并重新编译时它开始工作。当然我也做了上面的人做的是:

  • 将 complie 更改为整个实现 app.grade
  • 在整个过程中将 testcomplie 更改为 testimplementation app.grade
  • 确保我启用了 multidexenabled true
  • 更新compileSdk为26
  • 已删除 .gradle 文件(在项目中找到),清理项目并重新编译

最后...

  • 删除实现'com.firebaseui:firebase-ui:2.3.0'(因为我没有在我的代码中使用它)

可能存在重复的依赖项(可能嵌套在另一个依赖项中)。在命令行中使用 "gradlew app:dependencies" 来验证所有依赖项。

删除重复项后,问题就解决了。

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

这里有一些调试技巧。在此示例代码中,com.google.code.findbugs:jsr305:3.0.0 是有问题的库。

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

  1. 使用 --stacktrace 标志进行构建以获得更多详细信息。它会抱怨一个class,Google那个class找不到库。 Here's how you can set up Android studio to always run gradle with the --stacktrace flag.

  2. 构建

  3. 后,在 Android Studio View > Tool Windows > Gradle Console 中查看 Gradle 控制台
  4. 检查 运行 ./gradlew -q app:dependencies 的重复依赖项。每次修改 build.gradle.

  5. 时,您都可以 re-run
  6. 在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'
                }
            }
    }
    
  7. 在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'
        }
    }
    
  8. 在build.gradle,

    android {
        ...
        configurations.all {
            resolutionStrategy {
                // Completely exclude the library. Works for transitive
                // dependencies.
                exclude group: 'com.google.code.findbugs'
            }
        }
    }
    
  9. 如果您的某些依赖项在 jar 文件中,请打开 jar 文件并查看是否存在任何冲突的 class 名称。如果是,您可能需要 re-build 具有新 class 名称的罐子或查看 shading

更多背景阅读:

我花了 3 个多小时阅读和测试所有回复。 最后问题出在图书馆 "joda time",

阅读 我发现了问题,运行 gradle 具有完整堆栈跟踪的控制台 并阅读了错误详细信息。

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.xyz.name"
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 7
        versionName "1.6"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.volley:volley:1.0.0'
    implementation 'com.wang.avi:library:2.1.3'
    implementation 'com.android.support:design:27.1.0'
    implementation 'com.android.support:support-v4:27.1.0'
    implementation 'de.hdodenhof:circleimageview:2.1.0'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.0'
    implementation 'com.loopj.android:android-async-http:1.4.9'
    implementation 'com.google.firebase:firebase-messaging:11.8.0'
    implementation 'com.felipecsl.asymmetricgridview:library:2.0.1'
    implementation 'com.android.support:recyclerview-v7:27.1.0'
    implementation 'com.github.darsh2:MultipleImageSelect:3474549'
    implementation 'it.sephiroth.android.library.horizontallistview:hlistview:1.2.2'
    implementation 'com.android.support:multidex:1.0.1'
}

apply plugin: 'com.google.gms.google-services'

注意:像上面一样将所有支持库更新到 27.1.0 并删除重复项

我也遇到了这个问题。

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

检查依赖版本,如果它们不相同,然后将其设置为相同,这将解决您的问题

compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.android.support:recyclerview-v7:27.0.2'
compile 'com.android.support:cardview-v7:27.0.2'

[无法合并 DEX 已解决] 经过几个小时的堆栈溢出 我解决了“无法合并 DEX 错误”

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

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

  1. 确保存储库部分包含一个带有“https://maven.google.com”端点的 Maven 部分。例如:

    所有项目{ 存储库{ 中心() 行家{ url “https://maven.google.com” } } }

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

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

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

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

我尝试了所有其他解决方案,但没有一个适合我。最后,我通过编辑 build.gradle 使用相同的依赖版本解决了这个问题。我认为将库添加到 gradle 时会出现此问题,该库使用不同的依赖版本的支持或 google 库。

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

ps: that was old solution for me so you should use updated version of following libraries.

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

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

检查 build.gradle 中是否有 debugCompilereleaseCompile。将其更改为 debugImplementationreleaseImplementation。这就是我解决它的方法,gradle 应该通过这个成功标记为已弃用的指令,但突然它正好卡在这两个上。

确保所有相同的源库具有相同的版本号。在我的例子中,我使用了不同的 google api 版本。我对所有 google 库使用 11.6.0 但 google 地方 api 使用 10.4.0 .将 google 地方 api 更新为 11.6.0 后,问题已解决。

请注意,我的 multidex 也已为该项目启用。

将您的 buildToolsVersion 更改为其他版本并同步,将其更改回来并再次同步。它会再次起作用。

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

else if (2. 尝试删除 gradle 工作 ) 那么好

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

android.enableD8 = false

Edit 2021: This 3rd option is deprecated now, use the other options

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'

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

这可能不是您的问题,但当我不小心在项目的 dependencies{} 部分中包含两个相同(但名称不同)的库时出现此错误。

如果您在插件中使用 Firebsae,此问题将由于播放服务版本问题而出现,试试这个 `科尔多夫

cordova.system.library.4=com.google.gms:google-services:3.3.0
    cordova.system.library.5=com.google.android.gms:play-services-tagmanager:+
    cordova.system.library.6=com.google.firebase:firebase-core:16.0.1
    cordova.system.library.7=com.google.firebase:firebase-messaging:17.1.0
    cordova.system.library.8=com.google.firebase:firebase-crash:16.0.1
    cordova.system.library.9=com.google.firebase:firebase-config:16.0.0
    cordova.system.library.10=com.google.firebase:firebase-perf:16.0.0 

像这样改变你的project.properties,那么这个问题就会得到解决 Reference

在我的例子中,一个库造成了这个问题,库已成功添加到项目中,但是当我 运行 我的应用程序时,它显示了这个错误。所以如果你也遇到这种情况,你可以去 github 检查问题或提出新问题。如果您没有找到关于该库的任何解决方案,我建议您更换它。

这对我有用-

在platforms\android\build最后添加代码。gradle

configurations.all { 
 resolutionStrategy{
    force 'com.android.support:support-v4:26.0.0'
    }

    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == 'com.android.support') {
       if (!requested.name.startsWith("multidex")) {
      details.useVersion '26.0.0'
     }
   }
  }
}

我有同样的问题。 我刚刚启用 Instant Run(它被禁用)然后我的项目工作了。 您可以在-

中找到它
File->Settings-> Build,Execution,Deployment->Instant Run

在 Android studio 3.5 Instant 运行 中已被删除。 请参阅

将此添加到您的 app/build.gradle。它会起作用

实施'com.android.support:design:27.1.0'

只需在下面添加 build.gradle,

默认配置{ multiDexEnabled 真 }

这还没有在这里分享,所以希望它可以帮助别人。

按照这个 link 为我解决了同样的问题。

首先,我会注意到我不必将 multiDexEnabled 设置为 true。

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

dependencies:
  flutter:
    sdk: flutter
  cloud_firestore: ^0.8.2 

和 运行 flutter packages get 在我的 IDE 终端。

我还必须更改最低目标 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 有更完整的演练。

另外,请注意,我不必将 multiDexEnabled 设置为 true。

在我的例子中,以下工作

implementation 'com.google.android.gms:play-services-ads:17.2.0'
implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-analytics:16.5.0'
implementation 'com.google.firebase:firebase-messaging:18.0.0'

在我的 app 模块中。问题是他们的 firebase-analytics 最新库,它不能与其他最新库一起正常工作。

只需在app/build中添加这一行即可。gradle

defaultConfig {
       multiDexEnabled true 
    }

转到 Unity >> Edit Project settings>>player,查找 Minify 部分,然后选择 Proguard 选项

随着 无法合并 dex 我有一个错误 java.lang.OutOfMemoryError: Java heap space 构建项目时。

gradle.properties 文件中,我有以下注释行:

# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

取消注释此行并将堆内存增加到 3g 解决了问题:

org.gradle.jvmargs=-Xmx3g -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8