142

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
jeffmayn
  • 1,785
  • 2
  • 9
  • 22

16 Answers16

219

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

Mohammad Rabiee Nasri
  • 1,309
  • 2
  • 7
  • 26
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
26

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

11

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

  • 3
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Lies Feb 10 '22 at 21:53
  • In Flutter Got to Andriod/gradle/build.gradle File and Updaet – Asim Khan Jun 11 '23 at 12:33
10

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
Ary Anzh
  • 406
  • 3
  • 15
7

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.

Ansshkki
  • 760
  • 8
  • 14
7

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

Anand
  • 4,355
  • 2
  • 35
  • 45
4
  • 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"
      }
    }
    
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
4

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
Adel
  • 5,341
  • 2
  • 21
  • 31
4

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

saigopi.me
  • 14,011
  • 2
  • 83
  • 54
1

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

0

Maybe anyone else is stumbling across an issue I had after upgrading flutter: If you use flutter together with aws_amplify you have to also upgrade amplify. This is a issue I had with the same error message.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31640322) – A S M Sayem May 04 '22 at 19:25
0

You can reach the latest version from here.

Hernan
  • 163
  • 2
  • 6
0

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.

ahmnouira
  • 1,607
  • 13
  • 8
0

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

0

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

Asim Khan
  • 309
  • 3
  • 2
0

It is always worth checking that you are trying to build the right Flutter project. I ran into this issue recently only to discover that I had accidentally switched to an older version of the project - another folder - which I had replicated when I made a long delayed flutter pubgrade

DroidOS
  • 8,530
  • 16
  • 99
  • 171