Jetpack Compose 性能
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Jetpack Compose 开箱即可提供卓越性能。请运用最佳实践来配置应用,避免常见错误,并优化 Compose 应用的性能。
主要概念
以下是 Compose 中有关性能的一些关键概念:
- 阶段:了解组合、布局和绘制阶段对于优化 Compose 更新界面的方式至关重要。
- 基准配置文件:这些配置文件会对基本代码进行预编译,从而加快应用启动速度并实现更顺畅的互动。
- 稳定性:提高应用的稳定性,以更高效地跳过不必要的重组,从而提升性能。
正确配置您的应用
如果您的应用性能不佳,则可能存在配置问题。建议您首先检查以下配置选项:
熟悉一套可用于衡量和分析 Compose 应用性能的工具。
最佳实践
使用 Compose 开发应用时,请牢记以下最佳实践:
- 避免开销高的计算:使用
remember
来缓存开销大的计算的结果。
- 帮助延迟布局:使用
key
参数为延迟布局提供稳定的键,以最大限度地减少不必要的重组。
- 限制不必要的重组:在状态快速变化时使用
derivedStateOf
限制重组。
- 推迟状态读取:通过将状态读取封装在 lambda 函数中,尽可能延后读取状态。
- 使用 lambda 修饰符更改状态:对于经常更改的状态变量,请使用基于 lambda 的修饰符,如
Modifier.offset { ... }
。
- 避免向后写入:切勿向可组合项中已读取的状态写入数据。
如需了解详情,请参阅最佳实践指南。
次观看
如果您使用的是 View 而非 Compose,请参阅专门的改进布局性能指南。
其他资源
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# Jetpack Compose Performance\n\nJetpack Compose delivers excellent performance out of the box. Configure your\napp using best practices to avoid common pitfalls and optimize your Compose\napplication's performance.\n\nKey concepts\n------------\n\nThese are some of the key concepts for performance in Compose:\n\n- **[Phases](/develop/ui/compose/performance/phases):** Understanding the composition, layout, and drawing phases is crucial for optimizing how Compose updates your UI.\n- **[Baseline Profiles](/develop/ui/compose/performance/baseline-profiles):** These profiles precompile essential code, leading to faster app launches and smoother interactions.\n- **[Stability](/develop/ui/compose/performance/stability):** Increase the stability of your app to more efficiently skip unnecessary recompositions, improving performance.\n\nProperly configure your app\n---------------------------\n\nIf your app is performing poorly, there might be a configuration problem. A good\nfirst step is to check the following configuration options:\n\n- **Build in Release Mode with R8:** Try running your app in [release\n mode](/studio/run#changing-variant). Debug mode is useful for spotting many problems, but it imposes a performance cost and can make it hard to spot other issues. You should also [enable optimizing and shrinking](/build/shrink-code#enable) with the R8 compiler to ensure a performant and efficient release build.\n- **Use Baseline Profiles:** Baseline Profiles improve performance by precompiling code for critical user journeys. Compose includes a default profile, but ideally, you should create an app-specific one as well. [Learn\n more about Baseline Profiles in the general Android performance docs](/topic/performance/baselineprofiles/overview)\n\nTools\n-----\n\nFamiliarize yourself with the suite of [tools](/develop/ui/compose/performance/tooling) available to help you measure\nand analyze your Compose app's performance.\n\nBest Practices\n--------------\n\nWhen developing your app with Compose, keep these best practices in mind:\n\n- **[Avoid expensive calculations](/develop/ui/compose/performance/bestpractices#use-remember):** Use `remember` to cache the results of expensive calculations.\n- **[Help lazy layouts](/develop/ui/compose/performance/bestpractices#use-lazylist-keys):** Provide stable keys to lazy layouts using the `key` parameter to minimize unnecessary recompositions.\n- **[Limit unnecessary recompositions](/develop/ui/compose/performance/bestpractices#use-derivedstateof):** Use `derivedStateOf` to limit recompositions when rapidly changing state.\n- **[Defer state reads](/develop/ui/compose/performance/bestpractices#defer-reads):** Defer state reads as long as possible by wrapping them in lambda functions.\n- **[Use lambda modifiers for changing state](/develop/ui/compose/performance/bestpractices#defer-reads):** Use lambda-based modifiers like `Modifier.offset { ... }` for frequently changing state variables.\n- **[Avoid backwards writes](/develop/ui/compose/performance/bestpractices#avoid-backwards):** Never write to state that has already been read in a composable.\n\nFor more details, see the [best practices](/develop/ui/compose/performance/bestpractices) guide.\n\nViews\n-----\n\nIf you are working with views instead of Compose, see the dedicated [Improve\nlayout performance](/develop/ui/views/layout/improving-layouts) guide.\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."]]