遵循最佳实践
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
使用保留规则时,请务必达到适当的精细度,以确保您在保持应用行为的同时获得益处。如需了解良好的保留规则模式以及应避免的事项,请参阅以下部分。
保留规则中的良好模式
明确定义的保留规则应尽可能具体:
如果您无法遵守这些准则,可以暂时将需要保留的代码隔离到专用软件包中,然后将 keep 规则应用于该软件包。不过,这并非长久之计。如需了解详情,请参阅逐步采用优化。如需为软件包使用保留规则,请定义保留规则,如以下示例所示:
-keepclassmembers class com.example.pkg.** { *; }
需要避免的事项
保留规则语法有很多选项,但为了获得可衡量的可持续性能优势,我们建议不要使用以下选项:
- 避免在保留规则中使用反转运算符
!
,因为您可能会无意中将规则应用于应用中的几乎每个类。
- 请勿使用软件包范围的保留规则,例如
-keep class com.example.pkg.** {
*; }
(长期)。在配置 R8 时,可以暂时使用它们来解决问题。如需了解详情,请参阅限制优化范围。
一般来说,请谨慎使用通配符,确保仅保留所需的代码。
如果您无法遵守这些规则,则可能使用了大量开放式反射,应避免使用反射或避免使用使用反射的库(请参阅 Gson 案例研究)。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Follow the best practices\n\nWhile working with keep rules, it's important to reach the right amount of\nspecificity to make sure you see benefits while maintaining your app's\nbehaviour. See the following sections to learn about good patterns as well as\nthings to avoid in keep rules.\n\nGood patterns in keep rules\n---------------------------\n\nWell-defined keep rules are as specific as possible:\n\n- For the class specification, always specify a specific class, base class, or\n annotated class if possible, as shown in the following examples:\n\n -keepclassmembers class com.example.MyClass {\n void someSpecificMethod();\n }\n\n -keepclassmembers ** extends com.example.MyBaseClass {\n void someSpecificMethod();\n }\n\n -keepclassmembers @com.example.MyAnnotation class ** {\n void someSpecificMethod();\n }\n\n- Whenever possible, the member specification should be declared, and only\n reference the parts of the class that must be kept for the app to function.\n It's recommended to not apply a rule to an entire class by defining the\n optional member scope as `{ *; }` unless strictly needed.\n\n -keepclassmembers com.example.MyClass {\n void someSpecificMethod();\n void @com.example.MyAnnotation *;\n }\n\nIf you can't adhere to these guidelines, you can temporarily isolate the code\nthat needs to be kept in a dedicated package and apply your keep rule to the\npackage. However, this isn't a solution for the long term. To learn more, see\n[Adopt optimizations incrementally](/topic/performance/app-optimization/adopt-optimizations-incrementally#use-package-wide). To use a keep rule for a package define\na keep rule as shown in the following example: \n\n -keepclassmembers class com.example.pkg.** { *; }\n\nThings to avoid\n---------------\n\nThe keep rule syntax has many options, but for measurable sustainable\nperformance benefits we recommend not using the following:\n\n- Avoid using the inversion operator `!` in keep rules because you could unintentionally apply a rule to almost every class in your application.\n- Don't use package-wide keep rules such as `-keep class com.example.pkg.** {\n *; }` long-term. They can be used temporarily to work around issues when configuring R8. For more information, see [Limit the optimization scope](/topic/performance/app-optimization/adopt-optimizations-incrementally#limit-optimization). In general, be careful with wildcards--- make sure that you are keeping only the code that you need to.\n\nIf you're unable to follow these rules, you might be using a lot of open-ended\nreflection, and should either avoid the reflection or avoid the library using\nreflection (see the [Gson case study](/topic/performance/app-optimization/choose-libraries-wisely#gson-issues))."]]