我想在针对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
但这似乎无济于事。
答案 0 :(得分:1)
您必须为此使用特殊的帮助程序库:
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"