Karthy Sbk
Karthy Sbk

Reputation: 4093

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

In Stripe, my client wants email and cardholder name, but the Stripe payment UI doesn't provide that option in com.stripe.android.view.CardMultilineWidget. I wanted to give it a try with the latest stripe version,

  1. I was using Stripe version (14.1.1). So I updated it to the latest one (16.8.0)

  2. The build showed me the error that it doesn't take minSdkVersion 19. It requires 21 in manifest merger. So I updated minSdkVersion to 21.

  3. I got

    caches/transforms-2/files-2.1/4541b0189187e0017d23bbb0afebd16a/jetified-kotlin-stdlib-common-1.5.0.jar!/META-INF/kotlin-stdlib-common.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.
    

I tried changing the Gradle version, but I am still getting the same error. How can I solve the incompatible error and add the email and cardholder name in Stripe?

Upvotes: 397

Views: 638606

Answers (30)

Mehedi
Mehedi

Reputation: 607

[2025] Solution with Explanation

In Oct 2024, a lot of the answers got a bit outdated for my case. Due to mixing up in the process, I lost some time. But it is actually very easy. Hence, I'll sum up what will actually work with explanation.

[Normally, you only need THIS FIRST PART]

First, let's look at what Flutter is suggesting. Flutter suggestion for fixing the error

For older versions than Flutter 3.19, you'd work on build.gradle file, which is the case in most of the answers.

But most likely you are using a newer version, so you must work on primarily settings.gradle. But... If you have already mixed up things like me you need to make sure to fix a few things.

The error says Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.7.1. > here, the binary version is what is written in your settings.gradle file for kotlin, and the expected version is associated with your AGP version i.e. your AGP version is not compatible with your kotlin version. You can find the version compatibility here with AGP vs Kotlin (KGP).

Now, open your settings.gradle file, and put the versions (that you want and compatible with each other) here [NOTE: I have used a different version than my error message (as per my interest) - 2.0.20 for Kotlin, and 8.1.1 for AGP] -

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id 'com.android.application' version '8.1.1' apply false
    id 'org.jetbrains.kotlin.android' version '2.0.20' apply false
}

[You may need THIS PART if your other settings are messed up]

Now, you need to make sure that you don't overwrite or mix up these values in other places.

  • REMOVE (if present) this block from project gradle (android/build.gradle). It is not required in newer version of Flutter or android project. remove buildscript block
  • Go to app gradle (android/app/build.gradle). Make sure these 3 numbers match, if present (it threw an error until I matched them), otherwise ignore. I used Kotlin's JRE version 17 as jvmTarget. jvmTarget
  • IMPORTANT open graddle-wrapper.properties > MAKE SURE distributionUrl has the same version number as AGP. In my case, it's 8.1.1. distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-all.zip
  • Now, clean the gradle caches (usually C:\Users\NAME\.gradle\caches). Close your IDE if required.
  • Open project/android folder in Android Studio. Make sure you have Kotlin plugin installed on it (Settings > Plugins). Let Android Studio prepares the project. It will automatically download AGP and related files. If you have changed any project settings in File > Project Structure, make sure they are undone, and keep the defaults. Click the elephant icon at the top: Sync Project with the Gradle Files. It should complete the build normally. You can ignore AGP upgrade notification, if you are confident that you chose the compatible AGP in the earlier stage.
  • From terminal, flutter clean > flutter pub get > flutter run or flutter build apk --build-name=1.0 --build-number=1 --no-tree-shake-icons. These will complete the build normally as well.

Upvotes: 15

aaronvargas
aaronvargas

Reputation: 14192

Although this question has been answered, I think it's worth explaining what is happening

For the example:

The binary version of its metadata is 1.7.1, expected version is 1.5.1.

The expected version is the Kotlin for kotlin-gradle-plugin

The binary version is what is downloaded (or previously compiled) for com.android.tools.build:gradle

<project_dir>/android/build.gradle

buildscript {
    ext.kotlin_version = '1.5.20' // <= expected version is 1.5.1
}

dependencies {
    classpath 'com.android.tools.build:gradle:7.3.1' // downloads 1.7.1 Metadata
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // <= 1.5.20 used here
}

Why is this Happening So Much?

  1. The user updates the Kotlin version of the plugin to match the IDE version per the warning.

    Warning from Android Studio to match Kotlin Versions

  2. The user updates the android build tools gradle plugin per the warning

    Warning about android build tools

This is the WRONG version!

Now you don't have any warnings, but the version suggested is 7.1.3 which is not the latest. (I don't know why it suggests this older version) 7.3.1 is currently the latest and is meta data 1.7.1, so it will match the Kotlin version of 1.7.20 (which is also metadata 1.7.1)

What else could be wrong?

Due to caching, Gradle may be using an older dependency before you updated. To start clean:

  1. delete the ~/.gradle/cache directory
  2. delete the android/.gradle directory
  3. delete the project_dir/build dir
  4. ensure the android/gradle/gradle-wrapper.properies has the correct distributionUrl (currently distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip)
  5. from project_dir do flutter build apk

NOTE: your dependencies may need to be updated if their com.android.tools.build:gradle version is too old. Alternatively, both the Kotlin and tools:gradle versions can be downgraded to compatible version that match metadata (although Android Studio will warn for that not matching the IDE Kotlin version)

How to prevent this from happening again?

Upvotes: 350

Yash
Yash

Reputation: 165

I faced same issue with different version while updating libraries to latest versions in a legacy project, After looking around I found simple solution,

In project level gradle under buildScript tag add

ext {
    ext.kotlin_version = '1.9.0'

}
dependencies {
    ......
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    ....
}

In app level gradle under android tag

compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
}

These two changes worked for me perfectly fine for me,

Note: also some time updated libraries require compileSdk 34 so please set accordingly.

Happy coding :)

Thanks

Upvotes: 0

Photal Khan
Photal Khan

Reputation: 11

just go to settings.gradle and change kotlin version as i changed to

plugins {

    id "dev.flutter.flutter-plugin-loader" version "1.0.0"

    id "com.android.application" version "7.3.0" apply false
    // START: FlutterFire Configuration

    id "com.google.gms.google-services" version "4.3.15" apply false
    // END: FlutterFire Configuration

    id "org.jetbrains.kotlin.android" version "1.7.10" apply false. // change this line to latest version like below
}

plugins {

    id "dev.flutter.flutter-plugin-loader" version "1.0.0"

    id "com.android.application" version "7.3.0" apply false
    // START: FlutterFire Configuration

    id "com.google.gms.google-services" version "4.3.15" apply false
    // END: FlutterFire Configuration

    id "org.jetbrains.kotlin.android" version "1.9.0" apply false // 1.7.10 to 1.9.0
}

its worked for me

Upvotes: 0

Galahad
Galahad

Reputation: 373

In my case, changing the kotlin version did not help.

My problem was a compatibility issue between one of my imports using Kotlin and my version of Gradle (Here is the compatibility chart).

To resolve it, you can use the Android Studio's AGP Upgrade Assistant:

Tools (In toolbar) -> AGP Upgrade Assistant -> Select the comptatible Gradle version.

Upvotes: 0

Ziaad Aboelela
Ziaad Aboelela

Reputation: 449

in my case for

The binary version of its metadata is 1.7.1, expected version is 1.5.1

go to (dependencies) inside build.gradle(project) convert from 1.5.x (x) in my case is (20)

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"

to 1.7.10

 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10"

Upvotes: 13

Ahmed tharwat
Ahmed tharwat

Reputation: 15

I faced this problem and solved it by the way:

In the android/setting.gradle file, change this line:


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

**notice ext.kotlin_version = '1.9.0'

Upvotes: 0

dingoo
dingoo

Reputation: 173

I had an error like that it said..

Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.

Solution I went to pubspec.yaml and find the package that was causing this because for me it happened after adding couple of libraries, you need to test(comment & uncomment) untill you find (it/them), for me it was the latest package url_launcher

url_launcher: ^6.2.5 

I changed it to a specific old version

url_launcher: 6.0.5  
  • Clean project
  • build apk

and it worked!

in my android/settings.gradle

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

in my android/gradle/wrapper/gradle-wrapper.properties

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

in my android/build.gradle

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
}

Upvotes: 1

3c71
3c71

Reputation: 4541

A lot of answers that are not quite helpful. Setting this or that version does not help as Kotlin-AGP-Android Studio must all be using compatible versions.

A good start is the second table on this page:

https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-compatibility-and-versioning.html#kotlin-compatibility

Then you need to be sure your version of Android Studio is compatible with the gradle version you want to use which can be found here:

https://developer.android.com/build/releases/gradle-plugin

If you're still having issues, try to clean all cache folders:

  • ~/.gradle/cache
  • ~/.gradle/caches
  • <project_dir>/build
  • /.gradle

Upvotes: 0

Hassaan
Hassaan

Reputation: 120

I was using 1.92.22 in build gradle

ext.kotlin_version = '1.9.22'

in setting.gradle it was 1.7.2 in plugin when i correct this it work

Upvotes: 2

Iseulde Lange
Iseulde Lange

Reputation: 76

In my case it was a cryptic reason. It was running out of memory during build, yet wouldn't be explicit about it, instead throwing the exception about incompatible version of Kotlin.

For me, the solution was to change org.gradle.jvmargs=-Xmx1536m to org.gradle.jvmargs=-Xmx2048m in gradle.properties

Upvotes: 1

Antonin GAVREL
Antonin GAVREL

Reputation: 11259

Straightforward answer

You need to make sure compose_version, compose_compiler and gradle distribution url as well as Kotlin version are ALL compatible.

It is just a big headache because you need to check the following websites:


Step by step to make it work with most recent versions from March 2024:

flutter clean (and also trash content of .gradle/caches potentially)

Copy/paste the following inside android folder (make a backup, don't blame me if things don't go accordingly):

settings.gradle (don't forget to replace android { namespace with your package)

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 "my.package.appname"
    compileSdk 34
    ndkVersion flutter.ndkVersion


    buildFeatures {
        compose = true
    }


    composeOptions {
        kotlinCompilerExtensionVersion compose_compiler
    }
    dependencies {
        // stable 1.2.1 releases
        implementation "androidx.compose.material:material:$compose_version"
        //...
    }


    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 "stack.technology.mymap"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdk flutter.minSdkVersion
        targetSdk 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 {

}

gradle/wrapper/graddle-wrapper.properties

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

android/build.gradle

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
}

buildscript {
    ext.kotlin_version = '1.9.22' // Change here
    ext {
        compose_compiler = '1.5.10'
        compose_version = '1.6.3'
    }

    repositories {
        google()
        jcenter()
    }

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

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 "stack.technology.mymap"
    compileSdk 34
    ndkVersion flutter.ndkVersion

    buildFeatures {
        compose = true
    }

    composeOptions {
        kotlinCompilerExtensionVersion compose_compiler
    }

    dependencies {
        // stable 1.2.1 releases
        implementation "androidx.compose.material:material:$compose_version"
        //...
    }
    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 "stack.technology.mymap"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdk flutter.minSdkVersion
        targetSdk 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 {
}

How it should work smoothly, as of March 2024.

Upvotes: 0

AxelA
AxelA

Reputation: 53

Updated solution for flutter 3 :

Edit this line to set your kotlin to latest version :

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

in file : project/android/settings.gradle.

The latest koltin version can be find here : https://kotlinlang.org/docs/releases.html#release-details

Upvotes: 3

Rahul Hawge
Rahul Hawge

Reputation: 164

For me the trick was to update the android library version as well along with kotlin version

Before

plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.0' apply false

}

After

plugins {
id 'com.android.application' version '7.4.0' apply false
id 'com.android.library' version '7.4.0' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false

}

Upvotes: 1

Ramya Musunuru
Ramya Musunuru

Reputation: 471

If this happens on intelliJ go to preferences --> search for kotlin compiler --> update kotlin compiler version to 1.1.15

Upvotes: 0

Hpsaturn
Hpsaturn

Reputation: 2742

Kotlin config is a nightmare, I have two project, a 100% Java and other with Kotlin, wow, is a huge difference. (And I don't talk about Flutter, it is a similar nightmare). Well, many noise with that. At least for me worked the next steps:

  1. running the suggested Kotlin migrations from the IDE
  2. invalidate caches

And I added the kotlin-bom like this:

dependencies {
    ...
    implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    ...
}

in the main build.gradle file:

buildscript {
    ext.kotlin_version = '1.7.20'
    
    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

And also update the target to:

android {
    compileSdkVersion 34
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 34
        ...
    }
}

Upvotes: 2

basileus
basileus

Reputation: 455

Apparently there might be a vast variety of reasons of this fault. In my case, when I even did not use Kotlin but Java, the problem was I updated Firebase library version to the newest one. I switched from:

implementation 'com.google.firebase:firebase-analytics:21.2.2'

to:

implementation 'com.google.firebase:firebase-analytics:21.5.0'

an it caused analogous fault. I switched back to the old version and it worked.

"Funny" fact was that fault popped out only when I build apk file. When I build aab for Google Play everything went smooth.

Just for information for others who will check this thread.

Upvotes: 3

h-bomb
h-bomb

Reputation: 356

In our case, it was a change in an update to Auth0. In build.gradle we were using a prefix version range:

implementation 'com.auth0.android:auth0:2.+'

Since we are overriding the Kotlin version, we just needed to update the Auth0 version to an exact version to prevent the 6K+ build errors:

implementation 'com.auth0.android:auth0:2.10.1'

Upvotes: 0

Vaheed01
Vaheed01

Reputation: 1067

Has anyone tried changing "environment" setting inside pubspec.yaml? because It worked for me. I just copied one of my recently created projects "environment" setting to my older project.

Upvotes: 0

Apit John Ismail
Apit John Ismail

Reputation: 2155

I simply download the updated version.

enter image description here

I'm currently using macmini with m1 chip.

Before, the version of my android studio was bumble-bee. Now I've updated to version Giraffe.

Everything works fine after that.

Upvotes: 0

Aerim
Aerim

Reputation: 2036

In my case I was doing:

implementation("[depName]")

Instead of:

implementation "[depName]"

So basically I was trying to use kotlin instead groovy

Upvotes: -2

Sajjad Rahbari
Sajjad Rahbari

Reputation: 51

go to Build.gradle (Project : NAME_PROJECT) and set code id 'org.jetbrains.kotlin.android' version '1.8.0' apply false in plugins """ clear any code out of plugin """

Upvotes: 5

Devansh Maurya
Devansh Maurya

Reputation: 1012

Many times, the error can arise after a library integration. The integrated library may be compiled using a Kotlin version newer than the one used in your project. In that case, there can be two solutions:

  1. Downgrade the library dependency version.
  2. Upgrade the Kotlin version for your own project to be greater than or equal to the library's Kotlin version.

Upvotes: 1

Raj Kanchan
Raj Kanchan

Reputation: 618

In such case, read the build.gradle (:project) carefully. I overlooked the warning that's why I was getting this problem.

Initially my settings of build.gradle (:project) was

plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

As I was getting this error

Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.

So I changed this setting in the build.gradle (:project) to the following:

plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
}

Hope it solves your problem.

Upvotes: 19

Ann
Ann

Reputation: 1

I upgraded the android studio version to the latest and that resolved the issue

Upvotes: -2

Prasanna Kumar
Prasanna Kumar

Reputation: 172

Please check if you Kotlin version is compatible with compose version. if not, then please make the changes and it should work fine.

Compose Compiler Version Compatible Kotlin Compiler Version

1.3.0 --> 1.7.10
1.3.0-rc01 --> 1.7.10
1.3.0-beta01 --> 1.7.10
1.2.0 --> 1.7.0
1.2.0-rc01 --> 1.6.21
1.2.0-beta03 --> 1.6.21
1.2.0-alpha08 --> 1.6.20
1.1.0 --> 1.6.10
1.1.1 --> 1.6.10
1.1.0-rc031.6.10

Upvotes: 0

Harish
Harish

Reputation: 1

solution A failure occurred while executing com.android.build.gradle.internal.tasks

Upvotes: -1

reddy tintaya
reddy tintaya

Reputation: 144

For macOS you can

rm -r $HOME/.gradle/caches/

or you can invalidate caches

File >> Invalidate caches

Upvotes: 6

Confiance
Confiance

Reputation: 936

Go to the file build.gradle, change the version of kotlin. In case in my flutter project I opened build.gradle and changed

`ext.kotlin_version = '1.5.30'` 

to ext.kotlin_version = '1.6.0'

Here

buildscript {
ext.kotlin_version = '1.6.0'

repositories {
    google()
    jcenter()
}

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

}


Then save and do

flutter clean and flutter run.

Works fine for me.

Upvotes: 6

kaushal Kishore
kaushal Kishore

Reputation: 151

Changed the Project build gradle to

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

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

Upvotes: 15

Related Questions