android studio 3.0.1 显示无法合并 dex 为什么?

android studio 3.0.1 showing unable to merge dex why?

导入项目后androidstudio2.x.x升级到androidstudio 3.0.1gradle构建成功。但是在安装应用程序时显示错误消息

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

下面给出了我的应用程序 grdle

为什么会显示此错误以及如何解决?无法在模拟器上安装应用程序。

已经在 android studio 中尝试了清理和重建选项,但它没有解决问题

尝试以下

如果您的 minSdkVersion 设置为 21 或更高

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

如果您的 minSdkVersion 设置为 20 或更低,

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

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

如果仍未解决,请尝试浏览这些帖子 , android_dev or

要解决此 Dex 问题,只需实施一个依赖项即可。 当我们使用来自同一服务器的多个不同服务时,会发生此问题。假设我们在项目中使用 ads 和 Firestore,并且两者都有一个存储库 maven。所以我们需要在存储库上调用不同的数据,我们需要 dex 依赖来实现。 新的更新依赖:-

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

或在编译中使用 testCompile,例如:

implementation 'com.google.android.gms:play-services-ads:15.0.1'
    testCompile 'com.google.firebase:firebase-firestore:17.0.1'

并使multiDexEnabled true; 100 % 它会起作用!