Failed to resolve: react-native-0.71.0-rc.0-debug

ReactNative + Android studio编译打包apk,Failed to resolve: react-native-0.71.0-rc.0-debug,报错:


问题描述

2022年11月7日15:23:05,使用Android studio编译apk时,突然报错

Failed to resolve: react-native-0.71.0-rc.0-debug
Affected Modules: app, jcore-react-native, jpush-react-native, react-native-device-info, react-native-file-selector, react-native-webview

打包apk时出现以下错误

Could not determine the dependencies of task ':app:lintVitalRelease'.
> Could not resolve all artifacts for configuration ':app:debugCompileClasspath'.
   > Could not find react-native-0.71.0-rc.0-debug.aar (com.facebook.react:react-native:0.71.0-rc.0).
     Searched in the following locations:

在这里插入图片描述


原因分析:

根据官方给出的解释是这样的
2022 年 11 月 4 日,我们在多个公共存储库上发布了 React Native 版本 0.71.0-rc0,这是 71 版本系列的第一个候选版本。具体来说,这个版本也发布在Maven Central上,因为我们正在更新我们的工件分发策略(技术细节如下所述)。
此事件导致多个用户的 Android 构建失败,因为他们最终下载了错误的 React Native 版本(0.71.0-rc0 而不是他们在项目中使用的版本,例如 0.68.0)。
从历史上看,React Native 模板提供了 build.gradle 文件,其中包含对 React Native Android 库的依赖关系,如下所示:implementation(“com.facebook.react:react-native:+”)。

具体来说,此依赖项声明的 + 部分会导致 Gradle 在所有声明的存储库中选择最高版本(通常称为 Gradle 动态版本)。在 React Native 版本 0.70 之前,我们在 NPM 包中(在 ./android 文件夹中)中发布了一个 Maven 存储库。从 0.71 开始,我们移动了这样的文件夹并将其上传到 Maven Central。

使用 Gradle 动态版本(即 a + 依赖项)被视为一种反模式(请参阅本页上的各种警告和注释),特别是因为它可能导致此类场景,并且通常会使用户暴露于可重现性较低的构建。这正是此方案中发生的情况,因为构建开始失败,而没有对用户项目进行任何更改。
至于具体原因可以参考以下链接
构建失败原因以及解决办法

在这里插入图片描述


解决方案:

提示:在project下的buld.gradle文件中添加如下代码,即可成功编译打包

def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())

allprojects {
    
    
    configurations.all {
    
    
        resolutionStrategy {
    
    
            // Remove this override in 0.65+, as a proper fix is included in react-native itself.
            force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_44132277/article/details/127731985