androidfluttergradlecompiler-errorsapk

Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1 .8.0, expected version is 1.6.0


Hello everyone i have an issue when i try to build the apk for flutter app, i have this error in my terminal :

Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1 .8.0, expected version is 1.6.0.

for the detail i put my code below : flutter version : 3.19.1

// android/build.gradle
allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

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

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

// android/app/build.gradle
plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

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

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

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

android {
    namespace "com.example.edupass_mobile"
    compileSdk 34
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    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.example.edupass_mobile"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion 21
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

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


// settings.gradle
pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }
    settings.ext.flutterSdkPath = flutterSdkPath()

    includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    id "org.jetbrains.kotlin.android" version "1.8.0" apply false
}

include ":app"


i have search the same problem like mine but in my code i don't have the buildscript code like the others, so i confused how to change the gradle version

can anyone help me to find the solution of this problem ?


Solution

  • Even if Downgrading your kotlin version works, is not a good solution in the long term, your app will not evolve and will get stuck in the past version.

    The Error Explained:

    When you see an error like this Is obvious that the module was compiled with an incompatible version of Kotlin. Because the binary version of its metadata is 1.8.0, and the expected version is 1.6.0.

    This is most likely what's happening:

    The posibilities of why this Happens:

    How to Fix It:

    Unify Kotlin Versions: Ensure all parts of your project use the same Kotlin version. Update your build.gradle to use the latest version that all your dependencies support:

    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib:1.8.0"
    }
    

    Check Dependencies: Use tools like gradle dependencies or gradle --info to see which dependencies are using which Kotlin version. Force Version: If a library insists on using an older version, you might need to force the version you can add this:

    configurations.all {
        resolutionStrategy.force 'org.jetbrains.kotlin:kotlin-stdlib:1.8.0'
    }
    

    Clean and Rebuild: Sometimes, a simple clean and rebuild can resolve issues where old bytecode lingers.

    Check for Conflicts: Look for any plugins or other configurations that might be forcing a specific Kotlin version.

    If you follow all the above you wont need to downgrade your Kotlin Version, in fact if you can update your kotlin version the latest working nice is version 1.9.24