Skip to content

Module with the Main dispatcher is missing with version 1.3.0-RC2 and other versions as well #1532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
elPoyoLoco opened this issue Sep 13, 2019 · 12 comments
Labels

Comments

@elPoyoLoco
Copy link

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

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 ;
}`

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:
Screen Shot 2019-09-13 at 9 57 21 AM

So not sure when to go from here and what, and would appreciate any insights you may have on this.

@AChep
Copy link

AChep commented Sep 28, 2019

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

@ispbox
Copy link

ispbox commented Sep 29, 2019

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.

@qwwdfsad
Copy link
Collaborator

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

@elPoyoLoco
Copy link
Author

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.

@elPoyoLoco
Copy link
Author

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?

@qwwdfsad
Copy link
Collaborator

Most likely in the next release (1.3.3)

mshafrir-stripe added a commit to stripe/stripe-android that referenced this issue Nov 21, 2019
User reported crash:
```
java.lang.IllegalStateException: Module with the Main
dispatcher is missing. Add dependency providing the
Main dispatcher, e.g. 'kotlinx-coroutines-android'
```

Based on comments in related GitHub ticket,
attempting to see if downgrading
`kotlinx-coroutines` to `1.3.0` [0] and
adding Proguard rules [1] fixes the issue.

[0] Kotlin/kotlinx.coroutines#1532 (comment)
[1] Kotlin/kotlinx.coroutines#1532 (comment)

Fixes #1856
mshafrir-stripe added a commit to stripe/stripe-android that referenced this issue Nov 21, 2019
User reported crash:
```
java.lang.IllegalStateException: Module with the Main
dispatcher is missing. Add dependency providing the
Main dispatcher, e.g. 'kotlinx-coroutines-android'
```

Based on comments in related GitHub ticket,
attempting to see if downgrading
`kotlinx-coroutines` to `1.3.0` [0] and
adding Proguard rules [1] fixes the issue.

[0] Kotlin/kotlinx.coroutines#1532 (comment)
[1] Kotlin/kotlinx.coroutines#1532 (comment)

Fixes #1856
@Neristance
Copy link

Hi, any news when 1.3.3 is going to be released with the fix from #1572 ?
Thanks :)

@qwwdfsad
Copy link
Collaborator

During this year :)
The only blocker right now is fixing kotlinx-coroutines-test and then release is ready

@professorbk
Copy link

This error just happened to my Huawei device.
Resolved by "Rebuild project".
Hope this may help.
I am using Kotlin 1.3.2

@Malachiasz
Copy link

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.

@elPoyoLoco
Copy link
Author

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.

@qwwdfsad
Copy link
Collaborator

Fixed in 1.3.3

UweTrottmann added a commit to UweTrottmann/SeriesGuide that referenced this issue Jan 8, 2020
Most notably coroutines may fix
IllegalStateException: Module with the Main dispatcher is missing.
Kotlin/kotlinx.coroutines#1532
ashiagr added a commit to wordpress-mobile/WordPress-Android that referenced this issue Jan 14, 2020
Also had to upgrade Kotlin version to avoid a coroutines error:
"Main dispatcher is missing"
Kotlin/kotlinx.coroutines#1532
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants