React Native 0.75.2安卓构建失败:找不到com.facebook.react.settings插件
问题描述
按官方文档执行以下步骤创建React Native项目:
- 先运行
npm uninstall -g react-native-cli @react-native-community/cli - 再用
npx @react-native-community/cli@latest init AwesomeProject初始化项目
npm start 运行正常,但执行 npm run android 时构建失败。尝试降级React Native和Gradle版本都没用,旧版本RN项目能正常运行。
错误信息
FAILURE: Build failed with an exception. * Where: Settings file 'C:\Users\UserG\Desktop\PersonalProjects\IntradayMov\IntradayMovers\android\settings.gradle' line: 2 * What went wrong: Plugin [id: 'com.facebook.react.settings'] was not found in any of the following sources: - Gradle Core Plugins (plugin is not in 'org.gradle' namespace) - Plugin Repositories (plugin dependency must include a version number for this source) * 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 6s error Failed to install the app. Command failed with exit code 1: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081 FAILURE: Build failed with an exception. * Where: Settings file 'C:\Users\UserG\Desktop\PersonalProjects\IntradayMov\IntradayMovers\android\settings.gradle' line: 2 * What went wrong: Plugin [id: 'com.facebook.react.settings'] was not found in any of the following sources: - Gradle Core Plugins (plugin is not in 'org.gradle' namespace) - Plugin Repositories (plugin dependency must include a version number for this source) * 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 6s. info Run CLI with --verbose flag for more details.
注:运行 react-native doctor 未检测到异常。
当前配置文件
settings.gradle
pluginManagement { includeBuild("../node_modules/@react-native/gradle-plugin") } plugins { id("com.facebook.react.settings") } extensions.configure(com.facebook.react.ReactSettingsExtension){ ex -> ex.autolinkLibrariesFromCommand() } rootProject.name = 'IntraMovers' include ':app' includeBuild('../node_modules/@react-native/gradle-plugin')
build.gradle
buildscript { ext { buildToolsVersion = "34.0.0" minSdkVersion = 23 compileSdkVersion = 34 targetSdkVersion = 34 ndkVersion = "26.1.10909125" kotlinVersion = "1.9.24" } repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle") classpath("com.facebook.react:react-native-gradle-plugin") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin") } } apply plugin: "com.facebook.react.rootproject"
解决方案
问题出在settings.gradle里的插件引用没正确关联本地安装的React Native Gradle插件,按以下步骤修改:
调整
android/settings.gradle配置
替换为以下内容:pluginManagement { includeBuild("../node_modules/@react-native/gradle-plugin") repositories { google() mavenCentral() gradlePluginPortal() } } plugins { id("com.facebook.react.settings") version "0.0.1" apply false } rootProject.name = 'IntraMovers' include ':app'- 加
gradlePluginPortal()仓库,确保插件能被检索到 - 给
com.facebook.react.settings插件加版本号(对应本地@react-native/gradle-plugin版本),并设apply false避免提前加载 - 删掉重复的
includeBuild和多余的extensions.configure块(最新插件已集成该逻辑)
- 加
确认
android/build.gradle配置
保持现有内容即可,无需额外修改,确保依赖引用正确:buildscript { ext { buildToolsVersion = "34.0.0" minSdkVersion = 23 compileSdkVersion = 34 targetSdkVersion = 34 ndkVersion = "26.1.10909125" kotlinVersion = "1.9.24" } repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle") classpath("com.facebook.react:react-native-gradle-plugin") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin") } } apply plugin: "com.facebook.react.rootproject"清理缓存后重新构建
执行命令:cd android gradlew clean cd .. npm run android
内容的提问来源于stack exchange,提问作者gopal prabhu
相关产品推荐
相关产品推荐

