https://kotlinlang.org logo
Title
k

Krotick

06/07/2019, 3:22 PM
@Shawn Thanks! I meant, I already specify Java version (or so it seems) in` java` block, how can I not define it in
tasks
?
g

gildor

06/07/2019, 3:23 PM
java block is for java code, Kotlin is for Kotlin class files
k

Krotick

06/07/2019, 3:25 PM
Thank you! So, if I do not use Java directly, is it safe to remove java block completely? Do you happen to know if there is a similar directive for Kotlin, e.g.
kotlin {
  jvmTarget = 1.8
}
g

gildor

06/07/2019, 3:28 PM
Nope, it's not exposed as gradle extension, only as task, so have to configure Kotlin compiler tasks (by default there are 2 of them: compileKotlin and compileTestKotlin, but your snippet configure all Kotlin compile tasks applying config by task type
Also agree that it should be exposed as extension, always forget to report this proposal to issue tracker
k

Krotick

06/07/2019, 3:56 PM
So, should my build.gradle.kts have something like that:
val javaVersion = JavaVersion.VERSION_1_8.toString()
val kotlinVersion = KotlinVersion(1, 31).toString()

tasks.withType<KotlinCompile> {
  sourceCompatibility = javaVersion
  targetCompatibility = javaVersion

  kotlinOptions {
    jvmTarget = javaVersion
    apiVersion =kotlinVersion
    languageVersion = kotlinVersion
  }
}
Or I can do this but it seems extremely verbose:
val compileKotlin: KotlinCompile by tasks
val compileTestKotlin: KotlinCompile by tasks

compileKotlin.sourceCompatibility = javaVersion
compileKotlin.targetCompatibility = javaVersion
compileTestKotlin.sourceCompatibility = javaVersion
compileTestKotlin.targetCompatibility = javaVersion

compileKotlin.kotlinOptions {
  jvmTarget = javaVersion
  apiVersion = kotlinVersion
  languageVersion = kotlinVersion
}

compileTestKotlin.kotlinOptions {
  jvmTarget = javaVersion
  apiVersion = kotlinVersion
  languageVersion = kotlinVersion
}
g

gildor

06/07/2019, 4:10 PM
Exactly, so withType approach is the best what you have
k

Krotick

06/07/2019, 4:12 PM
gotcha. thank you!