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.
Để mang lại trải nghiệm tốt nhất cho người dùng, bạn nên tối ưu hoá ứng dụng để ứng dụng có kích thước nhỏ và chạy nhanh nhất có thể. Trình tối ưu hoá ứng dụng của chúng tôi (gọi là R8) sẽ tinh giản ứng dụng của bạn bằng cách xoá mã và tài nguyên không dùng đến, viết lại mã để tối ưu hoá hiệu suất thời gian chạy, v.v. Đối với người dùng, điều này có nghĩa là:
Để bật tính năng tối ưu hoá ứng dụng, hãy đặt isMinifyEnabled = true (để tối ưu hoá mã) và isShrinkResources = true (để tối ưu hoá tài nguyên) trong tập lệnh bản dựng cấp ứng dụng của bản phát hành như minh hoạ trong mã sau. Bạn nên luôn bật cả hai chế độ cài đặt này. Bạn cũng nên chỉ bật tính năng tối ưu hoá ứng dụng trong phiên bản cuối cùng của ứng dụng mà bạn kiểm thử trước khi xuất bản (thường là bản phát hành), vì các hoạt động tối ưu hoá sẽ làm tăng thời gian tạo của dự án và có thể khiến việc gỡ lỗi trở nên khó khăn hơn do cách tính năng này sửa đổi mã.
Ngoài ra, hãy xác minh rằng R8 sử dụng đầy đủ các khả năng tối ưu hoá bằng cách xoá dòng này khỏi tệp gradle.properties của dự án (nếu có):
android.enableR8.fullMode=false# Remove this line from your codebase.
Xin lưu ý rằng việc bật tính năng tối ưu hoá ứng dụng sẽ khiến bạn khó hiểu dấu vết ngăn xếp, đặc biệt là nếu R8 đổi tên lớp hoặc tên phương thức. Để nhận được dấu vết ngăn xếp tương ứng chính xác với mã nguồn của bạn, hãy xem phần Khôi phục dấu vết ngăn xếp ban đầu.
Nếu bật R8, bạn cũng nên tạo Hồ sơ khởi động để có hiệu suất khởi động tốt hơn nữa.
Nếu bạn bật tính năng tối ưu hoá ứng dụng và tính năng này gây ra lỗi, thì sau đây là một số chiến lược để khắc phục lỗi:
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-30 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-30 UTC."],[],[],null,["# Enable app optimization\n\nFor the best user experience, you should optimize your app to make it as small\nand fast as possible. Our app optimizer, called R8, streamlines your app by\nremoving unused code and resources, rewriting code to optimize runtime\nperformance, and more. To your users, this means:\n\n- Faster startup time\n- Improved rendering and runtime performance\n- Fewer [ANRs](/topic/performance/anrs/keep-your-app-responsive)\n\n| **Important:** You should always enable optimization for your app's release build; however, you probably don't want to enable it for tests or libraries. For more information about using R8 with tests, see [Test and troubleshoot the\n| optimization](/topic/performance/app-optimization/test-and-troubleshoot-the-optimization). For more information about enabling R8 from libraries, see [Optimization for library authors](/topic/performance/app-optimization/library-optimization).\n\nTo enable app optimization, set `isMinifyEnabled = true` (for code optimization)\nand `isShrinkResources = true` (for resource optimization) in your [release\nbuild's](/studio/publish/preparing#turn-off-debugging) app-level build script as shown in the following code. We recommend\nthat you always enable both settings. We also recommend enabling app\noptimization only in the final version of your app that you test before\npublishing---usually your release build---because the optimizations increase the\nbuild time of your project and can make debugging harder due to the way it\nmodifies code. \n\n### Kotlin\n\n```kotlin\nandroid {\n buildTypes {\n release {\n\n // Enables code-related app optimization.\n isMinifyEnabled = true\n\n // Enables resource shrinking.\n isShrinkResources = true\n\n proguardFiles(\n // Default file with automatically generated optimization rules.\n getDefaultProguardFile(\"proguard-android-optimize.txt\"),\n\n ...\n )\n ...\n }\n }\n ...\n}\n```\n\n### Groovy\n\n```groovy\nandroid {\n buildTypes {\n release {\n\n // Enables code-related app optimization.\n minifyEnabled true\n\n // Enables resource shrinking.\n shrinkResources true\n\n // Default file with automatically generated optimization rules.\n proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')\n\n ...\n }\n }\n}\n```\n\nAdditionally, verify that R8 uses its full optimization capabilities by\nremoving this line from your project's `gradle.properties` file, if it exists: \n\n android.enableR8.fullMode=false # Remove this line from your codebase.\n\nNote that enabling app optimization makes stack traces difficult to understand,\nespecially if R8 renames class or method names. To get stack traces that\ncorrectly correspond to your source code, see\n[Recover the original stack trace](/topic/performance/app-optimization/test-and-troubleshoot-the-optimization#recover-original-stack-trace).\n\nIf R8 is enabled, you should also [create Startup Profiles](/topic/performance/baselineprofiles/dex-layout-optimizations) for even better\nstartup performance.\n\nIf you enable app optimization and it causes errors, here are some strategies to\nfix them:\n\n- [Add keep rules](/topic/performance/app-optimization/add-keep-rules) to keep some code untouched.\n- [Adopt optimizations incrementally](/topic/performance/app-optimization/adopt-optimizations-incrementally).\n- Update your code to [use libraries that are better suited for optimization](/topic/performance/app-optimization/choose-libraries-wisely).\n\n| **Caution:** Tools that replace or modify R8's output can negatively impact runtime performance. R8 is careful about including and testing many optimizations at the code level, in [DEX layout](/topic/performance/app-optimization/6), and in correctly producing Baseline Profiles - other tools producing or modifying DEX files may break these optimizations, or otherwise regress performance."]]