6

I am trying to run my application on my phone but during build time it fails with the following error:

Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.

java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

What I tried so far, but to no avail: -> Clean and rebuild (fails on rebuild) -> Remove ./gradle file and all project build and cache files, then cache invalidation

My project gradle file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.51'
    repositories {
        jcenter()
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta6'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        jcenter()
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

My app Gradle file:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

def daggerVersion = '2.11'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.connectus.connectus"
        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'
        }
    }
    packagingOptions {
        exclude 'META-INF/INDEX.LIST'
    }
}

kapt {
    generateStubs = true
}

dependencies {
    /**
     * Kotlin
     */
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile 'org.jetbrains.anko:anko-support-v4:0.10.1'

    /**
     * Unit testing
     */
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.powermock:powermock-module-junit4:1.6.2'
    testCompile 'org.powermock:powermock-api-mockito:1.6.2'

    /**
     * Android views and widgets
     */
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'

    /**
     * Retrofit
     */
    compile 'com.squareup.retrofit2:retrofit:2.+'
    compile 'com.squareup.retrofit2:converter-gson:2.+'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.+'
    compile 'io.reactivex:rxjava:1.0.4'
    compile 'io.reactivex:rxandroid:0.24.0'

    /**
     * Glide
     */
    compile 'com.github.bumptech.glide:glide:3.7.0'

    /**
     * Dagger 2
     */
    kapt "com.google.dagger:dagger-compiler:$daggerVersion"
    compile "com.google.dagger:dagger:$daggerVersion"
    provided 'org.glassfish:javax.annotation:10.0-b28'

    /**
     * Butterknife for view injection
     */
    compile 'com.jakewharton:butterknife:8.8.1'
    kapt 'com.jakewharton:butterknife-compiler:8.8.1'

    /**
     * GSON
     */
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'org.powermock:powermock-api-mockito:1.6.2'

    /**
     * Facebook SDK
     */
    compile 'com.facebook.android:facebook-login:[4,5)'

    /**
     * UI Testing
     */
    androidTestCompile 'com.android.support:support-annotations:25.3.1'
    androidTestCompile 'junit:junit:4.12'

    testCompile 'com.google.dexmaker:dexmaker:1.2'
    testCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
        exclude module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') {
        exclude module: 'support-annotations'
        exclude module: 'appcompat-v7'
        exclude module: 'support-v4'
        exclude module: 'support-v13'
        exclude module: 'recyclerview-v7'
        exclude module: 'design'
    }
    androidTestCompile('com.android.support.test:runner:0.3') {
        exclude module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test:rules:0.3') {
        exclude module: 'support-annotations'
    }
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
}

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
repositories {
    mavenCentral()
}

I am using Kotlin and Android Studio 3.0.0 beta 6. What is the problem?

UltimateDevil
  • 2,807
  • 2
  • 18
  • 31
Andrej Naumovski
  • 573
  • 2
  • 9
  • 23
  • I would invalidate cache and restart, then retry. If problem does not get solved, then you must check your dependencies. sometimes AAR dependencies has internal JAR libraries with the same package name, and when Android Studio wants to merge these JAR files together, hits an error which is merge exception. I would search for a library dependecy which has a JAR inside of it ;) – Bardya Momeni Oct 01 '17 at 15:30
  • I have the same error as OP but slightly different code, I had compileSdkVersion set to 26 and the com.android.support libraries still on 25.3.1. Updating those to 26.0.2 fixed my the dex merge error for me. – Sherlock Oct 10 '17 at 09:53

3 Answers3

3

Although I am not completely sure about the cause for this, it seems, after doing some digging, that there is a version clash between the annotation libraries included by jetbrains in the kotlin plugin and somewhere else. You can verify this by running

gradle :app:assembleDebug --stacktrace

which gives, at the end,

Caused by: com.android.dex.DexException: Multiple dex files define Lorg/intellij/lang/annotations/JdkConstants$PatternFlags;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:661)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:616)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:598)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:198)
    at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:61)
    ... 1 more

I solved this by excluding the annotations library from kotlin dependency. (Clean and rebuild might be needed after this)

implementation ("org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version") {
    exclude group: 'org.jetbrains', module: 'annotations'
}

This works for me, but I am not sure about how good this solution is.

aksh1618
  • 2,245
  • 18
  • 37
  • Hi I'm having this exact issue after upgrading to beta 7, can you please provide your build.gradle files – Senzo Malinga Oct 04 '17 at 10:35
  • @SenzoMalinga Just replace your kotlin dependency with the one I specified in my answer. Make sure you clean and rebuild. – aksh1618 Oct 04 '17 at 18:15
  • 2
    Hi thanks, ended up running "./gradlew app:dependencies" to show me what lib is using what, then after that I excluded each transitive dependency that was older and duplicated and I didn't need – Senzo Malinga Oct 05 '17 at 11:03
2

add this

defaultConfig {
    multiDexEnabled true
}

like this:

android {
compileSdkVersion 25
buildToolsVersion "26.0.2"
defaultConfig {
    applicationId "com.connectus.connectus"
    minSdkVersion 16
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

defaultConfig {
   multiDexEnabled true
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/INDEX.LIST'
}

}

Et9
  • 389
  • 3
  • 7
0

I managed to fix it by just including the base play services and not the full ones:

-    compile 'com.google.android.gms:play-services:11.6.0'
+    compile 'com.google.android.gms:play-services-base:11.6.0'

and removed the appindexing dependencies in my Activity:

-import com.google.android.gms.appindexing.AppIndex;
-import com.google.android.gms.common.api.GoogleApiClient;

(though this might have been a leftover anyway from my upgrade to API 27).

B Porr
  • 305
  • 2
  • 11