Search code examples
androidandroidxandroid-multidex

Androidx migration getting DexArchiveMergerException: Unable to merge dex


I've migrated to androidx and after a ton of time with it, I can't move away from the error below. I've integrated multidex but I still get the error.

This is the exception:

FAILURE: Build failed with an exception.

What went wrong:

Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForBetaDebug'. java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

I can't make it work. Here's my build.gradle. Any ideas?

    buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

android {

    compileSdkVersion 28
    buildToolsVersion '27.0.3'

    signingConfigs {
        releaseSign {
            storeFile file("$rootProject.projectDir/keystore_release.jks")
            storePassword 'xxxxxxx'
            keyAlias 'xxxxxxx'
            keyPassword 'xxxxxxx'
        }
    }

    // Butterknife requires Java 8.
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "com.xxxxxxx.xxxxxxx"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 10
        versionName "1.0.10"

        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true

        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            signingConfig signingConfigs.releaseSign
        }
    }

    flavorDimensions "default"
    productFlavors {
        beta {
            applicationId "com.xxxxxxx.xxxxxxx.beta"

            dimension "default"
            resValue "string", "app_name", "xxxxxxx Beta"

            buildConfigField "String", "BASE_URL", '"http://xxxxxxx.net"'
            buildConfigField "Boolean", "IS_BETA", "true"
            buildConfigField "String", "TENANT", '"xxxxxxx"'

        }
        production {
            applicationId "com.xxxxxxx.driver"

            dimension "default"
            resValue "string", "app_name", "Driver"

            buildConfigField "String", "BASE_URL", '"http://apxxxxxxx"'
            buildConfigField "Boolean", "IS_BETA", "false"
            buildConfigField "String", "TENANT", '"xxxxxxx"'
        }
    }

    packagingOptions {
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/notice'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license'
        exclude 'META-INF/license.txt'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha05'

    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.google.android.material:material:1.1.0-alpha06'

    implementation 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'

    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'me.grantland:autofittextview:0.2.1'
    implementation 'com.android.volley:volley:1.1.1'
    implementation 'org.greenrobot:eventbus:3.1.1'
    implementation 'com.github.ybq:Android-SpinKit:1.2.0'

    implementation 'com.google.android.gms:play-services-location:16.0.0'

    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.0'

    implementation 'com.google.firebase:firebase-core:16.0.9'
    implementation 'com.google.firebase:firebase-messaging:18.0.0'

    implementation 'androidx.multidex:multidex:2.0.1'

    def work_version = "2.0.1"
    implementation "androidx.work:work-runtime:$work_version"
// Optional - RxJava2 support
    implementation "androidx.work:work-rxjava2:$work_version"

    def futures_version = "1.0.0-beta01"
    implementation "androidx.concurrent:concurrent-futures:$futures_version"
}

apply plugin: 'com.google.gms.google-services'

Solution

  • Difficult to see where is the problem. Try one of the following:

    1- update google services to latest version

    2- force all your external libraries using the old support lib to use the same version, add before dependencies:

    configurations.all {
        resolutionStrategy {
            eachDependency { DependencyResolveDetails details ->
                if (details.requested.group == 'com.android.support' && details.requested.name != 'multidex') {
                    details.useVersion "VERSION_HERE" // try 28.0.0
                }
            }
        }
    }
    
    dependencies {
        ...
    }
    

    this will make easier for gradle to replace all support lib classes to androidx classes

    3- "buildToolsVersion" is not needed anymore, gradle will choose the correct version for you, do you need this specific version?

    4- I stop using Fabric due to it caused a lot of compilation problems in my apps due to internal dependencies. If it is possible, try removing it.