How to set up Kotlin's byte code version in Gradle project to Java 8?

前端 未结 9 1779
挽巷
挽巷 2020-12-01 08:56

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 v

相关标签:
9条回答
  • 2020-12-01 09:37

    In your build.gradle.kts:

        tasks {
            val java = "11"
    
            compileKotlin { 
               kotlinOptions { jvmTarget = java }
               sourceCompatibility = java
            }
        }
    

    Or like this:

    tasks {
        withType<KotlinCompile> { kotlinOptions { jvmTarget = "11" } }
    }
    
    0 讨论(0)
  • 2020-12-01 09:46

    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.

    0 讨论(0)
  • 2020-12-01 09:46

    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"
        }
    }
    
    0 讨论(0)
提交回复
热议问题