0

I am trying to build react native project

but I got an error when running npm run android

> 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).
     Dependency: androidx.appcompat:appcompat:1.4.1.

Already gone through all the solution mentioned on this, this, this, and this but could not solve it.

android/build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "30.0.2"
        minSdkVersion = 21
        compileSdkVersion = 30
        targetSdkVersion = 30
        ndkVersion = "21.4.7075529"
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:4.2.2")
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:4.3.10'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }
        mavenCentral {
            // We don't want to fetch react-native from Maven Central as there are
            // older versions over there.
            content {
                excludeGroup "com.facebook.react"
            }
        }
        google()
        maven { url 'https://www.jitpack.io' }
    }
}

app/build.gradle dependencies

dependencies {
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules

    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"

    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.fbjni'
    }

    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
        exclude group:'com.squareup.okhttp3', module:'okhttp'
    }

    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }
}

Let me know if anyone require any additional information.

Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77
  • It clearly says that you should update the compileSdkVersion to 31 in order to work with this dependency. If it still doesn't work, try to update the targetSdkVersion along with the compileSdkVersion. Clearing the project caches etc. might be helpful too. – Mieczkoni Nov 29 '22 at 21:22
  • When I changed version to 31 then started getting this error: ```Execution failed for task ':expo-modules-core:compileDebugKotlin'. > Compilation error. See log for more details``` – Faisal Shaikh Nov 29 '22 at 21:27
  • Can you also paste the details about this error from the log? – Mieczkoni Nov 29 '22 at 21:28
  • @Mieczkoni here is the log after changing version to 31 https://codeshare.io/Ad3odx – Faisal Shaikh Nov 29 '22 at 21:33
  • Can you also show us the app/build.gradle file? At least the dependencies list – Mieczkoni Nov 29 '22 at 21:37
  • @Mieczkoni just updated the question – Faisal Shaikh Nov 29 '22 at 21:39
  • I think you should find the dependency that is responsible for kotlin-reflect-1.4.21.jar from your error and update it so it fit the other Kotlin versions. More details in this answer: https://stackoverflow.com/a/61159614/10508565 – Mieczkoni Nov 29 '22 at 21:47
  • do you think is there anyway we can fix this without updating ```compileSdkVersion```? – Faisal Shaikh Nov 29 '22 at 21:52

1 Answers1

0

Updated the compileSdkVersion & targetSdkVersion to 31 and added kotlinVersion = "1.7.20" in android/build.gradle

kotlinVersion = "1.7.20"
buildToolsVersion = "30.0.2"
minSdkVersion = 21
compileSdkVersion = 31
targetSdkVersion = 31
ndkVersion = "21.4.7075529"

You may have to specify REACT_NATIVE_VERSION so add these changes in 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())
allprojects {
    configurations.all {
        resolutionStrategy {
            force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
        }
    }

Add the android:exported="true" in Androidmanifest.xml in .MainActivity tag like this

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize"
        android:screenOrientation="portrait"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
        android:exported="true">

Anyone who still faces any issues comment on this answer, I will try my best to help you.

Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77