Module with the Main dispatcher is missing with version 1.3.0-RC2 and other versions as well
See original GitHub issueThis 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
ServiceLoader support
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {} -keepnames class kotlinx.coroutines.CoroutineExceptionHandler {} -keepnames class kotlinx.coroutines.android.AndroidExceptionPreHandler {} -keepnames class kotlinx.coroutines.android.AndroidDispatcherFactory {}
Most of volatile fields are updated with AFU and should not be mangled
-keepclassmembernames class kotlinx.** { volatile <fields>; }`
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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:12 (4 by maintainers)
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.
During this year 😃 The only blocker right now is fixing
kotlinx-coroutines-test
and then release is ready