kotlinx.coroutines: Module with the Main dispatcher had failed to initialize

Getting this error on android. coroutiens version 1.0.0 java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:79) at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:54) at kotlinx.coroutines.DispatchedKt.resumeCancellable(Dispatched.kt:373) at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:25) at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109) at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:160) at kotlinx.coroutines.channels.ActorKt.actor(Actor.kt:121) at kotlinx.coroutines.channels.ActorKt.actor$default(Actor.kt:112)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 10
  • Comments: 28 (2 by maintainers)

Most upvoted comments

Please add implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:version" the version as same as implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:version" I do this, solved it. it maybe help you.

Upgrading to 1.1.0 solved it for me as well. implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0

Agree, still reproducible with kotlinx-coroutines-android:1.1.1

Very lack of context here… If you got this error in the implementation code file, you just simply update the coroutines version and it works. If you got this error in the test file (error should be like this A test rule to allow testing coroutines that use the main dispatcher. Without this you'd run into "java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used" ). You can simply add coroutines rule. You can see the code in here https://gist.github.com/AniketSK/0fd48da9ed969eee307f92457115612a

This still happens on kotlinx-coroutines-android:1.1.1 80% Android 6.0.x, 20% Android 5.1 Unfortunately I couldn’t reproduce on a Samsung S5 test device.

Fatal Exception: java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize
       at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:79)
       at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:54)
       at kotlinx.coroutines.DispatchedKt.resumeCancellable(Dispatched.kt:373)
       at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:25)
       at kotlinx.coroutines.CoroutineStart.invoke(AbstractCoroutine.kt:109)
       at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Unknown Source:54)
       at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default$28f1ba1(Builders.common.kt:47)
       at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
       at my.app.NotesFragment.loadSomething(NotesFragment.kt:542)
       at my.app.NotesFragment.onCreateView(NotesFragment.kt:101)
       at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2530)
...
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by java.util.ServiceConfigurationError: Couldn't read jar:file:/data/app/my.app-1/base.apk!/META-INF/services/kotlinx.coroutines.internal.MainDispatcherFactory
       at java.util.ServiceLoader$ServiceIterator.readClass(ServiceLoader.java:245)
       at java.util.ServiceLoader$ServiceIterator.hasNext(ServiceLoader.java:202)
       at kotlin.collections.CollectionsKt___CollectionsKt.toCollection(_Collections.kt:1145)
       at kotlin.collections.CollectionsKt___CollectionsKt.toMutableList(_Collections.kt:1178)
       at kotlin.collections.CollectionsKt___CollectionsKt.toList(_Collections.kt:1169)
       at kotlinx.coroutines.internal.MainDispatcherLoader.loadMainDispatcher(MainDispatchers.kt:15)
       at kotlinx.coroutines.internal.MainDispatcherLoader.(MainDispatchers.kt:10)
       at kotlinx.coroutines.Dispatchers.getMain(Dispatchers.kt:55)
       at my.app.MainCoroutineScope$DefaultImpls.getCoroutineContext(MainCoroutineScope.kt:9)
       at my.app.NotesFragment.getCoroutineContext(NotesFragment.kt:64)
       at kotlinx.coroutines.CoroutineContextKt.newCoroutineContext(CoroutineContext.kt:57)
       at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Unknown Source:50)
       at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default$28f1ba1(Builders.common.kt:47)
       at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
       at my.app.NotesFragment.loadSomething(NotesFragment.kt:542)
       at my.app.NotesFragment.onCreateView(NotesFragment.kt:101)
       at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2530)
...
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by java.util.ConcurrentModificationException
       at java.util.HashMap$HashIterator.nextEntry(HashMap.java:788)
...
       at java.util.ServiceLoader$ServiceIterator.hasNext(ServiceLoader.java:202)
       at kotlin.collections.CollectionsKt___CollectionsKt.toCollection(_Collections.kt:1145)
       at kotlin.collections.CollectionsKt___CollectionsKt.toMutableList(_Collections.kt:1178)
       at kotlin.collections.CollectionsKt___CollectionsKt.toList(_Collections.kt:1169)
       at kotlinx.coroutines.internal.MainDispatcherLoader.loadMainDispatcher(MainDispatchers.kt:15)
       at kotlinx.coroutines.internal.MainDispatcherLoader.(MainDispatchers.kt:10)
       at kotlinx.coroutines.Dispatchers.getMain(Dispatchers.kt:55)
       at my.app.MainCoroutineScope$DefaultImpls.getCoroutineContext(MainCoroutineScope.kt:9)
       at my.app.NotesFragment.getCoroutineContext(NotesFragment.kt:64)
       at kotlinx.coroutines.CoroutineContextKt.newCoroutineContext(CoroutineContext.kt:57)
       at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Unknown Source:50)
       at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default$28f1ba1(Builders.common.kt:47)
       at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
       at my.app.NotesFragment.loadSomething(NotesFragment.kt:542)
       at my.app.NotesFragment.onCreateView(NotesFragment.kt:101)
       at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2530)
...
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Please update to 1.1.0 and see if it still reproduces. Also, it is useful to provide full stacktrace (this exception should have a meaningful cause) and some context.

I am reopening the issue to make it easier to find. But there is not much we can do from our side, this is an Android toolchain issue and it should be reported to Google bug tracker

Please add implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:version" the version as same as implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:version" I do this, solved it. it maybe help you.

solved for me, thanks

Upgrading to 1.1.0 solved it. Android Studio haven’t showed there is newer version, hm. Thanks.

@giovankabisano I have the same issue in test. The problem is that the test pass locally but failed in Jenkin with:

Exception in thread "default" java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used
	at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:96)
	at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:71)
	at kotlinx.coroutines.test.internal.TestMainDispatcher.isDispatchNeeded(MainTestDispatcher.kt:39)
	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)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
	at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)

Any suggestions ?