kotlinx.coroutines: Crash after upgrade from 1.3.0 to 1.3.1
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'
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 6
- Comments: 23 (6 by maintainers)
Commits related to this issue
- User Class.forName instead of ServiceLoader to instantiate Dispatchers.Main on Android Fixes #1557 Fixes #878 — committed to Kotlin/kotlinx.coroutines by qwwdfsad 5 years ago
- User Class.forName instead of ServiceLoader to instantiate Dispatchers.Main on Android Fixes #1557 Fixes #878 — committed to Kotlin/kotlinx.coroutines by qwwdfsad 5 years ago
- User Class.forName instead of ServiceLoader to instantiate Dispatchers.Main on Android Fixes #1557 Fixes #878 — committed to Kotlin/kotlinx.coroutines by qwwdfsad 5 years ago
- User Class.forName instead of ServiceLoader to instantiate Dispatchers.Main on Android Fixes #1557 Fixes #878 — committed to Kotlin/kotlinx.coroutines by qwwdfsad 5 years ago
- User Class.forName instead of ServiceLoader to instantiate Dispatchers.Main on Android Fixes #1557 Fixes #878 — committed to Kotlin/kotlinx.coroutines by qwwdfsad 5 years ago
- User Class.forName instead of ServiceLoader to instantiate Dispatchers.Main on Android (#1572) Fixes #1557 Fixes #878 Fixes #1606 — committed to Kotlin/kotlinx.coroutines by qwwdfsad 5 years ago
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.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!
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:What I tried so far:
Invalidate caches and restartin 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/.gradleand 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
cleanand--no-build-cachedid 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-cachedidn’t help. Instead, a rule inproguard-rules.txtdid the job:My setup is
kotlinx-coroutines-android:1.3.9, nokotlinx-coroutines-core(I removed it as it turned out to be unnecessary), Kotlin 1.3.72, and Gradle 3.2.1.But why it was working correctly until 1.3.1?
@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
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 throwClassNotFoundExceptionin your setup (where you observeIllegalStateException)?