Search code examples
kotlingradlekotlin-multiplatformkotlin-js

How to apply -Xopt-in=kotlin.ExperimentalUnsignedTypes to all subprojects?


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?


Solution

  • The recommended way to share configuration is via a convention plugin.

    I.e. create a file in buildSrc/src/main/kotlin/package/name/kotlin-mpp-conventions.kts with the contents:

    plugins {
        kotlin("jvm")
    }
    
    kotlin {
        sourceSets {
            all {
                languageSettings.useExperimentalAnnotation("kotlin.ExperimentalUnsignedTypes")
            }
        }
    }
    

    Then depend on this plugin from your Kotlin MPP subprojects by adding a reference to it in the plugins block: id("package.name.kotlin-mpp-conventions").

    Add more plugins for e.g. Kotlin JS projects. If there's some configuration you want to share between all types of projects you can create a common plugin that the other plugins depend on. You can also share data structures between plugins by simply putting them in a separate file and referencing them from the plugin files (like you would with normal code), I use this mechanism to share the list of Kotlin experimental annotations I want to allow in all plugins.

    Be sure to set up buildSrc/build.gradle.kts for plugins:

    plugins {
        `kotlin-dsl`
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21")
    }