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

Android app with coroutines 0.30.1-eap13 crashes in runtime #657

Closed
trashkalmar opened this Issue Oct 3, 2018 · 16 comments

Comments

Projects
None yet
9 participants
@trashkalmar

trashkalmar commented Oct 3, 2018

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.

@qwwdfsad

This comment has been minimized.

Member

qwwdfsad commented Oct 3, 2018

Could you please provide a self-contained example? Or check why loadMainDispatcher loads nothing?

@trashkalmar

This comment has been minimized.

trashkalmar commented Oct 3, 2018

Could you please provide a self-contained example?

It is enough to start any coroutine in UI context. For example:
GlobalScope.launch(Dispatchers.Main) { ... }

Or check why loadMainDispatcher loads nothing?

ServiceLoader is unable to load MainDispatcherFactory.

@qwwdfsad

This comment has been minimized.

Member

qwwdfsad commented Oct 3, 2018

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"

@trashkalmar

This comment has been minimized.

trashkalmar commented Oct 3, 2018

It was my fault. I've excluded some Kotlin meta files from APK (specifically, Meta-inf/services/**). It was safe for previous coroutines version, but for 0.30.1 it became destructive.
Sorry that wasted your time.

@trashkalmar trashkalmar closed this Oct 3, 2018

@trashkalmar

This comment has been minimized.

trashkalmar commented Oct 4, 2018

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 {}

@trashkalmar trashkalmar reopened this Oct 4, 2018

@kimcy929

This comment has been minimized.

kimcy929 commented Oct 12, 2018

I had this bug when exported file apk when using proguard with the version 0.30.2.

@qwwdfsad qwwdfsad self-assigned this Oct 12, 2018

@petitJAM

This comment has been minimized.

petitJAM commented Oct 12, 2018

Those proguard rules didn't work for me, so I used this:

-keepnames class kotlinx.** { *; }

Definitely not a great solution since it tells proguard to keep everything, but it works.

@bernaferrari

This comment has been minimized.

bernaferrari commented Oct 12, 2018

Same issue here with 0.30.2.

@jvlad

This comment has been minimized.

jvlad commented Oct 15, 2018

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.** { *; }
@qwwdfsad

This comment has been minimized.

Member

qwwdfsad commented Oct 15, 2018

Thank you @trashkalmar for the reproducer.
Jettifier seems to have an issue with ServiceLoader and name mangling.

I have (ugly as hell) fix which first tries to lookup main dispatcher in service loader and then invokes Class.forName("...") for android factory. It will be fixed in 1.0.0-RC

@bernaferrari

This comment has been minimized.

bernaferrari commented Oct 18, 2018

This happens to me even without jetfier. Just proguard on its enough.

@qwwdfsad qwwdfsad closed this in aba0edc Oct 19, 2018

@AAverin

This comment has been minimized.

AAverin commented Oct 21, 2018

It would be great if kotlinx.coroutines library could include proguard rules as part of distribution

@AAverin

This comment has been minimized.

AAverin commented Oct 22, 2018

@bernaferrari then they need to be updated to

-keepnames class kotlinx.** { *; }

for kotlinx-coroutines-android

@JakeWharton

This comment has been minimized.

Contributor

JakeWharton commented Oct 22, 2018

@raiytu4

This comment has been minimized.

raiytu4 commented Oct 26, 2018

I have the same issue with 0.30.2

I also get default rule at
https://github.com/Kotlin/kotlinx.coroutines/blob/master/core/kotlinx-coroutines-core/resources/META-INF/proguard/coroutines.pro

Got the same message @trashkalmar

-keepnames class kotlinx.** { *; } did work though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment