Khongor Bayarsaikhan
Khongor Bayarsaikhan

Reputation: 1704

Module with Main dispatcher is missing

I'm trying to make a background call to my local database and update the UI with the results using coroutines. Here is my relevant code:

import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.Dispatchers.IO
import kotlinx.coroutines.experimental.Dispatchers.Main
import kotlin.coroutines.experimental.CoroutineContext
import kotlin.coroutines.experimental.suspendCoroutine

class WarehousesViewModel(private val simRepository: SimRepository)
: BaseReactViewModel<WarehousesViewData>(), CoroutineScope {

private val job = Job()

override val coroutineContext: CoroutineContext
    get() = job + Main

override val initialViewData = WarehousesViewData(emptyList())

override fun onActiveView() {
    launch {
        val warehouses = async(IO) { loadWarehouses() }.await()
        updateViewData(viewData.value.copy(items = warehouses))
    }
}

private suspend fun loadWarehouses(): List<Warehouse> =
    suspendCoroutine {continuation ->
        simRepository.getWarehouses(object : SimDataSource.LoadWarehousesCallback {
            override fun onWarehousesLoaded(warehouses: List<Warehouse>) {
                Timber.d("Loaded warehouses")
                continuation.resume(warehouses)
            }

            override fun onDataNotAvailable() {
                Timber.d("No available data")
                continuation.resume(emptyList())
            }
        })
    }
}

My problem is that I get a runtime exception:

java.lang.IllegalStateException: Module with Main dispatcher is missing. Add dependency with required Main dispatcher, e.g. 'kotlinx-coroutines-android'

I already added these to my gradle:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.30.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.26.0'

I'm a bit new to this, can someone help me?

Upvotes: 45

Views: 37460

Answers (11)

Breno W.
Breno W.

Reputation: 46

This seems kind of an old issue but I'll document my case as it may help others: using Unity IAP @ 4.12.1 our app started to crash as soon we'd initialize the in-app purchase modules. This is due the Play Billing API changes on distribution

e.g.: the .aar is now injected through gradle dependency

Using Mujahid's awnser for gradle and Vishal's for proguard rules solved our case.

Useful notes

  • Remember to add both coroutines-android and coroutines-core at the same version
  • The version used in the latest Unity IAP 4.12.0 refers to PlayBilling 6.2.1 which depends on [email protected] Source

Upvotes: 0

NIKUNJ DHAMI
NIKUNJ DHAMI

Reputation: 21

I got this error due to enabled proguard

-keep class kotlinx.coroutines.android.AndroidDispatcherFactory {*;}
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
-keepclassmembernames class kotlinx.** {
   volatile <fields>;
}

Add above rules in proguard-rules.pro

Upvotes: 2

Vishal Naikawadi
Vishal Naikawadi

Reputation: 493

Well, in my case keeping minifyEnabled true was causing the issue. but to achieve the obfuscation it was mandatory for me to keep the minifyEnabled true on the release build. So I did the following changes.

//keep only 
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8'

in your proguard-rules.pro add

-keep class kotlinx.coroutines.android.AndroidDispatcherFactory {*;}

I also required to add the model classes used for API call via retrofit in the proguard-rules.pro

-keep class com.example.test.login.data.model.** { *; }
-keepclassmembers class com.example.test.login.data.model.** { *; }

Upvotes: 2

Mujahid Khan
Mujahid Khan

Reputation: 1824

I have fixed by doing both version same 'kotlinx-coroutines-android' and ensure it has the same version as 'kotlinx-coroutines-core'

// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5'//same as coroutines-android
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5"//same as coroutines-core

Upvotes: 3

makingthematrix
makingthematrix

Reputation: 385

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.

Upvotes: 3

Saurabh Thorat
Saurabh Thorat

Reputation: 20614

If you're facing this problem with coroutines version 1.3.8, add this rule in your proguard-rules.pro:

-keep class kotlinx.coroutines.android.AndroidDispatcherFactory {*;}

Upvotes: 10

Mohamed AbdelraZek
Mohamed AbdelraZek

Reputation: 2799

Use both Core and Android dependency

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

Upvotes: 11

MOHSEN ALIAKBARI
MOHSEN ALIAKBARI

Reputation: 39

"core" is transitive dependency of "android" version in current version of "kotlinx-coroutines" so only use "android"

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'

Upvotes: 4

Gabor
Gabor

Reputation: 7572

./gradlew assembleDebug --rerun-tasks

fixes it if the above answers don't work for you (because you already had the required dependencies and you are using R8 which does not need the proguard rules).

Upvotes: 4

Khongor Bayarsaikhan
Khongor Bayarsaikhan

Reputation: 1704

Using just the kotlinx-coroutines-android version solves the problem.

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.30.1'

Upvotes: 76

Ganesh Kanna
Ganesh Kanna

Reputation: 2275

You may be missing some Proguard rules.

I had the same problem in release build and solved it by adding the following rules:

-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
-keepclassmembernames class kotlinx.** {
    volatile <fields>;
}

from https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/kotlinx-coroutines-android/example-app/app/proguard-rules.pro

Upvotes: 18

Related Questions