如何在Gradle项目中将Kotlin的字节码版本设置为Java 8? [英] How to set up Kotlin's byte code version in Gradle project to Java 8?

查看:474
本文介绍了如何在Gradle项目中将Kotlin的字节码版本设置为Java 8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Kotlin项目中,什么是适当的Gradle脚本来确保我的类将被编译为字节代码ver 52(Java 8)?



由于某种原因,我的尽管我设置了源和目标兼容性,但类仍被编译为版本50(Java 6)。至少这是Idea在构建项目之后从目录 build / classes /...打开文件时显示的内容。



$ p

$ p $ buildscript {
ext {
kotlinVersion = '1.0.5-2'
springBootVersion ='1.4.2.RELEASE'
}
知识库{mavenCentral()}
依赖关系{
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
}

##我也试过这个,它没有帮助
#sourceCompatibility = 1.8
#targetCompatibility = 1.8

存储库{ mavenCentral()}

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

dependencyManagement {imports {mavenBomorg.springframework.cloud:spring-cloud-dependencies:Camden.SR2}}


解决方案

正如Mark在Debop的答案中指出的那样, compileKotlin compileTestKotlin 。你可以这样做而不用重复:

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

对于纯Kotlin项目,我不认为选项 sourceCompatibility targetCompatibility 做任何事情,所以你可以将它们移除。



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


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" } }

解决方案

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

这篇关于如何在Gradle项目中将Kotlin的字节码版本设置为Java 8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆