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` |
| 28 | is specified. |
| 29 | Additionally, for attributes describing a relationship such as `InnerClass` and |
| 30 | `EnclosingMethod`, non-compat mode requires both endpoints being kept. |
| 31 | |
| 32 | # Troubleshooting |
| 33 | |
| 34 | The rest of this document describes known issues with libraries that use |
| 35 | reflection. |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 36 | |
| 37 | ## GSON |
| 38 | |
| 39 | ### Member in a data object is always `null` |
| 40 | |
| 41 | For data classes used for serialization all fields that are used in the |
| 42 | serialization must be kept by the configuration. R8 can decide to replace |
| 43 | instances of types that are never instantiated with `null`. So if instances of a |
| 44 | given class are only created through deserialization from JSON, R8 will not see |
| 45 | that class as instantiated leaving it as always `null`. |
| 46 | |
| 47 | If the `@SerializedName` annotation is used consistently for data classes the |
| 48 | following keep rule can be used: |
| 49 | |
| 50 | ``` |
| 51 | -keepclassmembers,allowobfuscation class * { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 52 | @com.google.gson.annotations.SerializedName <fields>; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 53 | } |
| 54 | ``` |
| 55 | |
| 56 | This will ensure that all fields annotated with `SerializedName` will be |
| 57 | kept. These fields can still be renamed during obfuscation as the |
| 58 | `SerializedName` annotation (not the source field name) controls the name in the |
| 59 | JSON serialization. |
| 60 | |
| 61 | If the `@SerializedName` annotation is _not_ used the following conservative |
| 62 | rule can be used for each data class: |
| 63 | |
| 64 | ``` |
| 65 | -keepclassmembers class MyDataClass { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 66 | !transient <fields>; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 67 | } |
| 68 | ``` |
| 69 | |
| 70 | This will ensure that all fields are kept and not renamed for these |
| 71 | classes. Fields with modifier `transient` are never serialized and therefore |
| 72 | keeping these is not needed. |
| 73 | |
| 74 | ### Error `java.lang.IllegalArgumentException: class <class name> declares multiple JSON fields named <name>` |
| 75 | |
| 76 | 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] | 77 | several classes in a class hierarchy. Consider the following example: |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 78 | |
| 79 | ``` |
| 80 | class A { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 81 | private String fieldInA; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | class B extends A { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 85 | private String fieldInB; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 86 | } |
| 87 | ``` |
| 88 | |
| 89 | Here R8 can choose to rename both `fieldInA` and `fieldInB` to the same name, |
| 90 | e.g. `a`. This creates a conflict when GSON is used to either serialize an |
| 91 | instance of class `B` to JSON or create an instance of class `B` from JSON. If |
| 92 | the fields should _not_ be serialized they should be marked `transient` so that |
| 93 | they will be ignored by GSON: |
| 94 | |
| 95 | ``` |
| 96 | class A { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 97 | private transient String fieldInA; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | class B extends A { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 101 | private transient String fieldInB; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 102 | } |
| 103 | ``` |
| 104 | |
| 105 | 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] | 106 | 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] | 107 | annotated with `SerializedName` |
| 108 | |
| 109 | ``` |
| 110 | class A { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 111 | @SerializedName("fieldInA") |
| 112 | private String fieldInA; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | class B extends A { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 116 | @SerializedName("fieldInB") |
| 117 | private String fieldInB; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 118 | } |
| 119 | ``` |
| 120 | |
| 121 | ``` |
| 122 | -keepclassmembers,allowobfuscation class * { |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 123 | @com.google.gson.annotations.SerializedName <fields>; |
Søren Gjesse | ce1f427 | 2019-02-28 10:28:49 +0100 | [diff] [blame] | 124 | } |
| 125 | ``` |
| 126 | |
| 127 | |
| 128 | Both the use of `transient` and the use of the annotation `SerializedName` allow |
| 129 | the fields to be renamed by R8 to the same name, but GSON serialization will |
| 130 | work as expected. |
| 131 | |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 132 | ### GSON with full mode |
Christoffer Quist Adamsen | d96a2cf | 2019-03-07 09:58:51 +0100 | [diff] [blame] | 133 | |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 134 | GSON uses type tokens to serialize and deserialize generic types. |
| 135 | |
| 136 | ```TypeToken<List<String>> listOfStrings = new TypeToken<List<String>>() {};``` |
| 137 | |
| 138 | The anonymous class will have a generic signature argument of `List<String>` to |
| 139 | the super type `TypeToken` that is reflective read for serialization. It |
| 140 | is therefore necessary to keep both the `Signature` attribute, the |
| 141 | `com.google.gson.reflect.TypeToken` class and all sub-types: |
| 142 | |
| 143 | ``` |
| 144 | -keepattributes Signature |
| 145 | -keep class com.google.gson.reflect.TypeToken { *; } |
| 146 | -keep class * extends com.google.gson.reflect.TypeToken |
| 147 | ``` |
Christoffer Quist Adamsen | d96a2cf | 2019-03-07 09:58:51 +0100 | [diff] [blame] | 148 | |
| 149 | ## Retrofit |
| 150 | |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 151 | ### 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] | 152 | |
| 153 | This happens because Retrofit uses reflection to return an object that |
| 154 | implements a given interface. The issue can be resolved by using the most recent |
| 155 | keep rules from the Retrofit library. |
| 156 | |
| 157 | See also https://github.com/square/retrofit/issues/3005 ("Insufficient keep |
| 158 | rules for R8 in full mode"). |
Morten Krogh-Jespersen | 47229a4 | 2021-06-02 11:47:56 +0200 | [diff] [blame] | 159 | |
| 160 | ### Kotlin suspend functions and generic signatures |
| 161 | |
| 162 | For Kotlin suspend functions the generic signature is reflectively read. |
| 163 | Therefore keeping the `Signature` attribute is necessary. Full mode only keeps |
| 164 | the signature for kept classes thus a keep on `kotlin.coroutines.Continuation` in |
| 165 | addition to a keep on the api classes is needed: |
| 166 | ``` |
| 167 | -keepattributes Signature |
| 168 | -keep class kotlin.coroutines.Continuation |
| 169 | ``` |
| 170 | |
| 171 | This should be included automatically from versions built after the pull-request |
| 172 | https://github.com/square/retrofit/pull/3563 |
| 173 | |