What would be the best way with kotlin script grad...
# gradle
s
What would be the best way with kotlin script gradle to add the following to every gradle module with the multiplatform plugin applied. I haven't found a way to make the compiler happy using the
subprojects
block. Does anyone have any suggestions?
Copy code
afterEvaluate {
    kotlin.sourceSets.map {
        it.apply {
            languageSettings.apply {
                useExperimentalAnnotation("kotlin.ExperimentalStdlibApi")
                useExperimentalAnnotation("kotlinx.coroutines.FlowPreview")
                useExperimentalAnnotation("kotlinx.coroutines.ExperimentalCoroutinesApi")
            }
        }
    }
}
a
You could use
plugins.withId()
to execute only for modules with multiplatform applied. Something like this.
Copy code
subprojects {
    pluginManager.withId(<id here>) {
       // code here
    }
}
s
I can't get it to work. I need access to the underlying
project
object that it's applied too so that I can use
afterEvaluate
and the
kotlin
block.
a
subprojects implicitly has lambda with receiver on the project instance so you can access it.
s
You are right. After some fiddling I got it to work, but I had to define the kotlin extension myself... It's pretty ugly. Any thoughts on a solution?
Copy code
plugins.withType(KotlinMultiplatformPlugin::class) {
        afterEvaluate {
            val kotlin = (this as ExtensionAware).extensions.getByName("kotlin") as org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
            kotlin.sourceSets.map {
                it.apply {
                    languageSettings.apply {
                        useExperimentalAnnotation("kotlin.ExperimentalStdlibApi")
                        useExperimentalAnnotation("kotlinx.coroutines.FlowPreview")
                        useExperimentalAnnotation("kotlinx.coroutines.ExperimentalCoroutinesApi")
                    }
                }
            }
        }
    }
a
You can replace
Copy code
val kotlin = (this as ExtensionAware).extensions.getByName("kotlin") as org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
with
Copy code
val kotlin = the<KotlinJvmProjectExtension>
or
Copy code
configure<KotlinJvmProjectExtension> {
   sourceSets.map { }
}
m
Copy code
plugins.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMultiplatformPlugin> {
    afterEvaluate {
        configure<org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension> {
            sourceSets.configureEach  {
                languageSettings.apply {
                    useExperimentalAnnotation("kotlin.ExperimentalStdlibApi")
                    useExperimentalAnnotation("kotlinx.coroutines.FlowPreview")
                    useExperimentalAnnotation("kotlinx.coroutines.ExperimentalCoroutinesApi")
                }
            }
        }
    }
}
s
Thanks Mike. This is working
And thanks again Arunkumar