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

标签 android kotlin junit4 android-testing kotlinx.coroutines

我想在我的 Android 应用程序测试中使用 Kotlin 协程。我还没有在应用程序本身中使用它们。
我还为该应用程序运行 ProGuard。

我正在尝试像这样使用它们:

@RunWith(AndroidJUnit4::class)
class TheTest {

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

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

但是,在启动此测试时,它会失败并出现以下错误:
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

我尝试使用 testProguardFile内容如下:
-ignorewarnings
-dontobfuscate
-dontoptimize
-dontshrink
-dontusemixedcaseclassnames

但这似乎没有帮助。

最佳答案

您必须为此使用特殊的帮助程序库:

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

添加到依赖项:

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'
}

1.3.5 版本看起来比 1.3.6 更稳定。

或者将使用版本 1.3.7 ,它非常新鲜,今天发布。
https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-test/README.md

还要检查您是否使用了正确的测试运行程序(仅当您的项目迁移到 AndroidX 时):
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

关于android - 在带有 ProGuard 的 Android Instrumentation 测试中使用 Kotlin 协程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54197197/

相关文章:

android - 使用协同程序读取和复制文件

android - 当隐藏状态栏时,有没有办法阻止 "jumping"的布局?

java - Spring JUnit4 手动/自动布线困境

android - 使用空参数调用 super.onCreate()?

android - 比较图像和声音

Android:按下按钮时保持通知打开

kotlin - 运算符 '==' 不能应用于 'Boolean' 和 'Char'

maven - SureFire JUnitCore 和 JUnit4 Runners 之间有什么区别

java - 模拟服务抛出 nullPointerException

android - 某些样式在 TextView 中禁用 ImageSpan