4

I want to use Kotlin coroutines in my instrumentation tests for Android app. I do not use them in the app itself yet. I also run ProGuard for the app.

I am trying to use them like this:

@RunWith(AndroidJUnit4::class)
class TheTest {

    @Rule
    @JvmField
    val activityTestRule = ActivityTestRule(MyActivity::class.java)

    @Test
    fun test() {
        runBlocking {}
    }
}

However, when launching this test it fails with the following error:

java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/coroutines/jvm/internal/SuspendLambda;
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(Unknown Source:11)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(Unknown Source:2)
at org.junit.internal.runners.model.ReflectiveCallable.run(Unknown Source:0)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(Unknown Source:5)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(Unknown Source:14)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(Unknown Source:2)
at org.junit.runners.ParentRunner$3.run(Unknown Source:6)
at org.junit.runners.ParentRunner$1.schedule(Unknown Source:0)
at org.junit.runners.ParentRunner.runChildren(Unknown Source:25)
at org.junit.runners.ParentRunner.access$000(Unknown Source:0)
at org.junit.runners.ParentRunner$2.evaluate(Unknown Source:4)
at org.junit.runners.ParentRunner.run(Unknown Source:13)
at org.junit.runners.Suite.runChild(Unknown Source:0)
at org.junit.runners.Suite.runChild(Unknown Source:2)
at org.junit.runners.ParentRunner$3.run(Unknown Source:6)
at org.junit.runners.ParentRunner$1.schedule(Unknown Source:0)
at org.junit.runners.ParentRunner.runChildren(Unknown Source:25)
at org.junit.runners.ParentRunner.access$000(Unknown Source:0)
at org.junit.runners.ParentRunner$2.evaluate(Unknown Source:4)
at org.junit.runners.ParentRunner.run(Unknown Source:13)
at org.junit.runner.JUnitCore.run(Unknown Source:25)
at org.junit.runner.JUnitCore.run(Unknown Source:4)
at android.support.test.internal.runner.TestExecutor.execute(Unknown Source:24)
at android.support.test.runner.AndroidJUnitRunner.onStart(Unknown Source:46)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2075)
Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.coroutines.jvm.internal.SuspendLambda" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.myapp.test-d-f4s4j7q8wpEZJZvy9h4A==/base.apk", zip file "/data/app/com.myapp.dev-7Gs6UezsUbiSCx7s3jZ6LQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp.dev.test-d-f4s4j7q8wpEZJZvy9h4A==/lib/x86, /data/app/com.myapp.dev-7Gs6UezsUbiSCx7s3jZ6LQ==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 27 more

I tried using testProguardFile with the following content:

-ignorewarnings
-dontobfuscate
-dontoptimize
-dontshrink
-dontusemixedcaseclassnames

But it does not seem to help.

1 Answer 1

2

You must use special helper library for this:

https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-test/

Add to dependencies:

dependencies {
  implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5'
  implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5'
  testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.5'
  androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.5'
}

Version 1.3.5 looks more stable than 1.3.6.

Or would be use version 1.3.7, it very fresh and released today. https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-test/README.md

Also check that you are using correct test runner (it just if your project migrate to AndroidX):

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.