This crash has been haunting me occasionally for a few months now:
Fatal Exception: 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 + 90(MissingMainCoroutineDispatcher.java:90)
at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded + 71(MissingMainCoroutineDispatcher.java:71)
at kotlinx.coroutines.DispatchedKt.resumeCancellable + 420(DispatchedKt.java:420)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable + 26(CancellableKt.java:26)
at kotlinx.coroutines.CoroutineStart.invoke + 109(CoroutineStart.java:109)
at kotlinx.coroutines.AbstractCoroutine.start + 154(AbstractCoroutine.java:154)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch + 54(BuildersKt__Builders_commonKt.java:54)
at kotlinx.coroutines.BuildersKt.launch + 1(BuildersKt.java:1)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default + 47(BuildersKt__Builders_commonKt.java:47)
at kotlinx.coroutines.BuildersKt.launch$default + 1(BuildersKt.java:1)
at com.example.user.ui.screens.mapScreen.MapActivity$getLocation$1.onChanged + 667(MapActivity.java:667)
at com.example.user.ui.screens.mapScreen.MapActivity$getLocation$1.onChanged + 46(MapActivity.java:46)
at androidx.lifecycle.LiveData.considerNotify + 131(LiveData.java:131)
at androidx.lifecycle.LiveData.dispatchingValue + 149(LiveData.java:149)
at androidx.lifecycle.LiveData.setValue + 307(LiveData.java:307)
at androidx.lifecycle.MutableLiveData.setValue + 50(MutableLiveData.java:50)
at androidx.lifecycle.Transformations$1.onChanged + 76(Transformations.java:76)
at androidx.lifecycle.MediatorLiveData$Source.onChanged + 152(MediatorLiveData.java:152)
at androidx.lifecycle.LiveData.considerNotify + 131(LiveData.java:131)
at androidx.lifecycle.LiveData.dispatchingValue + 149(LiveData.java:149)
at androidx.lifecycle.LiveData.setValue + 307(LiveData.java:307)
at androidx.lifecycle.MutableLiveData.setValue + 50(MutableLiveData.java:50)
at com.example.user.domain.repository.location.LocationRepository.submitLocation + 95(LocationRepository.java:95)
at com.example.user.domain.repository.location.LocationRepository.access$submitLocation + 18(LocationRepository.java:18)
at com.example.user.domain.repository.location.LocationRepository$getLocation$callback$1.onLocationResult + 59(LocationRepository.java:59)
at com.google.android.gms.internal.location.zzau.notifyListener + 4(zzau.java:4)
at com.google.android.gms.common.api.internal.ListenerHolder.notifyListenerInternal + 17(ListenerHolder.java:17)
at com.google.android.gms.common.api.internal.ListenerHolder$zaa.handleMessage + 5(ListenerHolder.java:5)
at android.os.Handler.dispatchMessage + 106(Handler.java:106)
at com.google.android.gms.internal.base.zap.dispatchMessage + 8(zap.java:8)
at android.os.Looper.loop + 214(Looper.java:214)
at android.app.ActivityThread.main + 6981(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 493(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main + 1445(ZygoteInit.java:1445)
This is the latest crash with coroutines version kotlinx-coroutines-android:1.3.0-RC2, AS 3.5, Gradle 5.1.1, AGP 3.4.2,
R8 enabled, version 1.4.94.
This particular crash happened on Samsung 9 running android 9, but i have also seen the crash (crashlytics) it on other versions and devices as well:
Redmi 4x with android 7.1.2 and the following properties:
AS 3.4.2, gradle 5.1.1, AGP 3.4.1
and on older versions of coroutines, this time bundled with core:kotlinx-coroutines-android:1.1.1, gradle 5.1.1 and AGP 3.4.1 on pocophone f1 running android 9, samsung j5 running android 8.1.0.
i have been unable to reproduce this issue on any of our production builds. I have also gone through all the issues related to MissingMainCoroutineDispatcher i could find here and on SO, yet none of the solutions seems to work. Some suggested that using -core as well -android might be related, but this happens as well without -core. Other suggested the packaging might get corrupted during when signing the app, but we have used the same build flow before without issues. i have also added the following proguard rules, while fully aware those are not required when using R8:
`# Coroutines
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
-keepnames class kotlinx.coroutines.android.AndroidExceptionPreHandler {}
-keepnames class kotlinx.coroutines.android.AndroidDispatcherFactory {}
-keepclassmembernames class kotlinx.** {
volatile
}`
Also without any visible effect. I have also seen suggestions regarding the signing process and specifically the final production apk, and the META-INF/services folder - and it looks like this when i open the apk in AS:

So not sure when to go from here and what, and would appreciate any insights you may have on this.
I have got the same crash in logs for Samsung S8 Plus on Android Pie. He got this crash 14 times in a day (and probably uninstalled the app, idk).
I use version 1.2.1 of coroutines
1.3.0 version works for me on Android, but 1.3.2 has the same error. I use android version of coroutines
"org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2"
Nothing helps - only downgrade. The problem exist in debug build as well, so it is not connected with Proguard.
We will hopefully fix this issue in #1572
It's really hard to pinpoint as it's only reproduced on real devices under unknown circumstances
i wasnt able to reproduce it at all on any of our real devices nor emulators, and i am not familiar enough with service loader to be of any help here. the crashes are so sporadic, its about 1 user in 500, and by looking at the apk's i do not see anything problematic.
We will hopefully fix this issue in #1572
It's really hard to pinpoint as it's only reproduced on real devices under unknown circumstances
after a few releases that did not stumble into this issue, the problem has resurfaced in our latest deploy. any idea when #1572 will be merged into master?
Most likely in the next release (1.3.3)
Hi, any news when 1.3.3 is going to be released with the fix from #1572 ?
Thanks :)
During this year :)
The only blocker right now is fixing kotlinx-coroutines-test and then release is ready
This error just happened to my Huawei device.
Resolved by "Rebuild project".
Hope this may help.
I am using Kotlin 1.3.2
A workaround seems to be using: Handler(Looper.getMainLooper()).asCoroutineDispatcher() instead of Dispatchers.Main. IMHO it's a critical bug, because random crashes with stable versions are undermining users trust, that JetBrain delivers stable, reliable libraries. It undermines trust to Kotlin on Android as a mature solution.
A workaround seems to be using:
Handler(Looper.getMainLooper()).asCoroutineDispatcher()instead of Dispatchers.Main. IMHO it's a critical bug, because random crashes with stable versions are undermining users trust, that JetBrain delivers stable, reliable libraries. It undermines trust to Kotlin on Android as a mature solution.
could not agree more with the "critical bug" part, not just because i am experiencing it, but because it is so random and hard to reproduce. I have actually managed to stumble into it with a debug build, but it went away after an invalidate cache/restart so not much to lean on with that.
Thanks for the workaround, but ill wait for the merge, whenever that might happen.
Fixed in 1.3.3
Most helpful comment
1.3.0 version works for me on Android, but 1.3.2 has the same error. I use android version of coroutines
Nothing helps - only downgrade. The problem exist in debug build as well, so it is not connected with Proguard.