I have a project with multiple subprojects that use the kotlin-multiplatform plugin or the kotlin-js plugin and I want to use the experimental unsigned types in all of them.
So far I've tried this, which doesn't work:
subprojects {
tasks.withType<KotlinCompile>().all {
kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlin.ExperimentalUnsignedTypes"
}
extensions.findByType<KotlinMultiplatformExtension>()?.sourceSets {
all {
languageSettings.useExperimentalAnnotation("kotlin.ExperimentalUnsignedTypes")
}
}
}
Is there a way to add the kotlin compiler arg -Xopt-in=kotlin.ExperimentalUnsignedTypes
to all subprojects in Gradle?
gradle for one project? Yes. You can have multiple build files in one project.
With multi-project builds this becomes possible because each subproject contains its own source code and configuration. Not only can you create a different jar file based on different source code, but you could just as easily setup a subproject to compile Kotlin, Scala, Groovy, or even C++.
Applying a plugin to a project allows the plugin to extend the project's capabilities. It can do things such as: Extend the Gradle model (e.g. add new DSL elements that can be configured) Configure the project according to conventions (e.g. add new tasks or configure sensible defaults)
I've reached this point with trial and error, so I'm not sure this is the right approach.
I had a multiproject build with some multiplatform, JVM, and JS subprojects, and I wanted to enable the kotlin.RequiresOptIn
annotation. So I ended up setting this compiler argument for all kinds of kotlin compilation tasks:
subprojects {
val compilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.freeCompilerArgs += compilerArgs
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile> {
kotlinOptions.freeCompilerArgs = compilerArgs
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompileCommon> {
kotlinOptions.freeCompilerArgs = compilerArgs
}
}
I guess the same approach could work for ExperimentalUnsignedTypes
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With