SuperAPI AI 工具集

generating-baseline-profiles

通用与其它

查看项目

更新于 10-16 00:20

技能介绍

用途:为 Jetpack Compose 应用生成并测量 Baseline Profile,把热点 Compose 代码路径的 AOT 编译提示列表打包进 APK,让 ART 在安装时预编译,而不是首次运行时依赖 JIT。资料称在被剖析的路径上冷启动约快 30%、首次滚动约顺滑 40%。

主要能力:用 androidx.benchmark:benchmark-macro-junit4 的 BaselineProfileRule 生成 profile,用 MacrobenchmarkRule 测量;生成与测量是两个独立的 @Test 文件,放在 AGP 8.2+ 通过 New Module → Baseline Profile Generator 脚手架创建的 :baselineprofile 模块中。

使用方式:在 :baselineprofile/build.gradle.kts 应用 com.android.test、org.jetbrains.kotlin.android、androidx.baselineprofile 插件并设置 targetProjectPath = ":app";在 :app/build.gradle.kts 应用 androidx.baselineprofile 并添加 baselineProfile(project(":baselineprofile"))。生成器须覆盖启动加至少一次滚动,用 Modifier.testTag 配合 By.res 定位,并设置 setGestureMargin(device.displayWidth / 5)。运行 ./gradlew :app:generateBaselineProfile(指定变体用 :app:generateReleaseBaselineProfile),输出到 app/src/<variant>/generated/baselineProfiles/baseline-prof.txt。测量用 CompilationMode.Partial(BaselineProfileMode.Require),启动用 StartupTimingMetric,滚动用 FrameTimingMetric,在真机或 aosp_cf_x86_64_phone-userdebug 上运行 ./gradlew :baselineprofile:connectedReleaseAndroidTest。

适用场景:冷启动慢、LazyColumn/LazyVerticalGrid 首次滚动卡顿、发布前建立性能基线、验证稳定性或强跳过修复是否真正改善数字。

限制:早期原型阶段无稳定用户旅程时不适用;每 composable 重组计数、CI 稳定性门禁、debug 构建测量均不属于本技能。需 AGP 8.2+、release 变体(isMinifyEnabled、isShrinkResources、proguard-android-optimize.txt)、清单中 <profileable android:shell="true"/>。必须验证 APK 内含 assets/dexopt/baseline.prof 与 baseline.profm,否则收益为零;须报告中位数而非均值,启动建议 ≥10 次迭代、帧计时 ≥5 次。缺失事实暂未提供。