4

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?

shadowsheep
  • 10,376
  • 3
  • 44
  • 65
Daan
  • 1,356
  • 10
  • 17

1 Answers1

5

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.

Joffrey
  • 13,071
  • 1
  • 42
  • 68
  • This works for the Kotlin JS (and probably Java and common) projects but not the multiplatform projects. Thanks for the first part of the solution though! – Daan Oct 04 '20 at 11:22
  • This is most likely because my own multiplatform subprojects only support JS and JVM targets at the moment. For native there might be other compile tasks. I would hope for a more general approach though, this is kind of hacky. – Joffrey Oct 04 '20 at 11:51
  • 1
    This simply doesn't work for multiplatform projects with `languageSettings.useExperimentalAnnotation` (my own projects only have common and js code atm) – Daan Oct 05 '20 at 14:08
  • 1
    There is a common supertype for all these tasks. Maybe this will do the thing: `tasks.withType>().configureEach { kotlinOptions.freeCompilerArgs += compilerArgs }`? – Михаил Нафталь Oct 05 '20 at 15:39