https://kotlinlang.org logo
#random
Title
# random
g

Gilles Barbier

11/04/2023, 5:56 AM
Hi, I’m using Kotlin to publish a Java lib (https://infinitic.io). Currently I’m targeting Java 11, but some users request to use Java 17. I’m wondering what are the best practices here? Eg. Should I publish the lib under different Java version?
h

hfhbd

11/04/2023, 6:06 AM
What’s the problem with targeting Java 11 and consume it with Java 17, JavaEE? If so, I would either try to split the lib into core, java11 and java17, either with 3 gradle modules or if you prefer Gradle variants, one module with 2 variants.
g

Gilles Barbier

11/04/2023, 8:49 AM
There is no problem for targeting 11 and consume with 17, but some users think that targeting 17 will improve performance.
h

hfhbd

11/04/2023, 8:53 AM
I don’t think the target level has a real impact. But you can also use eg Java 21 to compile your code but target 11. This allows you to use some improvements of 21 compiler but you only need one code base.
1
g

Gilles Barbier

11/04/2023, 8:54 AM
I use Kotlin, can I specify Java compiler version? It seems an obvious question, but I’ve never looked at those subjects in details
h

hfhbd

11/04/2023, 9:02 AM
Yes:
Copy code
kotlin {
    jvmToolchain(21)
    compilerOptions {
        jvmTarget.set(JvmTarget.JVM_11)
    }
}
Copy code
java {
    targetCompatibility = JavaVersion.VERSION_11
}
👍 1
gratitude thank you 1
e

Edoardo Luppi

11/06/2023, 2:47 PM
The bytecode will be the same anyway. The JVM will consume it with better optimization in the latest releases but that's it. I don't recall any bytecode enhancements from the Kotlin compiler past Java 11.