How to set up Kotlin's byte code version in Gr

2019-01-14 20:51发布

问题:

In Kotlin project, what is a proper Gradle script to make sure my classes will be compiled to byte code ver 52 (Java 8)?

For some reason my classes are compiled as ver 50 (Java 6) even though I set up source and target compatibility. At least this is what Idea shows me when I open file from directory build/classes/... after building the project.

My current set up looks like this.

buildscript {
    ext {
        kotlinVersion = '1.0.5-2'
        springBootVersion = '1.4.2.RELEASE'
    }
    repositories { mavenCentral() }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
    }
}

apply plugin: 'kotlin'
apply plugin: 'org.springframework.boot'

tasks.withType(JavaCompile) {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

## I also tried this and it hasn't helped
#sourceCompatibility = 1.8
#targetCompatibility = 1.8

repositories { mavenCentral() }

dependencies {
    compile("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
    compile('org.springframework.cloud:spring-cloud-starter-stream-rabbit')
}

dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR2" } }

回答1:

As Mark pointed out on Debop's answer, you have to configure both compileKotlin and compileTestKotlin. You can do it without duplication this way:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
  kotlinOptions {
    jvmTarget = "1.8"
  }
}

For a pure Kotlin project, I don't think the options sourceCompatibility and targetCompatibility do anything, so you may be able to remove them.

Ref: https://kotlinlang.org/docs/reference/using-gradle.html#compiler-options



回答2:

Kotlin 1.1 in Gradle. in console you have error about inline (your installed compiler is 1.0.x) If run gradle task in IntelliJ IDEA, your code compiled by kotlin 1.1 compiler

compileKotlin {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8

    kotlinOptions {
        jvmTarget = "1.8"
        apiVersion = "1.1"
        languageVersion = "1.1"
    }
}


回答3:

In case someone uses gradle with kotlin-dsl instead of groovy:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

// compile bytecode to java 8 (default is java 6)
tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
}


回答4:

Kotlin 1.0 always produces Java 6 class files. Kotlin 1.1 will support generating Java 8 class files by passing -jvm-target 1.8 to the compiler. See

https://blog.jetbrains.com/kotlin/2016/07/first-glimpse-of-kotlin-1-1-coroutines-type-aliases-and-more/

for a discussion of Java 7/8 support.



回答5:

For anyone like me who use maven to build mixed code of kotlin and java in intellij , you need to set

<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>

because maven build doesn't respect jvmTarget set in project setting.



回答6:

thx for seanf's anwser ,but if your DSL kotlin is 1.3.0:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java).all {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}


标签: gradle kotlin