letsintegreat
letsintegreat

Reputation: 3366

Changing the kotlin version results in change in compilesdkversion for some reasons

I cloned a flutter project, and tried to run it on an android device. But I ran into this error:

Execution failed for task ':app:checkDebugAarMetadata'. Multiple task action failures occurred: 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.window:window-java:1.0.0-beta04. AAR metadata file: C:\Users\Harshit.gradle\caches\transforms-2\files-2.1\ac98fb722099a50563f635966aedbe29\jetified-window-java-1.0.0-beta04\META-INF\com\android\build\gradle\a ar-metadata.properties.

The error mentions that the current compileSdkVersion is 30, but some dependency works only with 31.

So, I just changed compileSdkVersion and targetSdkVersion from 30 to 31, in my app-level build.gradle, then I tried to run it again, and I ran into another error:

Task :location:compileDebugKotlin e: Incompatible classes were found in dependencies. Remove them from the classpath or use '-Xskip-metadata-version-check' to suppress errors e: C:/Users/Harshit/.gradle/caches/transforms-2/files-2.1/1028a8ca100cbb0fda6fd6257a450fc1/jetified-window-1.0.0-beta04-api.jar!/META-INF/window_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.

After a quick google search, I found a fix, that I need to mention the latest kotlin version in android/build.gradle file, so I changed this:

ext.kotlin_version = '1.3.50'

to this:

ext.kotlin_version = '1.6.0'

and downloaded the respective kotlin plugin. Then I tried to run the project again, and I ran into this error:

Execution failed for task ':google_maps_flutter:checkDebugUnitTestAarMetadata'. Multiple task action failures occurred: 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-29). Dependency: androidx.window:window-java:1.0.0-beta04. AAR metadata file: C:\Users\Harshit.gradle\caches\transforms-2\files-2.1\9d7a6c54f576894d03496d57f9d5a318\jetified-window-java-1.0.0-beta04\META-INF\com\android\build\gradle\a ar-metadata.properties.

This error is identical to the first error, except here it mentions that the current compileSdkVersion is 29, even though I didn't change it after setting it to 31.

So, for some reason, upgrading the version of kotlin results in compileSdkVersin being set to 29 automatically, and I have no idea why.

How can I get rid of this error which says the current compileSdkVersion is 29, even though I have it set to 31?

And why does changing kotlin version tricks it into believing that compileSdkVersion is 29, even though its value is set to 31 in build.gradle?

android/build.gradle:

buildscript {
    ext.kotlin_version = '1.6.0'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.8'
        
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app/build.gradle:

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 31

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.pranshupandya.locus_stalker"
        minSdkVersion 20
        targetSdkVersion 31
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:28.2.0')
    implementation 'com.google.firebase:firebase-analytics'
    def multidex_version = "2.0.1"
    implementation "androidx.multidex:multidex:$multidex_version"
}
apply plugin: 'com.google.gms.google-services'

Upvotes: 1

Views: 2950

Answers (3)

Yoosaf Abdulla
Yoosaf Abdulla

Reputation: 3978

  1. Go to the Project Structure and check your Sdks are rightly mapped to the latest. I had JavaSDK 17 and Android sDK 30

enter image description here

enter image description here

Navigate to your projects build.gradle

enter image description here

Make sure you have these values entered for the compileSdkVersion targetSdkVersion

I had them put same values earlier and the error persisted. So make sure they are one less than the other. In short it should be two different versions.

enter image description here

Upvotes: 0

Nasir
Nasir

Reputation: 1

android {
    compileSdkVersion 31  // update to 31 from 30 

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.ryx.mallties"
        minSdkVersion 21
        targetSdkVersion 31  // update to 31 from 30 
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

Upvotes: 0

Saajid
Saajid

Reputation: 31

  1. Try checking the flutter channel using : flutter channel.
  2. If it is beta then change to stable version using : flutter channel stable
  3. Then flutter clean. flutter pub get and then run the project.

Note : Do not try to modify existing kotlin/java configurations for flutter projects.

Upvotes: 3

Related Questions