Androidx 迁移获取 DexArchiveMergerException:无法合并 dex

Androidx migration getting DexArchiveMergerException: Unable to merge dex

我已经迁移到 androidx,并且在使用它很长时间后,我无法摆脱下面的错误。我已经集成 multidex 但我仍然收到错误。

这是例外情况:

FAILURE: Build failed with an exception.

出了什么问题:

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

我做不到。这是我的 build.gradle。有什么想法吗?

    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'

很难看出问题出在哪里。尝试以下操作之一:

1- 将 google 服务更新到最新版本

2-强制所有使用旧支持库的外部库使用相同版本,在依赖项之前添加:

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 {
    ...
}

这将使 gradle 更容易将所有支持库 类 替换为 androidx 类

3-"buildToolsVersion"不再需要,gradle会为您选择正确的版本,您需要这个特定版本吗?

4- 我停止使用 Fabric,因为它由于内部依赖性导致我的应用程序出现很多编译问题。如果可能,请尝试将其删除。

包中应该没有com.android.support。最好在 gradle.properties 文件中启用 Jetifier,而不是修复过时的库,甚至不应该包含这些库:

android.enableJetifier=true
android.useAndroidX=true