Is there a way to only allow experimental coroutin...
# coroutines
r
Is there a way to only allow experimental coroutines for tests? My gradle-fu is not exemplary.
Copy code
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        kotlinOptions.freeCompilerArgs += ["-Xuse-experimental=kotlin.Experimental"]
    }
}
l
You mean experimental coroutines api? You need to filter the tasks that compile tests. The Gradle tool window in the IDE might be helpful. Also, note that you're activating the Experimental feature of Kotlin in this snippet, not ExperimentalCoroutinesApi.
👍 1
r
so the warning I’m seeing is
This declaration is experimental and its usages should be marked with @ExperimentalCoroutinesApi or @UseExperimental(ExperimentalCoroutinesApi::class)
so I marked the class with the @UseExperimental annotation but then it warns me
This class can only be used with the compiler argument -Xuse-experimental=kotlin.Experimental
. Does that differ from what you just said? I’m also unsure on what operators to use to filter on test tasks only.
l
You can use their name to filter the ones for tests.
@UseExperimental
is experimental, so you need to opt-in to
kotlin.Experimental
, but an alternative is to opt-in to
kotlinx.coroutines.ExperimentalCoroutinesApi
and not need to add
@UseExperimental
annotation to its usages. I tend to use
@UseExperimental
so I know exactly where they are in case I want or need to change them while them not being deprecated, but I could as well opt-in for all.
m
using kotlinOptions is the recommemded one. As stated above, because when you annotating the
@EXperimentalApi
you'll refactor your code to the outside layer as well (the code that implemented it) and it's ugly imho
And for your question, maybe you can wrap the
task.withType
inside the if condition if the source directory is under
test
or
androidTest
. But that would be my suggestion, I didn't tried that one as well. *nice question