<@U74HWEPTP> Thanks! I meant, I already specify Ja...
# announcements
k
@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
java block is for java code, Kotlin is for Kotlin class files
k
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.
Copy code
kotlin {
  jvmTarget = 1.8
}
g
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
So, should my build.gradle.kts have something like that:
Copy code
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:
Copy code
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
Exactly, so withType approach is the best what you have
k
gotcha. thank you!