添加EMULATOR,DEBUGGABLE参数仍报错,如何在Android模拟器运行MacroBenchmark?
在Android模拟器上运行MacroBenchmark启动时间测试的解决办法
你遇到的问题是虽然配置了suppressErrors参数,但仍被阻止在模拟器上运行测试,可通过以下两种方式解决:
1. 确认build配置的正确性
检查你的Benchmark模块构建脚本中,参数是否配置在正确的位置:
- Groovy(build.gradle)
android { defaultConfig { testInstrumentationRunner "androidx.benchmark.junit4.AndroidBenchmarkRunner" testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "EMULATOR,DEBUGGABLE" } }
- Kotlin DSL(build.gradle.kts)
android { defaultConfig { testInstrumentationRunner = "androidx.benchmark.junit4.AndroidBenchmarkRunner" testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "EMULATOR,DEBUGGABLE" } }
确保参数是配置在Benchmark模块的defaultConfig下,而非主App模块。
2. 在测试类中添加注解抑制错误
直接在你的MacroBenchmark测试类上使用@SuppressBenchmarkErrors注解,显式指定忽略的错误类型:
import androidx.benchmark.annotation.SuppressBenchmarkErrors import androidx.benchmark.macro.junit4.MacrobenchmarkRule import org.junit.Test @SuppressBenchmarkErrors("EMULATOR", "DEBUGGABLE") class AppStartupBenchmark { @get:Rule val benchmarkRule = MacrobenchmarkRule() @Test fun coldStartup() { // 你的启动测试逻辑 } }
注意事项
- 建议更新
androidx.benchmark:benchmark-macro-junit4到最新稳定版本,旧版本可能存在参数解析问题 - 官方明确不推荐在模拟器上运行基准测试,因为模拟器性能与物理设备差异极大,测试结果仅适合验证测试逻辑,不能作为生产环境的性能参考
内容的提问来源于stack exchange,提问作者Naveen
相关产品推荐
相关产品推荐

