2

I have an error with my app in Android studio:

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

I use Gradle and Kotlin.

Android Studio 3.0.1.  
Windows 10 Pro 64. 

I have research this theme on SO and I have tried some solution from here, like that:

  1. Clean and rebuild project. No changes.
  2. Change 'compile' in gradle to 'implementation'. No changes.
  3. Delete .gradle and build directories. No changes.
  4. Add multiDexEnabled true in defaultConfig in gradle. In this case, I have another error:

     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]))
    

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

My 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>

What am I missing?

UPD
It seems I found out the point after that the problem has appeared. It was after adding Twilio configuration. I used this manual: Twilio and Android

This is all changes that have been done:

  1. Add to build.gradle:

      implementation 'com.twilio:client-android:1.2.21'
    
  2. Add to AndroidManifest:

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

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

That's all. Before this changes I didn't have this error

NaSt
  • 301
  • 1
  • 5
  • 16
  • do you have anything under libs folder? – global_warming Dec 12 '17 at 10:03
  • Invalidate caches/restart? – Aswin P Ashok Dec 12 '17 at 10:05
  • @global_warming, no, it seems I don't have the libs directory in the project structure – NaSt Dec 12 '17 at 10:08
  • @AswinPAshok, tried restart, no result :( – NaSt Dec 12 '17 at 10:08
  • I think you have conflicting dependencies. You have included `com.android.support:appcompat-v7:27.0.2` in your Gradle, but turbolinks library is [compiled with support library `25.1.1`](https://github.com/turbolinks/turbolinks-android/blob/master/turbolinks/build.gradle). So try compiling without turbolink library. Still not sure it is the reason for your problem. Read [this](https://blog.mindorks.com/avoiding-conflicts-in-android-gradle-dependencies-28e4200ca235) to avoid dependancy conflicts – Aswin P Ashok Dec 12 '17 at 10:38
  • @AswinPAshok it seems I found the point when the problem has appeared, and it's not turbolinks configuring – NaSt Dec 12 '17 at 11:28

1 Answers1

0

Use this in your build.gradle in app Module if you want to read an .aar file for your libs dir:

repositories{
      flatDir{
              dirs 'libs'
       }
 }

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

But it seems that your problem is multidex(means that you have more than 65K methods in your project so use this:

android {
    defaultConfig {
        multiDexEnabled true
    }
}

and if your minSdk < 20 then use this as well:

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

Hope it helps!!!

matrix
  • 314
  • 4
  • 24
  • Well, I have added `multiDexEnabled true` and `compile 'com.android.support:multidex:1.0.1'`, but it doesn't help, I see the first error: `Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. > com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex` – NaSt Dec 12 '17 at 10:54
  • But I'm sorry, I didn't understand about .aar files, it seems I don't have any .aar files in the project structure – NaSt Dec 12 '17 at 10:54
  • ok do this. Go to the home directory "C:\Users\your user\.gradle\ and delete the entire cache folder. The go to the project folder and delete the .gradle folder. Then open Android Studio and try to sync – matrix Dec 12 '17 at 10:59
  • I did it all, Android Studio had successful sync, but when I run the project I see the same error – NaSt Dec 12 '17 at 11:09
  • It seems I found the point when this problem has appeared. I have edited the post – NaSt Dec 12 '17 at 11:29