Getting error while running `react-native run-android`

33,719

Solution 1

I just change compileSdkVersion=29 to compileSdkVersion=30 and my issue solved . Its working

Solution 2

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.

The Fix

The fix is simply to specify android core version via androidXCore in build.gradle

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

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

and found in node_modules/@react-native-community/netinfo/android/build.gradle the following snippet

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"
  }
}

Solution 3

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):

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

Finally, the Gradle build was successfully completed. Ref. https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html

Solution 4

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. 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

Update: just change the compileSdkVersion

Solution 5

First never use + to define the version of Libraries in Gradle, Try to give the latest version always.

If you are using Core KTX like below in your main Gradle dependencies

implementation "androidx.core:core-ktx:+"

Just replace it to

implementation "androidx.core:core-ktx:1.6.0"

Core and Core-ktx Version 1.7.0 released yesterday(September 1, 2021), which is causing the issues.

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

Share:
33,719

Related videos on Youtube

Abhigyan Gaurav
Author by

Abhigyan Gaurav

Updated on September 03, 2021

Comments

  • Abhigyan Gaurav
    Abhigyan Gaurav over 2 years

    Whle running my react native code react-native run-android getting below error. It was working proper. But after taking fresh pull from git and npm ci and after I am running then getting this error.

    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.
    
    • ChandrasekarG
      ChandrasekarG almost 3 years
      Since everyone is facing it just today, I think this could probably be because of our dependencies. Some update to minor version/patch version broke the android builds probably? In the dependencies, we can try providing an exact stable version number for our dependencies.
    • MagogCZ
      MagogCZ almost 3 years
      same problem after update Android Studio to 4.2.2
    • Usama Altaf
      Usama Altaf almost 3 years
      try to change the core library with stable version 1.6.0
    • Akza
      Akza almost 3 years
      @UsamaAltaf where is the core lib defined? I couldn't found androidx.core:core:1.7.0-alpha01 in my project
  • Akza
    Akza almost 3 years
    do you know which dependency it is? Caused I already specified with implementation 'androidx.appcompat:appcompat:1.1.0-rc01'. But still got the same error
  • ChandrasekarG
    ChandrasekarG almost 3 years
    Probably the rc build could have caused this. And in general rc builds are quite buggy. Can you try a stable build?
  • Akza
    Akza almost 3 years
    did you use implementation "com.facebook.react:react-native:+"? Did you specified it, the version?
  • ChandrasekarG
    ChandrasekarG almost 3 years
    I don't use react-native. Mine is a native android project. So, you don't have to change it I believe since that did not break things for me.
  • Akza
    Akza almost 3 years
    I've try to change from androidx.appcompat:appcompat:1.1.0-rc01 to androidx.appcompat:appcompat:1.3.0 (like you did). But, still got the error
  • ChandrasekarG
    ChandrasekarG almost 3 years
    Can you get us the list of all dependencies?
  • Abhigyan Gaurav
    Abhigyan Gaurav almost 3 years
    compileSdkVersion=29 to compileSdkVersion=30
  • ChandrasekarG
    ChandrasekarG almost 3 years
  • Victor Sheyanov
    Victor Sheyanov almost 3 years
    0.64.5 will have all sdk versions updated to 30, so you can use ReactNative upgrade tool and upgrade them all upfront to 30.
  • ChandrasekarG
    ChandrasekarG almost 3 years
    Did you try changing the compileSdkVersion=29 to compileSdkVersion=30
  • Rohit Bhargava
    Rohit Bhargava almost 3 years
    @ChandrasekarG To most of us, this issue type is being observed in most recent, to be precise since yesterday. TBH, I haven't tried upgrading compileSdkVersion, will try and check.
  • Gilbert
    Gilbert almost 3 years
    See my answer below: stackoverflow.com/a/68222185/70488
  • Mr Chris
    Mr Chris almost 3 years
    I was able to build by only adjust 'androidx.appcompat:appcompat:1.3.0', leaving the compileSdkVersion and targetSdkVersion at 29 - thus avoid upgrading the React Native NPM packages.
  • Denis Tsoi
    Denis Tsoi over 2 years
    2021 React-native MVP
  • Rohit Aggarwal
    Rohit Aggarwal over 2 years
    My release build's size has increased by 2x after this.
  • kojo justine
    kojo justine over 2 years
    this worked for me
  • Nabil Freeman
    Nabil Freeman about 2 years
    Thank you! This helped me a lot. I also used ./gradlew :app:dependencies > dependencies.txt (from stackoverflow.com/a/69031232/1395078) which helped me find the random dependency that was forcing core 1.7. I had to hunt through several resolutions and eventually found it was Intercom bumping activity-compose between 10.6.2 and 10.6.3. I had 10+ so pinned it to 10.6.2. If it's a native package, finding the version to downgrade to is very easy with this repository explorer: mvnrepository.com/artifact/io.intercom.android/… - it shows dependencies.