Gradle上的Android应用程序:无法合并dex

我在Android Studio中遇到了一个错误:

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

我使用Gradle和Kotlin。

 Android Studio 3.0.1. Windows 10 Pro 64. 

我有研究这个主题,所以我尝试了一些解决方案,从这里 :

  1. 清理和重建项目。 没有变化。
  2. 将gradle中的“编译”更改为“实现”。 没有变化。
  3. 删除.gradlebuild目录。 没有变化。
  4. 在gradle的defaultConfig中添加multiDexEnabled true 。 在这种情况下,我有另一个错误:

      Execution failed for task ':app:transformClassesWithMultidexlistForDebug'. > java.io.IOException: Can't write [G:\work\myapp\app\build\intermediates\multi-dex\debug\componentClasses.jar] (Can't read [C:\Users\Public\.gradle\caches\transforms-1\files-1.1\support-core-utils-27.0.2.aar\e1c9881d763269b67bf59dc03d19c305\jars\classes.jar(;;;;;;**.class)] (Duplicate zip entry [classes.jar:android/support/v4/content/PermissionChecker$PermissionResult.class])) 

我的build.gradle

 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 27 defaultConfig { multiDexEnabled true applicationId "myapp" minSdkVersion 19 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { debug { applicationIdSuffix ".dev" } staging { debuggable true applicationIdSuffix ".sta" } preproduction { applicationIdSuffix ".pre" } release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } repositories { jcenter() mavenCentral() } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation 'com.android.support:appcompat-v7:27.0.2' implementation 'com.basecamp:turbolinks:1.0.7' 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' // The Client SDK resides on jCenter implementation 'com.twilio:client-android:1.2.21' } 

我的AndroidManifest

               

我错过了什么?

UPD
看来我发现问题出现之后的那一点。 这是在添加Twilio配置后。 我使用了这个手册: Twilio和Android

这是所有已经做的改变:

  1. 添加到build.gradle

      implementation 'com.twilio:client-android:1.2.21' 
  2. 添加到AndroidManifest

       
  3. 添加到proguard-rules.pro

      # Twilio Client -keep class com.twilio.** { *; } # Apache HttpClient -dontwarn org.apache.http.** 

就这样。 在这个变化之前,我没有这个错误

如果你想为你的libs dir读取一个.aar文件,可以在你的build.gradle中使用它:

 repositories{ flatDir{ dirs 'libs' } } dependencies { compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar') } 

但似乎你的问题是multidex(意味着你有超过65K的方法在你的项目中,所以使用这个:

 android { defaultConfig { multiDexEnabled true } } 

如果你的minSdk <20,那么也使用这个:

 compile 'com.android.support:multidex:1.0.1' 

希望能帮助到你!!!