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

添加MongoDB驱动后Android构建遇mergeExtDexDebug失败求助

Android添加mongodb-driver-sync后mergeExtDexDebug构建失败问题解决

问题描述

在Android项目的build.gradle依赖中添加mongodb-driver-sync后,构建时触发:app:mergeExtDexDebug任务失败。已尝试降级依赖版本、启用multiDex,均未解决问题。

错误信息

> Task :app:mergeExtDexDebug
AGPBI: {"kind":"error","text":"Invalid build configuration. Attempt to create a global synthetic for 'Record desugaring' without a global-synthetics consumer.","sources":[],"tool":"D8"}
Invalid build configuration. Attempt to create a global synthetic for 'Record desugaring' without a global-synthetics consumer.


> Task :app:mergeExtDexDebug FAILED
Execution failed for task ':app:mergeExtDexDebug'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Failed to transform bson-record-codec-4.8.1.jar (org.mongodb:bson-record-codec:4.8.1) to match attributes {artifactType=android-dex, asm-transformed-variant=NONE, dexing-enable-desugaring=true, dexing-enable-jacoco-instrumentation=false, dexing-is-debuggable=true, dexing-min-sdk=24, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}.
      > Execution failed for DexingNoClasspathTransform: C:\Users\user\.gradle\caches\modules-2\files-2.1\org.mongodb\bson-record-codec\4.8.1\339c92291f24acf896334ba9a2a5bf52fd462115\bson-record-codec-4.8.1.jar.
         > Error while dexing.

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

项目build.gradle配置

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.appname'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.appname"
        minSdk 21
        multiDexEnabled true
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    buildToolsVersion '33.0.1'
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.0'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'org.mongodb:mongodb-driver-sync:4.8.2' //<-mongodb依赖
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

解决方案

错误根源是bson-record-codec模块使用了Java 16的Record特性,而当前AGP版本或desugar配置不支持该特性的处理,提供两种可行方案:

方案一:启用核心库脱糖(保留Record支持)

  1. 升级Android Gradle Plugin(AGP)到7.4.0及以上版本(修改项目根目录build.gradle中com.android.tools.build:gradle的版本)
  2. 在模块build.gradle的compileOptions中添加核心库脱糖配置,并引入脱糖依赖:
android {
    // ... 其他配置
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
        // 新增以下配置
        coreLibraryDesugaringEnabled true
    }
}

dependencies {
    // ... 其他依赖
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4'
}

方案二:排除bson-record-codec模块(无需Record支持)

如果项目中不需要使用Java Record处理BSON数据,直接排除该依赖模块即可:

dependencies {
    // ... 其他依赖
    implementation('org.mongodb:mongodb-driver-sync:4.8.2') {
        exclude group: 'org.mongodb', module: 'bson-record-codec'
    }
}

执行上述修改后,重新执行构建任务即可解决问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 07:15:32