React Native构建失败:JCenter仓库访问异常求解决方案
解决React Native打包Release时JCenter依赖超时问题
问题背景
昨天React Native项目构建一切正常,今天打包Release版本时直接报错,核心问题是访问JCenter仓库的react-native元数据超时,导致react-native-community_masked-view的依赖解析失败。JCenter早就弃用了,但网上通用的Android解决方案对RN新手不友好,而且在node_modules的依赖场景下很多方案不管用,现在需要针对RN多依赖场景的可行解决办法。
报错日志
FAILURE: Build failed with an exception. * What went wrong: Could not determine the dependencies of task ':react-native-community_masked-view:verifyReleaseResources'. > Could not resolve all task dependencies for configuration ':react-native-community_masked-view:releaseRuntimeClasspath'. > Could not resolve com.facebook.react:react-native:+. Required by: project :react-native-community_masked-view > Failed to list versions for com.facebook.react:react-native. > Unable to load Maven meta-data from https://jcenter.bintray.com/com/facebook/react/react-native/maven-metadata.xml. > Could not HEAD 'https://jcenter.bintray.com/com/facebook/react/react-native/maven-metadata.xml'. > Read timed out * 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 Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. See https://docs.gradle.org/7.5.1/userguide/command_line_interface.html#sec:command_line_warnings BUILD FAILED in 2m 45s 5 actionable tasks: 5 up-to-date
可行解决方案
1. 替换项目全局仓库配置,彻底移除JCenter
打开项目根目录下的android/build.gradle,修改allprojects里的仓库列表:
- 删除
jcenter()这一行 - 添加
mavenCentral()作为主要替代仓库 - 保留React Native官方仓库
maven { url 'https://maven.facebook.com/' } - 国内用户可以加阿里云镜像加速:
maven { url 'https://maven.aliyun.com/repository/public/' }
修改后的示例代码:
allprojects { repositories { mavenCentral() maven { url 'https://maven.facebook.com/' } maven { url 'https://maven.aliyun.com/repository/public/' } google() // 移除掉原来的 jcenter() } }
2. 强制指定依赖来源,绕过node_modules里的硬编码JCenter
有些第三方库会在自己的gradle配置里硬写JCenter,这时候可以通过项目的gradle配置强制改依赖来源:
在android/app/build.gradle的android代码块里添加以下配置:
configurations.all { resolutionStrategy { eachDependency { details -> // 强制react-native依赖使用项目指定的版本,不从JCenter拉取 if (details.requested.group == 'com.facebook.react' && details.requested.name == 'react-native') { details.useVersion rootProject.ext.react.version } } // 禁止所有依赖从JCenter拉取 denyResolutionFor('jcenter') } }
3. 更新第三方依赖到兼容新版本
react-native-community_masked-view已经迁移到新的包名下,直接升级到最新版就能解决依赖问题:
- 打开项目的
package.json,把原来的react-native-community_masked-view替换成@react-native-masked-view/masked-view,指定最新稳定版本 - 执行
npm install或者yarn install重新安装依赖 - 同步Gradle后再尝试打包
4. 清理Gradle缓存,避免残留缓存干扰
执行以下命令清理本地缓存,彻底清除JCenter相关的缓存文件:
cd android ./gradlew cleanBuildCache ./gradlew clean # 可选:清理全局Gradle缓存 rm -rf ~/.gradle/caches
内容的提问来源于stack exchange,提问作者Engr.Aftab Ufaq
相关产品推荐
相关产品推荐

