You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

Android Unable to merge dex错误求助:开启multiDex仍无法解决

解决DexArchiveMergerException: Unable to merge dex问题

我仔细看了你遇到的错误和gradle配置,虽然你已经开启了multiDexEnabled true并尝试了clean/rebuild,但还是有几个配置问题导致了dex合并失败,下面是具体的修改点和解决步骤:

问题分析

1. 重复的依赖库

你的模块build.gradle里,com.google.firebase:firebase-database:11.2.0被重复添加了两次,这会导致生成重复的dex文件,直接触发合并异常。

2. FirebaseUI与Firebase版本不兼容

你使用的com.firebaseui:firebase-ui-database:2.1.1对应的Firebase版本是11.0.4,但你其他Firebase库用的是11.2.0,版本不匹配会引发依赖冲突,进而导致dex合并失败。

3. 引入了完整的Google Play Services包

com.google.android.gms:play-services:11.2.0是完整的服务包,包含了所有Google Play服务模块,会引入大量不必要的代码,不仅增加APK体积,还大大提升了dex合并出错的概率。

修改后的模块build.gradle配置

我帮你调整了依赖,解决了上述问题:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.ryan.mychatapp"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    //noinspection GradleCompatible
    implementation 'com.google.firebase:firebase-storage:11.2.0'
    // 替换完整play-services包为实际需要的子模块(这里假设你需要auth服务)
    implementation 'com.google.android.gms:play-services-auth:11.2.0'
    implementation 'com.android.support:support-v4:26.1.0'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.google.firebase:firebase-database:11.2.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.firebase:firebase-auth:11.2.0'
    implementation 'com.google.firebase:firebase-messaging:11.2.0'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:multidex:1.0.1'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'de.hdodenhof:circleimageview:2.2.0'
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.+'
    implementation 'com.squareup.okhttp:okhttp:2.5.0'
    // 升级FirebaseUI到与Firebase 11.2.0兼容的版本
    implementation 'com.firebaseui:firebase-ui-database:2.3.0'
    implementation 'id.zelory:compressor:2.1.0'
}

额外优化建议

你的根目录build.gradle里,com.google.gms:google-services:3.1.0可以升级到3.2.0,它对新版本的Firebase库兼容性更好:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        // 升级google-services版本
        classpath 'com.google.gms:google-services:3.2.0'
    }
}

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

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

最后执行步骤

  1. 替换上面修改后的两个gradle文件内容
  2. 执行File -> Clean Project
  3. 再执行Build -> Rebuild Project

这样应该就能解决dex合并的问题了。

内容的提问来源于stack exchange,提问作者user2399158

火山引擎 最新活动