任务的Android Studio 3.0执行失败:无法合并dex

问题描述 投票:60回答:23

android studio在构建执行时出现构建错误,如下所示:

错误:任务':app:transformDexArchiveWithExternalLibsDexMergerForDebug'的执行失败。 java.lang.RuntimeException:java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:无法合并dex“

我的app:build.gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.pdroid.foodieschoice"
        minSdkVersion 16
        targetSdkVersion 25
        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.constraint:constraint-layout:1.0.2'
    compile 'com.firebaseui:firebase-ui-auth:2.3.0'

    testCompile 'junit:junit:4.12'
}
configurations.all {
    resolutionStrategy {
        force 'com.android.support:appcompat-v7:26.0.1'
        force 'com.android.support:support-compat:26.0.1'
        force 'com.android.support:support-core-ui:26.0.1'
        force 'com.android.support:support-annotations:26.0.1'
        force 'com.android.support:recyclerview-v7:26.0.1'
    }
}

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

任何解决方案

编辑:我已经通过firebase github网站完成了样本并解决了

android android-studio-3.0 build-error
23个回答
56
投票

对于基于Cordova的项目,再次构建之前运行cordova clean android,正如@mkimmet建议的那样。


1
投票

对我来说,修复此错误的原因是在我的应用程序的gradle依赖项中更改了一行。

由此:

compile('com.google.android.gms:play-services-gcm:+') {
    force = true
}

对此:

compile('com.google.android.gms:play-services-gcm:11.8.0') {
    force = true
}

1
投票

还要确保您的应用程序是MultiDexApplication的子类

import android.support.multidex.MultiDexApplication

class App : MultiDexApplication()

或者如果不是子类化Application类,则添加到AndroidManifest.xml

<application
  android:name="android.support.multidex.MultiDexApplication"

1
投票

这对我有用。干净的准备和运行。

cordova clean android;
ionic cordova prepare andriod;
ionic cordova run andriod;

希望能帮助到你。


0
投票

您是介于SDK还是平台更新之间?如果是完全完成那些,然后尝试继续。我通常会更新需要更新的各个软件包,以便进行有时可能高达2.5 GB的整个更新。执行此完整更新有时会失败。一旦我升级到Android Studio 3.0,我就有了一些SDK更新,并且因为所有软件包都没有更新而得到上述错误。一旦我更新了所有包,上面的错误就消失了。


0
投票

在我的情况下,将firebase库版本从2.6.0更改为2.4.2修复了该问题


0
投票

对我来说,问题是在模块中使用Java 1.8,但在app模块中没有。我将此添加到app build gradle并工作:

android{
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

0
投票

同样的问题。我启用了multidex:defaultConfig {applicationId“xxx.xxx.xxxx”minSdkVersion 24 targetSdkVersion 26 multiDexEnabled true

我已清除缓存,运行gradle clean,rebuild,make,尝试确保导入的库中没有冲突(删除所有传递依赖项)并进行所有更新。仍然:

错误:任务':app:transformDexArchiveWithExternalLibsDexMergerForDebug'的执行失败。 java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:无法合并dex

事实证明构建不喜欢:实现'org.slf4j:slf4j-android:1.7.21'


0
投票

当Android Studio版本为3.0.1,Gradle版本为4.1且Android PluginVersion为3.0.0时,会遇到此问题。然后我降级Gradle版本是3.3,Android Android是零,没有这样的问题。


0
投票

当请求的SDK版本与依赖项不匹配时,我有这个。您可以单击突出显示的行,然后单击红色灯泡进行修复。无需找到实际版本,只需让IDE为您解决。并将google repo添加到maven配置。


0
投票

我收到了同样的错误,在我的情况下,当我修复了与我当前正在构建的构建版本不同的构建错误时,错误得到了解决。

我正在构建我看起来很好没有错误的构建变体,但尝试调试导致app:transformDexArchiveWithExternalLibsDexMergerForDebug错误。一旦我切换到构建其他构建变体,我在构建过程中捕获了我的错误并修复了它。这似乎解决了我所有构建变体的app:transformDexArchiveWithExternalLibsDexMergerForDebug问题。

请注意,此错误不在引用的外部模块中,而是在引用外部模块的构建变体的不同源集中。希望这对那些可能和我一样的人有所帮助!


40
投票

添加可能与compileSdkVersion不兼容的外部库时会发生此错误。

添加external library时要小心。

我花了2天时间解决这个问题,最后按照这些步骤解决了问题。

  • 确保所有支持库与compileSdkVersionbuild.gradle(Module:app)相同,在我的情况下是26enter image description here
  • 在defaultConfig类别中,键入multiDexEnabled true。这是重要的部分。 multiDexEnabled True image
  • 转到文件|设置|构建,执行,部署|即时运行并尝试启用/禁用即时运行热插拔...然后单击确定Enable Instant Run to Hot swap... Image
  • Sync您的项目。
  • 最后,转到Build |点击Rebuild Project。
  • 注意:重建项目首先清理然后构建项目。

0
投票

我已完成并通过在我的代码中执行此作业来解决此问题

打开 - > build.gradle更改值

compile 'com.google.code.gson:gson:2.6.1'

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

0
投票

要删除此Dex问题,只需实现一个依赖项。当我们从同一服务器使用多个不同的服务时,会发生此问题。假设我们在项目中使用广告和Firestore,并且都有一个存储库maven。所以我们需要在存储库上调用不同的数据,我们需要dex依赖来实现。新的更新依赖关系: -

 implementation 'com.android.support:multidex:1.0.3'

我相信它会永久解决你的问题


0
投票
1)Please add multiDexEnabled true
2)if you get compilation error then check your app level build.gradle weather same dependencies are implemented with different versions.
3)Remove one if you find same dependencies.

android {compileSdkVersion 28 defaultConfig {

    minSdkVersion 17
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
hope it works fine :) Happy Coding

0
投票

避免类似错误的最简单方法是:

- 改变库combilesdkversion与app compilesdkversion相同

- 更改库的supportLibrary版本与build.gradle(app)相同


26
投票

尝试在gradle中添加它

    android {
      defaultConfig {
        multiDexEnabled true
        }
   }

20
投票

只需尝试“构建 - >清洁项目”。这解决了我的问题。


12
投票

解析度:

请参阅to this link:由于有各种选项可以根据minSdkVersion关闭警告,因此它设置为低于20:

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

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

如果build.gradle集中的minSdkVersion大于20,请使用以下命令关闭警告:

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

更新依赖项如下:

     dependencies {
        implementation 'com.android.support:multidex:1.0.3'
     }

唯一的区别是依赖关系中的关键字:

minSdkVersion低于20:使用编译

minSdkVersion 20以上:使用实现

  1. 我希望这是有帮助的,如果它解决了你的问题请求upvote,谢谢你的时间。
  2. 另外有关更多信息,为什么会出现这种情况,请阅读链接中的第一段,它会详细说明原因?这个警告意味着什么

6
投票

对我来说,补充一下

multiDexEnabled true

packagingOptions {
        exclude 'META-INF/NOTICE' 
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/notice'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license'
        exclude 'META-INF/license.txt'
    }

进入应用程序级别Build.gradle文件解决了这个问题


4
投票

我尝试了很多上面提到的解决方案,包括multiDexEnabled,但是没有一个对我有用。

这是适合我的解决方案 - 在app \ build.gradle文件中复制此代码

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support' && requested.name != 'multidex') {
            details.useVersion "${rootProject.ext.supportLibVersion}"
        }
    }
}

确保在运行代码之前运行gradlew clean


2
投票

同样的事情发生在我身上,发现我正在更新到AS 3.0.1,在我做了更新,清理并重建了应用程序之后,一切都已修复。


2
投票

使用multiDexEnabled如下所示。

{
 minSdkVersion 17
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}

implementation 'com.android.support:multidex:1.0.3'

这个解决方案对我有用。

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