三星A15设备Android Espresso测试无法全部执行问题求助
问题概述
我的Android应用包含225个Espresso测试用例,此前在Android 7至14版本的三星手机和平板上均可完整执行。但在全新的三星A15设备上,测试无法完成全部用例——已执行的测试均通过,但会随机中断,大多在执行70至120个用例时停止。
错误日志
Logcat输出
2024-08-26 07:11:07.999 6224-6244 ShellCommandClient androidx.test.orchestrator E Couldn't find a published binder 2024-08-26 07:11:08.015 6224-6244 ShellExecutorImpl androidx.test.orchestrator E ShellCommandClient not connected. Is ShellCommandExecutor service started? androidx.test.services.shellexecutor.ClientNotConnected: Couldn't find a binder published by androidx.test.services. It is very likely that androidx.test.services was killed, which is usually due to low memory conditions. Consider switching to a device with more memory.
IDE构建错误
> Task :app:connectedDevelopmentDebugAndroidTest Test run failed to complete. Instrumentation run failed due to Process crashed. > Task :app:connectedDevelopmentDebugAndroidTest FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:connectedDevelopmentDebugAndroidTest'.
已排查的无效操作
- 重启设备、重新构建项目、清理IDE缓存
- 删除设备上的
androidx.test.orchestrator和TestServices应用 - 确认三星A15与同内存(4GB)、同系统版本(Android 14)的三星A13开发者设置完全一致,且A13可正常执行所有测试,内存仅1.5GB的A3也无异常,排除内存不足问题
原因分析
- 三星A15定制系统的进程查杀策略差异:尽管同为Android 14,三星对A15的后台进程管理做了更严格的定制,
androidx.test.services进程被系统判定为低优先级后台进程,在测试执行过程中被强制回收,而非内存不足导致。 - AndroidX测试库与A15系统的兼容性问题:较新版本的
androidx.test.orchestrator或test-services库可能与A15的OEM优化存在适配冲突,导致进程绑定失败。
解决方案
- 禁用测试编排器的进程隔离:修改项目的
build.gradle(或build.gradle.kts)配置,关闭编排器的独立进程模式,避免TestServices进程被单独查杀:android { testOptions { execution 'ANDROIDX_TEST_ORCHESTRATOR' orchestrator { enableProcessIsolation false } } } - 将测试相关进程加入电池优化白名单:
- 进入三星A15的「设置」→「电池和设备维护」→「电池」→「后台使用限制」
- 找到
androidx.test.orchestrator和你的应用测试进程(包名+.test后缀),设置为「允许后台活动」
- 调整AndroidX测试库版本:尝试降级到稳定兼容的版本,例如:
androidTestImplementation 'androidx.test:orchestrator:1.4.2' androidTestUtil 'androidx.test.services:test-services:1.4.2' - 给测试添加执行间隔:在测试基类的
@Before方法中添加短暂延迟,降低测试执行的密集度,避免触发系统的进程查杀阈值:@Before fun setup() { Thread.sleep(300) // 可根据实际情况调整时长 } - 强制TestServices以前台服务运行:在测试包的
AndroidManifest.xml中为TestServices的服务组件添加前台服务类型:<service android:name="androidx.test.services.shellexecutor.ShellCommandExecutorService" android:foregroundServiceType="dataSync" />
内容的提问来源于stack exchange,提问作者Laurent D.
相关产品推荐
相关产品推荐

