How to configure JVM 9 on Kotlin using Gradle?

Targeting JVM 1.8 on Kotlin with Gradle is as easy as

compileKotlin { kotlinOptions { jvmTarget = "1.8" } } 

But this does not work for Java 9 if I just change jvmTarget to 9 or 1.9 . How can i do this?

+9
source share
2 answers

Kotlin is currently for Java 6 and 8 only.

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

Who is currently talking

Kotlin only targets Java 6? No. Kotlin allows you to choose between bytecode generation compatible with Java 6 and Java 8. For higher versions of the platform, a more optimal bytecode can be generated.


Editing:

So ... since the compatibility of the bytecode that kotlin generates does not mean that the version of Java that you need to use is fixed.

Here's a gradle file that allows you to use Java 11 with 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 cannot "target the JVM 9" with Kotlin. But you can write Kotlin along with Java (9 | 10 | 11 | etc.)

+7
source

See Kotlin's Help “Using Gradle” “JVM-Specific Attributes” for possible jvmTarget 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

0
source

Source: https://habr.com/ru/post/1273296/


All Articles