Flutter_newer version of the Kotlin Gradle plugin

118 views Asked by At

I am new to Flutter/coding. I am trying to work on my first project. I am following an online tutorial to create a weather app. I followed all the instructions but when I am running the app, nothing shows up on my emulator. There is an error on the Debug Console as below:

Error on Debug Console

I tried to google the error and found that I need to update the kotlin version in the buildscript in the build.gradle file. But in my build.gradle file there is no buildscript section. See below:

build.gradle

I again googled for solution and I thought what if I add the code in my build.gradle file so I added the code with latest kotlin version 1.9.22 yet the error is still there.

Where am I going wrong.

I tried all solutions available in youtube and google. I have spent almost a day looking for it but no luck.

  • I am running it on a windows 10, android emulator.
2

There are 2 answers

5
Mehedi Hasan On

From my experience, Many times I have found that the latest Kotlin version is incompatible with the latest Flutter version.

Please, try to downgrade the version to 1.9.20 or less. Also try stable versions like 1.8.22, ..., 1.7.10, or others. Pick one that suits your project the best from here: https://kotlinlang.org/docs/releases.html#release-details

and also from your screenshot, I saw that the section below is missing in your android/build.gradle file.

Add this and update your Kotlin extension version accordingly:

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.3' // 8.2.2 // 7.1.3 etc
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
0
doron On

So it seems they changed the way they are loading kotlin to a plugin mode.

What I've done is to open the settings.gradle file which is location under the android folder and changed the version of org.jetbrains.kotlin.android to my desired version.

So the file now looks like

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

include ":app"

Note: I had to restart my computer in order for this to work... Hope it helps.