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

React Native构建报错:Failed to apply plugin 'com.facebook.react.rootproject'

React Native构建报错:无法加载构建文件已编译类(Failed to apply plugin 'com.facebook.react.rootproject')

问题描述

昨天还能正常向模拟器构建开发版本,今天突然报错:

评估根项目'xxx'时发生错误。
Failed to apply plugin 'com.facebook.react.rootproject'.
配置':app'项目时发生错误。
无法加载构建文件的已编译类

根目录build.gradle内容如下:

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

buildscript {
ext {
buildToolsVersion = findProperty('android.buildToolsVersion') ?: '34.0.0'
minSdkVersion = Integer.parseInt(findProperty('android.minSdkVersion') ?: '23')
compileSdkVersion = Integer.parseInt(findProperty('android.compileSdkVersion') ?: '34')
targetSdkVersion = Integer.parseInt(findProperty('android.targetSdkVersion') ?: '34')
kotlinVersion = findProperty('android.kotlinVersion') ?: '1.8.10'

        ndkVersion = "25.1.8937393"
    }
    repositories {
        mavenCentral()
        google()
    }
    dependencies {
        classpath('com.android.tools.build:gradle')
        classpath('com.facebook.react:react-native-gradle-plugin')
    }

}

apply plugin: "com.facebook.react.rootproject"

allprojects {
repositories {
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url(new File(['node', '--print', "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim(), '../android'))
}
maven {
// Android JSC is installed from npm
url(new File(['node', '--print', "require.resolve('jsc-android/package.json', { paths: [require.resolve('react-native/package.json')] })"].execute(null, rootDir).text.trim(), '../dist'))
}

        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }

}

解决方法

1. 清理缓存与构建文件

先彻底清理旧的构建缓存,消除过期文件干扰:

  • 在项目根目录执行命令:./gradlew clean
  • 删除根目录下的.gradle文件夹,以及android/.gradle、android/app/build文件夹
  • 重置Metro缓存并重新构建:npx react-native start --reset-cache && npx react-native run-android

2. 明确Gradle依赖版本

你的buildscript中Gradle相关依赖未指定具体版本,容易引发版本冲突或拉取失败:

  • 修改buildscript.dependencies块,给插件指定与React Native版本兼容的明确版本(示例以RN 0.72为例):
    dependencies {
        classpath('com.android.tools.build:gradle:7.4.2')
        classpath('com.facebook.react:react-native-gradle-plugin:0.72.6')
    }
    
  • 同步Gradle wrapper版本:打开android/gradle/wrapper/gradle-wrapper.properties,确保distributionUrl与Android Gradle插件版本匹配(AGP 7.4.2对应Gradle 7.5):
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
    

3. 重新安装项目依赖

可能是Node依赖缓存损坏导致的连锁问题:

  • 删除node_modules文件夹和对应锁文件(package-lock.json/yarn.lock/pnpm-lock.yaml)
  • 重新安装依赖:npm install(或yarn install/pnpm install)

4. 检查文件权限与完整性

  • 确保项目文件夹没有权限限制,Mac/Linux用户可执行:chmod -R 755 ./
  • 通过Git历史对比,确认build.gradle及其他配置文件未被意外修改

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 23:13:17