90

I'm running a flutter project and recently updated a bunch of dependencies. I'm getting this error:

* What went wrong:
Execution failed for task ':flutter_google_places_sdk_android:compileDebugKotlin'.
> Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'compileDebugKotlin' (17).

  Consider using JVM Toolchain: https://kotl.in/gradle/jvm/toolchain
  Learn more about JVM-target validation: https://kotl.in/gradle/jvm/target-validation 

I've already updated by build.gradle file to include the following, having already seen these questions

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_17
    }

    kotlin {
        jvmToolchain(17)
    }

    java {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

However, I still get the above error. Here's my flutter doctor:

[✓] Flutter (Channel stable, 3.13.6, on macOS 13.2.1 22D68 darwin-x64, locale en-AU)
    • Flutter version 3.13.6 on channel stable at /Users/sophiahuynh/Documents/_gitLabProjects/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ead455963c (8 weeks ago), 2023-09-26 18:28:17 -0700
    • Engine revision a794cf2681
    • Dart version 3.1.3
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/sophiahuynh/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Users/sophiahuynh/Library/Java/JavaVirtualMachines/jbrsdk-17.0.9/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment JBR-17.0.9+7-1087.3-nomod (build 17.0.9+7-b1087.3)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.13.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.84.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.76.0

[✓] Connected device (3 available)
    • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 8.1.0 (API 27) (emulator)
    • macOS (desktop)                    • macos         • darwin-x64     • macOS 13.2.1 22D68 darwin-x64
    • Chrome (web)                       • chrome        • web-javascript • Google Chrome 119.0.6045.159

[✓] Network resources
    • All expected network resources are available.

  • I'm running Java 17.0.9 (incl. my java home, android studio gradle jdk)
  • I'm running Gradle 8.4 (was running 8.0 before and still facing the same issue)
  • Kotlin 1.9.20 (also faced the same issue when I was on 1.8.0)

I'm quite lost at what else I can try to do. Any help is appreciated. Thanks!!

1
  • Might be this can help! Please check, Commented Jan 15, 2024 at 6:29

16 Answers 16

141

I was facing same error when upgrading Gradle 8.2 and Kotlin 1.9.22.


1. Make sure you have the same version here:

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

and here

    kotlinOptions {
        jvmTarget = "17"
    }

2. Add in your gradle.properties:

kotlin.jvm.target.validation.mode = IGNORE

This solution maybe doesn't solve the issue but can bypass it until it is fixed.

Only if step 1 didn't work.


Hope it helps.

Sign up to request clarification or add additional context in comments.

7 Comments

Why do I need to ignore this?
@JesséLopesPereira Because Flutter is years behind in their gradle config. They've dropped the ball there
are you sure of that? Did not work for me at least, Execution failed for task ':disk_space_plus:compileReleaseKotlin'. Inconsistent JVM-target compatibility detected for tasks 'compileReleaseJavaWithJavac' (1.8) and 'compileReleaseKotlin' (17).
This is causing an error in Hilt for me: Unsupported metadata version. Check that your Kotlin version is >= 1.0
kotlin.jvm.target.validation.mode = IGNORE This is definitive solution also for shared_preferences: ^2.3.3 and its shared_preferences_android issue for his jvmTarget 11 Vs my jvmTarget 17, during upgrade of Android Studio to Koala and 2024-11 flutter dependency upgrade to Major Version. It saved me after 1 weeks of hard search and try, close to random, on the web! Thank you! @Jago
|
19

Java version: openjdk 17.0.13
Gradle version: 8.12

ext.kotlin_version = '2.0.21'

android/app/build.gradle:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
    jvmTarget = '17'
}

Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'compileDebugKotlin' (17).

Solution:
Add inside android/gradle.properties this code :

kotlin.jvm.target.validation.mode = IGNORE

1 Comment

That work also with JavaVersion.VERSION_1_8 and jvmTarget = '1.8'
13

This worked for me as per Kotlin docs https://kotlinlang.org/docs/gradle-configure-project.html#gradle-java-toolchains-support.

android {
  compileOptions {
    // I had this but now was able to remove it
    //sourceCompatibility = JavaVersion.VERSION_17
    //targetCompatibility = JavaVersion.VERSION_17
  }
}

kotlin {
  // Adding this solved it
  jvmToolchain(17)
}

This is me on Kotlin 2.0.21, AGP 8.7.0, Gradle 8.10.2.

1 Comment

Only correct answer in this thread, thx!
8

A warning for Android users. To use Gradle toolchain support, use the Android Gradle plugin (AGP) version 8.1.0-alpha09 or higher.

This worked for me:

android {
    compileSdk 34

    compileOptions {
        sourceCompatibility = 17
        targetCompatibility = 17
    }

1 Comment

May I know the version you've used for kotlin and gradle?
7

I encountered the "Inconsistent JVM target compatibility detected" error while working with the flutter_native_picker_contact package in my Flutter project. The issue arose because Java and Kotlin were targeting different JVM versions—Java was set to 1.8, while Kotlin was using 17.

Initially, adding the following line to gradle.properties seemed to resolve the issue:

kotlin.jvm.target.validation.mode = IGNORE

However, while this approach allows the project to compile, it feels more like a workaround than a proper solution. Ignoring JVM target validation does not fix the root cause of the issue. Instead, it simply bypasses the validation, which could lead to unexpected runtime errors, performance issues, or even compatibility problems with future updates.

Rather than applying a global ignore flag, a better approach is to ensure that Java and Kotlin target the same JVM version within the affected library. What ultimately resolved my issue was modifying the android/build.gradle file and explicitly setting the compatibility within the package:

    project(':flutter_native_contact_picker').afterEvaluate {
project(':flutter_native_contact_picker').android {
    compileSdkVersion flutter.compileSdkVersion
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }
}}

1 Comment

thanks! I did similar but it applies to ALL libraries which I don't want. your approach is better and working fine.
4

This worked for me in Gradle 8.14.3, Kotlin 2.2.0 and Android Studio Narwhal Feature Drop. Here's my build.gradle.kts.

compileOptions {
  sourceCompatibility = JavaVersion.VERSION_23
  targetCompatibility = JavaVersion.VERSION_23
}

kotlin {
  jvmToolchain(23)
}

You can also check here the Java and Gradle Compatiblity Matrix

2 Comments

What file, precisely, are you editing here?
@JohnPerry build.gradle.kts
3

I faced the same issue and almost did the same as the asker but it turns out that the solution is so easy and did not require any Gradle update above 8.0, below is the error and solution:

THE ERROR:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileReleaseKotlin' // Note Below

> Inconsistent JVM-target compatibility detected for tasks
 'compileReleaseJavaWithJavac' (1.8) and 'compileReleaseKotlin' (17).

Consider using JVM Toolchain: https://kotl.in/gradle/jvm/toolchain
Learn more about JVM-target validation: https://kotl.in/gradle/jvm/target-validation

Note: The file causing the error in the question is different from the line above since it is coming from this package the asker is using.

Also note: This package has a mark on its page of FlutterTests: Failing, So it should be avoided.

After recognizing the question date, It appeared that the package had this issue at the date of the question and its maintainer fixed it on 17-08-2024 in this commit specifically in this line.

THE SOLUTION (regardless of the file the solution remains the same) :

is to simply correct the error i.e. resolving the inconsistency by looking at file mentioned in the error, mine was android/app/build.gradle:

...

android {
    namespace = <my_app_namespace>
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8 // lines of interest
        targetCompatibility = JavaVersion.VERSION_1_8 // lines of interest
    }

    defaultConfig {

....

Since there wasn't any kotlinOptions block in the file I just added a kotlinOptions block with the compatibile jvmTarget like this:

...

android {
    namespace = <my_app_namespace>
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8 // lines of interest
        targetCompatibility = JavaVersion.VERSION_1_8 // lines of interest
    }

    /// Add this block according to lines of interest
    kotlinOptions {
        jvmTarget = '1.8'
    }

    defaultConfig {

....

Worked for me without any other modifications in the android folder.

Comments

2

Just make sure that compatibility versions

compileOptions {
    sourceCompatibility = JavaVersion.VERSION_22
    targetCompatibility = JavaVersion.VERSION_22
}

and JvmTarget versions match

jvmTarget.set(JvmTarget.JVM_22)

Comments

1

Add this under android/build.graldle, outside buildscript {}; don't put it inside of buildscript:

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty("android")) {
            project.android {
                compileOptions {
                    sourceCompatibility JavaVersion.VERSION_17
                    targetCompatibility JavaVersion.VERSION_17
                }
            }
        }
        if (project.hasProperty("kotlinOptions")) {
            project.kotlinOptions.jvmTarget = "17"
        }
    }
}

Add this under gradle.properties:

kotlin.jvm.target.validation.mode = IGNORE

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?
0

In some cases, you can have more than one build.gradle file.

One way you can quickly find them is by doing a universal search for "compileOptions" and seeing the results and in which places.

If the problem still persists, you will need to make this change matching CompileOptions and KotlinOptions (as described above) in the other ones. Typically the error will mention the module. List of gradle files This was my scenario.

Comments

0

Try:

  • Kotlin Gradle plugin: 2.0.21
  • Android Gradle plugin: 8.5
  • Gradle version: 8.8

In android\build.gradle:


    dependencies {
                classpath('com.android.tools.build:gradle:8.5')
        
                classpath('org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21')
                
                //... others classpath lines...
    }

In android\gradle\wrapper\gradle-wrapper.properties:


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

Comments

0

The answers all use a deprecated API. Meanwhile it's more alike this:

kotlin {
    compilerOptions {
        jvmTarget = JvmTarget.JVM_17
    }
}

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
}

The difference in Kotlin script is:

jvmTarget.set(JvmTarget.JVM_17)

2 Comments

Could not get unknown property 'JvmTarget' for object of type org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptionsDefault
The example is Groovy. In Kotlin the syntax is different: jvmTarget.set(JvmTarget.JVM_17)
0

In my case it was a wrong configuration in IntelliJ / Android Studio. How I solved it:

  1. go to Settings | Build, Execution, Deployment | Build Tools | Gradle

  2. choose the proper Gradle JVM version

Comments

-1

For me the issue was solved by adding this to the general build.gradle.kts.

   allprojects {
      tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java).configureEach {
         kotlinOptions {
             jvmTarget = "17"
         }
      }
    ...
}

1 Comment

I get error: "No such property: java for class: org.jetbrains.kotlin.gradle.tasks.KotlinCompile"
-1

The active installed JDK version must match what you have in guild.gradle settings for sourceCompatibility = JavaVersion.VERSION_18 and `jvmTarget = "18"`.If you have a higher JDK version installed and configured, you will still get the error.. It must be an exact match!

I experienced this error becasue my gradle files were set up to use a sourceCompatibility/jvmTarget of 18, but I had JDK 21 configured in Android Studio (causing me to get the error there) and on the path (causing me to get the error on the CLI). As soon as I installed an older JDK 18 and configured that in AndroidStudio, the problem went away there. As soon as I put this JDK 18 on the path, the problem went away when running a build on the CLI.

Comments

-2

This works for me

    compileOptions {
    // sourceCompatibility JavaVersion.VERSION_17
    // targetCompatibility JavaVersion.VERSION_17
    coreLibraryDesugaringEnabled true

}

kotlin {
    jvmToolchain(17)
}

1 Comment

Please add more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.