Kotlinx.coroutines: IllegalStateException: Module with the Main dispatcher is missing

Created on 6 Nov 2018  路  16Comments  路  Source: Kotlin/kotlinx.coroutines

Our user with Xiaomi Redmi Note 4 device (Android 7.1.2) suddenly started to experience crash that had to be fixed with #657.
We're unable to reproduce it on our side. Here is reproducer app with the same lib dependencies as our main application: https://www.dropbox.com/s/eay696h9qmxjl5e/sample.zip?dl=1

AndroidX AppCompat: 1.0.0
Kotlin: 1.3.0
Coroutines: 1.0.0
Gradle wrapper: 4.10.2-bin

Caused by java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android'
       at kotlinx.coroutines.MissingMainCoroutineDispatcher.missing(Dispatchers.kt:123)
       at kotlinx.coroutines.MissingMainCoroutineDispatcher.dispatch(Dispatchers.kt:116)
       at kotlinx.coroutines.DispatchedKt.resumeCancellable(Dispatched.kt:279)
       at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:23)
       at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109)
       at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:160)
       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)
       at our.app.ui.base.BaseActivity.startAsync(BaseActivity.kt:229)
       at our.app.ui.main.MainActivity.refreshServerConfig(MainActivity.kt:63)
       at our.app.ui.main.MainActivity.onCreate(MainActivity.kt:134)
       at android.app.Activity.performCreate(Activity.java:6738)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2652)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2766)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6236)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
question

Most helpful comment

I has same issue and fixed it by adding options from coroutines.pro to proguard. Hope it helps somebody.

All 16 comments

UPD: We also use jetifier processor:

classpath "com.android.tools.build.jetifier:jetifier-processor:1.0.0-alpha10"

Newer version (1.0.0-beta02) is not compatible with databinding.

Uncomment the last two lines in your sample's proguard-rules.pro

I enabled R8 and it is the problem. Only proguard no problem. coroutines version 1.0.1

I has same issue and fixed it by adding options from coroutines.pro to proguard. Hope it helps somebody.

@evgentset thanks, worked for me. coroutines version 1.0.1

Seconding @evgentset's fix! Worked for me on coroutines 1.1.1

I have the same issue. but the link is not available now. Could @evgentset provide again? please

coroutines.pro @dendrocyte a copy from @evgentset

@dendrocyte if you are having options for Kotlin in build.gradle packageOptions you can remove them. It will work again without need adding the proguard rule. In my case, I could export file APK but it had the RunTimeException when opening the app so I removed all packageOptions for kotlin and it worked fine with Kotlin 1.3.31 and coroutine 1.2.1

In our case, we put signingConfigs in the gradle file, then use the Generate Signed Bundle / APK to create the bundle. :D
Removing the signingConfigs solves the problem.

@endru-kargo I will try your solution later. thank you!!!

Did it help?

my client phone are Samsung and Sharp which are hit this error.
At the end, I upgrade your lib to 1.2.1, and make the error
from
Job = launch{}
change to
Job = launch{} as CompletableJob.
Then it fix!

I has same issue and fixed it by adding options from coroutines.pro to proguard. Hope it helps somebody.

Correcting the git link 'coroutines.pro'.

For me updating Koltin to 1.3.50 and coroutines to 1.3.2 (having AGP 3.5.1) fixed the issue (I guess that it was initially broken by APG update or Dexguard update)

It was also discussed in issue #1532. Turning kotlin [1.3.50 -> 1.3.61], coroutines [1.3.1 -> 1.3.3] did the work for me.

Was this page helpful?
0 / 5 - 0 ratings