Since I updated to coroutines 1.3.1 on Android projects it started to crash on all existing 3rd libraries that using coroutines too.
It is just minor bugfix version I would not expect backward incompatibility on such release.
But there is this small unimportant note:
Note: Kotlin/Native artifacts are now published with Gradle metadata format version 1.0, so you will need Gradle version 5.3 or later to use this version of kotlinx.coroutines in your Kotlin/Native project.
Can this be a reason?
My project is using Gradle 5.6.2, but the library is using an older version than 5.3
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(MainDispatchers.kt:90)
at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:71)
at kotlinx.coroutines.DispatchedKt.resumeCancellable(Dispatched.kt:420)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:26)
at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109)
at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:154)
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)
I have both dependencies
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.1'
In 1.3.1 there were changes neither in kotlinx-coroutines-android nor in dispatching and service loading. It tends to be an R8 (or another Android tool) bug that keeps popping here and there, depending on a lot of details such as a minor version of R8.
It would be nice if you could attach a self-reproducing project, so I can report it to google bugtracker
We are still using just Proguard, so we are not using R8. And it was on debug build, so without Proguard. Sadly the library which I'm able to reproduce it is not publicly available.
I hoped that it will be a more general problem or caused that library and project itself using different Gradle version and service loader using metadata generated by Gradle.
I will try to wait until this library will also update to the 1.3.1 and latest Gradle tools
And it was on debug build, so without Proguard.
That's really weird. Probably, we should give up on using ServiceLoader at all.
Could you please check if Class.forName("kotlinx.coroutines.android.AndroidDispatcherFactory") doesn't throw ClassNotFoundException in your setup (where you observe IllegalStateException)?
I started getting this as well - updated to 1.3.1 from 1.3.0.
While updating I was testing another lib which resulted in kotlinx-coroutines-core-common to be pulled in causing the build to fail due to duplicate META-INF files. After excluding it/resyncing and running app, started getting same error as OP.
Running Class.forName("kotlinx.coroutines.android.AndroidDispatcherFactory") did correctly retrieve the class, so it was definitely there. Looked inside kotlinx-coroutines-android 1.3.1 JAR and could see the service file definitions, then checked the debug APK which did not have them though.
After removing the other lib, still had same error as OP, did a full rebuild, and problem went away - no idea if it was due to "common" being pulled in causing the build to fail and get into a broken state, or if it was just updating from 1.3.0 to 1.3.1 - so YMMV.
Having the same problem here after upgrading to 1.3.1. The error seems to happen randomly, not every time. Downgrading to 1.3.0 fixes it. R8 config was not changed between these two versions.
Update: Also happening for debug builds without R8!
Thanks for the information!
Could someone please attach a simple project that reproduces the issue? Usually minor versions of toolchain matter, so it's hard to craft such an example by myself
I tried to reproduce this with a minimal sample project but was not yet successful :/ The original project is quite complex, so I'm not sure which part may be the source of the problem here.
Some used versions:
Gradle: 5.6.2
AGP: 3.5.0
Build tools: ~29.0.2~ 28.0.3
Kotlin: 1.3.50
So it is probably not related to library. I asked the author of the library to update and now the library itself using 1.3.1 and is build by the latest Gradle and it sill happen. Then I tried to run the same code
GlobalScope.launch(Dispatchers.Main) {}
And it happens also on my project, but only with 1.3.1, 1.3.0 is completely fine.
I also tried to put Class.forName("kotlinx.coroutines.android.AndroidDispatcherFactory") to my app and nothing happen I mean no exception. So the class itself exists.
I have some additional information: I was in the middle of development, regularly deploying builds onto my device when suddenly the error occurred again with coroutines 1.3.0! In contrast to my previous statement I was using build tools version 28.0.3. I did not specify a concrete build tools version in my build config. I'm on Android SDK 29 and assumed that an appropriate build tools version (29.x) is used automatically. I now explicitly specified build tools 29.0.2 and the next build was working again. But since this issue is very random, this might not be the solution at all.
was in the middle of development, regularly deploying builds onto my device when suddenly the error occurred again with coroutines 1.3.0
This is probably related to "Apply changes" bug in Android: https://github.com/Kotlin/kotlinx.coroutines/issues/1072#issuecomment-532023046 & https://issuetracker.google.com/issues/130141145
Meanwhile, I'll start prototyping solution based on Class.forName for Android.
I'm not really convinced this is related to "Apply Changes" since customers are reporting this error. They use the release version which is build on a CI server, obviously without "Apply Changes".
Can confirm, facing this issue on debug builds. Tested on Emulator as well as a real device.
@haroldadmin could you please provide a reproducing project for the problem?
@qwwdfsad I am unable to reproduce the crash consistently. I haven't been able to reproduce it at all on a new project, and on my existing project it does not seem to crash after a clean build ¯\_(ツ)_/¯
If it's any use, here's the existing project
I just tested 1.3.1 and it works on the same project.
Update: So, it still happens sometimes.
But why it was working correctly until 1.3.1?
FWIW, my team was also running into this crash when we upgraded from 1.3.1 to 1.3.2. The workaround we've used was to run ./gradlew clean followed by the corresponding install task using the --no-build-cache argument.
We're able to consistently reproduce this error on a Nokia 7.1 with Android 9 (build 00WW_2_59E) even with Coroutines 1.3.0.
Update v1.3.3 solved the issue for me.
Still not working with v1.3.3
Could you please provide a reproducing project?
Does it work after specifying --no-build-cache? Could you please attach the full stacktrace?
We are also running into this issue from time to time (Coroutines 1.3.2, Gradle 5.1.1). I inspected the crashing apk and compared it to a working one. The crashing app is missing multiple entries in META-INF/services/ which explains the issue:
** Crashing **
/META-INF/services/javax.servlet.ServletContainerInitializer
/META-INF/services/com.fasterxml.jackson.core.JsonFactory
** Correct **
/META-INF/services/kotlin.reflect.jvm.internal.impl.resolve.ExternalOverridabilityCondition
/META-INF/services/kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoader
/META-INF/services/kotlinx.coroutines.CoroutineExceptionHandler
/META-INF/services/kotlinx.coroutines.internal.MainDispatcherFactory
/META-INF/services/javax.servlet.ServletContainerInitializer
/META-INF/services/com.fasterxml.jackson.core.JsonFactory
What I tried so far:
Invalidate caches and restart in AS: Did not help.gradlew :app:installDebug --no-build-cache: Did not help.gradlew :app:clean: Did not help.gradlew :clean: Did not help.project/.gradle and do clean re-build: Did not help.gradlew :clean :app:installDebug --no-build-cache: WORKS!gradlew :app:installDebug: WORKS!I have the feeling that the combination of clean and --no-build-cache did the trick. But maybe one if the intermediate steps is also required. Will try this again in case I run into this error again.
Just an additional hint if anyone tries the solutions from above and they didn't work.
For me, --no-build-cache didn't help. Instead, a rule in proguard-rules.txt did the job:
-keep class kotlinx.coroutines.android.** {*;}
My setup is kotlinx-coroutines-android:1.3.9, no kotlinx-coroutines-core (I removed it as it turned out to be unnecessary), Kotlin 1.3.72, and Gradle 3.2.1.
Most helpful comment
FWIW, my team was also running into this crash when we upgraded from
1.3.1to1.3.2. The workaround we've used was to run./gradlew cleanfollowed by the correspondinginstalltask using the--no-build-cacheargument.