jeffmayn
jeffmayn

Reputation: 2611

Your project requires a newer version of the Kotlin Gradle plugin. (Android Studio)

I've just updated my flutter project packages to be null-safety compliant and now Android Studio wants me to update my project to use the latest version of Kotling Gradle Plugin. Can't see where to change this though. I have tried to change "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" into "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10" but this has no effect.

My build.grade-file looks like this:

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

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

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

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

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 31

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "*********"
        minSdkVersion 30
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }



    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 '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.google.firebase:firebase-analytics:17.2.2'
}
apply plugin: 'com.google.gms.google-services'

Build output:

BUILD FAILED in 8s
[!] Your project requires a newer version of the Kotlin Gradle plugin.
    Find the latest version on https://kotlinlang.org/docs/gradle.html#plugin-and-versions, then update project/android/build.gradle:
    ext.kotlin_version = '<latest-version>'
Exception: Gradle task assembleDebug failed with exit code 1

Upvotes: 215

Views: 351303

Answers (27)

eManuel
eManuel

Reputation: 23

Quick workaround: Delete this file, if you notice a file at this directory below:

android/app/src/main/kotlin/com/example/myapp/MainActivity.kt

Upvotes: 0

Muhammad Umair Saqib
Muhammad Umair Saqib

Reputation: 1865

For old version go to app/build.gradle file and update the kotlin version.

For the latest versions, open settings.gradle file. find id "org.jetbrains.kotlin.android" version "x.x.x" apply false and replace the version with 2.0.0 and this line will look like id "org.jetbrains.kotlin.android" version "2.0.0" apply false.

Upvotes: 1

Roscoe - ROSCODE
Roscoe - ROSCODE

Reputation: 39

What worked for me was simply adding these 2 lines to my android/settings.gradle file in the 'plugins' section:

id "com.android.application" version "7.3.0" apply false

id "org.jetbrains.kotlin.android" version "2.0.0" apply false

Original:

   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")

    plugins {
        id "dev.flutter.flutter-gradle-plugin" version "1.0.0" apply false
    }
}

include ":app"

apply from: "${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle/app_plugin_loader.gradle"

Updated:

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")

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

include ":app"

apply from: "${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle/app_plugin_loader.gradle"

Upvotes: 0

9Dragons
9Dragons

Reputation: 165

Easy way to solve this kind of problem is just create new project in android studio and check the related files. Current settings.gradle is as follows.

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 "8.1.0" apply false
    id "org.jetbrains.kotlin.android" version "1.8.22" apply false
}

include ":app"

Upvotes: -1

RITTIK
RITTIK

Reputation: 926

For me, I just updated android/settings.gradle file:

plugins {
    id "org.jetbrains.kotlin.android" version "1.9.24" apply false
}

for newer versions of kotlin check here

Upvotes: 0

Rishita Joshi
Rishita Joshi

Reputation: 415

to solve this problem, you have to change the kotlin version. i used flutter version 3.22.10 . so i changed my plugin version 2.0.0 . or else you have to change the settings.gradle file i attched build.gradle file please find it for refrance enter image description here find the kotlin version

https://kotlinlang.org/docs/releases.html#release-details

Upvotes: 2

Vivek Yadav
Vivek Yadav

Reputation: 171

If you are getting error of latest version of kotlin in flutter then this will work for you:

  1. Open file : Your Project/android/settings.gradle

  2. Update the line:

    id "org.jetbrains.kotlin.android" version "2.0.0" apply false
    

    with the most recent version of kotlin.

  3. Run flutter clean then flutter run or invalidate cache.

Update the last line with the latest version of kotlin.

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

As of July 2024 latest stable release version of kotlin is 2.0.0

Upvotes: 3

Tim Kariuki
Tim Kariuki

Reputation: 2070

  1. Open file : Your Project/android/settings.gradle

  2. Update the line:

    id "org.jetbrains.kotlin.android" version "2.0.0" apply false

    with the most recent version of kotlin

    You can find latest version of kotlin here

  3. Run flutter clean then flutter run or invalidate cache

Update the last line with the latest version of kotlin

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

As of June 2024 latest stable release version of kotlin is 2.0.0

Upvotes: 131

St&#233;phane COUGET
St&#233;phane COUGET

Reputation: 221

This settings work also :

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

Upvotes: 1

shereef hamed
shereef hamed

Reputation: 79

In the new version of flutter you should upgrade your Kotlin version in android/setting.gradel file

    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
    }

in the last line upgrade the Kotlin version

Upvotes: 4

A.Ktns
A.Ktns

Reputation: 653

In the settings.gradle file under Android folder, add the following inside the plugins { \\\..}

id "org.jetbrains.kotlin.android" version "2.0.0" apply false

Upvotes: 2

mikyll98
mikyll98

Reputation: 2283

Here's my experience as a Windows user.

I had this error, when trying to build with flutter build apk --split-per-abi:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:packageRelease'.
> java.io.IOException: Unable to delete directory 'D:\Users\mikyll\flutter_application\build\app\outputs\apk\release'
    Failed to delete some children. This might happen because a process has files open or has its working directory set in the target directory.
    - D:\Users\mikyll\flutter_application\build\app\outputs\apk\release\app-arm64-v8a-release.apk
    - D:\Users\mikyll\flutter_application\build\app\outputs\apk\release\app-armeabi-v7a-release.apk
    - D:\Users\mikyll\flutter_application\build\app\outputs\apk\release\app-x86_64-release.apk

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1m 56s
Running Gradle task 'assembleRelease'...                          117,7s

┌─ 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 D:\Users\mikyll\flutter_application\android\build.gradle: │
│ ext.kotlin_version = '<latest-version>'                                                                                                                                                         │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Gradle task assembleRelease failed with exit code 1

I updated ext.kotlin_version to the latest (1.9.22 when I'm writing this answer), but it kept failing.

As the error is suggesting, the problem was the OpenJDK Platform binary process was frozen for some reason and flutter build could not replace the previous built APKs with the new one, since they were busy (open in that process).

Solution

I solved by terminating OpenJDK Platform binary process from task manager, deleting the old APK releases, and the next build went straight forward

Upvotes: 3

Veerendra Hullatti
Veerendra Hullatti

Reputation: 61

This is latest version Works for me.

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

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

Upvotes: 3

Keshav Singh
Keshav Singh

Reputation: 83

After spending so many hours I have found the exact version of gradle for this issue.

  1. Clear gradle cache from home directory <YOUR HOME DIRECTORY>/.gradle/caches
  2. Delete folder in your project <YOUR PROJECT DIRECTORY>/android/.gradle
  3. Set latest version of gradle in android/build.gradle. At this moment its 1.9.10. So my build.gradle file looks like enter image description here
  4. Update gradle-wrapper.properties with below distribtutionUrl distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
  5. flutter clean
  6. flutter pub get
  7. Build/Run your project now.

Upvotes: 7

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29794

You need to change the kotlin version in your android root project, projectName/android/build.gradle, instead of projectName/android/app/build.gradle.

Change the version at ext.kotlin_version line:

buildscript {
    ext.kotlin_version = '1.6.10' // Change here
    repositories {
        google()
        jcenter()
    }

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

UPDATE

The above solution will also works if your android part of the project is using java where the dependencies are using kotlin and you've encountered the following error:

Incompatible classes were found in dependencies. Remove them from the classpath or use '-Xskip-metadata-version-check' to suppress errors
e: /home/user/.gradle/caches/transforms-3/36814238b86d8b6b6f9e4e1263bce879/transformed/jetified-kotlinx-coroutines-core-jvm-1.5.2.jar!/META-INF/kotlinx-coroutines-core.kotlin_module: 
Module was compiled with an incompatible version of Kotlin. 
The binary version of its metadata is 1.5.1, expected version is 1.1.15.

AndroidStudio or IntelliJ Idea will give hint in the project build.gradle file if the kotlin is not the same with the installed kotlin in the IDE.

Note: to see the new versions of Kotlin check this link : https://kotlinlang.org/docs/releases.html#release-details

Upvotes: 276

Lakshan Kodithuwakku
Lakshan Kodithuwakku

Reputation: 133

Change ext.kotlin_version on projectName/android/build.gradle

buildscript {
   ext.kotlin_version = '1.8.22' //latest version
   repositories {
      google()
      mavenCentral()
   }

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

You can get the "latest kotlin_version" using the below link https://kotlinlang.org/docs/gradle-configure-project.html#apply-the-plugin

Upvotes: 10

Asim Khan
Asim Khan

Reputation: 365

enter image description here In Flutter Project Navigate to Android/gradle/build.gradle File and Update Kotline Version to New Version

Upvotes: 2

saigopi.me
saigopi.me

Reputation: 14938

change kotlin version to ext.kotlin_version = '1.8.21' inside android/build.gradle , it fixes your problem.

check all versions here Find Latest Kotlin Version

Upvotes: 8

Iosvany Alvarez
Iosvany Alvarez

Reputation: 101

This method in latest versions works

Directory

  • android/build.gradle
    buildscript {
     ext.kotlin_version = '1.5.31' // Put this number
     repositories {
        google()
        jcenter()
     }
   }

FAQ https://docs.flutter.dev/release/breaking-changes/kotlin-version

Upvotes: 1

ahmnouira
ahmnouira

Reputation: 3481

A huge solution for updating the Android application is to delete the android folder and then run this:

flutter create .

Note: you can also run flutter upgrade command to upgrade flutter first.

Upvotes: 1

Anandh Krishnan
Anandh Krishnan

Reputation: 6022

You have to change the kotlin version in your android root, go to

projectName/android/build.gradle

buildscript {
    ext.kotlin_version = '1.7.0'
    repositories {
        google()
        jcenter()
    }

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

And change the gradle wrapper version to 6.7.1-all or higher in the gradle-wrapper.properties

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

Also you can get the lattest version here

Upvotes: 14

Adel
Adel

Reputation: 6288

1- First change ext.kotlin_version on projectName/android/build.gradle

buildscript {
    ext.kotlin_version = '1.6.10'
}

2- Second change build gradle to this :

distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip

Upvotes: 5

Codemaker2015
Codemaker2015

Reputation: 15649

  • Change the build gradle version to 4.1.0 or higher in the project-level build.gradle file:

    classpath 'com.android.tools.build:gradle:4.1.0'
    
  • Change the gradle wrapper version to 6.7-all or higher in the gradle-wrapper.properties file:

    distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
    
  • Change the ext.kotlin_version to 1.6.10 or higher in the project level build.gradle file:

    buildscript {
      ext.kotlin_version = '1.6.10' // Change like this
      repositories {
          google()
          jcenter()
      }
    
      dependencies {
          classpath 'com.android.tools.build:gradle:4.1.0'
          classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
      }
    }
    

Upvotes: 5

Update your Kotlin to the Latest version. You can see the latest version here

ext.kotlin_version = '1.6.10' //

You can see the latest version of Kotlin from the below link https://kotlinlang.org/docs/gradle.html#plugin-and-versions

enter image description here

Upvotes: 29

I Update Flutter to 2.10.1 and I found All My Project Has Same Error in app /build.gradl you change to ext.kotlin_version = '1.6.10'enter image description here

enter image description here

Upvotes: 12

Ansshkki
Ansshkki

Reputation: 848

In my case, I don't use kotlin in my flutter project, but some of my dependencies use it (It was assets_audio_player for me), so I had to go to build.gradle for that dependency and update the kotlin version as other answers here:

ext.kotlin_version = '<latest-version>'

Note 1: you can open build.gradle for the library you want by opening the android module in Android Studio then go to Gradle Scripts from Project tab.

enter image description here

Note 2: you can know which library is causing the error by looking at:

Execution failed for task ':assets_audio_player:compileDebugKotlin'.

It could be more than one library, so after you change kotlin version the error will be changed.

Upvotes: 7

Ary Anzh
Ary Anzh

Reputation: 474

change build gradle to this :

classpath 'com.android.tools.build:gradle:4.1.0'

and gradle-wrapper to this :

distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip

Upvotes: 15

Related Questions