23

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'
}
Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23

10 Answers10

17

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)
}
deive
  • 862
  • 7
  • 19
13

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"
        }
    }
}
p.matsinopoulos
  • 7,655
  • 6
  • 44
  • 92
Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23
8

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'
}
Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23
5
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"
    }
}
tanranran
  • 59
  • 1
  • 1
  • 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](/help/how-to-answer). – Community May 25 '23 at 11:12
  • 2
    this one worked for me, `KaptGenerateStubs` was also required – Aviran May 26 '23 at 21:09
  • It work for me. Thanks – Thân Hoàng Jul 09 '23 at 08:07
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.

yuroyami
  • 814
  • 6
  • 24
1

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()
    }
andrey2ag
  • 3,670
  • 1
  • 12
  • 12
0

I've used, It works

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

kotlin {
    jvmToolchain(8)
}
Đốc.tc
  • 530
  • 4
  • 12
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.

pravingaikwad07
  • 482
  • 1
  • 7
  • 24
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.
Heath Kornblum
  • 111
  • 1
  • 4
0

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

}
Binu Jasim
  • 297
  • 3
  • 9