运行“react-native run-android”时出错

Abh*_*rav 36 javascript android android-sdk-2.3 react-native

运行我的本机代码时react-native run-android出现错误。它工作正常。但是在从 git 中获取新的 pullnpm ci并且在我运行之后然后收到这个错误。

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugAarMetadata'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
   > The minCompileSdk (30) specified in a
     dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties)
     is greater than this module's compileSdkVersion (android-29).
     Dependency: androidx.core:core:1.7.0-alpha01.
     AAR metadata file: C:\Users\gauraab\.gradle\caches\transforms-2\files-2.1\9e02d64f5889006a671d0a7165c73e72\core-1.7.0-alpha01\META-INF\com\android\build\gradle\aar-metadata.properties.
Run Code Online (Sandbox Code Playgroud)

Gil*_*ert 24

正如一些人提到的,问题是 RN 构建会自动“升级”到 androidx.core:core:1.7.0-alpha01,这取决于 SDK 版本 30。

修复

修复只是通过 build.gradle 中的 androidXCore 指定 android 核心版本

buildscript {
    ext {
        buildToolsVersion = "29.0.3"
        minSdkVersion = 23
        compileSdkVersion = 29
        targetSdkVersion = 29
        ndkVersion = "20.1.5948944"
        kotlin_version = "1.5.0"
        androidXCore = "1.5.0"
    }
Run Code Online (Sandbox Code Playgroud)

我是怎么想出来的

弄清楚这一点很痛苦。我搜索了 gradle 文件,这些文件会像这样自动升级包

find . -name '*.gradle' -exec grep -H "\.+" {} \;
Run Code Online (Sandbox Code Playgroud)

并在node_modules/@react-native-community/netinfo/android/build.gradle以下代码段中找到

def androidXCore = getExtOrInitialValue('androidXCore', null)
  if (supportLibVersion && androidXVersion == null && androidXCore == null) {
    implementation "com.android.support:appcompat-v7:$supportLibVersion"
  } else {
    def defaultAndroidXVersion = "1.+"
    if (androidXCore == null) {
      androidXCore = androidXVersion == null ? defaultAndroidXVersion : androidXVersion
    }
    implementation "androidx.core:core:$androidXCore"
  }
}
Run Code Online (Sandbox Code Playgroud)


Kri*_*lsh 11

显然他们今天刚刚打破了这个。

您可以通过将以下行添加到您的应用程序级 build.gradle 文件(android { }作为兄弟的块上方)来修复它:

configurations.all {
    resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' }
}
Run Code Online (Sandbox Code Playgroud)

最后,Gradle 构建成功完成。参考 https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html


Akz*_*kza 8

我有同样的问题。就是今天。当我尝试在本地运行时,我得到了完全相同的错误。为了让它再次运行,我将字段compileSdkVersiontargetSdkVersion文件 android > build.gradle 从 29 更新到 30。然后,它可以再次运行。如果这个答案适合您,您可以采用这种方式。但是,就我个人而言,我正在寻找不改变价值的解决方案,compileSdkVersion并且targetSdkVersion

更新:只需更改 compileSdkVersion


Aft*_*min 5

这是我所做的事情的屏幕截图,它运行完美。将这些行从

 compileSdkVersion = 29
 targetSdkVersion = 29
Run Code Online (Sandbox Code Playgroud)

 compileSdkVersion = 30
 targetSdkVersion = 30
Run Code Online (Sandbox Code Playgroud)

并将这一行从

implementation 'androidx.appcompat:appcompat:1.+'
Run Code Online (Sandbox Code Playgroud)

实施'androidx.appcompat:程序兼容性:1.3.0'

在此处输入图片说明


Gur*_*sad 5

首先不要在 Gradle 中使用 + 定义库的版本,尽量提供最新版本。

如果您在主要 Gradle 依赖项中使用如下所示的 Core KTX

implementation "androidx.core:core-ktx:+"
Run Code Online (Sandbox Code Playgroud)

只需将其替换为

implementation "androidx.core:core-ktx:1.6.0"
Run Code Online (Sandbox Code Playgroud)

Core 和 Core-ktx 版本 1.7.0 昨天(2021 年 9 月 1 日)发布,这导致了这些问题。

https://developer.android.com/jetpack/androidx/releases/core