You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击操作后应用需重启,但Espresso测试instrumentation进程崩溃求助

解决「Test instrumentation process crashed」问题的方案

可能的原因与修复步骤

1. 统一测试依赖版本,移除冲突依赖

你的依赖中混合了新旧版本的测试库,同时存在beta版与稳定版组件,容易引发兼容性冲突:

  • 同时引入了com.android.support.test:rules:1.0.2和androidx.test:rules:1.4.1-beta01,旧版support库与AndroidX库会产生冲突
  • espresso-intents:3.4.0与其他Espresso组件版本不匹配

修改后的依赖配置如下:

dependencies {
    // Espresso Dependencies - 统一使用匹配的稳定版本
    androidTestImplementation 'androidx.test:core:1.5.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test:runner:1.5.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation 'androidx.test:rules:1.5.0'
    androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.5'
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.1'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.5.1'
    androidTestImplementation 'com.jakewharton.espresso:okhttp3-idling-resource:1.0.0'
    androidTestImplementation('com.schibsted.spain:barista:3.6.0') {
        exclude group: 'org.jetbrains.kotlin'
    }

    // Orchestrator 版本与runner保持一致
    androidTestUtil 'androidx.test:orchestrator:1.5.2'
}

同时移除旧的com.android.support.test:rules:1.0.2依赖,确保所有AndroidX测试库版本统一。

2. 调整Orchestrator与重启场景的适配

当测试涉及应用硬重启时,Orchestrator的clearPackageData配置可能与重启逻辑冲突,导致instrumentation进程无法正常恢复:

  • 暂时关闭clearPackageData配置,或在测试用例中手动处理数据清理:
    defaultConfig {
        // 注释或移除该行
        // testInstrumentationRunnerArguments clearPackageData:'true'
    }
    
  • 针对重启场景的测试,单独禁用Orchestrator,改用@RunWith(AndroidJUnit4.class)执行,因为Orchestrator在应用进程重启后可能无法重新建立连接。

3. 优化测试用例的重启等待逻辑

应用硬重启后,instrumentation进程需要时间重新附着到新启动的应用进程,直接执行断言会导致进程崩溃,需添加显式等待:

// 点击按钮触发重启
onView(withId(R.id.restart_btn)).perform(click())

// 使用UiAutomator等待应用进程重启完成
val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
device.wait(Until.hasObject(By.pkg("your.app.package").depth(0)), 10000)

// 执行后续断言,例如检查启动页是否显示
onView(withId(R.id.splash_layout)).check(matches(isDisplayed()))

4. 适配API 31+的进程限制

API 31及以上系统对进程管理更严格,需确保测试应用拥有必要权限:

  • 在AndroidManifest.xml的instrumentation标签中添加权限配置:
    <instrumentation
        android:name="androidx.test.runner.AndroidJUnitRunner"
        android:targetPackage="your.app.package"
        android:permission="android.permission.INTERNET"
        android:clearUserData="false"/>
    
  • 关闭测试设备的「优化电池使用」功能,避免系统主动杀死instrumentation进程。

内容的提问来源于stack exchange,提问作者Mohamed Gaber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 22:21:40