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

升级Android Studio Giraffe后Instrumentation Tests异常:显示取消却实际运行

Android Studio Giraffe 下 Instrumentation Tests 显示“已取消”但实际运行的解决方案

问题复现

  • 升级Android Studio到Giraffe版本后,运行Instrumentation Tests时,AS立即弹出“测试已取消”警告,原有的adb“Connected”状态不再显示
  • 测试实际会在设备上正常执行,仅AS端显示异常
  • 终端执行adb shell am instrument -w -m --no-window-animation -e debug false -e class 'com.**sample.tests.PoiInteractionTests#test_rich_poi_213' com.**demo.mock.prod.test/android.support.test.runner.AndroidJUnitRunner命令可成功运行测试
  • 已尝试升级Gradle插件至7.4.2、Invalidate Caches并重启AS,问题未解决

可行解决方案

1. 调整测试运行配置的超时设置

打开AS的测试运行配置:

  • 点击工具栏的运行配置下拉框 → 选择Edit Configurations
  • 在左侧列表找到对应的Android Instrumented Tests配置
  • 切换到General标签页,找到Test launch timeout选项,将默认的30秒延长至120秒或更久
  • 保存配置后重新运行测试

2. 启用Android Test Orchestrator

在模块的build.gradle(或build.gradle.kts)中添加配置:

android {
  defaultConfig {
    testInstrumentationRunnerArguments clearPackageData: 'true'
  }
  testOptions {
    execution 'ANDROIDX_TEST_ORCHESTRATOR'
  }
}

dependencies {
  androidTestImplementation 'androidx.test:orchestrator:1.4.2'
}

同步Gradle后重新运行测试,Orchestrator会隔离每个测试的运行环境,可能解决AS与设备的通信异常问题

3. 切换测试运行器版本

将原有的android.support.test.runner.AndroidJUnitRunner替换为AndroidX版本的运行器:

  • 修改build.gradle中的testInstrumentationRunner:
android {
  defaultConfig {
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }
}

同时确保依赖中引入了AndroidX测试库:

dependencies {
  androidTestImplementation 'androidx.test.ext:junit:1.1.5'
  androidTestImplementation 'androidx.test:runner:1.5.2'
}

4. 重置ADB连接

  • 在AS的Terminal执行:
adb kill-server
adb start-server
  • 同时在AS的Device File Explorer右上角点击Refresh按钮,确认设备连接状态

5. 降级Android Studio的测试工具组件

打开AS的Settings → Appearance & Behavior → System Settings → Updates → Configure Updates,查看已安装的组件,找到Android Testing Support Library,尝试降级到与Giraffe兼容的旧版本(比如对应Gradle 7.4的版本)

临时Workaround

如果以上方案均无效,可暂时使用终端命令运行测试,同时等待Android Studio后续补丁修复该兼容性问题

内容的提问来源于stack exchange,提问作者Elican Doenyas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 14:25:04