Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Ladybug - Unknown Kotlin JVM target: 21

As the title says, I've installed new Android Studio but can't compile my project anymore. After failing in kapt I have migrated everything to ksp but still no luck. I do have following configurations from day 1:

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

kotlinOptions {
    jvmTarget = "17"
}

After looking everywhere and trying multiple solutions - nothing works.

I have tried this but it DOES NOT work: enter image description here

like image 744
shadox Avatar asked Sep 10 '25 18:09

shadox


2 Answers

After digging for a solution I found this one working for me. Might be useful for you too.

I. For kapt:

  1. Select Gradle JDK jbr-17

enter image description here

  1. Add following configurations to your app level build.gradle.kts

android{
//...
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
    kotlin {
        jvmToolchain(17)
    }
//...
}

I'm guessing that's because both JDKs are under the same folder AS and gradle needs to know what version of compiler/toolchain to pick. Apparently compileOptions and kotlinOptions aren't obvious enough.

II. For ksp

Update to id("com.google.devtools.ksp") version "2.0.0-1.0.22" and kotlin id("org.jetbrains.kotlin.android") version "2.0.0". Should work without specifying jvmToolchain(17).

Also make sure that: id("com.android.application") version "8.7.1"

like image 72
shadox Avatar answered Sep 12 '25 10:09

shadox


First of all Unknown Kotlin JVM target: 21 itself says that current set target jvm is 21.

Reason: After updating android studio sometimes sets default jvm target automatically without informing / prompting and that causes the confusion.

Steps

  1. Go to settings in android studio and select Gradle under Build, Execution, Deployment Menu as it is visible in screenshot below.

enter image description here

  1. Change default jvm target to JetBrains JDK 17.0.12

enter image description here

  1. Delete .gradle generated directory / folder from the project explorer in android studio.

enter image description here

  1. Optionally gradle command can also be used ./gradlew clean build

  2. Run sync project with gradle

  3. Rebuild the project from Build -> Rebuild project

It starts working.

like image 36
Vikash Kumar Tiwari Avatar answered Sep 12 '25 09:09

Vikash Kumar Tiwari