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

Android执行MacroBenchmark时特定Activity无法启动的问题排查

排查Macrobenchmark中Activity启动失败的问题

问题描述

编写了用于对比MVVM重构前后性能的Macrobenchmark测试,先针对特定Activity做启动基准测试,后续计划添加交互操作测试FrameTimingMetric效果。测试代码如下:

@get:Rule
val benchmarkRule = MacrobenchmarkRule()

@Test
fun startupEntryActivity() = benchmarkRule.measureRepeated(
    packageName = "io.demo",
    metrics = listOf(FrameTimingMetric()),
    iterations = 5,
    startupMode = StartupMode.COLD,
) {
    pressHome()

    val intent = Intent()
    intent.setPackage("io.demo")
    intent.action = "io.demo.views.splash.SplashIntroSelectionActivity"
    startActivityAndWait(intent)
}

运行时出现错误:

Error: Activity not started, unable to resolve Intent { act=io.demo.views.splash.SplashIntroSelectionActivity flg=0x10008000 pkg=io.demo }

排查步骤

1. 检查Activity的Manifest配置

确保目标Activity在AndroidManifest.xml中正确配置,尤其注意以下几点:

<activity
    android:name="io.demo.views.splash.SplashIntroSelectionActivity"
    android:exported="true"> <!-- 必须设为true,外部进程(包括Macrobenchmark)才能启动 -->
    <intent-filter>
        <action android:name="io.demo.views.splash.SplashIntroSelectionActivity" />
        <category android:name="android.intent.category.DEFAULT" /> <!-- 必须添加该category -->
    </intent-filter>
</activity>
  • android:exported不能省略或设为false,否则跨进程启动会被系统拦截
  • intent-filter中的action要和代码里的设置完全一致,大小写敏感
  • 必须包含DEFAULT category,否则仅通过action无法匹配到Activity

2. 简化Intent构造逻辑

跳过action匹配,直接通过类名构造Intent,避免配置依赖:

val intent = Intent()
intent.setClassName("io.demo", "io.demo.views.splash.SplashIntroSelectionActivity")
startActivityAndWait(intent)

这种方式不需要依赖Intent Filter,更适合基准测试的稳定启动需求。

3. 验证目标应用状态

  • 确认测试设备/模拟器上已安装最新版本的io.demo应用
  • 用命令验证包存在:adb shell pm list packages | grep io.demo
  • 检查安装的变体是否正确(如debug、benchmark专用构建变体)

4. 测试Intent的有效性

用adb命令手动启动Activity,排查是否是应用本身的问题:

# 通过类名启动
adb shell am start -n io.demo/io.demo.views.splash.SplashIntroSelectionActivity
# 通过action启动(对应代码中的方式)
adb shell am start -a io.demo.views.splash.SplashIntroSelectionActivity -p io.demo

如果adb命令也无法启动,说明是应用配置问题;如果adb能启动,再回头检查测试代码的Intent构造细节。

5. 检查Macrobenchmark模块配置

确保测试模块的build.gradle配置正确:

android {
    defaultConfig {
        targetSdk = 34
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            isMinifyEnabled = false
        }
    }
}

dependencies {
    implementation "androidx.benchmark:benchmark-macro-junit4:1.2.2"
    implementation "androidx.test.ext:junit:1.1.5"
    implementation "androidx.test.uiautomator:uiautomator:2.2.0"
}

运行测试时要选择对应的构建变体(如benchmarkRelease)。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 10:55:20