简体   繁体   English

运行 `react-native run-android` 时出错

[英]Getting error while running `react-native run-android`

Whle running my react native code react-native run-android getting below error.在运行我的本机代码react-native run-android出现错误。 It was working proper.它工作正常。 But after taking fresh pull from git and npm ci and after I am running then getting this error.但是在从 git 和npm ci提取新的 pull 之后,在我运行之后然后得到这个错误。

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.

As some has mentioned, the problem is that the RN build automatically "upgraded" to androidx.core:core:1.7.0-alpha01, which depends on SDK version 30.正如一些人提到的,问题是 RN 构建会自动“升级”到 androidx.core:core:1.7.0-alpha01,这取决于 SDK 版本 30。

The Fix修复

The fix is simply to specify android core version via androidXCore in build.gradle修复只是通过 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"
    }

How I figured it out我是怎么想出来的

Figuring this out was painful.弄清楚这一点很痛苦。 I grepped for gradle files that would automatically upgrade packages like so我搜索了 gradle 文件,这些文件会像这样自动升级包

find . -name '*.gradle' -exec grep -H "\.+" {} \;

and found in node_modules/@react-native-community/netinfo/android/build.gradle the following snippet并在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"
  }
}

I got same issue.我有同样的问题。 Just today.就是今天。 When I try to run locally, I got the exact same error.当我尝试在本地运行时,我得到了完全相同的错误。 To get it run again, I update the field compileSdkVersion and targetSdkVersion in file android > build.gradle from 29 to 30. Then, it can run again.为了让它再次运行,我将文件 android > build.gradle 中的compileSdkVersiontargetSdkVersion字段从 29 更新到 30。然后,它可以再次运行。 If this answer fits with you, you can go with this way.如果这个答案适合您,您可以采用这种方式。 But, personally, I'm lookin for solution without to change the value compileSdkVersion and targetSdkVersion但是,就我个人而言,我正在寻找不改变compileSdkVersiontargetSdkVersion值的解决方案

Update: just change the compileSdkVersion更新:只需更改compileSdkVersion

Apparently they just broke this today.显然他们今天刚刚打破了这个。

You can fix it by adding the following line to your app level build.gradle file (above the android { } block as a sibling):您可以通过将以下行添加到您的应用程序级 build.gradle 文件(作为兄弟的android { }块上方)来修复它:

configurations.all {
    resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' }
}

Finally, the Gradle build was successfully completed.最后,Gradle 构建成功完成。 Ref.参考https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html

Here is a screenshot of what I have done and it's run perfectly.这是我所做的事情的屏幕截图,它运行完美。 change these lines from这些行从

 compileSdkVersion = 29
 targetSdkVersion = 29

to

 compileSdkVersion = 30
 targetSdkVersion = 30

and also change this line from并将这一行从

implementation 'androidx.appcompat:appcompat:1.+'

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

在此处输入图片说明

The issue was just what I guessed.这个问题正是我的猜测。 So, some updates to a minor version/patch version of an android dependency caused all this today.因此,今天对 android 依赖项的次要版本/补丁版本的一些更新导致了这一切。

To solve this, for the dependencies in your build.gradle file, if you have specified it to take the latest minor/patch version every time you build it, make it take an exact stable version.为了解决这个问题,对于build.gradle文件中的依赖build.gradle ,如果您已将其指定为每次构建时采用最新的次要/补丁版本,请使其采用完全稳定的版本。

For example, my appcompact dependecy was,例如,我的 appcompact 依赖是,

implementation "androidx.appcompat:appcompat:1.+

This means that it can update to 1.2.x or 1.3.x etc.. as and when such version updates get published.这意味着它可以更新到 1.2.x 或 1.3.x 等。当此类版本更新发布时。 I changed this to depend on an exact stable version like below,我将其更改为依赖于如下所示的确切稳定版本,

implementation "androidx.appcompat:appcompat:1.3.0"

Hopefully, this solves the issue for everyone.希望这可以解决每个人的问题。

在此处输入图片说明

For me the issue is caused by react-native-netinfo 's old version 4.0.0 which was configured to automatically pick up the latest published package of androidx.core ... So in that case it did I realized androidx.core.core-1.7.0-alpha01 had been published right as the issue started occurring.对我来说,这个问题是由react-native-netinfo的旧version 4.0.0 ,它被配置为自动获取最新发布的androidx.core包......所以在这种情况下,我意识到了androidx.core.core-1.7.0-alpha01已在问题开始发生时发布。

So to fix that I updated my react-native-netinfo package from 4.0.0 to 6.0.0 and this issue was resolved for me.所以为了解决这个问题,我将我的react-native-netinfo包从4.0.06.0.0 ,这个问题已经为我解决了。

I just change compileSdkVersion=29 to compileSdkVersion=30 and my issue solved .我只是将 compileSdkVersion=29 更改为 compileSdkVersion=30 并且我的问题解决了。 Its working它的工作

Observing the same core library dependency is causing Gradle build issues for all.观察到相同的核心库依赖会导致所有的 Gradle 构建问题。

Also, clean project or Invalidate Caches/Restart... options wouldn't help here.此外, clean projectInvalidate Caches/Restart...选项在这里也无济于事。

Affecting dependency: 'androidx.core:core-ktx:1.7.0-alpha01'影响依赖: 'androidx.core:core-ktx:1.7.0-alpha01'

This dependency which was causing issue, for that I have made it stable in build.gradle file, then added a new line to solve dependency conflicts:这个导致问题的依赖项,为此我在build.gradle文件中使它稳定,然后添加了一个新行来解决依赖项冲突:

configurations.all {
   resolutionStrategy {
     force 'androidx.core:core-ktx:1.6.0'
   }
}

Simple work around with complie version使用 complie 版本的简单解决方法

  • Change compileSdkVersion=29 to compileSdkVersion=30 and sync.compileSdkVersion=29更改为compileSdkVersion=30并同步。

If you not willing to change the complie version,then如果您不愿意更改编译版本,那么

 configurations.all {
        resolutionStrategy {
            force 'androidx.core:core-ktx:1.6.0'
        }
    }

add the above code in build.gradle inside android and apply this加在上面的代码build.gradleandroid和应用此

implementation 'androidx.core:core-ktx:1.7.0-alpha01'

Try to add this on your top build.gradle at the bottom of buildscript .试着在你的顶部添加此build.gradle在底部buildscript This will force anything to a specific version.这将强制任何特定版本。

subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'androidx.core'
                    && !details.requested.name.contains('androidx') ) {
                details.useVersion "1.5.0"
            }
            
            if (details.requested.group == 'androidx.core-ktx'
                    && !details.requested.name.contains('androidx') ) {
                details.useVersion "1.5.0"
            }
        }
    }
}

Updating version numbers to match those shown in the official template build.gradle file worked for me: https://github.com/facebook/react-native/blob/master/template/android/build.gradle更新版本号以匹配官方模板 build.gradle 文件中显示的版本号对我有用: https : //github.com/facebook/react-native/blob/master/template/android/build.gradle

 buildToolsVersion = "30.0.2"
 compileSdkVersion = 30
 targetSdkVersion = 30
 ndkVersion = "21.4.7075529"

您可以在此处找到有关此问题的信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 react-native run-android 卡住 - react-native run-android stuck react-native run-android 失败 - react-native run-android Failed 运行react-native run-android时,任务':react-native-icons:compileReleaseAidl'的执行失败 - Execution failed for task ':react-native-icons:compileReleaseAidl' when running react-native run-android React Native,为什么 react-native run-android 不起作用? - React Native, why is react-native run-android not working? React Native,为什么 react-native run-android 不起作用? - React Native, why react-native run-android not working? 如何为 windows 运行 react-native? 我有一个问题:npx react-native run-android 错误 - How to run react-native for windows? I have a problem: npx react-native run-android error 运行 react-native run-android 时任务 ':app:transformClassesWithDexForDebug' 执行失败 - Execution failed for task ':app:transformClassesWithDexForDebug' when running react-native run-android react-native run-android build 成功但不工作 - react-native run-android build successful but not working 我在尝试运行“react-native run-android”时遇到了这个错误。我已经厌倦了很多解决方案,但似乎都不起作用。 我只需要一个成功的构建 - I got this error while trying to run 'react-native run-android' .I've tired many solutions but non seem to work. I just need a successful build 反应本机:BeoNews应用程序运行Android执行任务':app:transformClassesWithJarMergingForDebug'失败 - React-native: BeoNews app run-android Execution failed for task ':app:transformClassesWithJarMergingForDebug'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM