shadox
shadox

Reputation: 3779

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

Upvotes: 21

Views: 16392

Answers (10)

ashish kumar
ashish kumar

Reputation: 1

I was getting this error after upgrading my project to Ionic Capacitor 7 and also upgrading some plugins. Adding the following line to my android->build.gradle file resolved the issue:

dependencies {
    classpath 'com.android.tools.build:gradle:8.7.2'
    classpath 'com.google.gms:google-services:4.4.2'
  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    //Removed this line ---- classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.20'
 // Added the following line for kotlin-gradle-plugin version
  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21"
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

Upvotes: 0

Manisha Saini
Manisha Saini

Reputation: 61

Changing these settings worked for me

From this >> 

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


To this>> 

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

Gradle JDK : coretto-17

Upvotes: 0

user29543152
user29543152

Reputation: 1

Android Studio Ladybug Feature Drop | 2024.2.2

Changing kotlin version from 1.8.0 to 2.0.0 saves my life

Upvotes: 0

kufo
kufo

Reputation: 1

I solved my case for this error.

Step 1: I changed environments:

android-studio-2024.2.2.13-windows
Flutter 83.0.3

<SDK manager>
Android SDK :  API Level 35 and 24 installed
Kotlin Compiler: Target JVM version -> 21

<Project Structure>
Project Settings-Project-SDK(21 Oracle OpenJDK 21.0.6)
Platform Settings-SDKs : 21(jdk-21 installed) - windows system setting also(JAVA_HOME, Path)

Step 2:

<gradle-wrapper.properties>
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-all.zip

<settings.gradle>
plugis{
   id "com.android.application" version "8.7.1" apply false
   id "org.jetbrains.kotlin.android" version "2.0.20" apply false
}

<app\build.gradle>
ndkVersion = "27.0.12077973"
compileOptions {
    sourceCompatibility = JavaVersion.VERSION_21
   targetCompatibility = JavaVersion.VERSION_21
}

kotlinOptions {
        jvmTarget = JavaVersion.VERSION_21
}

defaultConfig {
   minSdk = 24 //flutter.minSdkVersion  
}

Upvotes: 0

Azhar Ali
Azhar Ali

Reputation: 2506

After struggling for many hours, my issue is fixed here is a solution (worked for my two projects)

app\build.gradle:

android {
  ndkVersion "25.1.8937393"

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

This in settings.gradle:

id "com.android.application" version "8.3.2" apply false
id "org.jetbrains.kotlin.android" version "2.0.20" apply false

And this in gradle-wrapper.properties:

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

How I find this solution, My logs after the Android Studio update (Beta version)

Android Studio Meerkat | 2024.3.1 Nightly 2025-01-14

App logs

Github link - https://github.com/flutter/flutter/issues/156304

Credit Go to @deakjahan here is his solution - https://github.com/flutter/flutter/issues/156304#issuecomment-2397707812

I hope it will work for others as well

Upvotes: 2

Leonardo Souza
Leonardo Souza

Reputation: 1

After hours on it, I could get everything working as:

  • Removing Android Studio 2024.2.
  • Installing Android Studio 2024.1.

Note: Don't forget to restart your machine, as environments vars and editors may keep old values.

Upvotes: -1

JulianSymes
JulianSymes

Reputation: 2215

It seems to be a misleading error message. The problem is that the kapt processing doesn't recognise JVM 21. The answer for me was to declare the android kotlin gradle plugin version explicitly in the project build.gradle, thus:

buildscript {
     repositories {
        mavenCentral()
        // ...

    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21"
        // ...

Upvotes: 6

shadox
shadox

Reputation: 3779

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"

Upvotes: 16

Evilripper
Evilripper

Reputation: 1185

If you're working on an Ionic app with Capacitor and encounter issues related to text-to-image => Unknown Kotlin JVM target: 21, ensure that the Kotlin JVM target and Java versions are consistent.

In my case, I needed to explicitly set the jvmTarget to "17" in the kotlinOptions section of the build.gradle file. Here’s the configuration that worked for me:

in main build.gradle added this dependency

 buildscript {
    repositories {
       mavenCentral()
       // ...

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

and in the gradle of the plugin text-to-image this:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17" // Ensure consistency with JavaVersion.VERSION_17
    }
}
  • The sourceCompatibility and targetCompatibility specify the Java version used for compiling and running the code.
  • The jvmTarget in kotlinOptions must match the Java version specified above; otherwise, you might encounter runtime errors or compatibility issues.

This setup ensures that the Kotlin and Java configurations align, resolving potential issues when using Java 17 features or APIs in your project.

Upvotes: 1

Vikash Kumar Tiwari
Vikash Kumar Tiwari

Reputation: 835

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.

Upvotes: 9

Related Questions