Targeting JVM 1.8 on Kotlin with Gradle is as easy as

compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

But that doesn't work for Java 9 if I simply change the jvmTarget to 9 or 1.9. How can I do it?

推荐答案

Kotlin目前只针对Java6和Java8

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

现在说

Kotlin是否只针对Java 6?


编辑:

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)

Kotlin相关问答推荐

外键是主键的一部分,但不是索引的一部分.房间

修改器的属性是什么,我需要更改以使角变圆且宽度更小?喷气背包组合

Jetpack Compose:当状态从另一个活动改变时强制重组

关于 Kotlin 函数类型转换的问题

如何使用 Hilt 注入应用程序:ViewModel 中的上下文?

OnClickListener 未在 ConstraintLayout 上触发

为什么 android studio 不为所有安全参数生成代码?

Kotlin 中的 Java Scanner 相当于什么?

为什么我在使用 Jetpack Compose clickable-Modifier 时收到后端内部错误:Exception during IR lowering error?

Kotlin 有 array.indexOf 但我无法弄清楚如何做 array.indexOfBy { lambda }

如何在Spring Boot应用程序上启用承载身份验证?

禁用 IntelliJ kotlin * 导入?

ObjectAnimator.ofFloat 不能直接在kotlin中取Int作为参数

Gradle:无法连接到 Windows 上的 Kotlin 守护程序

如果kotlin已经有了getter和setter,为什么在数据类中有componentN函数?

将字符串转换为HashMap的最简单方法

如何在kotlin用mockito模仿lambda

你如何在 Kotlin 中注释 Pair 参数?

如何在 Kotlin 中定义新的运算符?

Kotlin:访问 when 语句的参数