g
image.png
c
same as you would without kotlin: add java plugin and set its target and source versions to 13
g
With only what I have here I can run Java 8 alongside Kotlin, so there is no modifaction to only the kotlin jvm plugin that will allow it?
f
Kotlin will always produce JVM 8 bytecode but that's fine since any JVM can run older bytecode.
b
@Fleshgrinder, not entirely correct, as kotlin can be explicitly set to target java 6
f
True, forgot about the older versions. It can also produce JVM 7 bytecode. 😉
m
The Kotlin plugin brings in the Java plugin. So, you'd need to ensure that JDK13 is the one you're using for building, and possibly have to set source and target compatibility to 13 for the Java plugin (see Gradle docs). Kotlin options do support 11,12,13 numbers. Currently doesn't change any produced bytecode, but it should be compatible with JDK13 regardless.
g
Ok, so do I need to include the Java plugin to set source compatability to 13 for Java? Or can I do that with the Kotlin plugin somehow
Nevermind I think I've got it
I only need to specify source and target version for Java. So I can set the source and target to Java 13, then Kotlin will continue targeting Java 8. Ill give that a try
For those interested, this got it working:
f
Gradle, by default, uses the JDK you use do run it for the Java source and target compatibility. So if you use e.g. OpenJDK 13 to run Gradle your Java code will be Java 13. Kotlin uses 1.6 by default. I prefer to use http://www.jenv.be/ because there are always projects with a many different JDKs and this ensures that I don't need to think about it.
jenv local 13
and a file called
.java-version
is created containing the value
13.0
. Then I use the following snippet in my Gradle file:
Copy code
val javaVersion = file(".java-version").readText().trim().let { v -> v.substringBefore('.').takeIf { it != "1" } ?: v }

    tasks.withType<KotlinCompile> {
        kotlinOptions {
            allWarningsAsErrors = true
            freeCompilerArgs = mutableListOf(
                "-progressive",
                "-Xassertions=jvm",
                "-Xjvm-default=enable"
            )
            jvmTarget = javaVersion
        }
    }
Now my desired version is picked up automatically. To ensure that I don't have to call
jenv ./gradlew
I use https://gist.github.com/Fleshgrinder/b4411e49fb97f000d57b65755d49740c which allows me to use
gradle
from anywhere and it automatically picks up
jenv
and whether I need a
gradlew
, or not. I have the same for Maven https://gist.github.com/Fleshgrinder/4dea4a703e850fed44b6a6fe47876237 and here is a script to install it all on a Mac: https://gist.github.com/Fleshgrinder/dd19ccd6ab90621ab1c3fd5d9d11e26b