React Native版本0.71.0-rc.0发布后,过去几天内在构建Android应用时出现了不同的错误,尽管代码没有进行任何更改。

49

注意:错误可能不同,但如果您在过去两天内获取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:
5个回答

95

Android构建失败是因为发布了React Native版本0.71.0-rc0

注意:错误可能不同,但如果过去两天中没有对代码进行任何更改而出现Android构建失败,则这将是解决方案。

在尝试这些方法之前,请恢复您所做的每个更改:https://dev59.com/oHwQtIcB2Jgan1znKSXp#74371195

方法1

将以下修复程序添加到您的android -> build.gradle文件中:

buildscript {
    // ...
}


allprojects {
    repositories {
       exclusiveContent {
           filter {
               includeGroup "com.facebook.react"
           }
           forRepository {
               maven {
                   url "$rootDir/../node_modules/react-native/android"
               }
           }
       }
        // ...
    }
}

这个修复将应用一个名为 exclusiveContent 的决议规则,强制解析 React Native Android 库,使用位于 node_modules 内部的库。

方法2

如果您的 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
          }
    }
    // ...  
}

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


好的,再次感谢。但是伙计,你知道为什么吗?这是因为在这行代码的末尾有一个 + 吗?android/app/build.gradle 中的代码如下:implementation "com.facebook.react:react-native:+" - Mathias Bradiceanu
1
请查看以下链接中提到的原因和所有细节:https://github.com/facebook/react-native/issues/35210 - ZFLOC TECHNOLOGIES
1
https://dev59.com/JsXsa4cB1Zd3GeqPfHFH 也解决了这个问题 - woodpecker
我之前使用的是React Native 0.66.3版本,根据React Native最新修复建议,我将其更新至0.66.5版本。但不幸的是,它仍然无法正常工作,我收到了以下错误提示: Execution failed for task ':app:checkDebugAarMetadata'。
A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction The minCompileSdk (31) specified in a dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties) is greater than this module's compileSdkVersion (android-30)。
- Ravis Farooq
1
@RavisFarooq,请尝试通过更改build.gradle文件中的compileSdkVersion31来重新构建...... 如果这不起作用,请撤销更改并将react-native降级到之前正常工作的版本0.66.3,然后应用此处提到的解决方案并重试。 - ZFLOC TECHNOLOGIES
https://dev59.com/oHwQtIcB2Jgan1znKSXp#74371195 - ZFLOC TECHNOLOGIES

5

在原有回答的基础上,需要分享一些知识。

再次重申,正如@Thanhal所发布的,解决方案和官方说明可以在此处找到:Android build failures No matching variant 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不兼容的版本。其元数据的二进制版本是1.6.0,预期版本是1.4.1.

某种程度上,Kotlin也成为了我的问题。

是谁/什么要求最新的react-native?

对于我的情况,问题不在于我的项目使用的react-native版本。而是关于我的库使用了什么。

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

maven {
    url "$rootDir/../node_modules/react-native/android"
}

但是在库的 build.gradle 文件中,声明了更多的仓库,可能会像这样:

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

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

dependencies {
  implementation 'com.facebook.react:react-native:+'
}

由于 "+" 是 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 错误。


1

0
我尝试为我的React Native应用程序生成一个APK,但是我不知道是什么问题导致新创建的APK文件与之前的APK相比没有任何变化。
我正在使用命令"./gradlew assembleRelease"来创建APK。
我已经删除了文件"android/app/src/main/assets/index.android.bundle"并运行了以下命令:
npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

我还清理了项目并重新构建了它,但问题仍然存在。
我已经修复了它。问题出在android/build.gradle文件中的versionName上。我之前使用的是versionName'4.0.2',现在我已将其更改为versionName'4.0.3',现在一切正常运行。

这并没有回答问题。一旦你获得足够的声望,你就能够评论任何帖子;相反,提供不需要提问者澄清的答案。- 来自审核 - inyourdream
这并没有回答问题。一旦你拥有足够的声望,你就可以评论任何帖子;相反,提供不需要提问者澄清的答案。- 来自评论 - undefined

-1

这个修复方法有效:

失败原因:Android的构建失败是由于React Native版本0.71.0-rc0发布到Maven,因此当gradle同步时,它会选择0.71.0-rc0版本的react-native而不是您当前版本的react-native。

通过在build.gradle中添加以下内容,使其在不升级react-native版本的情况下工作,这个方法有效(无论是否启用hermes,以及flipper)

 exclusiveContent {
            // We get React Native's Android binaries exclusively through npm,
            // from a local Maven repo inside node_modules/react-native/.
            // (The use of exclusiveContent prevents looking elsewhere like Maven Central
            // and potentially getting a wrong version.)
            filter {
                includeGroup "com.facebook.react"
            }
            forRepository {
                maven {
                    url "$rootDir/../node_modules/react-native/android"
                }
            }
        }

最终的代码片段如下所示

allprojects {
    repositories {
        exclusiveContent {
            // We get React Native's Android binaries exclusively through npm,
            // from a local Maven repo inside node_modules/react-native/.
            // (The use of exclusiveContent prevents looking elsewhere like Maven Central
            // and potentially getting a wrong version.)
            filter {
                includeGroup "com.facebook.react"
            }
            forRepository {
                maven {
                    url "$rootDir/../node_modules/react-native/android"
                }
            }
        }
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")

在此修复后,执行gradle clean和rebuild。然后您就可以成功运行React Native Android了。

这个修复的作用是应用一个exclusiveContent解析规则,强制解析React Native Android库,使用node_modules内部的库。

现在,React Native有一些不同版本的补丁发布如果您不想使用此修复,您可以按照此处提到的方法将当前的React Native版本更新为相应的React Native补丁版本。

https://github.com/facebook/react-native/issues/35210


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接