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

Flutter项目Gradle D8编译错误:合并含'j$'前缀类的DEX文件失败

Flutter Gradle构建失败:mergeExtDexDebug错误处理

错误信息

构建失败时的核心错误输出:

> Task :app:mergeExtDexDebug FAILED
ERROR: /home/abadidi/.gradle/caches/8.8/transforms/56c9a3fbcd208cf82b6416faec55188a/transformed/jetified-desugar_jdk_libs-2.0.4/classes.dex: D8: Attempt at compiling intermediate artifact without its context
ERROR: D8: com.android.tools.r8.internal.Ge: Merging DEX file containing classes with prefix 'j$.' with other classes, except classes with prefix 'java.', is not allowed: _COROUTINE.ArtificialStackFrames, _COROUTINE.CoroutineDebuggingKt, _COROUTINE._BOUNDARY, _COROUTINE._CREATION, android.annotation.SuppressLint, android.annotation.TargetApi, android.support.customtabs.ICustomTabsCallback$Default, android.support.customtabs.ICustomTabsCallback$Stub$Proxy, android.support.customtabs.ICustomTabsCallback$Stub, android.support.customtabs.ICustomTabsCallback$_Parcel, android.support.customtabs.ICustomTabsCallback, android.support.customtabs.ICustomTabsService$Default...

错误详情:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeExtDexDebug'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.DexMergingTaskDelegate
   > There was a failure while executing work items
      > A failure occurred while executing com.android.build.gradle.internal.tasks.DexMergingWorkAction
         > com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:

当前配置文件

app/build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'dev.flutter.flutter-gradle-plugin'
    id 'com.google.gms.google-services' apply false
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader("UTF-8") { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
if (flutterVersionCode == null) {
    flutterVersionCode = "1"
}

def flutterVersionName = localProperties.getProperty("flutter.versionName")
if (flutterVersionName == null) {
    flutterVersionName = "1.0"
}

android {
    namespace = "com.example.example"
    compileSdk = 34
    ndkVersion = "26.1.10909125"

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
        coreLibraryDesugaringEnabled true
    }
    kotlinOptions {
        jvmTarget = "11"
    }

    packagingOptions {
        exclude("META-INF/DEPENDENCIES")
        exclude("META-INF/INDEX.LIST")
        exclude("META-INF/LICENSE")
        exclude("META-INF/LICENSE.txt")
        exclude("META-INF/license.txt")
        exclude("META-INF/NOTICE")
        exclude("META-INF/NOTICE.txt")
        exclude("META-INF/notice.txt")
    }

    defaultConfig {
        applicationId = "com.example.example"
        minSdk = 26
        multiDexEnabled true
        targetSdk = 34
        versionCode = flutterVersionCode.toInteger()
        versionName = flutterVersionName
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64', 'x86'
        }
    }

    buildTypes {
        release {
            signingConfig = signingConfigs.debug
            minifyEnabled true
        }
    }
}

flutter {
    source = "../../"
}

dependencies {
    implementation platform('com.google.firebase:firebase-bom:33.0.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation("com.google.firebase:firebase-appcheck")
    implementation("com.google.firebase:firebase-auth")
    implementation("com.google.firebase:firebase-firestore")
    implementation 'com.google.auth:google-auth-library-oauth2-http:1.23.0'
    implementation 'com.google.firebase:firebase-messaging:24.0.0'
    implementation 'com.android.tools:desugar_jdk_libs:2.0.4'
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4'
    implementation 'com.android.support:multidex:2.0.1'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation "androidx.window:window:1.0.0"
}

项目级build.gradle

buildscript {
    ext.kotlin_version = '1.9.24'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.4.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.4.1'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:3.0.1'
    }
}

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

rootProject.buildDir = "../build"
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(":app")
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

gradle.properties

org.gradle.jvmargs=-Xmx4608m
org.gradle.parallel=true
org.gradle.caching=true
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
kotlin.code.style=official
org.gradle.daemon=true
org.gradle.configureondemand=true
org.gradle.vfs.watch=true
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip

解决步骤

  1. 清理重复依赖

    • 删除dependencies中的implementation 'com.android.support:multidex:2.0.1',保留AndroidX版本androidx.multidex:multidex:2.0.1即可,避免support和AndroidX依赖冲突。
    • 删除implementation 'com.android.tools:desugar_jdk_libs:2.0.4',desugar_jdk_libs只需要通过coreLibraryDesugaring声明,不需要添加到implementation依赖。
  2. 升级desugar_jdk_libs版本
    将coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4'升级到兼容Gradle 8.8和AGP 8.4.1的最新版本,比如:

    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.2'
    
  3. 统一Firebase依赖版本
    移除firebase-messaging的手动版本号,让Firebase BOM统一管理版本:

    implementation 'com.google.firebase:firebase-messaging'
    
  4. 修复项目级配置
    删除项目级build.gradle中的以下代码块,避免配置评估顺序异常:

    subprojects {
        project.evaluationDependsOn(":app")
    }
    
  5. 清理Gradle缓存
    执行命令清理缓存,排除旧缓存干扰:

    ./gradlew clean
    rm -rf ~/.gradle/caches
    
  6. 重新构建项目
    执行./gradlew assembleDebug重新构建,验证问题是否解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 08:07:05