You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建APK时出现dexBuilderDebug失败错误,求原因与解决方案

构建APK时dexBuilderDebug失败的问题排查与解决

问题概述

无法构建项目APK,每次尝试都会触发dexBuilderDebug FAILED错误,错误集中在Dex构建的类合并、转换阶段。

以下是项目的Gradle配置文件:

apply plugin: 'com.android.application'
apply plugin: 'realm-android'

android {
    compileSdkVersion 34

    defaultConfig {
        applicationId ""
        minSdkVersion 21
        targetSdkVersion 34
        versionCode 1
        versionName "1.0.0"
        multiDexEnabled true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    implementation 'androidx.recyclerview:recyclerview:1.3.2'

    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.google.android.material:material:1.11.0'

    //okhttp logging interceptor
    implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0'

    //Retrofit Library
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

    implementation 'io.github.shashank02051997:FancyToast:2.0.2'

    //External Libraries-------------------
    implementation 'io.realm:android-adapters:1.4.0'
    implementation 'com.github.ceryle:RadioRealButton:v1.3'
    implementation 'com.yarolegovich:lovely-dialog:1.1.1'
    implementation 'de.hdodenhof:circleimageview:3.1.0'
}

可能的原因及解决方案

1. 排查依赖冲突

dexBuilder失败最常见的原因是依赖冲突——多个库包含相同的类文件,导致合并时冲突:

  • 执行命令查看完整依赖树:
    # Windows
    gradlew app:dependencies
    # Mac/Linux
    ./gradlew app:dependencies
    
  • 查找重复的依赖(比如不同版本的androidx库、okhttp相关类),用exclude排除重复项。例如,如果RadioRealButton包含了重复的appcompat依赖,修改为:
    implementation('com.github.ceryle:RadioRealButton:v1.3') {
        exclude group: 'androidx.appcompat'
    }
    

2. 完善MultiDex配置

虽然已开启multiDexEnabled true,但仍需确保配置完整:

  • 添加MultiDex依赖:
    implementation 'androidx.multidex:multidex:2.0.1'
    
  • 若自定义了Application类,让它继承MultiDexApplication;如果无法继承,在attachBaseContext方法中添加:
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    

3. 升级Realm插件与依赖

当前Realm插件版本可能与compileSdk 34不兼容,导致Dex构建异常:

  • 在项目根目录的build.gradle中更新Realm插件版本(替换为最新稳定版):
    buildscript {
        dependencies {
            classpath "io.realm:realm-gradle-plugin:10.15.1"
        }
    }
    
  • 同步更新io.realm:android-adapters的版本,保持与插件版本一致。

4. 清理缓存并重新构建

缓存损坏也可能导致构建失败,执行以下操作:

  • 清理Gradle缓存并重新构建:
    gradlew clean build --refresh-dependencies
    
  • 或者在Android Studio中点击File -> Invalidate Caches... -> Invalidate and Restart。

5. 检查本地libs目录

libs目录下的jar包可能与远程依赖重复(比如存在okhttp的jar会和logging-interceptor冲突)。建议移除libs中不必要的jar包,优先使用远程依赖管理。

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 11:30:13