Skip to content

Commit 9008b09

Browse files
authored
Extend Troubleshooting Guide with some ProGuard / R8 information (#2656)
1 parent 05652c3 commit 9008b09

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Troubleshooting.md

+13-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ This guide describes how to troubleshoot common issues when using Gson.
99

1010
**Symptom:** `ClassCastException` is thrown when accessing an object deserialized by Gson
1111

12-
**Reason:** Your code is most likely not type-safe
12+
**Reason:**
13+
14+
- Your code is most likely not type-safe
15+
- Or, you have not configured code shrinking tools such as ProGuard or R8 correctly
1316

1417
**Solution:** Make sure your code adheres to the following:
1518

@@ -19,6 +22,8 @@ This guide describes how to troubleshoot common issues when using Gson.
1922
The overloads with `Type` parameter do not provide any type-safety guarantees.
2023
- When using `TypeToken` make sure you don't capture a type variable. For example avoid something like `new TypeToken<List<T>>()` (where `T` is a type variable). Due to Java [type erasure](https://dev.java/learn/generics/type-erasure/) the actual type of `T` is not available at runtime. Refactor your code to pass around `TypeToken` instances or use [`TypeToken.getParameterized(...)`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/reflect/TypeToken.html#getParameterized(java.lang.reflect.Type,java.lang.reflect.Type...)), for example `TypeToken.getParameterized(List.class, elementType)` where `elementType` is a type you have to provide separately.
2124

25+
If you are using a code shrinking tool such as ProGuard or R8 (for example when building an Android app), make sure it is correctly configured to keep generic signatures and to keep Gson's `TypeToken` class. See the [Android example](examples/android-proguard-example/README.md) for more information.
26+
2227
## <a id="reflection-inaccessible"></a> `InaccessibleObjectException`: 'module ... does not "opens ..." to unnamed module'
2328

2429
**Symptom:** An exception with a message in the form 'module ... does not "opens ..." to unnamed module' is thrown
@@ -197,7 +202,7 @@ Gson then tries to use reflection and expects that the data is a JSON object (he
197202
**Reason:**
198203

199204
- A built-in adapter does not support JSON null values
200-
- You have written a custom `TypeAdapter` which does not properly handle JSON null values
205+
- Or, you have written a custom `TypeAdapter` which does not properly handle JSON null values
201206

202207
**Solution:** If this occurs for a custom adapter you wrote, add code similar to the following at the beginning of its `read` method:
203208

@@ -245,7 +250,7 @@ If you want to prevent using reflection on third-party classes in the future you
245250

246251
**Reason:** You used `GsonBuilder.excludeFieldsWithModifiers` to overwrite the default excluded modifiers
247252

248-
**Solution:** When calling `GsonBuilder.excludeFieldsWithModifiers` you overwrite the default excluded modifiers. Therefore, you have to explicitly exclude `static` fields if desired. This can be done by adding `Modifier.STATIC` as additional argument.
253+
**Solution:** When calling `GsonBuilder.excludeFieldsWithModifiers` you overwrite the default excluded modifiers. Therefore, you have to explicitly exclude `static` fields if desired. This can be done by adding `Modifier.STATIC` as additional argument to the `excludeFieldsWithModifiers` call.
249254

250255
## <a id="no-such-method-error"></a> `NoSuchMethodError` when calling Gson methods
251256

@@ -272,12 +277,13 @@ If that fails with a `NullPointerException` you have to try one of the other way
272277
**Reason:**
273278

274279
- The name you have specified with a [`@SerializedName`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/annotations/SerializedName.html) annotation for a field collides with the name of another field
275-
- The [`FieldNamingStrategy`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/FieldNamingStrategy.html) you have specified produces conflicting field names
276-
- A field of your class has the same name as the field of a superclass
280+
- Or, the [`FieldNamingStrategy`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/FieldNamingStrategy.html) you have specified produces conflicting field names
281+
- Or, a field of your class has the same name as the field of a superclass
282+
- Or, you are using an obfuscation tool such as ProGuard or R8 and it has renamed the fields; in that case see [this troubleshooting point](#android-app-random-names)
277283

278284
Gson prevents multiple fields with the same name because during deserialization it would be ambiguous for which field the JSON data should be deserialized. For serialization it would cause the same field to appear multiple times in JSON. While the JSON specification permits this, it is likely that the application parsing the JSON data will not handle it correctly.
279285

280-
**Solution:** First identify the fields with conflicting names based on the exception message. Then decide if you want to rename one of them using the [`@SerializedName`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/annotations/SerializedName.html) annotation, or if you want to [exclude](UserGuide.md#excluding-fields-from-serialization-and-deserialization) one of them. When excluding one of the fields you have to include it for both serialization and deserialization (even if your application only performs one of these actions) because the duplicate field check cannot differentiate between these actions.
286+
**Solution:** First identify the fields with conflicting names based on the exception message. Then decide if you want to rename one of them using the [`@SerializedName`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/annotations/SerializedName.html) annotation, or if you want to [exclude](UserGuide.md#excluding-fields-from-serialization-and-deserialization) one of them. When excluding one of the fields you have to apply the exclusion for both serialization and deserialization (even if your application only performs one of these actions) because the duplicate field check cannot differentiate between these actions.
281287

282288
## <a id="java-lang-class-unsupported"></a> `UnsupportedOperationException` when serializing or deserializing `java.lang.Class`
283289

@@ -325,7 +331,7 @@ For older Gson versions a `RuntimeException` with message 'Missing type paramete
325331
**Reason:**
326332

327333
- You created a `TypeToken` without type argument, for example `new TypeToken() {}` (note the missing `<...>`). You always have to provide the type argument, for example like this: `new TypeToken<List<String>>() {}`. Normally the compiler will also emit a 'raw types' warning when you forget the `<...>`.
328-
- You are using a code shrinking tool such as ProGuard or R8 (Android app builds normally have this enabled by default) but have not configured it correctly for usage with Gson.
334+
- Or, you are using a code shrinking tool such as ProGuard or R8 (Android app builds normally have this enabled by default) but have not configured it correctly for usage with Gson.
329335

330336
**Solution:** When you are using a code shrinking tool such as ProGuard or R8 you have to adjust your configuration to include the following rules:
331337

0 commit comments

Comments
 (0)