skip to Main Content

I can’t run my Flutter app and I am getting this 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 /Users/blabla/Desktop/projects/blabla/android/build.gradle:           │
│ ext.kotlin_version = '<latest-version>' 

I already updated /android/build.gradle file like this to include newest version:

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

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

                          

I also have this in gradle-wrapper.properties:

distributionUrl=https://services.gradle.org/distributions/gradle-7.6.4-all.zip   

My Flutter version is:

Flutter 3.19.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision abb292a07e (4 days ago) • 2024-02-20 14:35:05 -0800
Engine • revision 04817c99c9
Tools • Dart 3.3.0 • DevTools 2.31.1

Whatever I try I am getting back to this error, so I am out of ideas and I would appreciate some help.

Adding 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 "hr.ags.blabla"
    compileSdkVersion 34
    ndkVersion "25.1.8937393"

    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 "hr.ags.blabla"
        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.7.10" apply false
}

include ":app"

2

Answers


  1. Check out this article about migrating the imperative apply block.

    the buildscript block should be replaced with a plugins block at the top of your file, like

    plugins {
      id 'com.android.application'
      id 'kotlin-android'
      id "dev.flutter.flutter-gradle-plugin"
    }
    

    In settings.gradle (after any definitions):

    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.7.10" apply false
    }
    
    :app
    

    After which your build.gradle might look like:

    plugins {
        id 'com.android.application'
        id 'kotlin-android'
        id "dev.flutter.flutter-gradle-plugin"
    }
    
    android {
        namespace "<SOMETHING>"
        compileSdkVersion 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 {
            minSdkVersion 21
            targetSdkVersion flutter.targetSdkVersion
            versionCode flutterVersionCode.toInteger()
            versionName flutterVersionName
        }
    
        signingConfigs {
            release {
                keyAlias keystoreProperties['keyAlias']
                keyPassword keystoreProperties['keyPassword']
                storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
                storePassword keystoreProperties['storePassword']
            }
        }
    
        buildTypes {
            release {
                signingConfig signingConfigs.release
            }
        }
    }
    
    flutter {
        source '../..'
    }
    
    Login or Signup to reply.
  2. 1- Update the ext.kotlin_version to latest version
    e.g: ext.kotlin_version = ‘1.9.22’

    2- Open the build.gradle file located at

    /Users/blabla/Desktop/projects/blabla/android/

    3- Update the compileSdkVersion to 34

        android {
            compileSdkVersion 34
              ....
            }
    

    4- After making changes, sync your Gradle configuration by running the Gradle sync task in your IDE or executing ./gradlew command in the terminal in your project directory.

    5- Make sure to run flutter clean and flutter pub get

    6- Make sure to check your flutter doctor too if there are any problems.

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