Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 1 | # R8 FAQ |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 2 | |
| 3 | R8 uses the same configuration specification language as ProGuard, and tries to |
| 4 | be compatible with ProGuard. However as R8 has different optimizations it can be |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 5 | necessary to change the configuration when switching to R8. R8 provides two |
| 6 | modes, R8 compatibility mode and R8 full mode. R8 compatibility mode is default |
| 7 | in Android Studio and is meant to make the transition to R8 from ProGuard easier |
| 8 | by limiting the optimizations performed by R8. |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 9 | |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 10 | ## R8 full mode |
| 11 | In non-compat mode, also called “full mode”, R8 performs more aggressive |
| 12 | optimizations, meaning additional ProGuard configuration rules may be required. |
| 13 | Full mode can be enabled by adding `android.enableR8.fullMode=true` in the |
| 14 | `gradle.properties` file. The main differences compared to R8 compatibility mode |
| 15 | are: |
| 16 | |
| 17 | - The default constructor (`<init>()`) is not implicitly kept when a class is |
| 18 | kept. |
| 19 | - The default constructor (`<init>()`) is not implicitly kept for types which |
| 20 | are only used with `ldc`, `instanceof` or `checkcast`. |
| 21 | - The enclosing classes of fields or methods that are matched by a |
| 22 | `-keepclassmembers` rule are not implicitly considered to be instantiated. |
| 23 | Classes that are only instantiated using reflection should be kept explicitly |
| 24 | with a `-keep` rule. |
| 25 | - Default methods are not implicitly kept as abstract methods. |
| 26 | - Attributes (such as `Signature`) and annotations are only kept for classes, |
| 27 | methods and fields which are matched by keep rules even when `-keepattributes` |
Morten Krogh-Jespersen | 04752e1 | 2022-10-11 12:55:57 +0200 | [diff] [blame] | 28 | is specified. The weakest rule that will keep annotations and attributes is |
| 29 | `-keep[classmembers],allowshrinking,allowoptimization,allowobfuscation,allowaccessmodification class-specification` |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 30 | Additionally, for attributes describing a relationship such as `InnerClass` and |
| 31 | `EnclosingMethod`, non-compat mode requires both endpoints being kept. |
Søren Gjesse | 7a177c7 | 2024-04-03 15:23:52 +0200 | [diff] [blame] | 32 | - When optimizing or minifying the `SourceFile` attribute will always be |
| 33 | rewritten to `SourceFile` unless `-renamesourcefileattribute` is used in which |
| 34 | case the provided value is used. The original source file name is in the mapping |
| 35 | file and when optimizing or minifying a mapping file is always produced. |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 36 | |
Ian Zerny | 42b4baf | 2023-04-26 11:55:47 +0200 | [diff] [blame] | 37 | ## Stack traces and retracing |
| 38 | When compiling with R8, a mapping file can be produced to support mapping stack |
| 39 | traces of the R8 optimized program back to the original source information. |
| 40 | |
| 41 | Starting with R8 version 8.2, the compiler will retain full original source file |
| 42 | names in the mapping information without the need to specify |
| 43 | `-keepattributes SourceFile`. |
| 44 | |
| 45 | In addition, builds that target API level 26 or above (and don't specify custom |
| 46 | source-file information) will also retain mapping information for all lines |
| 47 | without the need to specify `-keepattributes LineNumberTable`. |
| 48 | |
| 49 | More information on R8 mapping files can be found in the [retrace doc](doc/retrace.md). |
| 50 | |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 51 | # Troubleshooting |
| 52 | |
| 53 | The rest of this document describes known issues with libraries that use |
| 54 | reflection. |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 55 | |
| 56 | ## GSON |
| 57 | |
| 58 | ### Member in a data object is always `null` |
| 59 | |
| 60 | For data classes used for serialization all fields that are used in the |
| 61 | serialization must be kept by the configuration. R8 can decide to replace |
| 62 | instances of types that are never instantiated with `null`. So if instances of a |
| 63 | given class are only created through deserialization from JSON, R8 will not see |
| 64 | that class as instantiated leaving it as always `null`. |
| 65 | |
| 66 | If the `@SerializedName` annotation is used consistently for data classes the |
| 67 | following keep rule can be used: |
| 68 | |
| 69 | ``` |
| 70 | -keepclassmembers,allowobfuscation class * { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 71 | @com.google.gson.annotations.SerializedName <fields>; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 72 | } |
| 73 | ``` |
| 74 | |
| 75 | This will ensure that all fields annotated with `SerializedName` will be |
| 76 | kept. These fields can still be renamed during obfuscation as the |
| 77 | `SerializedName` annotation (not the source field name) controls the name in the |
| 78 | JSON serialization. |
| 79 | |
| 80 | If the `@SerializedName` annotation is _not_ used the following conservative |
| 81 | rule can be used for each data class: |
| 82 | |
| 83 | ``` |
| 84 | -keepclassmembers class MyDataClass { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 85 | !transient <fields>; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 86 | } |
| 87 | ``` |
| 88 | |
| 89 | This will ensure that all fields are kept and not renamed for these |
| 90 | classes. Fields with modifier `transient` are never serialized and therefore |
| 91 | keeping these is not needed. |
| 92 | |
| 93 | ### Error `java.lang.IllegalArgumentException: class <class name> declares multiple JSON fields named <name>` |
| 94 | |
| 95 | This can be caused by obfuscation selecting the same name for private fields in |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 96 | several classes in a class hierarchy. Consider the following example: |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 97 | |
| 98 | ``` |
| 99 | class A { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 100 | private String fieldInA; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | class B extends A { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 104 | private String fieldInB; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 105 | } |
| 106 | ``` |
| 107 | |
| 108 | Here R8 can choose to rename both `fieldInA` and `fieldInB` to the same name, |
| 109 | e.g. `a`. This creates a conflict when GSON is used to either serialize an |
| 110 | instance of class `B` to JSON or create an instance of class `B` from JSON. If |
| 111 | the fields should _not_ be serialized they should be marked `transient` so that |
| 112 | they will be ignored by GSON: |
| 113 | |
| 114 | ``` |
| 115 | class A { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 116 | private transient String fieldInA; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | class B extends A { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 120 | private transient String fieldInB; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 121 | } |
| 122 | ``` |
| 123 | |
| 124 | If the fields _are_ to be serialized, the annotation `SerializedName` can be |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 125 | used to fix the `IllegalArgumentException` together with the rule to keep fields |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 126 | annotated with `SerializedName` |
| 127 | |
| 128 | ``` |
| 129 | class A { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 130 | @SerializedName("fieldInA") |
| 131 | private String fieldInA; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | class B extends A { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 135 | @SerializedName("fieldInB") |
| 136 | private String fieldInB; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 137 | } |
| 138 | ``` |
| 139 | |
| 140 | ``` |
| 141 | -keepclassmembers,allowobfuscation class * { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 142 | @com.google.gson.annotations.SerializedName <fields>; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 143 | } |
| 144 | ``` |
| 145 | |
| 146 | |
| 147 | Both the use of `transient` and the use of the annotation `SerializedName` allow |
| 148 | the fields to be renamed by R8 to the same name, but GSON serialization will |
| 149 | work as expected. |
| 150 | |
Morten Krogh-Jespersen | 72d7310 | 2022-09-28 13:51:53 +0200 | [diff] [blame] | 151 | ### GSON |
Christoffer Quist Adamsen | d96a2cf | 2019-03-07 09:58:51 +0100 | [diff] [blame] | 152 | |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 153 | GSON uses type tokens to serialize and deserialize generic types. |
| 154 | |
| 155 | ```TypeToken<List<String>> listOfStrings = new TypeToken<List<String>>() {};``` |
| 156 | |
| 157 | The anonymous class will have a generic signature argument of `List<String>` to |
| 158 | the super type `TypeToken` that is reflective read for serialization. It |
| 159 | is therefore necessary to keep both the `Signature` attribute, the |
Morten Krogh-Jespersen | 72d7310 | 2022-09-28 13:51:53 +0200 | [diff] [blame] | 160 | `com.google.gson.reflect.TypeToken` class and all sub-types. |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 161 | |
| 162 | ``` |
| 163 | -keepattributes Signature |
| 164 | -keep class com.google.gson.reflect.TypeToken { *; } |
| 165 | -keep class * extends com.google.gson.reflect.TypeToken |
| 166 | ``` |
Christoffer Quist Adamsen | d96a2cf | 2019-03-07 09:58:51 +0100 | [diff] [blame] | 167 | |
Morten Krogh-Jespersen | 72d7310 | 2022-09-28 13:51:53 +0200 | [diff] [blame] | 168 | This is also needed for R8 in compat mode since multiple optimizations will |
| 169 | remove the generic signature such as class merging and argument removal. |
| 170 | |
Christoffer Quist Adamsen | d96a2cf | 2019-03-07 09:58:51 +0100 | [diff] [blame] | 171 | ## Retrofit |
| 172 | |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 173 | ### Objects instantiated with Retrofit's `create()` method are always replaced with `null` |
Christoffer Quist Adamsen | d96a2cf | 2019-03-07 09:58:51 +0100 | [diff] [blame] | 174 | |
| 175 | This happens because Retrofit uses reflection to return an object that |
| 176 | implements a given interface. The issue can be resolved by using the most recent |
| 177 | keep rules from the Retrofit library. |
| 178 | |
| 179 | See also https://github.com/square/retrofit/issues/3005 ("Insufficient keep |
| 180 | rules for R8 in full mode"). |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 181 | |
Morten Krogh-Jespersen | 573f37b | 2023-05-03 10:42:23 +0200 | [diff] [blame] | 182 | ### Return type must be parameterized |
| 183 | |
| 184 | Consider a service as the following: |
| 185 | ``` |
| 186 | interface Api { |
| 187 | |
| 188 | @GET("<uri>") |
| 189 | fun getData(): Observable<Data> |
| 190 | |
| 191 | } |
| 192 | ``` |
| 193 | |
| 194 | Retrofit instantiate a return type by inspecting the generic signature, here |
| 195 | `Observable` from `io.reactivex.rxjava3.core`. Those classes are not guarded by |
| 196 | keep rules prior to https://github.com/square/retrofit/pull/3886 so one has to |
| 197 | manually add keep rules to prevent R8 in full mode stripping the generic |
| 198 | signature. The proposed rule in https://github.com/square/retrofit/pull/3886 is: |
| 199 | ``` |
| 200 | -if interface * { @retrofit2.http.* public *** *(...); } |
| 201 | -keep,allowoptimization,allowshrinking,allowobfuscation class <3> |
| 202 | ``` |
| 203 | After https://github.com/square/retrofit/pull/3886 is merged, the above rule |
| 204 | is automatically included in your build. You can add the rule to your build |
| 205 | until then. |
| 206 | |
| 207 | Note, the `Data` class probably also needs to be kept if used since this is |
| 208 | also constructed reflectively. |
| 209 | |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 210 | ### Kotlin suspend functions and generic signatures |
| 211 | |
| 212 | For Kotlin suspend functions the generic signature is reflectively read. |
| 213 | Therefore keeping the `Signature` attribute is necessary. Full mode only keeps |
| 214 | the signature for kept classes thus a keep on `kotlin.coroutines.Continuation` in |
| 215 | addition to a keep on the api classes is needed: |
| 216 | ``` |
| 217 | -keepattributes Signature |
| 218 | -keep class kotlin.coroutines.Continuation |
| 219 | ``` |
| 220 | |
| 221 | This should be included automatically from versions built after the pull-request |
| 222 | https://github.com/square/retrofit/pull/3563 |
| 223 | |