Kotlinx.coroutines: java.lang.IllegalStateException: Module with the Main dispatcher is missing. Occurs for AWS device farm

Created on 21 Nov 2018  Â·  15Comments  Â·  Source: Kotlin/kotlinx.coroutines

For all devices in Amazon Web Service Device Farm we see crash evan for basi Android app which is using corutines 1.0.1 (implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'). This totally block our ui testing

Caused by: java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android'
11-21 07:47:17.723 10755 10755 E AndroidRuntime:    at kotlinx.coroutines.MissingMainCoroutineDispatcher.missing(Dispatchers.kt:123)
11-21 07:47:17.723 10755 10755 E AndroidRuntime:    at kotlinx.coroutines.MissingMainCoroutineDispatcher.dispatch(Dispatchers.kt:116)
11-21 07:47:17.723 10755 10755 E AndroidRuntime:    at kotlinx.coroutines.DispatchedKt.resumeCancellable(Dispatched.kt:282)
11-21 07:47:17.723 10755 10755 E AndroidRuntime:    at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:23)
11-21 07:47:17.723 10755 10755 E AndroidRuntime:    at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109)
11-21 07:47:17.723 10755 10755 E AndroidRuntime:    at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:160)
11-21 07:47:17.723 10755 10755 E AndroidRuntime:    at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:54)
11-21 07:47:17.723 10755 10755 E AndroidRuntime:    at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
11-21 07:47:17.723 10755 10755 E AndroidRuntime:    at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
bug

Most helpful comment

You can find it here: https://github.com/int-mszczepanik/SampleAppForDeviceFarm

it crashes even when I run it manually via remote sessions on AWS (Samsung S5 (4.4.4)

All 15 comments

Android (and especially new androidx toolchain) is a complicated beast, please provide additional information along with report:

All dependencies versions (androidx, API level, other dependencies etc.), whether R8 or Proguard is used and its version. E.g. have you added coroutines proguard rules (https://github.com/Kotlin/kotlinx.coroutines#r8-and-proguard)?

What even better is to provide a sample project which consistently crashes.

It occurs for:

  • Google Pixel (7.1.2)
  • Google Pixel 2 (8.0.0)
  • Samsung Galaxy S6 (T-Mobile) (6.0.1)
  • Samsung Galaxy S9 (Unlocked) (8.0.0)
  • Samsung Galaxy Tab 4 10.1" (WiFi) (4.4.2)

All these devices are in AWS device farm and only there this issue was seen.
I cannot reproduce it on my device (Google Pixel 2 (9.0)) and . It also not occurs for experimental versions of corutines

It is crashing even for code like that:

class MainActivity : AppCompatActivity(), CoroutineScope {

private val job = Job()
override val coroutineContext: CoroutineContext = job + Dispatchers.Main

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        launch {
            val profile = "test"
            val number = " ${getProfile()}"
            Toast.makeText(applicationContext, profile+number, Toast.LENGTH_LONG).show()
        }
    }

    private suspend fun getProfile(): Int {
        return withContext(Dispatchers.IO) {
            delay(100)
            10
        }
    }
}

Are you using R8 or ProGuard?

No, It occurs even for debug version without R8 and Proguard

Could you please attach a self-contained project which I can run on AWS farm and that will crash?
Just to be sure that I'm debugging the problematic pattern of usage

You can find it here: https://github.com/int-mszczepanik/SampleAppForDeviceFarm

it crashes even when I run it manually via remote sessions on AWS (Samsung S5 (4.4.4)

This bug was also persistent for me, even not on AWS device. I fixed it by adding an additional line into my gradle file:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'

Before, my gradle only contained a core library, but by adding android coroutine library this error was fixed. I see that in your sample project you have imported only android coroutine library, so add core lib too. It may fix the problem.

With core or without same error occurs on AWS device farm (tested for 1.0.1 and 1.0.0) BTW Android include dependency to core lib as I know and it is important to have same version for both libraries.

@qwwdfsad what would be the next step towards fixing this issue?

I also have this problem with AWS device farm. No Proguard.

We are also having this problem on AWS device farm with a debug (no proguard) build of an APK.

I spoke to Device Farm Support and they indicated that Kotlin is not supported.

It the problem still actual?

We have same issue on out app, which only happends on Pixel and Samsung devcies so far.

Make sure you are not stripping the meta-inf in your build. That's almost
certainly the problem.

On Thu, 26 Sep 2019, 08:13 kuno, notifications@github.com wrote:

We have same issue on out app, which only happends on Pixel and Samsung
devcies so far.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/Kotlin/kotlinx.coroutines/issues/849?email_source=notifications&email_token=AABP6ZOJ5B7U3TWKYUZA6RTQLRHIZA5CNFSM4GFU47QKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7UNXVY#issuecomment-535354327,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AABP6ZOFIB7J4OXDQDW6KZTQLRHIZANCNFSM4GFU47QA
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaozinfs picture jaozinfs  Â·  3Comments

elizarov picture elizarov  Â·  3Comments

ZakTaccardi picture ZakTaccardi  Â·  3Comments

zach-klippenstein picture zach-klippenstein  Â·  3Comments

iTanChi picture iTanChi  Â·  3Comments