React Native Android构建失败:已配置local.properties和ANDROID_HOME仍报错
问题解决指南:SDK路径未找到 + 版本兼容性检查
一、SDK路径找不到的排查步骤
1. 确认local.properties文件的正确性
- 位置:必须放在项目的
android/目录下,路径为D:\Saraya\Saraya\android\local.properties,不能放错层级 - 格式:Windows系统下路径需用双反斜杠转义,例如:
或使用正斜杠:sdk.dir=D:\\Android\\Sdk
确保路径指向真实存在的Android SDK目录,无多余空格或拼写错误sdk.dir=D:/Android/Sdk - 权限:右键文件,取消「只读」属性,确保Gradle能正常读取
2. 验证环境变量的有效性
- 重启终端:配置
ANDROID_HOME后必须关闭并重新打开命令行/IDE,否则变量不会生效 - 检查变量值:在终端执行
echo %ANDROID_HOME%,确认输出的路径与Android SDK实际路径一致 - 检查Path变量:确保系统Path中包含
%ANDROID_HOME%\platform-tools和%ANDROID_HOME%\cmdline-tools\latest\bin(新版SDK需添加cmdline-tools路径)
3. 排查第三方库对Gradle的影响
- 打开
android/build.gradle,检查是否有硬编码SDK路径或覆盖local.properties读取逻辑的代码 - 打开
android/app/build.gradle,确认没有重写sdk.dir的配置项(部分第三方库可能会注入此类代码)
4. 清理缓存后重新构建
- 清理Gradle缓存:进入
android/目录,执行gradlew clean - 清理Node依赖:在项目根目录执行
rd /s /q node_modules && npm install(Windows) - 清理全局Gradle缓存:删除
C:\Users\<你的用户名>\.gradle\caches目录,再重新构建
二、React Native与第三方库的版本兼容性检查
1. 查看第三方库官方文档
- 以
react-native-firebase为例,其官方文档会明确标注支持的React Native版本范围(如v19.x要求RN 0.72+),直接对照当前项目RN版本判断是否匹配
2. 输出当前项目环境信息
- 在项目根目录执行
react-native info,获取RN版本、Node版本、Android SDK版本等核心信息,与第三方库的要求逐一对比
3. 检查Peer Dependencies
- 执行
npm ls react-native,查看终端输出的警告信息,若存在版本不匹配提示,说明第三方库与当前RN版本冲突 - 直接查看第三方库的
package.json文件中的peerDependencies字段,确认RN版本是否在要求范围内
4. 参考社区反馈
- 查看第三方库的GitHub Issues,搜索关键词(如「RN 0.73 compatibility」),确认其他用户是否遇到相同版本组合的问题
附报错日志
› Building app... Configuration on demand is an incubating feature. FAILURE: Build failed with an exception. * What went wrong: Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'. > Could not determine the dependencies of null. > SDK location not found. Define a valid SDK location with an ANDROID_HOME environment variable or by setting the sdk.dir path in your project's local properties file at 'D:\Saraya\Saraya\android\local.properties'. * 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 16s 5 actionable tasks: 5 up-to-date Error: D:\Saraya\Saraya\android\gradlew.bat exited with non-zero code: 1 Error: D:\Saraya\Saraya\android\gradlew.bat exited with non-zero code: 1 at ChildProcess.completionListener (D:\Saraya\Saraya\node_modules\@expo\spawn-async\build\spawnAsync.js:52:23) at Object.onceWrapper (node:events:633:26) at ChildProcess.emit (node:events:518:28) at cp.emit (D:\Saraya\Saraya\node_modules\cross-spawn\lib\enoent.js:34:29) at maybeClose (node:internal/child_process:1101:16) at ChildProcess._handle.onexit (node:internal/child_process:304:5) ... at Object.spawnAsync [as default] (D:\Saraya\Saraya\node_modules\@expo\spawn-async\build\spawnAsync.js:17:21) at spawnGradleAsync (D:\Saraya\Saraya\node_modules\@expo\cli\build\src\start\platforms\android\gradle.js:72:46) at Object.assembleAsync (D:\Saraya\Saraya\node_modules\@expo\cli\build\src\start\platforms\android\gradle.js:52:18) at runAndroidAsync (D:\Saraya\Saraya\node_modules\@expo\cli\build\src\run\android\runAndroidAsync.js:36:24) at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
内容的提问来源于stack exchange,提问作者RedSean
相关产品推荐
相关产品推荐

