How to target JVM 9 on Kotlin with Gradle?

See the Kotlin "Using Gradle" reference "Attributes specific for the JVM" for possible values of the attribute jvmTarget:

Name: jvmTarget

Description: Target version of the generated JVM bytecode (1.6, 1.8, 9, 10, 11 or 12), default is 1.6

Possible values: "1.6", "1.8", "9", "10", "11", "12"

Default value: "1.6"

Copied 2019/10/01


Kotlin currently only targets Java 6 and 8

See the FAQs here https://kotlinlang.org/docs/reference/faq.html#does-kotlin-only-target-java-6

Which at the moment says

Does Kotlin only target Java 6? No. Kotlin lets you choose between generating Java 6 and Java 8 compatible bytecode. More optimal byte code may be generated for higher versions of the platform.


edit:

So... because it's the compatibility of the bytecode that kotlin is generating it doesn't mean that fixes the Java version you need to use.

Here's a gradle file that let's you use Java 11 alongside kotlin generating Java 8 compatible bytecode

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.2.71'
}

group 'com.dambra.paul.string-calculator'
version '0.0.0'

sourceCompatibility = 11.0

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(
            'org.junit.jupiter:junit-jupiter-api:5.1.0'
    )
    testRuntimeOnly(
            'org.junit.jupiter:junit-jupiter-engine:5.1.0'
    )
    testCompile("org.assertj:assertj-core:3.11.1")
    testCompile 'org.junit.jupiter:junit-jupiter-params:5.1.0'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

You can't "target jvm 9" with Kotlin. But you can write Kotlin alongside Java (9|10|11|etc)

Tags:

Gradle

Kotlin