Is it possible to apply specific Kotlin compiler a...
# gradle
t
Is it possible to apply specific Kotlin compiler arguments for an Android build variant ? I can configure arguments with
Copy code
tasks.withType<KotlinCompile>().configureEach {
  kotlinOptions {
    freeCompilerArgs = freeCompilerArgs + listOf(
      "-Xno-param-assertions",
      "-Xno-call-assertions",
      "-Xno-receiver-assertions"
    )
  }
}
but it's applied to all build variants, which is not what I want. The
kotlin.android
Gradle plugin creates compilation tasks (
compileDebugKotlin
,
compileReleaseUnitTestKotlin
, etc) for each source set, but it seems there is no way to configure their
kotlinOptions
individually.
p
You can probably do a matching and filter by task name?
t
It works, but only from within
afterEvaluate
. It's because compile*Kotlin tasks are created lazily based on the created variants. Because relying on
afterEvaluate
is flagged as a bad practice by Gradle, I'd expect the kotlin Android plugin to define an alternative configuration DSL. Maybe it hasn't leveraged the new extension DSL introduced in Android gradle Plugin v7.0.0 ?
v
I have no idea about android. But matching by name should not require
afterEvaluate
or you are doing something wrong. Paul did probably not mean to use
tasks.named("compileDebugKotlin")
where that might be true, but more something like
Copy code
tasks.withType<KotlinCompile>().matching { it.name == "compileDebugKotlin" }.configureEach {
  kotlinOptions {
    freeCompilerArgs = freeCompilerArgs + listOf(
      "-Xno-param-assertions",
      "-Xno-call-assertions",
      "-Xno-receiver-assertions"
    )
  }
}