使用RN-CLI搭建React Native项目失败:react-native-gradle-plugin不存在
React Native CLI构建失败:react-native-gradle-plugin不存在的解决方法
问题场景
首次通过React Native CLI搭建项目,遵循官方文档操作后构建失败,报错指定路径下的react-native-gradle-plugin不存在。尝试在node_modules/react-native下安装该插件未解决问题。
错误日志:
info 💡 Tip: Make sure that you have set up your development environment correctly, by running react-native doctor. To read more about doctor command visit: https://github.com/react-native-community/cli/blob/main/packages/cli-doctor/README.md#doctor FAILURE: Build failed with an exception. * What went wrong: Included build 'D:\React-native_Projects\Infovision_demo_rn_js_ts_final_projects\rn_cli\CustomApp1_new\CustomApp1\node_modules\react-native-gradle-plugin' does not exist. * 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 28s
环境变量:
JAVA_HOME=C:\Program Files\Java\jdk-11.0.12 Path=C:\Program Files\Java\jdk-11.0.12\bin;C:\Users\shubham.bhatt\AppData\Local\Android\Sdk\tools\bin;C:\Users\shubham.bhatt\AppData\Local\Android\Sdk\platform-tool;C:\Gradle\gradle-7.3.1\bin;D:\React-native Projects\rn-starter-main\.expo-shared ANDROID_HOME=C:\Users\shubham.bhatt\AppData\Local\Android\Sdk ANDROID_SDK_ROOT=C:\Users\shubham.bhatt\AppData\Local\Android\Sdk
解决步骤
彻底重置依赖
- 删除本地依赖文件夹和锁文件:
# Windows rmdir /s /q node_modules del package-lock.json # macOS/Linux rm -rf node_modules rm package-lock.json - 清除npm缓存并重新安装:
npm cache clean --force npm install
- 删除本地依赖文件夹和锁文件:
正确安装gradle插件
不要在node_modules/react-native目录下手动安装插件,直接在项目根目录执行:npm install react-native-gradle-plugin --save-dev检查gradle配置路径
打开android/settings.gradle文件,确认插件引入路径是否正确,正确配置应为:includeBuild('../node_modules/react-native-gradle-plugin')如果路径错误(比如指向了react-native子目录下的插件),修改为上述路径。
验证环境完整性
运行react-native doctor检查所有开发环境依赖,按照工具提示修复缺失或配置错误的项。清理Android构建缓存
进入项目的android目录,执行:# Windows gradlew clean # macOS/Linux ./gradlew clean回到项目根目录重新运行构建命令:
npx react-native run-android
内容的提问来源于stack exchange,提问作者Shubham
相关产品推荐
相关产品推荐

