what's the difference between ```tasks.withType<Ko...
# gradle
k
what's the difference between
Copy code
tasks.withType<KotlinCompile> {
  kotlinOptions {
    freeCompilerArgs = listOf("-Xjsr305=strict", "-Xallow-any-scripts-in-source-roots")
  }
}
and
Copy code
kotlin {
  compilerOptions {
    freeCompilerArgs.add("-Xjsr305=strict")
    freeCompilerArgs.add("-Xallow-any-scripts-in-source-roots")
  }
}
or to be more precise why the latter one does not work ?:)
m
The first one seems to be deprecated now: https://kotlinlang.org/docs/compatibility-guide-18.html#changes-in-compiler-options
he
kotlinOptions
task input and the
kotlinOptions{...}
task DSL are in a support mode and will be deprecated in upcoming releases.
ah, I missed the question 🤦‍♂️ Does setting
compilerOptions
instead of
kotlinOptions
on the tasks directly work?
k
yes
-Xallow-any-scripts-in-source-roots
is respected in the first one
m
You can
println
what are tasks's
compilerOptions.freeCompilerArgs
in both scenarios and compare them 🙂 They both should work the same way. I can't play with your source code, but maybe you have custom kotlinCompile tasks that are not configured by KGP, maybe the later scenario has some extra arguments in the
freeCompilerArgs
already, which are overwritten in the first scenario (by
freeCompilerArgs = listOf(
)
k
thanks, you are right. in parent module I had
Copy code
tasks.withType<KotlinCompile> {
  kotlinOptions {
    freeCompilerArgs = listOf("-Xjsr305=strict")
  }
}
👌 1
t
Do you have a repro? Generally second approach should also work
btw first one replaces any previously configured
freeCompilerArgs
- better to use
+=
Overall better to not use
kotlinOptions
- they are semi-deprecated and we should add normal
@Deprecated
annotation in 2.0
k
If I may continue here. I have in
Copy code
tasks.withType<KotlinCompile> {
    kotlinOptions {
      freeCompilerArgs += "-Xjsr305=strict"
      jvmTarget = "17"
    }
  }
in my root build file in
allprojects
block. How can I use
kotlin
there? or how can I achieve globally set compile options that can be fine tuned in subprojects? right now this will apparently override anything I set in subprojects with
kotlin
even when using
+=
t
hm, something like this may work:
Copy code
plugins {
    id("org.jetbrains.kotlin.jvm")
}

allprojects {
    kotlin.compilerOptions {
        // common configuration for compiler args
   }
}
k
that unfortunately did not work, but this looks like it did
Copy code
tasks.withType<KotlinCompile> {
    compilerOptions {
      freeCompilerArgs.add("-Xjsr305=strict")
      jvmTarget.set(JvmTarget.JVM_17)
    }
  }