由于 React Native 版本 0.71.0-rc.0 的发布,React Native Android 构建失败并出现不同的错误,过去几天没有对代码进行任何更改

Tha*_*P A 50 javascript android gradle android-debug react-native

注意:错误可能会有所不同,但如果您在过去两天没有对代码进行任何更改的情况下进行 Android 构建时遇到任何错误

我的错误 - 无法安装应用程序。错误:命令失败:./gradlew app:installDebug -PreactNativeDevServerPort=8081

error Failed to install the app. Make sure you have the Android development environment set up:

Error: Command failed: ./gradlew app:installDebug
-PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* Where: Build file '/Users/....../node_modules/react-native-month-year-picker/android/build.gradle' line: 115

* What went wrong: A problem occurred configuring project ':react-native-month-year-picker'.

> Could not resolve all files for configuration ':react-native-month-year-picker:implementation'.
   > Could not resolve com.facebook.react:react-native:+.
     Required by:
         project :react-native-month-year-picker
      > Cannot choose between the following variants of com.facebook.react:react-native:0.71.0-rc.0:
          - debugVariantDefaultRuntimePublication
          - releaseVariantDefaultRuntimePublication
        All of them match the consumer attributes:
          - Variant 'debugVariantDefaultRuntimePublication' capability com.facebook.react:react-native:0.71.0-rc.0:
Run Code Online (Sandbox Code Playgroud)

Tha*_*P A 98

Android 的构建失败是由于 React Native 版本的发布造成的0.71.0-rc0

注意:错误可能会有所不同,但如果您在过去两天没有对代码进行任何更改的情况下遇到 android 构建失败,这将是解决方案

在尝试这些方法之前,请恢复您所做的所有更改:/sf/answers/5205983681/

方法一

将此修复添加到您的android -> build.gradle文件中,如下所示:

buildscript {
    // ...
}


allprojects {
    repositories {
       exclusiveContent {
           filter {
               includeGroup "com.facebook.react"
           }
           forRepository {
               maven {
                   url "$rootDir/../node_modules/react-native/android"
               }
           }
       }
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

此修复将应用一项exclusiveContent解析规则,强制解析 React Native Android 库,以使用其中的解析规则node_modules

方法二

如果您的 gradle 不支持上述功能,请将其添加到您的android -> build.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())


buildscript {
     // ...
}
    
    
allprojects {
    configurations.all {
          resolutionStrategy {
            force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
          }
    }
    // ...  
}
Run Code Online (Sandbox Code Playgroud)

参考:自 2022 年 11 月 4 日以来发生的 Android 构建失败的修复和更新 #35210


小智 6

添加到投票的答案中以进行一些知识共享。

重申一下,正如 @Thanhal 所发布的,解决方案和官方解释可以在这里找到:Android build failure Nomatchingvariant of com.facebook.react:react-native:0.71.0-rc.0 was found。

错误发生后我需要回答的最大问题是:

在package.json中指定我的react-native版本后,为什么我的项目仍然下载另一个react-native版本?

我什至曾经npm install --save-exact确保我获得正确的版本

给出的错误消息让我更加困惑:

该类从 ~/.gradle/caches/transforms-3/9a8c596b7e1788d5bad7c80991eefff1/transformed/jetified-kotlin-stdlib-1.6.10.jar!/kotlin/Unit.class e: .../node_modules/expo-modules- 加载core/android/src/main/java/expo/modules/adapters/react/permissions/PermissionsService.kt: (351, 32): 类“kotlin.Unit”是使用不兼容版本的 Kotlin 编译的。其元数据的二进制版本是1.6.0,预期版本是1.4.1。

不知何故,Kotlin 也成为了我的一个问题。

谁/什么在请求最新的react-native?

就我而言,这里的问题与我的项目正在使用的react-native版本无关。这是关于我的图书馆正在使用的内容。

直到 .react-native 团队一直在 NPM 包 (node_modules/react-native/android/) 内提供 Maven 存储库0.71.0-rc.0。大多数库都将其 build.gradle 配置为引用此目录。这是通过在库的build.gradle中声明自定义存储库来完成的:

maven {
    url "$rootDir/../node_modules/react-native/android"
}
Run Code Online (Sandbox Code Playgroud)

但在库的 build.gradle 文件中,声明了更多存储库,可能如下所示:

repositories {
  maven {
    url "$rootDir/../node_modules/react-native/android"
  }
  google()
  mavenLocal()
  mavenCentral()
}
Run Code Online (Sandbox Code Playgroud)

然后,该库的依赖关系声明如下:

dependencies {
  implementation 'com.facebook.react:react-native:+'
}
Run Code Online (Sandbox Code Playgroud)

因为“+”作为react-native依赖项的版本,Gradle将从各个声明的存储库中获取最新的react-native版本。

由于过去react-native是随npm包一起提供的,所以最新的Gradle总是会在node_modules. 然而,现在 React-Native 团队正在将库发布到包括 MavenCentral 在内的公共存储库,Gradle 尊重“+”并采用 MavenCentral 上的版本。

为什么我会收到 Kotlin 错误?

我的项目使用旧版本的react-native,从版本0.68起,react-native开始使用Kotlin版本1.6.10(请参阅更改历史记录)。所以是的,react-native 版本的差异也会导致 Kotlin 错误。