Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 1 | # Guide to Keep Annotations |
| 2 | |
| 3 | ## Disclaimer |
| 4 | |
| 5 | The annotation library described here is in development and considered to be in |
| 6 | its prototype phase. As such it is not yet feature complete, but we are actively |
| 7 | working on supporting all of the use cases we know of. Once the design exits the |
| 8 | prototype phase, it is intended to move to an R8 independent library as part of |
| 9 | androidx. All feedback: criticism, comments and suggestions are very welcome! |
| 10 | |
| 11 | [File new feature requests and |
| 12 | bugs](https://issuetracker.google.com/issues/new?component=326788) in the |
| 13 | [R8 component](https://issuetracker.google.com/issues?q=status:open%20componentid:326788). |
| 14 | |
| 15 | |
Ian Zerny | 4e219c1 | 2023-11-27 13:27:55 +0100 | [diff] [blame] | 16 | [[[TOC]]] |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 17 | |
Ian Zerny | 4e219c1 | 2023-11-27 13:27:55 +0100 | [diff] [blame] | 18 | |
| 19 | ## [Introduction](introduction) |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 20 | |
| 21 | When using a Java/Kotlin shrinker such as R8 or Proguard, developers must inform |
| 22 | the shrinker about parts of the program that are used either externally from the |
| 23 | program itself or internally via reflection and therefore must be kept. |
| 24 | |
| 25 | Traditionally these aspects would be kept by writing keep rules in a |
| 26 | configuration file and passing that to the shrinker. |
| 27 | |
| 28 | The keep annotations described in this document represent an alternative method |
| 29 | using Java annotations. The motivation for using these annotations is foremost |
| 30 | to place the description of what to keep closer to the program point using |
| 31 | reflective behavior. Doing so more directly connects the reflective code with |
Ian Zerny | 8442796 | 2023-12-06 10:57:30 +0100 | [diff] [blame] | 32 | the keep specification and makes it easier to maintain as the code develops. |
| 33 | Often the keep annotations are only in effect if the annotated method is used, |
| 34 | allowing more precise shrinking. In addition, the annotations are defined |
| 35 | independent from keep rules and have a hopefully more clear and direct meaning. |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 36 | |
| 37 | |
Ian Zerny | 4e219c1 | 2023-11-27 13:27:55 +0100 | [diff] [blame] | 38 | ## [Build configuration](build-configuration) |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 39 | |
| 40 | To use the keep annotations your build must include the library of |
| 41 | annotations. It is currently built as part of each R8 build and if used with R8, |
| 42 | you should use the matching version. You can find all archived builds at: |
| 43 | |
| 44 | ``` |
| 45 | https://storage.googleapis.com/r8-releases/raw/<version>/keepanno-annotations.jar |
| 46 | ``` |
| 47 | |
| 48 | Thus you may obtain version `8.2.34` by running: |
| 49 | |
| 50 | ``` |
| 51 | wget https://storage.googleapis.com/r8-releases/raw/8.2.34/keepanno-annotations.jar |
| 52 | ``` |
| 53 | |
| 54 | You will then need to set the system property |
| 55 | `com.android.tools.r8.enableKeepAnnotations` to instruct R8 to make use of the |
| 56 | annotations when shrinking: |
| 57 | |
| 58 | ``` |
| 59 | java -Dcom.android.tools.r8.enableKeepAnnotations=1 \ |
| 60 | -cp r8.jar com.android.tools.r8.R8 \ |
| 61 | # ... the rest of your R8 compilation command here ... |
| 62 | ``` |
| 63 | |
Ian Zerny | 8442796 | 2023-12-06 10:57:30 +0100 | [diff] [blame] | 64 | |
Ian Zerny | 4e219c1 | 2023-11-27 13:27:55 +0100 | [diff] [blame] | 65 | ## [Annotating code using reflection](using-reflection) |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 66 | |
| 67 | The keep annotation library defines a family of annotations depending on your |
| 68 | use case. You should generally prefer `@UsesReflection` where applicable. |
Ian Zerny | 8442796 | 2023-12-06 10:57:30 +0100 | [diff] [blame] | 69 | Common uses of reflection are to lookup fields and methods on classes. Examples |
| 70 | of such use cases are detailed below. |
| 71 | |
| 72 | |
| 73 | ### [Invoking methods](using-reflection-methods) |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 74 | |
| 75 | [[[INCLUDE DOC:UsesReflectionOnVirtualMethod]]] |
| 76 | |
| 77 | [[[INCLUDE CODE:UsesReflectionOnVirtualMethod]]] |
| 78 | |
| 79 | |
Ian Zerny | 8442796 | 2023-12-06 10:57:30 +0100 | [diff] [blame] | 80 | ### [Accessing fields](using-reflection-fields) |
| 81 | |
| 82 | [[[INCLUDE DOC:UsesReflectionFieldPrinter]]] |
| 83 | |
| 84 | [[[INCLUDE CODE:UsesReflectionFieldPrinter]]] |
| 85 | |
Ian Zerny | e9981bd | 2024-01-18 13:26:54 +0100 | [diff] [blame] | 86 | ### [Accessing annotations](using-reflection-annotations) |
| 87 | |
| 88 | [[[INCLUDE DOC:UsesReflectionOnAnnotations]]] |
| 89 | |
| 90 | [[[INCLUDE CODE:UsesReflectionOnAnnotations]]] |
| 91 | |
| 92 | If the annotations that need to be kept are not runtime |
| 93 | visible annotations, then you must specify that by including the `RetentionPolicy.CLASS` value in the |
| 94 | `@AnnotationPattern#retention` property. |
| 95 | An annotation is runtime visible if its definition is explicitly annotated with |
| 96 | `Retention(RetentionPolicy.RUNTIME)`. |
| 97 | |
| 98 | |
Ian Zerny | 8442796 | 2023-12-06 10:57:30 +0100 | [diff] [blame] | 99 | |
Ian Zerny | 4e219c1 | 2023-11-27 13:27:55 +0100 | [diff] [blame] | 100 | ## [Annotating code used by reflection (or via JNI)](used-by-reflection) |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 101 | |
Ian Zerny | d8bc17b | 2024-01-04 09:48:47 +0100 | [diff] [blame] | 102 | Sometimes reflecting code cannot be annotated. For example, the reflection can |
| 103 | be done in native code or in a library outside your control. In such cases you |
| 104 | can annotate the code that is being used by reflection with either |
| 105 | `@UsedByReflection` or `@UsedByNative`. These two annotations are equivalent. |
| 106 | Use the one that best matches why the annotation is needed. |
| 107 | |
| 108 | Let's consider some code with reflection outside our control. |
| 109 | [[[INCLUDE DOC:UsedByReflectionFieldPrinterOnFields]]] |
| 110 | |
| 111 | [[[INCLUDE CODE:UsedByReflectionFieldPrinterOnFields]]] |
| 112 | |
| 113 | [[[INCLUDE DOC:UsedByReflectionFieldPrinterOnClass]]] |
| 114 | |
| 115 | [[[INCLUDE CODE:UsedByReflectionFieldPrinterOnClass]]] |
| 116 | |
| 117 | [[[INCLUDE DOC:UsedByReflectionFieldPrinterConditional]]] |
| 118 | |
| 119 | [[[INCLUDE CODE:UsedByReflectionFieldPrinterConditional]]] |
Ian Zerny | e692bce | 2023-11-27 13:51:43 +0100 | [diff] [blame] | 120 | |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 121 | |
Ian Zerny | 4e219c1 | 2023-11-27 13:27:55 +0100 | [diff] [blame] | 122 | ## [Annotating APIs](apis) |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 123 | |
Ian Zerny | 5f8936a | 2024-01-04 11:43:47 +0100 | [diff] [blame] | 124 | If your code is being shrunk before release as a library, or if you have an API |
| 125 | surface that is used via dynamic loading at runtime, then you need to keep the |
| 126 | API surface. For that you should use the `@KeepForApi` annotation. |
| 127 | |
| 128 | [[[INCLUDE DOC:ApiClass]]] |
| 129 | |
| 130 | [[[INCLUDE CODE:ApiClass]]] |
| 131 | |
| 132 | [[[INCLUDE DOC:ApiClassMemberAccess]]] |
| 133 | |
| 134 | [[[INCLUDE CODE:ApiClassMemberAccess]]] |
| 135 | |
| 136 | [[[INCLUDE DOC:ApiMember]]] |
| 137 | |
| 138 | [[[INCLUDE CODE:ApiMember]]] |
Ian Zerny | e692bce | 2023-11-27 13:51:43 +0100 | [diff] [blame] | 139 | |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 140 | |
Ian Zerny | 4e219c1 | 2023-11-27 13:27:55 +0100 | [diff] [blame] | 141 | ## [Migrating rules to annotations](migrating-rules) |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 142 | |
Ian Zerny | e692bce | 2023-11-27 13:51:43 +0100 | [diff] [blame] | 143 | There is no automatic migration of keep rules. Keep annotations often invert the |
| 144 | direction and rules have no indication of where the reflection is taking |
| 145 | place or why. Thus, migrating existing keep rules requires user involvement. |
| 146 | Keep rules also have a tendency to be very general, matching a large |
| 147 | number of classes and members. Often the rules are much too broad and are |
| 148 | keeping more than needed which will have a negative impact on the shrinkers |
| 149 | ability to reduce size. |
| 150 | |
| 151 | First step in converting a rule is to determine the purpose of the rule. Is it |
| 152 | API surface or is it reflection? Note that a very general rule may be covering |
| 153 | several use cases and even a mix of both reflection and API usage. |
| 154 | |
| 155 | When migrating it is preferable to use `@UsesReflection` instead of |
| 156 | `@UsedByReflection`. For very general rules it might not be easy or worth it to |
| 157 | migrate without completely reevaluating the rule. If one still wants to replace |
| 158 | it by annotations, the general `@KeepEdge` can be used to define a context |
| 159 | independent keep annotation. |
| 160 | |
| 161 | [[[INCLUDE DOC:KeepMainMethods]]] |
| 162 | |
| 163 | [[[INCLUDE CODE:KeepMainMethods]]] |
| 164 | |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 165 | |
Ian Zerny | 4e219c1 | 2023-11-27 13:27:55 +0100 | [diff] [blame] | 166 | ## [My use case is not covered!](other-uses) |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 167 | |
Ian Zerny | e692bce | 2023-11-27 13:51:43 +0100 | [diff] [blame] | 168 | The annotation library is in active development and not all use cases are |
| 169 | described here or supported. Reach out to the R8 team by |
| 170 | [filing a new issue in our tracker](https://issuetracker.google.com/issues/new?component=326788). |
| 171 | Describe your use case and we will look at how best to support it. |
| 172 | |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 173 | |
Ian Zerny | 4e219c1 | 2023-11-27 13:27:55 +0100 | [diff] [blame] | 174 | ## [Troubleshooting](troubleshooting) |
Ian Zerny | e692bce | 2023-11-27 13:51:43 +0100 | [diff] [blame] | 175 | |
| 176 | If an annotation is not working as expected it may be helpful to inspect the |
| 177 | rules that have been extracted for the annotation. This can be done by |
| 178 | inspecting the configuration output of the shrinker. For R8 you can use the |
| 179 | command line argument `--pg-conf-output <path>` to emit the full configuration |
| 180 | used by R8. |