IllegalStateException when launching on Dispatchers.Main: which artifact is actually required?
0 reputation · 15 Jun 2025, 14:24 UTC
Missing Main dispatcher at runtime
I have a Kotlin project that depends only on kotlinx-coroutines-core, and any coroutine launched on Dispatchers.Main fails at runtime with an IllegalStateException indicating that the Main dispatcher is missing. I understand the core artifact intentionally ships no Main implementation and that platform modules such as kotlinx-coroutines-android or kotlinx-coroutines-swing provide it via service loading.
What I need to clarify
The uncertainty is about resolution rules rather than the crash itself. My module is consumed both by an Android app and by plain JVM unit tests, so I cannot simply hard-depend on one UI artifact everywhere. I also want to know whether version skew between the core library and the platform dispatcher artifact can silently change which implementation gets loaded, or whether mismatches always surface as this same exception.
Assuming kotlinx-coroutines 1.7.x or newer:
- Which exact artifact must be on the classpath for
Dispatchers.Mainto resolve on each supported target (Android, Swing, JavaFX, JS)? - In a non-UI JVM environment such as a console app or unit test, is a test/mock dispatcher the only supported way to satisfy
Dispatchers.Main? - Does mixing core and platform artifact versions risk a runtime failure even when both resolve at build time?