使用基准配置文件
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
基准配置文件可以避免对包含的代码路径执行解译和即时 (JIT) 编译步骤,从而让代码执行速度从首次启动开始提高约 30%。通过在应用或库中分发基准配置文件,您可以让 Android 运行时 (ART) 通过预先 (AOT) 编译来优化包含的代码路径,从而针对每次新应用安装和每个应用更新提升性能。这种配置文件引导的优化 (PGO) 可让应用优化启动、减少互动卡顿,并提高整体的运行时性能(从首次启动开始适用于最终用户)。
Compose 性能注意事项
Compose 以库的形式分发,而不是作为 Android 平台的一部分进行分发。此方法可让 Compose 团队频繁更新 Compose,并支持各种 Android 版本。不过,以库形式分发 Compose 会产生费用。
Android 平台代码已完成编译并安装到设备上。不过,在应用启动时需要加载库,并在需要时对 JIT 进行解释。这可能会导致应用在启动时以及首次使用库功能时变慢。
基准配置文件的优势
您可以通过定义基准配置文件来提高性能。这些配置文件定义了关键用户历程所需的类和方法,并与应用的 APK 或 AAB 文件一起分发。在应用安装期间,ART 会编译此关键代码 AOT,以便在应用启动时可供使用。
良好的基准配置文件定义并非总是很简单,因此,Compose 默认附带了一个。您无需执行任何操作即可看到这项优势。不过,Compose 附带的基准配置文件仅包含针对 Compose 库中代码的优化。
Macrobenchmark
为了获得最佳优化效果,请为使用 Macrobenchmark 的应用创建基准配置文件,以涵盖关键用户历程。定义自己的配置文件时,您必须测试该配置文件以验证它是否发挥作用。为实现此目的,一种好方法是为您的应用编写 Macrobenchmark 测试,并在编写和修改基准配置文件时查看测试结果。
如需查看如何为 Compose 界面编写 Macrobenchmark 测试的示例,请参阅 Macrobenchmark Compose 示例。
其他资源
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Use a baseline profile\n\n[Baseline Profiles](/baseline-profiles) improve code execution speed by about 30% from the first\nlaunch by avoiding interpretation and just-in-time (JIT) compilation steps for\nincluded code paths. By shipping a Baseline Profile in an app or library, you\nenable Android Runtime (ART) to optimize included code paths through\nahead-of-time (AOT) compilation, providing performance enhancements for every\nnew app install and every app update. This profile-guided optimization (PGO)\nlets apps optimize startup, reduce interaction jank, and improve overall runtime\nperformance from the first launch for end users.\n\nCompose performance considerations\n----------------------------------\n\nCompose is distributed as a library instead of as part of the Android platform.\nThis approach lets the Compose team update Compose frequently and support a wide\nrange of Android versions. However, distributing Compose as a library imposes a\ncost.\n\nAndroid platform code is already compiled and installed on the device.\nLibraries, however, need to be loaded when the app launches and interpreted JIT\nwhen needed. This can slow the app on startup and when it uses a library feature\nfor the first time.\n\nBenefits of baseline profiles\n-----------------------------\n\nYou can improve performance by defining [Baseline Profiles](/topic/performance/baselineprofiles/overview). These profiles\ndefine classes and methods needed on critical user journeys and are distributed\nwith your app's APK or AAB. During app installation, ART compiles this critical\ncode AOT so that it's ready for use when the app launches.\n\nA good Baseline Profile definition is not always straightforward, and because\nof this, Compose ships with one by default. You might not have to do any work to\nsee this benefit. However, the Baseline Profile that ships with Compose only\ncontains optimizations for the code within the Compose library.\n\n### Macrobenchmark\n\nTo get the best optimization, [create a Baseline Profile](/topic/performance/baselineprofiles/create-baselineprofile)\nfor your app that uses [Macrobenchmark](/studio/profile/macrobenchmark-overview) to cover critical user journeys. When\nyou define your own profile, you must test the profile to verify that it's\nhelping. A good way to do that is to write [Macrobenchmark](/studio/profile/macrobenchmark-overview) tests for your\napp and check the test results as you write and revise your Baseline Profile.\n\nFor an example of how to write Macrobenchmark tests for your Compose UI, see the\n[Macrobenchmark Compose sample](https://github.com/android/performance-samples/tree/main/MacrobenchmarkSample).\n\nAdditional Resources\n--------------------\n\n- **[App performance guide](/topic/performance/overview)**: Discover best practices, libraries, and tools to improve performance on Android.\n- **[Inspect Performance](/topic/performance/inspecting-overview):** Inspect app performance.\n- **[Benchmarking](/topic/performance/benchmarking/benchmarking-overview):** Benchmark app performance.\n- **[App startup](/topic/performance/appstartup/analysis-optimization):** Optimize app startup.\n- **[Baseline profiles](/baseline-profiles):** Understand baseline profiles."]]