【问题标题】:Android application on Gradle: Unable to merge dexGradle 上的 Android 应用程序:无法合并 dex
【发布时间】:2017-12-12 09:57:14
【问题描述】:

我在 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. 

我在 SO 上研究过这个主题,并尝试了here 的一些解决方案,例如:

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

     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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tunnll.passenger">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name="com.twilio.client.TwilioClientService" 
                           android:exported="false" android:stopWithTask="true"/>   
</application>

我错过了什么?

UPD
问题出现后,我似乎找到了重点。这是在添加 Twilio 配置之后。我用过这本手册:Twilio and Android

这是已完成的所有更改:

  1. 添加到build.gradle:

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

      <service android:name="com.twilio.client.TwilioClientService" 
                   android:exported="false" android:stopWithTask="true"/>
    
  3. 添加到proguard-rules.pro

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

就是这样。在此更改之前,我没有此错误

【问题讨论】:

  • 你的libs文件夹下有什么吗?
  • 缓存无效/重启?
  • @global_warming,不,我的项目结构中好像没有libs目录
  • @AswinPAshok,尝试重启,没有结果:(
  • 我认为你有冲突的依赖关系。您已在 Gradle 中包含 com.android.support:appcompat-v7:27.0.2,但 turbolinks 库为 compiled with support library 25.1.1。因此,请尝试在没有 turbolink 库的情况下进行编译。仍然不确定这是您问题的原因。阅读this 以避免依赖冲突

标签: android android-gradle-plugin kotlin twilio


【解决方案1】:

如果您想读取.aar 文件,请在应用模块中的build.gradle 中使用它 对于您的库目录:

repositories{
      flatDir{
              dirs 'libs'
       }
 }

dependencies {
   compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
}

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

android {
    defaultConfig {
        multiDexEnabled true
    }
}

如果你的 minSdk

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

希望对你有帮助!!!

【讨论】:

  • 嗯,我已经添加了multiDexEnabled truecompile 'com.android.support:multidex:1.0.1',但是没有用,我看到了第一个错误:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. &gt; com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
  • 但是很抱歉,我对 .aar 文件不太了解,我的项目结构中似乎没有任何 .aar 文件
  • 好的,这样做。进入主目录“C:\Users\your user\.gradle\ 并删除整个缓存文件夹。进入项目文件夹并删除.gradle文件夹。然后打开Android Studio并尝试同步
  • 我都做了,Android Studio 同步成功,但是当我运行项目时,我看到了同样的错误
  • 看来我找到了这个问题出现的重点。我已经编辑了帖子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 1970-01-01
  • 2018-04-11
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
  • 1970-01-01
相关资源
最近更新 更多