React Native 打包出现报错:ninja: error: mkdir.../projects/Rn-book/node_modules/:No such file or directory

如何解决 Android 打包报错:ninja: error: mkdir(src/main/cpp/reanimated/CMakeFiles/reanimated.dir/C_/Users/Administrator/Desktop/projects/Rn-book/node_modules/react-native-reanimated): No such file or directory

错误信息
ninja: error: mkdir(src/main/cpp/reanimated/CMakeFiles/reanimated.dir/C_/Users/Administrator/Desktop/projects/Rn-book/node_modules/react-native-reanimated): No such file or directory
问题分析

错误信息表明在打包过程中,构建工具 ninja 无法创建指定目录,通常是由于以下原因:

react-native-reanimated 没有更新到最新版本,导致与项目中的其他依赖不兼容。

解决步骤

以下是解决该问题的几个常见步骤:

1. 更新 react-native-reanimated 到最新版本

如果你的版本不是最新版本,可以通过以下命令更新:

npm install react-native-reanimated@latest

或者,如果你使用 yarn,可以使用:

yarn add react-native-reanimated@latest

安装完成后,清理项目并重新编译,以确保所有依赖都已正确更新。

2. 清理缓存并重新安装依赖

在 React Native 中,缓存问题可能会导致构建错误。执行以下命令清理项目缓存:

# 删除 node_modules 目录和锁文件
rm -rf node_modules/
# 重新安装依赖
npm install # 或者 yarn install

在清理完缓存并重新安装依赖后,再次尝试构建项目。

 cd android
 ./gradlew assembleRelease
D:\RnProject\AwesomeProject>npx react-native run-android * daemon not running; starting now at tcp:5037 * daemon started successfully info Installing the app... Starting a Gradle Daemon (subsequent builds will be faster) > Task :gradle-plugin:settings-plugin:compileKotlin FAILED 5 actionable tasks: 5 executed info 💡 Tip: Make sure that you have set up your development environment correctly, by running npx 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 e: file:///D:/RnProject/AwesomeProject/node_modules/@react-native/gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt:26:16 Unresolved reference 'layout'. e: file:///D:/RnProject/AwesomeProject/node_modules/@react-native/gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt:28:16 Unresolved reference 'layout'. e: file:///D:/RnProject/AwesomeProject/node_modules/@react-native/gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt:50:42 Unresolved reference 'layout'. e: file:///D:/RnProject/AwesomeProject/node_modules/@react-native/gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt:52:20 Unresolved reference 'layout'. FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':gradle-plugin:settings-plugin:compileKotlin'. > A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction > Compilation error. See log for more details * 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 3m 54s error Failed to install the app. Command failed with exit code 1: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081 e: file:///D:/RnProject/AwesomeProject/node_modules/@react-native/gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt:26:16 Unresolved reference 'layout'. e: file:///D:/RnProject/AwesomeProject/node_modules/@react-native/gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt:28:16 Unresolved reference 'layout'. e: file:///D:/RnProject/AwesomeProject/node_modules/@react-native/gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt:50:42 Unresolved reference 'layout'. e: file:///D:/RnProject/AwesomeProject/node_modules/@react-native/gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt:52:20 Unresolved reference 'layout'. FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':gradle-plugin:settings-plugin:compileKotlin'. > A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction > Compilation error. See log for more details * 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 3m 54s. info Run CLI with --verbose flag for more details. 报错分析
最新发布
12-19
从你提供的错误日志来看,React Native 项目在执行 `npx react-native run-android` 时构建失败,关键错误信息是: ``` Unresolved reference 'layout' ``` 这个错误出现在 `ReactSettingsExtension.kt` 文件中,表明 Kotlin 编译器找不到 `layout` 的引用。以下是可能的原因和解决方法: --- ### 🔍 报错分析 1. **依赖版本不兼容** - `@react-native/gradle-plugin` 插件中引用的 `layout` 可能是某个 Gradle API 或 Kotlin API,但当前项目中使用的 Gradle 或 Kotlin 版本不支持该 API。 - 通常发生在 React Native 升级或手动修改 `build.gradle` 后,版本之间不匹配。 2. **node_modules 中的 gradle-plugin 损坏** - 由于网络问题或安装中断,`@react-native/gradle-plugin` 可能未正确安装。 3. **Gradle 版本与插件不兼容** - React Native 项目中使用的 Gradle 版本可能与 `@react-native/gradle-plugin` 不兼容。 4. **Kotlin 插件版本问题** - `compileKotlin` 失败说明 Kotlin 编译器可能版本过旧或配置错误。 --- ### ✅ 解决方法 1. **运行 `npx react-native doctor`** - 检查是否提示 Gradle、Android SDK、Kotlin 等环境问题。 2. **清除缓存并重新安装依赖** ```bash cd D:\RnProject\AwesomeProject npm cache clean --force rm -rf node_modules npm install ``` 3. **升级或降级 React Native 版本** - 如果你刚升级 React Native,建议回退到稳定版本。 - 或者尝试升级到最新稳定版本: ```bash npm install react-native@latest ``` 4. **检查并更新 Gradle 和 Kotlin 版本** - 打开 `/android/build.gradle`,确保: ```gradle ext { buildToolsVersion = "34.0.0" minSdkVersion = 21 compileSdkVersion = 34 targetSdkVersion = 34 kotlinVersion = '1.9.x' // 确保与插件兼容 ndkVersion = "25.1.8937393" } ``` 5. **更新 Gradle Wrapper** - 修改 `/android/gradle/wrapper/gradle-wrapper.properties` 中的版本为兼容版本: ``` distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip ``` 6. **重新生成项目** - 如果以上方法无效,可以尝试创建一个新的 React Native 项目,然后逐步迁移代码。 --- ### 🧪 验证方式 重新运行以下命令: ```bash npx react-native run-android ``` 如果构建成功,说明问题已解决。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值