in a KMM project, how to set java17 on the shared ...
# multiplatform
b
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
Add
jvmToolchain(17)
b
in which block
j
Sorry, was adding that 😬 It goes in the
kotlin {}
block.
b
you mean having something like
Copy code
kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "17"
            }
        }
    }
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(17))
    }
}
j
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
basically having
Copy code
android {
    compileOptions {
        sourceCompatibility(JavaVersion.VERSION_17)
        targetCompatibility(JavaVersion.VERSION_17)
    }
}

kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "17"
            }
        }
    }
}
j
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
thank you guys for your support
e
@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
I suppose you could detect the JDK version invoking Gradle and conditionally set a toolchain if it was too low
e
no reason to use anything but the latest locally
What about on CI?
j
The same. Why would you use an old version? You're deliberately using something slower and with fewer features.
e
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
just change all 17 to 1.8and it should work fine while changing to 17 i also faced the same issue
m
What is currently the highest version which is still compatible with Android?
j
I build everything with JDK 20
m
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
my jvm target is usually set to 8 or 11
m
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.
569 Views