Search code examples
androidkotlinkotlinx.coroutines

Exception "Module with the Main dispatcher is missing." in release APK only


I'm using Kotlin 1.3 EAP and kotlinx-coroutines-android:1.0.0-RC1 in my Android project. My development build succeeds and the app runs fine. However, when I Generate Signed APK, the app still builds and runs, but then crashes with

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

Since the dev build runs fine, clearly there's no omission in the gradle files. I have these settings in place:

Project build.gradle:

buildscript {
    ext.kotlin_version = '1.3.0-rc-190'
    ....

Module build.gradle:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0-RC1'

The same application has been running with many different versions of experimental coroutines over time, this is the first time I have experienced this issue. I suspect some interim problem in the EAP artifacts.

What could I try to make this work?


Solution

  • If you're using proguard, add these proguard rules.

    # ServiceLoader support
    -keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
    -keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
    
    # Most of volatile fields are updated with AFU and should not be mangled
    -keepclassmembernames class kotlinx.** {
        volatile <fields>;
    }
    

    Source