kotlinx.coroutines: Android app with coroutines 0.30.1-eap13 crashes in runtime

java.lang.IllegalStateException: Module with Main dispatcher is missing. Add dependency with required Main dispatcher, e.g. 'kotlinx-coroutines-android'
        at kotlinx.coroutines.Dispatchers.getMain(Dispatchers.kt:61)

kotlinx-coroutines-android is obviously added. Version 0.30.0-eap13 works fine.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 8
  • Comments: 22 (4 by maintainers)

Commits related to this issue

Most upvoted comments

If an app is obfustacted with default proguard settings, it crashes in runtime. Here is sample project to reproduce an issue: https://www.dropbox.com/s/eay696h9qmxjl5e/sample.zip?dl=1 UPD: I’ve created PR #662 but it will not work, because @Keep annotation is Android specific. Is the only way to add proguard rules?

-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}

The same issue with 0.30.1 in release build only (i. e. when minifyEnabled true is specified)

Android Studio 3.1.2 Build #AI-173.4720617, built on April 14, 2018 JRE: 1.8.0_152-release-1024-b01 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Mac OS X 10.13.4

Didn’t work:

-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}

Worked:

-keepnames class kotlinx.** { *; }

Same issue here with 0.30.2.

The suggested proguard rules should be enough. I mean:

# ServiceLoader support
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}

# Most of volatile fields are updated with AFU and should not be mangled
-keepclassmembernames class kotlinx.** {
    volatile <fields>;
}

I was recently facing this issue and realised that it was my fault and nothing to do with the proguard rules. Basically, we were re-signing the apk and removing the META-INF folder in the process. So, for the ones that are still facing the issue, please make sure that you are not doing the same (re-signing your apk and removing the META-INF folder after the release build is done). If that’s the case, I would suggest you to generate an unsigned build and sign it afterwards.

Just for reference, I’m using the version 1.0.1. implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1")

Hope it helps.

We were having this crash without even using ProGuard 😅 As mentioned by @djrsousa, it’s because we were:

Signing the APK twice,

and replacing the META-INF folder in the process…

@bernaferrari then they need to be updated to

-keepnames class kotlinx.** { *; }

for kotlinx-coroutines-android

It is enough to start any coroutine in UI context.

I did it in sample Android project and it works in both debug and release mode.

ServiceLoader is unable to load MainDispatcherFactory.

Again, works on my machine.

Please be cooperative. I’m willing to help you, but I can’t do anything with reports like “it doesn’t work”