https://kotlinlang.org logo
b

brabo-hi

07/19/2023, 6:03 PM
in a KMM project, how to set java17 on the shared module? i have the following
Copy code
kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "17"
            }
        }
    }
i am getting the following error
'compileDebugJavaWithJavac' task (current target is 1.8) and 'compileDebugKotlinAndroid' task (current target is 17) jvm target compatibility should be set to the same Java version.
j

Jacob Ras

07/19/2023, 6:05 PM
Add
jvmToolchain(17)
b

brabo-hi

07/19/2023, 6:06 PM
in which block
j

Jacob Ras

07/19/2023, 6:07 PM
Sorry, was adding that 😬 It goes in the
kotlin {}
block.
b

brabo-hi

07/19/2023, 6:08 PM
you mean having something like
Copy code
kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "17"
            }
        }
    }
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(17))
    }
}
j

jw

07/19/2023, 6:09 PM
A toolchain declaration will force the presence of JDK 17 rather than allowing any JDK 17 or newer to build your project. The Java compiler is designed to cross-compiler older releases. I would not recommend forcing old JDKs as there really should be no reason to use anything but the latest locally (or maybe N-1 while waiting for Gradle support). You can set the Java compiler compatibility with
Copy code
android {
  compileOptions {
      sourceCompatibility(JavaVersion.VERSION_17)
      targetCompatibility(JavaVersion.VERSION_17)
💯 2
b

brabo-hi

07/19/2023, 6:13 PM
basically having
Copy code
android {
    compileOptions {
        sourceCompatibility(JavaVersion.VERSION_17)
        targetCompatibility(JavaVersion.VERSION_17)
    }
}

kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "17"
            }
        }
    }
}
j

Jacob Ras

07/19/2023, 6:15 PM
I checked, in my projects I have:
Copy code
android {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
I was very sure I was using the toolchain, forgot I later updated it; removing it in favour of what Jake said 🙂
b

brabo-hi

07/19/2023, 6:26 PM
thank you guys for your support
e

eygraber

07/19/2023, 6:49 PM
@jw is there a way to enforce a minimum JDK version for building the project other than toolchains? Another nice thing about them is provisioning, which is helpful when onboarding iOS devs
j

jw

07/19/2023, 6:51 PM
I suppose you could detect the JDK version invoking Gradle and conditionally set a toolchain if it was too low
e

eygraber

07/20/2023, 2:43 AM
no reason to use anything but the latest locally
What about on CI?
j

jw

07/20/2023, 2:45 AM
The same. Why would you use an old version? You're deliberately using something slower and with fewer features.
e

eygraber

07/20/2023, 3:23 AM
So for example in SqlDelight is there a technical reason the toolchain is set to 17, or is that something that can be configured with what you said above (conditionally set a toolchain if the version invoking Gradle is too low)?
s

SanjayKarki

07/20/2023, 5:15 AM
just change all 17 to 1.8and it should work fine while changing to 17 i also faced the same issue
m

Michael Paus

07/20/2023, 7:10 AM
What is currently the highest version which is still compatible with Android?
j

jw

07/20/2023, 10:33 AM
I build everything with JDK 20
m

Michael Paus

07/20/2023, 2:04 PM
How do you do that? I only get
Copy code
Execution failed for task ':shared:compileKotlinDesktop'.
> Error while evaluating property 'compilerOptions.jvmTarget' of task ':shared:compileKotlinDesktop'.
   > Failed to calculate the value of property 'jvmTarget'.
      > Unknown Kotlin JVM target: 20
when I try.
j

jw

07/20/2023, 2:04 PM
my jvm target is usually set to 8 or 11
m

Michael Paus

07/20/2023, 2:22 PM
Ahhh, I interpreted “build everything with JDK 20” as meaning really everything, i.e,, including the jvm target. At least I can confirm now that “everything” works with JDK 17, even on Android.
33 Views