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 | b7199f2 | 2024-07-03 15:07:59 +0200 | [diff] [blame] | 124 | If your code is being shrunk before release as a library, then you need to keep |
| 125 | the API surface. For that you should use the `@KeepForApi` annotation. |
Ian Zerny | 5f8936a | 2024-01-04 11:43:47 +0100 | [diff] [blame] | 126 | |
| 127 | [[[INCLUDE DOC:ApiClass]]] |
| 128 | |
| 129 | [[[INCLUDE CODE:ApiClass]]] |
| 130 | |
| 131 | [[[INCLUDE DOC:ApiClassMemberAccess]]] |
| 132 | |
| 133 | [[[INCLUDE CODE:ApiClassMemberAccess]]] |
| 134 | |
| 135 | [[[INCLUDE DOC:ApiMember]]] |
| 136 | |
| 137 | [[[INCLUDE CODE:ApiMember]]] |
Ian Zerny | e692bce | 2023-11-27 13:51:43 +0100 | [diff] [blame] | 138 | |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 139 | |
Ian Zerny | b7199f2 | 2024-07-03 15:07:59 +0200 | [diff] [blame] | 140 | ## [Constraints](constraints) |
| 141 | |
| 142 | When an item is kept (e.g., items matched by `@KeepTarget` or annotated by |
| 143 | `@UsedByReflection` or `@KeepForApi`) you can additionally specify constraints |
| 144 | about what properties of that item must be kept. Typical constraints are to keep |
| 145 | the items *name* or its ability to be reflectively *looked up*. You may also be |
| 146 | interested in keeping the generic signature of an item or annotations associated |
| 147 | with it. |
| 148 | |
| 149 | ### [Defaults](constraints-defaults) |
| 150 | |
| 151 | By default the constraints are to retain the item's name, its ability to be |
| 152 | looked-up as well as its normal usage. Its normal usage is: |
| 153 | |
| 154 | - to be instantiated, for class items; |
| 155 | - to be invoked, for method items; and |
| 156 | - to be get and/or set, for field items. |
| 157 | |
| 158 | [[[INCLUDE DOC:UsesReflectionFieldPrinterWithConstraints]]] |
| 159 | |
| 160 | [[[INCLUDE CODE:UsesReflectionFieldPrinterWithConstraints]]] |
| 161 | |
| 162 | |
| 163 | ### [Generic signatures](constraints-signatures) |
| 164 | |
| 165 | The generic signature information of an item is not kept by default, and |
| 166 | requires adding constraints to the targeted items. |
| 167 | |
| 168 | [[[INCLUDE DOC:GenericSignaturePrinter]]] |
| 169 | |
| 170 | [[[INCLUDE CODE:GenericSignaturePrinter]]] |
| 171 | |
| 172 | |
Ian Zerny | 4e219c1 | 2023-11-27 13:27:55 +0100 | [diff] [blame] | 173 | ## [Migrating rules to annotations](migrating-rules) |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 174 | |
Ian Zerny | e692bce | 2023-11-27 13:51:43 +0100 | [diff] [blame] | 175 | There is no automatic migration of keep rules. Keep annotations often invert the |
| 176 | direction and rules have no indication of where the reflection is taking |
| 177 | place or why. Thus, migrating existing keep rules requires user involvement. |
| 178 | Keep rules also have a tendency to be very general, matching a large |
| 179 | number of classes and members. Often the rules are much too broad and are |
| 180 | keeping more than needed which will have a negative impact on the shrinkers |
| 181 | ability to reduce size. |
| 182 | |
| 183 | First step in converting a rule is to determine the purpose of the rule. Is it |
| 184 | API surface or is it reflection? Note that a very general rule may be covering |
| 185 | several use cases and even a mix of both reflection and API usage. |
| 186 | |
| 187 | When migrating it is preferable to use `@UsesReflection` instead of |
| 188 | `@UsedByReflection`. For very general rules it might not be easy or worth it to |
| 189 | migrate without completely reevaluating the rule. If one still wants to replace |
| 190 | it by annotations, the general `@KeepEdge` can be used to define a context |
| 191 | independent keep annotation. |
| 192 | |
| 193 | [[[INCLUDE DOC:KeepMainMethods]]] |
| 194 | |
| 195 | [[[INCLUDE CODE:KeepMainMethods]]] |
| 196 | |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 197 | |
Ian Zerny | 4e219c1 | 2023-11-27 13:27:55 +0100 | [diff] [blame] | 198 | ## [My use case is not covered!](other-uses) |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 199 | |
Ian Zerny | e692bce | 2023-11-27 13:51:43 +0100 | [diff] [blame] | 200 | The annotation library is in active development and not all use cases are |
| 201 | described here or supported. Reach out to the R8 team by |
| 202 | [filing a new issue in our tracker](https://issuetracker.google.com/issues/new?component=326788). |
| 203 | Describe your use case and we will look at how best to support it. |
| 204 | |
Ian Zerny | 8f0adab | 2023-11-23 15:12:13 +0100 | [diff] [blame] | 205 | |
Ian Zerny | 4e219c1 | 2023-11-27 13:27:55 +0100 | [diff] [blame] | 206 | ## [Troubleshooting](troubleshooting) |
Ian Zerny | e692bce | 2023-11-27 13:51:43 +0100 | [diff] [blame] | 207 | |
| 208 | If an annotation is not working as expected it may be helpful to inspect the |
| 209 | rules that have been extracted for the annotation. This can be done by |
| 210 | inspecting the configuration output of the shrinker. For R8 you can use the |
| 211 | command line argument `--pg-conf-output <path>` to emit the full configuration |
| 212 | used by R8. |