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

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
    
    或使用正斜杠:
    sdk.dir=D:/Android/Sdk
    
    确保路径指向真实存在的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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 02:10:05