kotlinx.coroutines: Crash after upgrade from 1.3.0 to 1.3.1

Since I updated to coroutines 1.3.1 on Android projects it started to crash on all existing 3rd libraries that using coroutines too.

It is just minor bugfix version I would not expect backward incompatibility on such release.

But there is this small unimportant note:

Note: Kotlin/Native artifacts are now published with Gradle metadata format version 1.0, so you will need Gradle version 5.3 or later to use this version of kotlinx.coroutines in your Kotlin/Native project.

Can this be a reason?

My project is using Gradle 5.6.2, but the library is using an older version than 5.3

    java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android'
        at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:90)
        at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:71)
        at kotlinx.coroutines.DispatchedKt.resumeCancellable(Dispatched.kt:420)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:26)
        at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109)
        at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:154)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:54)
        at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
        at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source:1)

I have both dependencies

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.1'

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 6
  • Comments: 23 (6 by maintainers)

Commits related to this issue

Most upvoted comments

FWIW, my team was also running into this crash when we upgraded from 1.3.1 to 1.3.2. The workaround we’ve used was to run ./gradlew clean followed by the corresponding install task using the --no-build-cache argument.

Having the same problem here after upgrading to 1.3.1. The error seems to happen randomly, not every time. Downgrading to 1.3.0 fixes it. R8 config was not changed between these two versions.

Update: Also happening for debug builds without R8!

We are also running into this issue from time to time (Coroutines 1.3.2, Gradle 5.1.1). I inspected the crashing apk and compared it to a working one. The crashing app is missing multiple entries in META-INF/services/ which explains the issue:

** Crashing **
/META-INF/services/javax.servlet.ServletContainerInitializer
/META-INF/services/com.fasterxml.jackson.core.JsonFactory
** Correct **
/META-INF/services/kotlin.reflect.jvm.internal.impl.resolve.ExternalOverridabilityCondition
/META-INF/services/kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoader
/META-INF/services/kotlinx.coroutines.CoroutineExceptionHandler
/META-INF/services/kotlinx.coroutines.internal.MainDispatcherFactory
/META-INF/services/javax.servlet.ServletContainerInitializer
/META-INF/services/com.fasterxml.jackson.core.JsonFactory

What I tried so far:

  1. Running Invalidate caches and restart in AS: Did not help.
  2. Running gradlew :app:installDebug --no-build-cache: Did not help.
  3. Running gradlew :app:clean: Did not help.
  4. Running gradlew :clean: Did not help.
  5. Delete project/.gradle and do clean re-build: Did not help.
  6. Stop gradle daemons and do clean re-build: Did not help.
  7. Running gradlew :clean :app:installDebug --no-build-cache: WORKS!
  8. Running gradlew :app:installDebug: WORKS!

I have the feeling that the combination of clean and --no-build-cache did the trick. But maybe one if the intermediate steps is also required. Will try this again in case I run into this error again.

Just an additional hint if anyone tries the solutions from above and they didn’t work.

For me, --no-build-cache didn’t help. Instead, a rule in proguard-rules.txt did the job:

-keep class kotlinx.coroutines.android.** {*;}

My setup is kotlinx-coroutines-android:1.3.9, no kotlinx-coroutines-core (I removed it as it turned out to be unnecessary), Kotlin 1.3.72, and Gradle 3.2.1.

But why it was working correctly until 1.3.1?

@qwwdfsad I am unable to reproduce the crash consistently. I haven’t been able to reproduce it at all on a new project, and on my existing project it does not seem to crash after a clean build ¯\(ツ)

If it’s any use, here’s the existing project

And it was on debug build, so without Proguard.

That’s really weird. Probably, we should give up on using ServiceLoader at all.

Could you please check if Class.forName("kotlinx.coroutines.android.AndroidDispatcherFactory") doesn’t throw ClassNotFoundException in your setup (where you observe IllegalStateException)?