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

升级React Native至0.72.7编译报错:找不到com.facebook.react:react-native:0.72.7

问题描述

将React Native从0.64版本升级到0.72.7后,编译Android应用时出现以下错误:

FAILURE: Build failed with an exception.
- What went wrong:
无法确定任务 ':app:compileDebugJavaWithJavac' 的依赖项。

无法解析配置 ':app:debugCompileClasspath' 的所有任务依赖项。找不到 com.facebook.react:react-native:0.72.7。该依赖由项目 :app 所需。

当前使用的依赖版本:

  • "react": "18.2.0"
  • "react-native": "0.72.7"

项目根目录build.gradle内容如下:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "29.0.3"
        minSdkVersion = 21
        compileSdkVersion = 33
        targetSdkVersion = 33
        ndkVersion = "20.1.5948944"
    }
    repositories {
        mavenCentral()
        google()
    }
    dependencies {
        classpath('com.android.tools.build:gradle:4.2.2')
        classpath("com.facebook.react:react-native-gradle-plugin")
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())

allprojects {
    configurations.all {
        resolutionStrategy {
            // Remove this override in 0.65+, as a proper fix is included in react-native itself.
            force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
            force 'com.facebook.flipper:flipper-fresco-plugin:0.182.0'
        }
    }
    repositories {
        mavenLocal()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }
        maven {
            // react-native-background-fetch
            url("${project(':react-native-background-fetch').projectDir}/libs")
        }

        google()
        jcenter()
        maven { url 'https://www.jitpack.io' }
    }
}
解决方案

1. 升级Gradle和Android Gradle Plugin(AGP)版本

React Native 0.72要求更高版本的AGP和Gradle,当前使用的com.android.tools.build:gradle:4.2.2版本过低,无法兼容RN 0.72。

  • 修改根目录build.gradle的buildscript.dependencies:
dependencies {
    classpath('com.android.tools.build:gradle:7.3.1') // RN 0.72对应AGP版本
    classpath("com.facebook.react:react-native-gradle-plugin:${REACT_NATIVE_VERSION}") // 插件版本与RN保持一致
}
  • 修改gradle/wrapper/gradle-wrapper.properties中的distributionUrl:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip

2. 移除过时的依赖强制配置

resolutionStrategy中保留的force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION是0.65版本之前的兼容配置,会干扰新版本RN的依赖解析,直接删除该行:

configurations.all {
    resolutionStrategy {
        // 移除该行
        // force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
        force 'com.facebook.flipper:flipper-fresco-plugin:0.182.0'
    }
}

3. 更新仓库配置,移除废弃的jcenter

jcenter已停止维护,RN 0.72不再依赖它,从allprojects.repositories中删除jcenter(),并确保mavenCentral()和google()优先级靠前:

allprojects {
    repositories {
        mavenCentral()
        google()
        mavenLocal()
        maven {
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            url("$rootDir/../node_modules/jsc-android/dist")
        }
        maven {
            url("${project(':react-native-background-fetch').projectDir}/libs")
        }
        maven { url 'https://www.jitpack.io' }
    }
}

4. 清理缓存并重新安装依赖

执行以下命令清理缓存,确保依赖正确安装:

# 删除node_modules和锁文件
rm -rf node_modules package-lock.json yarn.lock
# 重新安装依赖
npm install # 或 yarn install
# 清理Gradle缓存
cd android && ./gradlew clean

完成以上步骤后,重新编译Android应用即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 01:57:37