Hi, I tried to add languageSettings to android sou...
# multiplatform
l
Hi, I tried to add languageSettings to android sourceSets for an experimental annotation, unccessfully, following the example there: https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#language-settings I tried the following:
Copy code
kotlin {
    sourceSets {
        filter { it.name.startsWith("android") }.forEach {
            it.languageSettings.apply {
                useExperimentalAnnotation("kotlin.Experimental")
            }
        }
    }
}
but I'm getting an error because the test sourceSets defined by the Android Gradle plugin (version 3.3.1) don't have the experimental annotation. How can I solve that issue to enable this experimental annotation in my android source sets defined my Kotlin multiplatform gradle plugin? The exact error message is the following:
Copy code
Inconsistent settings for Kotlin source sets: 'androidDebugAndroidTest' depends on 'androidAndroidTest'
'androidDebugAndroidTest': set of experimental annotations in use is []
'androidAndroidTest': set of experimental annotations in use is [kotlin.Experimental]
The dependent source set must use all experimental annotations that its dependency uses.
h
I believe some of the source sets are not yet created during the project evaluation phase, so your code doesn't add the experimental annotation to them.
As Android variants are created in
afterEvaluate
, some of the Kotlin MPP model parts are also only available late in project configuration.
I suppose this should help: replace
filter { ... }.forEach { ... }
with
matching { ... }.all { ... }
Copy code
kotlin {
    sourceSets {
        matching { it.name.startsWith("android") }.all {
            it.languageSettings.apply {
                 useExperimentalAnnotation("kotlin.Experimental")
            }
        }
    }
}
matching
+
all
will apply the lambda to the items that are not created yet, once they are created.
l
I just had to remove the
it.
in the lambda because the
Action
lambda passed to
all { ... }
is a receiver. It now works perfectly, thanks for your help!