@DelicateCoroutinesApi annotation is a serious hassle

Our mobile app (Android Studio) uses GlobalScope to launch an app-wide constant connection to a socket server. Globally-scoped coroutines now require @DelicateCoroutinesApi annotation, with no way to disable or override. This wouldn’t be a problem if the class where the coroutine was declared was the only class we had to annotate. The problem occurs because Android Studio ALSO flags every dependent class with the delicate warning. It also flags every dependent class of every dependent class, etc. to infinity.

This is a serious pain for our app. Our App class references the socket connection class. And EVERY other class in our application (hundreds) reference the App class.

Can it really be true that I’m now required to open and annotate HUNDREDS of classes just to deal with the ONE class that actually defines the globally scoped coroutine?

You can use @OptIn(DelicateCoroutinesApi::class) instead of a @DelicateCoroutinesApi and then it won’t propagate.

1 Like

Thanks for the info. But of course now Android Studio has just replaced one warning with another:

This class can only be used with the compiler argument '-Xopt-in=kotlin.RequiresOptIn'.

Usage of this compiler argument is explicitly listed by Kotlin team as experimental, So even if it works as expected, it’s brittle.

And where do even I specify this option? I can find nowhere in Android Studio to set compiler options, having never had to set them before for any other project or feature or module or anything else.

The example from the Kotlin website shows a syntax I’ve never seen, and I can find nowhere to ‘put’ it:

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
    kotlinOptions.freeCompilerArgs += "-opt-in=org.mylibrary.OptInAnnotation"
}

Help!

You can annotate only the class that uses GlobalScope. As you said, this wouldn’t be a problem if the class where the coroutine was declared was the only class we had to annotate.

You can read more about how to use the opt-in annotations in Opt-in requirements | Kotlin Documentation. This feature is going to become stable quite soon, tentatively in Kotlin 1.7.

2 Likes

That should be placed in your build.gradle.kts

@broot is correct that using the following annotation above the class that uses GlobalScope:

@OptIn(DelicateCoroutinesApi::class)

instead of Android Studio’s recommended approach:

 @DelicateCoroutinesApi

prevents the error message from being propagated to every dependent class.

And after digging around forever, I found the correct place in build.gradle to set the compiler option:

compileOptions {
        ...
        kotlin {
            kotlinOptions {
                freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
            }
        }
    }

I appreciate that Kotlin is striving to help developers write better code, but there ABSOLUTELY should be a MUCH easier way to dismiss the “Delicate Coroutines” warning, other than being forced to not only annotate, but research and find the very specific compiler option that enables the annotation in the first place.

Maybe it depends on the version of IntelliJ/Android Studio, but don’t you see automatic suggestions for adding @OptIn in your IDE?

Screenshot from 2022-01-04 07-46-51

Regarding the compiler argument, there is probably some room for improvement, IDE could try to add it automatically. Unfortunately, that room is pretty small. Gradle is very advanced tool and IDE can’t always know how to modify its build scripts. I personally never do this, I always modify these files manually.

I guess it’s hard to work with Gradle projects without some knowledge about Gradle. And if you know Gradle then adding RequiresOptIn argument is really a 5 minute search in Google (been there, done that).

2 Likes

AND you won’t need the compiler option for RequiresOptIn when Opt-in annotations become stable in (hopefully) 1.7