Android应用添加Baseline Profile时遇DROP_SHADER_CACHE广播未接收错误
解决Baseline Profile基准测试中DROP_SHADER_CACHE广播未接收的问题
我正尝试给Android应用添加Baseline Profile,参考官方文档和相关视频创建了基准测试模块及对应配置。试过网上多种解决方案后,当前使用androidx.profileinstaller:1.4.1版本,在安装了基准测试构建的设备上运行测试时,出现如下错误:
java.lang.IllegalStateException: The DROP_SHADER_CACHE broadcast was not received. This most likely means that the `androidx.profileinstaller` library used by the target apk is old. Please use `1.3.0-alpha02` or newer. For more information refer to the release notes at https://developer.android.com/jetpack/androidx/releases/profileinstaller. at androidx.benchmark.macro.MacrobenchmarkScope.dropShaderCache(MacrobenchmarkScope.kt:300) at androidx.benchmark.macro.MacrobenchmarkKt$macrobenchmarkWithStartupMode$1.invoke(Macrobenchmark.kt:454) at androidx.benchmark.macro.MacrobenchmarkKt$macrobenchmarkWithStartupMode$1.invoke(Macrobenchmark.kt:436) at androidx.benchmark.macro.MacrobenchmarkKt$macrobenchmark$measurements$1.invoke(Macrobenchmark.kt:265) at androidx.benchmark.macro.MacrobenchmarkKt$macrobenchmark$measurements$1.invoke(Macrobenchmark.kt:255) at androidx.benchmark.perfetto.PerfettoTraceProcessor$Companion.runServer(PerfettoTraceProcessor.kt:105) at androidx.benchmark.macro.MacrobenchmarkKt.macrobenchmark(Macrobenchmark.kt:255) at androidx.benchmark.macro.MacrobenchmarkKt.macrobenchmarkWithStartupMode(Macrobenchmark.kt:436) at androidx.benchmark.macro.junit4.MacrobenchmarkRule.measureRepeated(MacrobenchmarkRule.kt:107) at androidx.benchmark.macro.junit4.MacrobenchmarkRule.measureRepeated$default(MacrobenchmarkRule.kt:97) at StartupBenchmark.startup(StartupBenchmark.kt:15)
解决方案
- 确认主应用依赖的profileinstaller版本:错误提示的是目标应用APK(即你的主应用)里的profileinstaller版本过旧,而非基准测试模块的。检查主应用的
build.gradle文件,确保主应用和基准测试模块使用的是同一个1.4.1版本的profileinstaller,避免主应用还停留在旧版本。 - 清理重建项目:执行
./gradlew clean命令清理构建缓存,然后重新构建主应用和基准测试模块,确保新的依赖版本被正确打包到APK中。 - 检查设备系统版本:Baseline Profile对低版本Android支持有限,建议使用Android 10及以上版本的设备进行测试,避免旧系统的广播机制问题。
- 临时跳过Shader缓存清理:如果上述方法都无效,可以在基准测试代码中禁用Shader缓存清理,修改
measureRepeated的调用参数:
benchmarkRule.measureRepeated( packageName = "你的应用包名", metrics = listOf(StartupTimingMetric()), iterations = 5, startupMode = StartupMode.COLD, dropShaderCache = false // 跳过Shader缓存清理 ) { // 你的测试逻辑 }
- 同步Benchmark库版本:确保基准测试模块使用的
androidx.benchmark:benchmark-macro-junit4等相关库是最新稳定版,和profileinstaller版本保持匹配,避免版本不兼容问题。
内容的提问来源于stack exchange,提问作者Divy
相关产品推荐
相关产品推荐

