我正在处理一个多模块项目,并且从支持库过渡到AndroidX后,./gradlew connectedCheck
失败了:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':MyProject:connectedDebugAndroidTest'.
> There were failing tests. See the report at: file:///Users/justinpollard/Dev/android-app/MyProject/build/reports/androidTests/connected/index.html
再往上走,我看到以下消息:
11:48:13 I/RemoteAndroidTest: Running am instrument -w -r com.myproject.test/androidx.test.runner.AndroidJUnitRunner on Pixel XL - 9
11:48:13 V/ddms: execute: running am instrument -w -r com.myproject.test/androidx.test.runner.AndroidJUnitRunner
11:48:15 V/InstrumentationResultParser: INSTRUMENTATION_RESULT: shortMsg=Process crashed.
11:48:15 I/InstrumentationResultParser: test run failed: 'Instrumentation run failed due to 'Process crashed.''
Starting 0 tests on Pixel XL - 9
Tests on Pixel XL - 9 failed: Instrumentation run failed due to 'Process crashed.'
11:48:15 I/XmlResultReporter: XML test result file generated at /Users/justinpollard/Dev/android-app/MyProject/build/outputs/androidTest-results/connected/TEST-Pixel XL - 9-MyProject-.xml. Total tests 0,
11:48:15 V/InstrumentationResultParser: INSTRUMENTATION_CODE: 0
11:48:15 V/InstrumentationResultParser:
11:48:15 V/ddms: execute 'am instrument -w -r com.myproject.test/androidx.test.runner.AndroidJUnitRunner' on 'HT73Y0200438' : EOF hit. Read: -1
11:48:15 V/ddms: execute: returning
com.android.builder.testing.ConnectedDevice > No tests found.[Pixel XL - 9] FAILED
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack @Test annotations).
这使得故障似乎是由测试apk崩溃导致的,然后才能运行测试。当我从Android Studio运行连接的测试之一时,会看到更多信息:
2019-05-13 11:55:55.927 29839-29839/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myproject, PID: 29839
java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo{com.myproject.test/androidx.test.runner.AndroidJUnitRunner}: java.lang.ClassNotFoundException: Didn't find class "androidx.test.runner.AndroidJUnitRunner" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.myproject.test-2MzWVeeaNLfexBOU_5tLPw==/base.apk", zip file "/data/app/com.myproject-Hd0c3YX8pUlOxZVK3mlgMA==/base.apk"],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5855)
at android.app.ActivityThread.access$1100(ActivityThread.java:200)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.test.runner.AndroidJUnitRunner" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.myproject.test-2MzWVeeaNLfexBOU_5tLPw==/base.apk", zip file "/data/app/com.myproject-Hd0c3YX8pUlOxZVK3mlgMA==/base.apk"],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5851)
at android.app.ActivityThread.access$1100(ActivityThread.java:200)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/test/runner/MonitoringInstrumentation;
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
... 10 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.test.runner.MonitoringInstrumentation" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.myproject.test-2MzWVeeaNLfexBOU_5tLPw==/base.apk", zip file "/data/app/com.myproject-Hd0c3YX8pUlOxZVK3mlgMA==/base.apk"],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 13 more
令我惊讶的是,由于我在应用程序级AndroidJUnitRunner
依赖项中包含MonitoringInstrumentation
,因此在路径上找不到androidx.text:runner
和build.gradle
:
dependencies {
...
androidTestImplementation(
"androidx.test.ext:junit:1.1.0",
"androidx.test:rules:1.1.0",
"androidx.test:runner:1.1.0",
"androidx.test.espresso:espresso-core:3.1.0",
)
...
}
我还确保我遵循https://developer.android.com/training/testing/set-up-project此处列出的说明:
1)我的项目build.gradle
包含google()
储存库(说明确实说要在应用程序级build.gradle
中包含此储存库,但是我假设给定{{1 }}是否存在于项目级allprojects
?)
2)我的依赖项已列在应用程序级别build.gradle
中(见上文)
3)将dependencies.androidTestImplementation
添加到我的应用useLibrary 'android.test.runner'
的{{1}}部分中
4)将android
添加到应用程序的build.gradle
中(说明表明,由于我使用gradle来运行测试,因此似乎不需要这样做)
作为参考,不包括整个测试,这是我的设置的说明性示例:
<uses-library android:name="android.test.runner" android:required="false" />
有人可以帮助我重新启动这些测试吗?谢谢!