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

使用Android Test Orchestrator执行插桩测试时无法获取异常堆栈跟踪

如何获取Android Test Orchestrator测试崩溃的堆栈跟踪

我来帮你搞定这个问题——用Android Test Orchestrator跑插桩测试时,崩溃后只提示看设备本地的txt文件,Jenkins报告里又没完整堆栈,确实挺头疼的。下面几个实用方案能帮你拿到需要的错误信息:

1. 配置Orchestrator把崩溃日志导出到可拉取的目录

默认情况下崩溃日志存在设备内部,Jenkins访问不到,你可以先指定Orchestrator的输出路径,再把日志拉到Jenkins工作区:

  • 打开模块的build.gradle,添加以下配置,指定设备上的日志存储路径:
android {
    defaultConfig {
        // 其他现有配置...
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        // 新增Orchestrator输出目录参数
        testInstrumentationRunnerArguments += [
            "orchestratorOutputDir": "/sdcard/orchestrator_test_crashes"
        ]
    }
    // 确保已经启用Orchestrator(如果还没配置)
    testOptions {
        execution 'ANDROIDX_TEST_ORCHESTRATOR'
    }
}
  • 在Jenkins的构建步骤里加一段shell命令(Windows环境换成批处理),测试跑完后拉取日志:
# 先在本地建个目录存日志
mkdir -p ${WORKSPACE}/orchestrator_crash_logs
# 从设备把日志拉到Jenkins工作区
adb pull /sdcard/orchestrator_test_crashes ${WORKSPACE}/orchestrator_crash_logs

之后你就能在Jenkins的构建产物里找到带完整堆栈的txt文件了。

2. 让HTML测试报告直接显示堆栈信息

默认的Android测试报告可能只展示简短提示,你可以通过gradle配置让报告包含更多细节,同时确保用最新版本的依赖:

  • 在build.gradle里优化测试报告配置:
android {
    testOptions {
        connectedTests {
            reports {
                html.enabled = true
                xml.enabled = true
                // 可选:如果需要自定义报告样式,可以加这行
                // html.stylesheet file("${rootDir}/custom-report-style.css")
            }
        }
    }
}

另外,把Android Gradle插件和androidx.test:orchestrator依赖升级到最新版本,新版本对报告的错误信息展示做了优化,会自动把堆栈信息整合到HTML报告里。

3. 捕获设备Logcat日志备用

测试崩溃时,Logcat里肯定会记录完整的堆栈,你可以在Jenkins里加步骤实时捕获这些日志:

  • 在运行测试前启动Logcat记录:
# 后台启动Logcat并写入文件
adb logcat -v threadtime > ${WORKSPACE}/full_test_logcat.log &
# 记录Logcat进程ID,方便后续停止
LOGCAT_PID=$!
  • 测试完成后停止Logcat,并单独保存错误日志:
kill $LOGCAT_PID
# 筛选错误级别日志单独保存,方便快速排查
adb logcat -d *:E > ${WORKSPACE}/test_error_summary.log

这样就算Orchestrator的日志拉取有问题,你也能从Logcat文件里找到崩溃的完整堆栈。

小提示

  • 要确保设备有写入指定目录的权限,不然Orchestrator没法生成日志;
  • 在Jenkins里配置构建产物归档,把拉取的日志、Logcat文件都归档起来,方便后续回溯;
  • 如果用的是旧版Orchestrator,优先升级依赖,很多日志相关的小问题在新版本里已经修复了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:51:15