skip to Main Content

Flutter build fails after updating to new Kotlin version
I’m working on a Flutter project, and after updating some dependencies, I encountered a build error. I’m using the latest version of Kotlin, and when I run flutter build apk, it fails with the following errors:

e: C:/Users/Moham/.gradle/caches/transforms-3/1a36c150372f3edd1303b1fe7aa45a27/transformed/fragment-1.7.1-api.jar!/META-INF/fragment_release.kotlin_module: 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.
Running Gradle task 'assembleRelease'...                          251.8s

┌─ Flutter Fix ──────────────────────────────────────────────────────────────────────────────────────────┐
│ [!] Your project requires a newer version of the Kotlin Gradle plugin.                                 │
│ Find the latest version on https://kotlinlang.org/docs/releases.html#release-details, then update the  │
│ version number of the plugin with id "org.jetbrains.kotlin.android" in the plugins block of            │
│ C:flutter_applklk-liveandroidsettings.gradle.                                                      │
│                                                                                                        │
│ Alternatively (if your project was created before Flutter 3.19), update                                │
│ C:flutter_applklk-liveandroidbuild.gradle                                                          │
│ ext.kotlin_version = '<latest-version>'                                                                │
└────────────────────────────────────────────────────────────────────────────────────────────────────────┘


It seems to suggest that the Kotlin versions are incompatible. Here’s the setup in my 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
}

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 "2.0.20" apply false
}

include ":app"

It seems I need to update the Kotlin Gradle plugin, but I’m unsure how to properly resolve this issue. I also see a suggestion to update the plugin in C:flutter_applklk-liveandroidbuild.gradle, but I’ve already set the kotlin_version to the latest version (2.0.20).

Has anyone experienced this issue? How can I resolve this Kotlin version incompatibility?
Flutter (Channel stable, 3.24.0, on Microsoft Windows [Version 10.0.19045.2486], locale en-US)

2

Answers


  1. Add this code in your android/build.gradle:

    buildscript {
        ext.kotlin_version = '2.0.20' //Your preferred Kotlin Version
        repositories {
            google()
            mavenCentral()
        }
    
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
    Login or Signup to reply.
  2. If you can’t find buildscript in android/build.gradle like Nipul Rathod Answer, you probably already have a newer version flutter project that have a different flutter gradle plugin structure (read this migration guide for older project). In the newest flutter gradle plugin structure:

    Find this code in your android/settings.gradle:

    plugins {
        id "dev.flutter.flutter-plugin-loader" version "1.0.0"
        id "com.android.application" version "8.5.0" apply false
        id "org.jetbrains.kotlin.android" version "1.9.20" apply false
        id "com.google.gms.google-services" version "4.3.10" apply false
    }
    

    To edit your kotlin version change the "org.jetbrains.kotlin.android" version.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search