当前位置: 首页 > news >正文

【Android性能优化 | 启动速度01】 Baseline Profiles笔记


1. 这个优化是做什么的

其实就是一种让App启动更快,滑动更流畅。原理是在你的工程里放一个配置文件,列出用户一打开就会用到的代码,系统在安装的时候把这些代码提前编译好。这样用户第一次打开就不用等JIT边跑边编译了。

支持Android7.0及以上,性能大概提升15% - 30%,低端机比较明显。

2. 他的编译方式有什么不同

常见的三种编译方式;

标题功能优点缺点
纯JIT边运行边编译节省空间第一次打开慢
全量AOT安装时编译所有代码启动快安装慢,体积大
Baseline Profile预编译热点代码兼顾了性能,安装效率与体积

3. Baseline实现具体步骤(Benchmark)

1.创建benchmark,模拟启动和生成baseline

可以通过创建benchmark实现。
创建StartupBenchmark类,模拟启动和首次的交互等。

为了对比差异数据,使用CompilationMode.None()表示使用JIT模式编译,用CompilationMode.Partial()表示使用baseline的方式,一会看查看测试数据。

//benchmark -> StartupBenchmark.kts@RunWith(AndroidJUnit4::class)classStartupBenchmark{@get:RulevalbenchmarkRule=MacrobenchmarkRule()@TestfunstartupCompilationNone()=startup(CompilationMode.None())@TestfunstartupCompilationPartial()=startup(CompilationMode.Partial())funstartup(compilationMode:CompilationMode)=benchmarkRule.measureRepeated(packageName="com.jettx.empty",metrics=listOf(StartupTimingMetric()),iterations=5,compilationMode=compilationMode,startupMode=StartupMode.COLD){pressHome()startActivityAndWait()}}
//benchmark -> BaselineProfileGenerator.kts@RunWith(AndroidJUnit4::class)classBaselineProfileGenerator{@get:RulevalbaselineProfileRule=BaselineProfileRule()@Testfungenerate()=baselineProfileRule.collect(packageName="com.jettx.empty",includeInStartupProfile=true){// 定义关键用户旅程pressHome()startActivityAndWait()// 可添加滑动、点击等交互// val recyclerView = device.findObject(By.res("recycler_view"))// recyclerView.waitForExists(5000)// recyclerView.fling(androidx.test.uiautomator.Direction.DOWN)}}

2. 运行生成

./gradlew :benchmark:AndroidTest -P android.testInstrumentationRunnerArguments.androidx.benchmark.enableRules=BaselineProfile

我是在模拟器上运行的,建议在真机上运行,模拟器上运行需要配置支持。

//benchmark -> build.gradle defaultConfig { testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "EMULATOR" }

生成后把
/benchmark/build/outputs/connected_android_test_additional_output/benchmark/connected/Pixel8Api34(AVD) - 14/BaselineProfileGenerator_generate-startup-prof.txt
重命名为baseline-prof.txt放入/app/src/main/目录中。

3. 编译打包App

查看生成后的APK会有合入的baseline

4. 测试运行StartupBenchmark

纯JIT编译和Baseline编译冷启动各5次后的数据:

StartupBenchmark_startupCompilationPartial timeToInitialDisplayMs min 1,694.4, median 1,873.2, max 2,458.6 Traces: Iteration 0 1 2 3 4 Pixel8Api34(AVD) - 14 Tests 1/2 completed. (0 skipped) (0 failed) Timed out waiting for process (com.jettx.empty) to appear on Pixel8Api34 [emulator-5554]. WARNING: Running on Emulator Benchmark is running on an emulator, which is not representative of real user devices. Use a physical device to benchmark. Emulator benchmark improvements might not carry over to a real user's experience (or even regress real device performance). StartupBenchmark_startupCompilationNone timeToInitialDisplayMs min 1,593.7, median 2,602.0, max 51,479.6 Traces: Iteration 0 1 2 3 4 Finished 2 tests on Pixel8Api34(AVD) - 14

从median来看,优化了729ms。

4 .参考文件

https://developer.android.com/topic/performance/baselineprofiles/overview

http://www.jsqmd.com/news/619666/

相关文章:

  • SunnyUI:重新定义C WinForm开发体验的现代化控件库
  • 一篇理解MySQL索引及面试常见问题
  • 计算机中级-数据库系统工程师-操作系统-设备管理
  • AI原生Serverless架构实战指南(SITS2026标准版首发)
  • 两级式电源专题(1)【报告+讲解】单相交直交变频(AC–DC–AC)两级式变换器, 电源频率输出可调
  • 打造沉浸式智能AI问答助手:Vue + UniApp 全端实战(支持 Markdown/公式/多模态交互)晃
  • 大模型中的格式修复、json_repair示例
  • 【信息科学与工程学】【管理科学】第六十篇 企业运营运作表02
  • 从Keep到微信:我是如何用小程序Skyline复刻主流跑步App核心功能的
  • AI原生系统吞吐量骤降之谜(GPU上下文切换+Token缓存伪共享深度拆解)
  • OpenClaw替代方案:Phi-3-mini-128k-instruct与AutoGPT的实测对比
  • 参数化三相电流/电压波形发生器,主要用于**电机控制、变频器测试、堵转实验**等场景。它根据用户设置的“工况参数”,生成带有相位差的三相正弦波信号,并支持“正常工况”和“堵转”模式
  • MogFace-large教程:如何修改webui.py源码以支持视频流实时检测
  • 如何快速掌握专业咖啡烘焙软件:Artisan烘焙数据分析工具完整指南
  • 右值和右值引用学习笔记
  • 别再浪费定时器了!用STM32F4的TIM4输出比较模式,一个定时器搞定四路独立PWM(附步进电机控制代码)
  • 2026年工业仿真动画优质服务商推荐榜:三维动画、机械动画、设备动画、三维仿真动画、工艺原理动画公司,幻像空间领衔精品动画制作 - 海棠依旧大
  • cmake之旅(2)
  • STM32嵌入AI模型实战指南
  • 打造沉浸式智能AI问答助手:Vue + UniApp 全端实战(支持 Markdown/公式/多模态交互)夹
  • 避坑指南:QTableWidget中使用QCheckBox时容易忽略的5个细节问题
  • 避坑指南|2026靠谱GEO服务商推荐 - 品牌测评鉴赏家
  • Spring Boot + Redis 缓存优化方案,解决 Redis 缓存的三大经典问题
  • 学习dp入门
  • 3步打造轻量Windows 11:tiny11builder精简系统实战指南
  • SGLang实战:如何用Python DSL编写带分支的LLM生成任务(附完整代码)
  • 喔去,litellm 竟然被投毒了,赶紧检查你的机器中招了没有侥
  • 物联网云平台工业设备对接远程控制数据采集视频接入开源可二次开发 该物联网云平台使用 Java ...
  • 如何用OnmyojiAutoScript实现阴阳师全自动托管:每天节省2小时游戏时间的完整指南
  • 互联网企业项目管理的核心挑战