divijsahu
divijsahu

Reputation: 31

2024 - Flutter Fix [!] Your project requires a newer version of the Kotlin Gradle plugin

even after updating the ext.kotlin_version = '1.9.23'

I am still getting the same Flutter Fix error

[!] 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 C:\project_name\android\build.gradle: ext.kotlin_version = 'latest-version'

flutter version 3.19.6

flutter clean flutter pub get flutter pub upgrade, etc. tried but none worked

my android/build.gradle file

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

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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
}

my android/app/build.gradle file

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.project_name"
    compileSdk flutter.compileSdkVersion
    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 {
        applicationId "com.example.project_name"
        minSdkVersion 20
        targetSdkVersion 31
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {}

my gradle-wrapper.properties file

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
# distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-all.zip

Upvotes: 3

Views: 5926

Answers (2)

Rafhaela
Rafhaela

Reputation: 93

According to the docs the away we deal with gradle plugins has changed. Follow these steps to your project. Basically the build.gradle on android level is now like:

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
}

no buildScript{...} anymore.

And the settings.gradle is now 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
    }()

    includeBuild("$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 "{agpVersion}" apply false
    id "org.jetbrains.kotlin.android" version "{kotlinVersion}" apply false
}

include ":app"

You need then especify the latest kotlin version on plugins as the message says. Here's an exemple of mine:

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.21" apply false
    id "com.google.gms.google-services" version "4.4.0" apply false
    id "com.google.firebase.crashlytics" version "2.9.9" apply false
}

I had to delete my .gradle folder on users to the changes apply properly, not just the caches folder inside. Folder's path

  1. on Mac: /Users/{your_user}/.gradle
  2. on Windows: %USERPROFILE%/.gradle

I'm on Mac so I just ran rm -rf .gradle.

Next go to your project, run flutter clean and finally procede to run the app. This run took a little bit longer here but it works.

Hope it helps.

Upvotes: 3

Eleaus Hossain Evan
Eleaus Hossain Evan

Reputation: 11

I faced the same problem, but when I gave the latest Kotlin version on android/build.gradle file it worked fine for me.

but, as you face issues after updating the Kotlin version it's hard to find where wrong as I found Flutter Team changed the code inside the Android portion from Flutter SDK 3.16.

so that, the easiest way would be, first delete the whole Android folder. then run

flutter create .

this will create the Android folder with the newest code.

before that, upgrade the Flutter SDK into the newest one, minimum of 3.16. Also, change the SDK version into pubspec.yaml

And remember if you made any changes on AndroidManifest.xml or build.gradle or app/build.gradle file, and do the same after.

I hope that it will solve your issue and run your app.

Upvotes: 0

Related Questions