Gradle托管设备结合Test Orchestrator运行异常排查
问题场景
已在app/build.gradle.kts中完成GMD设备配置,并启用Test Orchestrator,配置代码如下:
testOptions { execution = "ANDROIDX_TEST_ORCHESTRATOR" animationsDisabled = true managedDevices { localDevices { create("pixel2api30") { device = "Pixel 2" apiLevel = 30 systemImageSource = "google-atd" } } } }
执行测试命令./gradlew app:pixel2api30DebugAndroidTest时,控制台输出Starting 0 tests on pixel2api30后构建失败。注释掉execution = "ANDROIDX_TEST_ORCHESTRATOR"后,可正常启动94个测试,但业务要求必须使用Test Orchestrator,导致测试无法达标。
开启--info模式后,核心日志片段如下:
INFO: Installing [/Users/barryirvine/StudioProjects/Go City/app/build/outputs/apk/debug/app-debug.apk] on device emulator-5554. INFO: Installing [/Users/barryirvine/.gradle/caches/modules-2/files-2.1/androidx.test/orchestrator/1.4.2/e39fb0a062aa533b8c2805bbf9648829a9cd977e/orchestrator-1.4.2.apk, /Users/barryirvine/.gradle/caches/modules-2/files-2.1/androidx.test.services/test-services/1.4.2/d051210a53d266ad436e79bea473cc5e843245b8/test-services-1.4.2.apk] on device emulator-5554. INFO: Installing [/Users/barryirvine/StudioProjects/Go City/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk] on device emulator-5554. INFO: Start logcat streaming. INFO: Running Android Instrumentation driver. SEVERE: Fatal error while executing main with args: --proto_config=/Users/barryirvine/.android/utp/runnerConfig14768629602247508805.pb --proto_server_config=/Users/barryirvine/.android/utp/serverConfig9633560546492066251.pb SEVERE: Fatal error while executing main with args: --proto_config=/Users/barryirvine/.android/utp/runnerConfig14768629602247508805.pb --proto_server_config=/Users/barryirvine/.android/utp/serverConfig9633560546492066251.pb WARNING: Cancellation triggered in Android Instrumentation Driver. WARNING: "Instrumentation command failed with non zero exit code: Instrumentation command: CLASSPATH=$(pm path androidx.test.services) app_process / androidx.test.services.shellexecutor.ShellMain am instrument -w -r --no-window-animation -e clearPackageData true -e useTestStorageService true -e additionalTestOutputDir /sdcard/Android/media/com.gocitypass.debug/additional_test_output -e testTimeoutSeconds 31536000 -e targetInstrumentation com.gocitypass.debug.test/com.gocitypass.HiltTestRunner androidx.test.orchestrator/androidx.test.orchestrator.AndroidTestOrchestrator StatusCode: 143 INFO: Stop logcat stream
解决方案
1. 修正Test Orchestrator配置方式
全局execution = "ANDROIDX_TEST_ORCHESTRATOR"配置会与GMD默认使用的UTP(AndroidX Test Platform)执行逻辑冲突,需改为在GMD设备配置中单独启用:
testOptions { animationsDisabled = true managedDevices { localDevices { create("pixel2api30") { device = "Pixel 2" apiLevel = 30 systemImageSource = "google-atd" // 针对该设备启用Test Orchestrator testInstrumentationRunnerArguments["useTestOrchestrator"] = "true" } } } }
同时移除全局的execution配置项。
2. 统一Test Orchestrator依赖版本
确保androidx.test:orchestrator、androidx.test.services:test-services与AndroidX Test Runner版本一致,建议升级到最新稳定版:
androidTestImplementation "androidx.test:runner:1.5.2" androidTestUtil "androidx.test:orchestrator:1.5.2" androidTestImplementation "androidx.test.services:test-services:1.5.2"
版本不匹配会导致进程启动失败,对应日志中的SEVERE级错误。
3. 检查自定义Test Runner配置
若使用自定义的HiltTestRunner,需确保其继承自AndroidJUnitRunner,且未重写影响Orchestrator执行的逻辑,示例配置如下:
class HiltTestRunner : AndroidJUnitRunner() { override fun newApplication( cl: ClassLoader?, className: String?, context: Context? ): Application { return super.newApplication(cl, HiltTestApplication::class.java.name, context) } }
4. 清理缓存并重建
执行以下命令清理Gradle缓存和设备状态,避免旧缓存干扰:
./gradlew clean ./gradlew app:pixel2api30DebugAndroidTest --no-build-cache
同时关闭当前运行的GMD模拟器,让Gradle重新创建干净的模拟器实例。
5. 切换系统镜像(可选)
google-atd镜像对Test Orchestrator的支持可能存在限制,可尝试切换为google_apis或default镜像:
systemImageSource = "google_apis"
原因解析
日志中StatusCode 143表示进程被终止(SIGTERM),核心冲突点在于:
- 全局
execution配置与GMD的UTP执行流程不兼容,导致测试启动逻辑混乱。 - Test Orchestrator与依赖库版本不匹配,引发启动致命错误。
- ATD镜像的特殊限制,导致Orchestrator无法正常运行。
内容的提问来源于stack exchange,提问作者Barry Irvine

