39

Execution failed for task ':app:kspDebugKotlin'. Unable to build with ksp

'compileDebugJavaWithJavac' task (current target is 1.8) and 'kspDebugKotlin' task (current target is 17) JVM target compatibility should be set to the same Java version.

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}

14 Answers 14

25

As well as

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Add (at the same level as android {}):

kotlin {
    jvmToolchain(8)
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have had to do this with Android Studio Giraffe | 2022.3.1 Canary 7 to force ksp to continue using jdk 11 - I found this setting from youtrack.jetbrains.com/issue/KT-55947
23

I don't know why it causing problem, but I added this in gradle(project) and it worked.

allprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
}

3 Comments

Not work for me
compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = '17' } try this
thksssssssssssssss
20

For those, who still facing issue. Try updating versions of Java and Kotlin to same 17 as follow:

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

1 Comment

You save me a lot of time! Thanks!
10

KSP version must be aligned with your project's Kotlin version.

According to the official documentation

First, declare the KSP plugin in your top level build.gradle.kts file. Make sure that you choose a KSP version aligned with your project's Kotlin version. You can find a list of releases on the KSP GitHub page.

If you are using Kotlin 1.8.0 ensure to use/downgrade to KPS 1.8.0-1.0.8 because 1.8.0-1.0.9 has a known bug.

That's how I resolved the issue in my case.

// match Kotlin version 1.8.0 and use KPS 1.0.8, not 1.0.9
classpath("com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:1.8.0-1.0.8") 
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }

1 Comment

Fixed, thank you. Weird issue, it didn't even happened when using compose with 17
8
allprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions.jvmTarget = "1.8"
    }

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs).configureEach {
        kotlinOptions.jvmTarget = "1.8"
    }
}

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
this one worked for me, KaptGenerateStubs was also required
3

For me it was KSP version not compatible with kotlin version 2.0.0-Beta4. I am using kotlin version 1.9.22 (as recommended) and KSP version 1.9.22-1.0.17. I hope this helps.

id 'org.jetbrains.kotlin.android' version "$kotlin_version" apply false
id 'com.google.devtools.ksp' version "$ksp_version" apply false

Comments

2

To make sure @deive's solution works, you need JDK 1.8 downloaded also (You can do this by navigating to File -> Project Structure -> Java Options -> Download JDK). If you don't have JDK 1.8 installed, then the Gradle Kotlin DSL will keep throwing errors.

Comments

2

I got the same error when I wanted to implement Room + Hilt in the same project. So to solve this, you have to add inside your libs.versions.toml file:

[versions]
#Android
gradle = "8.5.2"
#Kotlin
kotlin = "2.0.20"
#Ksp
devtoolsKsp = "2.0.20-1.0.24"
#Hilt
hiltAndroid = "2.52"
#Libraries
composeBom = "2024.08.00"
compose = "1.5.15"
navigationCompose = "1.2.0"
room = "2.6.1"

[plugins]
android-application = { id = "com.android.application", version.ref = "gradle" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
devtools-ksp = { id = "com.google.devtools.ksp", version.ref = "devtoolsKsp" }
hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hiltAndroid" }

[libraries]
compose-bom = { module = "androidx.compose:compose-bom", version.ref = "composeBom" }
compose-material = { module = "androidx.compose.material:material" }
room-runtime = { module = "androidx.room:room-runtime", version.ref = "room" }
room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
hilt = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroid" }
hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hiltAndroid" }
hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "navigationCompose" }

And inside the build.gradle (Project file):

plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.kotlin.android) apply false
    alias(libs.plugins.compose.compiler) apply false
    alias(libs.plugins.devtools.ksp) apply false
    alias(libs.plugins.hilt.android) apply false
}

And inside the build.gradle (Module file):

dependencies {
    //Compose
    implementation(platform(libs.compose.bom))
    implementation(libs.compose.material)
    //Room
    implementation(libs.room.runtime)
    implementation(libs.room.ktx)
    ksp(libs.room.compiler)
    //Hilt
    implementation(libs.hilt)
    ksp(libs.hilt.compiler)
    //Hilt Navigation Compose
    implementation(libs.hilt.navigation.compose)
}

Make also sure to use:

compileOptions {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
    jvmTarget = JavaVersion.VERSION_17.toString()
}

And add the plugins at the beginning of the file:

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.compose.compiler)
    alias(libs.plugins.devtools.ksp)
    alias(libs.plugins.hilt.android)
}

Comments

1

If you got a similar problem with kapt instead of ksp, then copy this in your gradle kts file after android{} block

tasks.withType(type = KaptGenerateStubsTask::class) {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()

}

Comments

1

Try to add this to .toml:

[versions]
kotlin_jvm = "2.1.20-RC"
// ...

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin_jvm" }

and add this to project .gradle file:

plugins {
    // ...
    alias(libs.plugins.kotlin.jvm) apply false
}

Comments

0

I've used, It works

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(8))
    }
}

kotlin {
    jvmToolchain(8)
}

Comments

0

Solution that worked for me:

in your project level build.gradle add this at the end:

subprojects {
    afterEvaluate{
        tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
            if (project.plugins.hasPlugin("com.android.application") || project.plugins.hasPlugin("com.android.library")) {
                kotlinOptions.jvmTarget = android.compileOptions.sourceCompatibility
            } else {
                kotlinOptions.jvmTarget = sourceCompatibility
            }
        }
    }
}

This is error is coming after applying Android Studio Flamingo patch 2. But the above workaround worked for me.

Comments

0

I was able to resolve this issue using the Android Studio settings recommendation on the Android Developer site to "Set the JDK Version" https://developer.android.com/studio/intro/studio-config#jdk

For whatever reason, I had Gradle JDK option set to 18. After selecting Embedded JDK from the options, I was able to build the project. I hope this helps someone.

From the link above:

A copy of the latest OpenJDK comes bundled with current versions Android Studio, and this is the JDK version we recommend you use for your Android projects. To use the bundled JDK, do the following:

  1. Open your project in Android Studio and select File > Settings (on macOS, Android Studio > Preferences).
  2. In the left pane, navigate to Build, Execution, Deployment > Build Tools > Gradle.
  3. Under Gradle JDK, choose the Embedded JDK option.
  4. Click OK.

Comments

0

your current kotlin version is not compatible so update org.jetbrains.kotlin.android plugin version in porject-level build.gradle

1 Comment

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.

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.