简体   繁体   English

在带有 ProGuard 的 Android Instrumentation 测试中使用 Kotlin 协程

[英]Using Kotlin coroutines in Android Instrumentation test with ProGuard

I want to use Kotlin coroutines in my instrumentation tests for Android app.我想在 Android 应用程序的检测测试中使用 Kotlin 协程。 I do not use them in the app itself yet.我还没有在应用程序本身中使用它们。 I also run ProGuard for the app.我还为该应用程序运行 ProGuard。

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:我尝试使用具有以下内容的testProguardFile

-ignorewarnings
-dontobfuscate
-dontoptimize
-dontshrink
-dontusemixedcaseclassnames

But it does not seem to help.但这似乎没有帮助。

You must use special helper library for this:您必须为此使用特殊的帮助程序库:

https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-test/ 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. 1.3.5 版本看起来比 1.3.6 更稳定。

Or would be use version 1.3.7 , it very fresh and released today.或者将使用版本1.3.7 ,它非常新鲜并于今天发布。 https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-test/README.md 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):还要检查您是否使用了正确的测试运行程序(仅当您的项目迁移到 AndroidX 时):

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM