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

React Native构建报io.invertase.firebase.BuildConfig重复定义

问题描述
  • 构建场景:在VSCode中开发React Native应用,执行Android Release包构建
  • 故障表现:构建流程执行到:app:mergeDexRelease任务时失败,此前尝试调整Firebase依赖版本、更新Gradle版本均未解决问题
  • 完整报错信息:
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDexRelease'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
     Type io.invertase.firebase.BuildConfig is defined multiple times: /path/node_modules/@react-native-firebase/app/android/build/.transforms/7d2ce1f5b074ed872b704906f3d41618/classes/classes.dex, /path/node_modules/react-native-firebase/android/build/.transforms/0b00760c914c9cf3e84f7c87e9135514/classes/classes.dex

BUILD FAILED in 3m 50s
现有配置参考

项目app/build.gradle配置内容

apply plugin: "com.android.application"
apply plugin: 'com.google.gms.google-services'

import com.android.build.OutputFile;

project.ext.react = [
    enableHermes: false,
]

apply from: "../../node_modules/react-native/react.gradle"

def jscFlavor = 'org.webkit:android-jsc:+'
def enableHermes = project.ext.react.get("enableHermes", false);

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "com.odotime"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 40
        versionName "1.41"
        multiDexEnabled true
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
        release {
            if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
                storeFile file(MYAPP_UPLOAD_STORE_FILE)
                storePassword MYAPP_UPLOAD_STORE_PASSWORD
                keyAlias MYAPP_UPLOAD_KEY_ALIAS
                keyPassword MYAPP_UPLOAD_KEY_PASSWORD
            }
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.facebook.react:react-native:+"

    implementation platform('com.google.firebase:firebase-bom:28.0.1') 
    
    compile project(':react-native-sms-retriever')
    
    implementation 'com.google.firebase:firebase-analytics:18.0.3'

    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation 'com.android.support:multidex:1.0.3'
    implementation "com.android.support:support-v4:22.1.0"
    implementation 'com.android.support:design:27.1.0'

    implementation project(':react-native-push-notification')

    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
      exclude group:'com.facebook.fbjni'
    }

    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
        exclude group:'com.squareup.okhttp3', module:'okhttp'
    }

    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }
}

task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

项目package.json依赖配置

"dependencies": {
    "@capacitor/android": "^3.6.0",
    "@capacitor/core": "^3.6.0",
    "@react-native-community/art": "^1.2.0",
    "@react-native-community/async-storage": "^1.12.0",
    "@react-native-community/masked-view": "^0.1.10",
    "@react-native-community/netinfo": "^5.9.6",
    "@react-native-community/push-notification-ios": "^1.8.0",
    "@react-native-firebase/app": "^12.1.0",
    "@react-native-firebase/messaging": "^12.1.0",
    "@react-navigation/bottom-tabs": "^5.8.0",
    "@react-navigation/material-bottom-tabs": "^5.2.16",
    "@react-navigation/native": "^5.7.3",
    "@react-navigation/stack": "^5.9.0",
    "amazon-cognito-identity-js": "^4.3.3",
    "aws-amplify": "^3.0.23",
    "axios": "^0.20.0",
    "cordova-plugin-androidx-adapter": "^1.1.3",
    "i18n-js": "^3.7.1",
    "jetifier": "^2.0.0",
    "lodash.memoize": "^4.1.2",
    "moment": "^2.29.1",
    "moment-range": "^4.0.2",
    "moment-timezone": "^0.5.33",
    "react": "16.13.1",
    "react-devtools": "^4.8.2",
    "react-native": "0.63.2",
    "react-native-calendars": "^1.343.0",
    "react-native-contacts": "^6.0.3",
    "react-native-country-picker-modal": "^2.0.0",
    "react-native-date-picker": "^3.2.3",
    "react-native-drop-shadow": "^0.0.2",
    "react-native-dropdown-picker": "^3.7.1",
    "react-native-email-link": "^1.11.3",
    "react-native-firebase": "^5.6.0",
    "react-native-gesture-handler": "^1.7.0",
    "react-native-hyperlinked-text": "^1.0.2",
    "react-native-image-picker": "^2.3.4",
    "react-native-loading-spinner-overlay": "^1.1.0",
    "react-native-localize": "^2.0.1",
    "react-native-paper": "^4.1.0",
    "react-native-pie": "^1.1.2",
    "react-native-push-notification": "^7.3.1",
    "react-native-raw-bottom-sheet": "^2.2.0",
    "react-native-reanimated": "^1.13.0",
    "react-native-safe-area-context": "^3.1.6",
    "react-native-screens": "^2.10.1",
    "react-native-sectioned-multi-select": "^0.8.1",
    "react-native-simple-radio-button": "^2.7.4",
    "react-native-simple-toast": "^1.1.2",
    "react-native-sms-retriever": "^1.1.1",
    "react-native-svg-transformer": "^0.14.3",
    "react-native-switch-selector": "^2.0.6",
    "react-native-vector-icons": "^7.1.0",
    "react-redux": "^7.2.1",
    "redux": "^4.0.5",
    "redux-form": "^8.3.6",
    "redux-persist": "^6.0.0",
    "redux-persist-transform-filter": "0.0.20",
    "redux-saga": "^1.1.3",
    "reselect": "^4.0.0",
    "save": "^2.4.0",
    "toggle-switch-react-native": "^2.3.0"
  },
  "devDependencies": {
    "@babel/core": "^7.8.4",
    "@babel/runtime": "^7.8.4",
    "@capacitor/cli": "^3.6.0",
    "@react-native-community/eslint-config": "^1.1.0",
    "babel-jest": "^25.1.0",
    "eslint": "^6.8.0",
    "jest": "^25.1.0",
    "metro-react-native-babel-preset": "^0.59.0",
    "react-test-renderer": "16.13.1",
    "remote-redux-devtools": "^0.5.16"
  }
故障根因

报错核心是Dex合并阶段检测到重复类:io.invertase.firebase.BuildConfig同时存在于两个Firebase依赖包中。
项目依赖里同时引入了两套互不兼容的React Native Firebase包:

  • 已停止维护的旧版包react-native-firebase@5.6.0
  • 当前官方维护的新版命名空间包@react-native-firebase/app@12.1.0、@react-native-firebase/messaging@12.1.0
    两套包使用了完全相同的原生类路径,打包时必然产生类冲突,调整Gradle版本、修改Firebase BOM版本都无法解决这类根源冲突。另外现有Gradle配置中同时存在AndroidX和旧版com.android.support依赖,后续也可能触发其他构建冲突。
修复步骤

按以下操作即可解决构建失败问题:

  • 卸载冲突的旧版Firebase包,在项目根目录执行对应包管理命令:
# npm用户执行
npm uninstall react-native-firebase --save
# yarn用户执行
yarn remove react-native-firebase
  • 全局搜索项目中所有从react-native-firebase导入的代码,替换为新版@react-native-firebase/*对应的导入方式,和已安装的v12版本API保持一致。
  • 清理全量构建缓存,依次执行以下命令:
cd android
./gradlew clean
cd ..
rm -rf node_modules
npm install # yarn用户执行yarn install
  • (可选,避免后续构建问题)替换过时的support依赖:将app/build.gradle中implementation 'com.android.support:multidex:1.0.3'替换为implementation 'androidx.multidex:multidex:2.0.1',移除其他冗余的com.android.support开头的依赖项,统一AndroidX依赖生态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 15:09:21