Hiệu suất của Jetpack Compose
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Jetpack Compose mang lại hiệu suất vượt trội ngay từ đầu. Định cấu hình ứng dụng bằng các phương pháp hay nhất để tránh các lỗi phổ biến và tối ưu hoá hiệu suất của ứng dụng Compose.
Khái niệm chính
Dưới đây là một số khái niệm chính đối với hiệu suất trong Compose:
- Giai đoạn: Việc hiểu rõ các giai đoạn thành phần, bố cục và vẽ đóng vai trò quan trọng trong việc tối ưu hoá cách Compose cập nhật giao diện người dùng.
- Hồ sơ cơ sở: Các hồ sơ này biên dịch trước mã thiết yếu, giúp khởi chạy ứng dụng nhanh hơn và tương tác mượt mà hơn.
- Độ ổn định: Tăng độ ổn định của ứng dụng để bỏ qua các thành phần kết hợp lại không cần thiết một cách hiệu quả hơn, cải thiện hiệu suất.
Định cấu hình đúng cách cho ứng dụng của bạn
Nếu ứng dụng của bạn hoạt động kém, có thể có sự cố về cấu hình. Bước đầu tiên bạn nên làm là kiểm tra các tuỳ chọn cấu hình sau:
- Xây dựng ở Chế độ phát hành với R8: Thử chạy ứng dụng của bạn ở chế độ phát hành. Chế độ gỡ lỗi rất hữu ích để phát hiện nhiều vấn đề, nhưng cũng gây ra chi phí hiệu suất và có thể khó phát hiện các vấn đề khác. Bạn cũng nên bật tính năng tối ưu hoá và rút gọn bằng trình biên dịch R8 để đảm bảo bản phát hành hoạt động hiệu quả và hiệu quả.
- Sử dụng Hồ sơ cơ sở: Hồ sơ cơ sở cải thiện hiệu suất bằng cách biên dịch mã cho các hành trình trọng yếu của người dùng. Compose bao gồm một hồ sơ mặc định, nhưng tốt nhất bạn cũng nên tạo một hồ sơ dành riêng cho ứng dụng. Tìm hiểu thêm về Hồ sơ cơ sở trong tài liệu chung về hiệu suất của Android
Làm quen với bộ công cụ có sẵn để giúp bạn đo lường và phân tích hiệu suất của ứng dụng Compose.
Các phương pháp hay nhất
Khi phát triển ứng dụng bằng Compose, hãy ghi nhớ các phương pháp hay nhất sau đây:
Để biết thêm thông tin, hãy xem hướng dẫn về các phương pháp hay nhất.
Số lượt xem
Nếu bạn đang xử lý các thành phần hiển thị thay vì Compose, hãy xem hướng dẫn dành riêng cho việc Cải thiện hiệu suất bố cục.
Tài nguyên khác
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[],[],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."]]