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

React Native旧项目升级后Android构建失败求助

React Native 0.69.5升级后Android构建错误排查解决

项目相关配置

package.json

{
    "name": "unknown",
    "version": "0.0.1",
    "private": true,
    "scripts": {
        "start": "react-native start",
        "flow": "flow --show-all-errors",
        "flow start": "flow start",
        "flow stop": "flow stop",
        "flow status": "flow status",
        "flow coverage": "flow coverage",
        "eslint": "eslint",
        "eslint-check": "eslint --print-config .eslintrc.js | eslint-config-prettier-check",
        "build:ios": "react-native bundle --entry-file='index.js' --bundle-output='./ios/main.jsbundle' --dev=false --platform='ios'"
    },
    "dependencies": {
        "@react-native-community/cli-platform-android": "^9.1.0",
        "babel-plugin-transform-remove-console": "6.9.4",
        "braces": "^2.3.2",
        "crypto-js": "4.1.1",
        "currency-formatter": "1.5.9",
        "d3": "7.3.0",
        "d3-array": "3.1.1",
        "d3-scale": "4.0.2",
        "d3-shape": "3.1.0",
        "moment": "^2.29.4",
        "react": "^17.0.2",
        "react-dom": "^18.2.0",
        "react-native": "^0.69.5",
        "react-native-appstore-version-checker": "3.0.0",
        "react-native-device-info": "^10.1.1",
        "react-native-dropdownalert": "4.5.1",
        "react-native-elements": "3.4.2",
        "react-native-fabric": "^0.5.2",
        "react-native-fingerprint-scanner": "6.0.0",
        "react-native-gesture-handler": "^2.6.0",
        "react-native-keychain": "8.0.0",
        "react-native-loading-spinner-overlay": "3.0.0",
        "react-native-maps": "0.30.1",
        "react-native-modal-datetime-picker": "13.0.1",
        "react-native-pathjs-charts": "0.0.30",
        "react-native-segmented-control-tab": "4.0.0",
        "react-native-sentry": "0.43.2",
        "react-native-simple-dialogs": "1.4.0",
        "react-native-svg": "12.3.0",
        "react-native-table-component": "1.2.2",
        "react-native-vector-icons": "9.1.0",
        "react-navigation": "4.4.4",
        "react-redux": "7.2.6",
        "realm": "10.13.0",
        "redux": "4.1.2",
        "redux-logger": "3.0.6",
        "redux-thunk": "2.4.1"
    },
    "devDependencies": {
        "@babel/core": "7.17.5",
        "@babel/plugin-proposal-class-properties": "7.16.7",
        "@babel/plugin-transform-runtime": "7.17.0",
        "@babel/preset-env": "7.16.11",
        "@babel/preset-react": "7.16.7",
        "babel-cli": "6.26.0",
        "babel-eslint": "10.1.0",
        "babel-jest": "27.5.1",
        "babel-plugin-module-resolver": "4.1.0",
        "babel-preset-flow": "6.23.0",
        "babel-preset-react-native": "2.1.0",
        "eslint": "8.10.0",
        "eslint-config-airbnb": "^19.0.4",
        "eslint-config-prettier": "8.5.0",
        "eslint-import-resolver-babel-module": "5.3.1",
        "eslint-plugin-import": "2.25.4",
        "eslint-plugin-jsx-a11y": "6.5.1",
        "eslint-plugin-react": "7.29.3",
        "flow-bin": "0.173.0",
        "invariant": "2.2.4",
        "jest": "27.5.1",
        "metro-react-native-babel-preset": "0.69.0",
        "react-test-renderer": "17.0.2"
    },
    "jest": {
        "preset": "react-native"
    },
    "rnpm": {
        "assets": [
            "./assets/fonts/"
        ]
    }
}

项目级build.gradle

// 顶级构建文件,可添加所有子项目/模块通用的配置选项
buildscript {
    repositories {
        jcenter()
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.1'
    }
}

allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven {
            // React Native的所有内容(JS、Obj-C源码、Android二进制包)均从npm安装
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}

App级build.gradle

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'io.fabric.tools:gradle:'
    }
}

apply plugin: "com.android.application"
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
    google()
}

构建报错信息

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:mergeDebugAssets'.
> Could not resolve all task dependencies for configuration ':app:debugRuntimeClasspath'.
   > Could not resolve project :realm.
     Required by:
         project :app
      > No matching configuration of project :realm was found. The consumer was configured to find a runtime of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.2.1' but:
          - None of the consumable configurations have attributes.

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

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

解决方案

1. 修复Realm依赖与Gradle版本兼容性问题

报错核心是Realm模块的配置无法匹配当前Gradle 7.2.1的属性要求:

  • 打开android/settings.gradle,删除旧版Realm的本地模块引用(比如include ':realm'),当前Realm通过npm集成aar包,不需要本地模块。
  • 升级Realm到兼容RN 0.69.5的版本,执行命令:
npm install realm@10.21.1 --save

2. 修正Gradle配置

项目级build.gradle调整

在allprojects.repositories中添加Realm所需仓库:

allprojects {
    repositories {
        mavenLocal()
        google()
        mavenCentral()
        jcenter()
        maven {
            url "$rootDir/../node_modules/react-native/android"
        }
        // 添加以下仓库
        maven { url 'https://jitpack.io' }
        maven { url 'https://maven.google.com' }
    }
}

App级build.gradle调整

  • 移除Fabric相关配置(Fabric已停止服务,与新版Gradle不兼容):删除buildscript块中Fabric仓库、依赖,以及apply plugin: 'io.fabric'和对应仓库配置。
  • 添加适配RN 0.69.5的Android构建配置:
android {
    compileSdkVersion 33
    buildToolsVersion "33.0.0"

    defaultConfig {
        applicationId "你的应用ID"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"
    }
}

3. 清理缓存并重新构建

执行以下命令清理缓存后重新运行:

cd android
./gradlew clean
cd ..
rm -rf node_modules
rm package-lock.json
npm install
npx react-native run-android

4. 替换过时依赖

项目中react-native-fabric、react-native-sentry均已停止维护,建议:

  • 将react-native-sentry替换为官方新版@sentry/react-native
  • 移除react-native-fabric,改用Firebase Crashlytics等替代方案

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 23:01:07