运行启动基线基准测试时遇ProfileInstaller广播接收失败问题求助
解决Baseline Profile基准测试中"The baseline profile install broadcast was not received"错误
运行启动基线基准测试时抛出如下异常:
java.lang.RuntimeException: The baseline profile install broadcast was not received. This most likely means that the profileinstaller library is missing from the target apk. at androidx.benchmark.macro.CompilationMode$Partial.compileImpl$benchmark_macro_release(CompilationMode.kt:311) at androidx.benchmark.macro.CompilationMode.resetAndCompile$benchmark_macro_release(CompilationMode.kt:115) at androidx.benchmark.macro.CompilationMode.resetAndCompile$benchmark_macro_release$default(CompilationMode.kt:77) at androidx.benchmark.macro.MacrobenchmarkKt.macrobenchmark(Macrobenchmark.kt:186) at androidx.benchmark.macro.MacrobenchmarkKt.macrobenchmarkWithStartupMode(Macrobenchmark.kt:388) at androidx.benchmark.macro.junit4.MacrobenchmarkRule.measureRepeated(MacrobenchmarkRule.kt:107) at com.example.baselineprofile.StartupBenchmarks.benchmark(StartupBenchmarks.kt:49) at com.example.baselineprofile.StartupBenchmarks.startupCompilationBaselineProfiles(StartupBenchmarks.kt:46)
已在主Android模块的build.gradle中添加依赖:
implementation("androidx.profileinstaller:profileinstaller:1.3.1") baselineProfile(project(":baselineprofile"))
测试类代码如下:
@RunWith(AndroidJUnit4::class) @LargeTest class StartupBenchmarks { @get:Rule val rule = MacrobenchmarkRule() @Test fun startupCompilationNone() = benchmark(CompilationMode.None()) @Test fun startupCompilationBaselineProfiles() = benchmark(CompilationMode.Partial(BaselineProfileMode.Require)) private fun benchmark(compilationMode: CompilationMode) { rule.measureRepeated( packageName = "com.example.android", metrics = listOf(StartupTimingMetric()), compilationMode = compilationMode, startupMode = StartupMode.COLD, iterations = 10, setupBlock = { pressHome() }, measureBlock = { startActivityAndWait() } ) }}
以下是具体解决思路:
检查依赖是否真正打入目标APK
用Android Studio的Build > Analyze APK功能打开测试用APK,查看是否存在androidx.profileinstaller相关类。如果缺失,尝试:- 同步Gradle项目(点击Sync Now按钮)
- 清理并重新构建项目(
Build > Clean Project→Build > Rebuild Project) - 确认主模块的
build.gradle中没有排除该依赖的配置(比如exclude group: 'androidx.profileinstaller')
确认基准测试模块配置正确性
在:baselineprofile模块的build.gradle中,确保配置正确:plugins { id 'com.android.test' id 'org.jetbrains.kotlin.android' id 'androidx.baselineprofile' } dependencies { implementation 'androidx.test.ext:junit:1.1.5' implementation 'androidx.test.espresso:espresso-core:3.5.1' implementation 'androidx.benchmark:benchmark-macro-junit4:1.2.0' } android { targetProjectPath ":app" // 替换为你的主应用模块名称 }验证Baseline Profile生成与打包逻辑
- 主模块
build.gradle中需启用基线profile构建:android { buildTypes { release { baselineProfiles true } } } - 确认基准测试模块生成的
baseline-prof.txt文件存在于src/main/generated/baselineProfiles目录下。
- 主模块
检查设备兼容性
确保测试设备运行Android 11(API 30)及以上版本,低版本系统不兼容Baseline Profile的安装广播机制。确认测试变体正确性
运行基准测试时,选择包含基线profile的构建变体(如release),避免使用debug变体(debug模式可能禁用profile安装逻辑)。
内容的提问来源于stack exchange,提问作者Sagar Khurana
相关产品推荐
相关产品推荐

