React Native 0.75升级后引入@react-native-voice/voice致构建失败崩溃
React Native 0.75集成@react-native-voice/voice构建失败解决方案
问题概述
将RN项目从0.68升级至0.75.4后,其余依赖均正常工作,但集成@react-native-voice/voice时出现构建失败,移除该包后应用可正常运行。核心报错为Gradle任务依赖缺失:packageDebugResources任务使用了generateDebugResValues的输出,但未声明显式依赖。
核心报错日志
> Task :react-native-voice_voice:packageDebugResources FAILED FAILURE: Build failed with an exception. * What went wrong: A problem was found with the configuration of task ':react-native-voice_voice:packageDebugResources' (type 'MergeResources'). - Gradle detected a problem with the following location: 'F:\ZURU\SmartHomeApp\node_modules\@react-native-voice\voice\android\build\generated\res\resValues\debug'. Reason: Task ':react-native-voice_voice:packageDebugResources' uses this output of task ':@react-native-voice_voice:generateDebugResValues' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Possible solutions: 1. Declare task ':@react-native-voice_voice:generateDebugResValues' as an input of ':react-native-voice_voice:packageDebugResources'. 2. Declare an explicit dependency on ':@react-native-voice_voice:generateDebugResValues' from ':react-native-voice_voice:packageDebugResources' using Task#dependsOn. 3. Declare an explicit dependency on ':@react-native-voice_voice:generateDebugResValues' from ':react-native-voice_voice:packageDebugResources' using Task#mustRunAfter.
已尝试的方案
- 检查AndroidManifest.xml配置无错误
- 验证Gradle 8.7与Android Gradle Plugin 8.6.0兼容性
- 卸载并重装@react-native-voice/voice
- 确认其他依赖包可正常工作
环境信息
- React Native版本:0.75.4
- @react-native-voice/voice版本:最新版(2024年12月)
- Gradle版本:8.7
- Android Gradle Plugin版本:8.6.0
- Node.js版本:22.12.0
- 操作系统:Windows 11
可行解决方案
1. 补丁Gradle任务依赖(临时修复)
在项目根目录的android/build.gradle中添加以下配置,强制为react-native-voice_voice子项目声明任务依赖:
subprojects { if (name.contains('react-native-voice_voice')) { afterEvaluate { // 为Debug构建添加依赖 tasks.named('packageDebugResources') { dependsOn tasks.named('generateDebugResValues') } // 为Release构建添加依赖 tasks.named('packageReleaseResources') { dependsOn tasks.named('generateReleaseResValues') } } } }
添加后重新执行构建命令,该配置会在Gradle评估子项目后,明确资源打包任务对资源值生成任务的依赖关系。
2. 降级至兼容版本
社区反馈@react-native-voice/voice@3.2.4可在RN 0.75环境下正常工作,尝试降级:
npm uninstall @react-native-voice/voice npm install @react-native-voice/voice@3.2.4
安装完成后清理缓存并重新构建。
3. 清理构建缓存
执行以下命令清理所有缓存,避免旧构建文件导致的冲突:
# 清理Node依赖和缓存 rm -rf node_modules package-lock.json npm install # 清理Android构建缓存 cd android gradlew clean cd .. # 重新运行项目 npx react-native run-android
4. 统一Android SDK版本
RN 0.75默认使用compileSdk 34,当前项目设置为35,可尝试修改为34以减少版本冲突:
在android/build.gradle的buildscript.ext块中调整:
buildscript { ext { compileSdkVersion = 34 targetSdkVersion = 34 // 保留其他原有配置 } }
内容的提问来源于stack exchange,提问作者Ayush Bharadva
相关产品推荐
相关产品推荐

