Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ local.properties
build

.DS_Store

examples/android-proguard-example/gen
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ Older Gson versions may also support lower API levels, however this has not been

Please use the ['gson' tag on StackOverflow](https://stackoverflow.com/questions/tagged/gson), [GitHub Discussions](https://github.com/google/gson/discussions) or the [google-gson Google group](https://groups.google.com/group/google-gson) to discuss Gson or to post questions.

### ProGuard / R8

See the details in the related section in the [Troubleshooting guide](Troubleshooting.md#proguard--r8).

### Related Content Created by Third Parties
* [Gson Tutorial](https://www.studytrails.com/java/json/java-google-json-introduction/) by `StudyTrails`
* [Gson Tutorial Series](https://futurestud.io/tutorials/gson-getting-started-with-java-json-serialization-deserialization) by `Future Studio`
Expand Down
19 changes: 15 additions & 4 deletions Troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ This guide describes how to troubleshoot common issues when using Gson.
The overloads with `Type` parameter do not provide any type-safety guarantees.
- 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.

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.
If you are using a code shrinking tool such as ProGuard / 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 [ProGuard / R8](#proguard--r8) section for more information.

## <a id="reflection-inaccessible"></a> `InaccessibleObjectException`: 'module ... does not "opens ..." to unnamed module'

Expand Down Expand Up @@ -67,15 +68,17 @@ Or in case this occurs for a field in one of your classes which you did not actu

**Reason:** You probably have not configured ProGuard / R8 correctly

**Solution:** Make sure you have configured ProGuard / R8 correctly to preserve the names of your fields. See the [Android example](examples/android-proguard-example/README.md) for more information.
**Solution:** Make sure you have configured ProGuard / R8 correctly to preserve the names of your fields.
See the section below, it's related to this issue.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly link to the section as well?

Suggested change
See the section below, it's related to this issue.
See the [ProGuard / R8](#r8--proguard) section for more information.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intended. As I see it, the "random property names" and "field names are being obfuscated" are the same issue; I just wanted to link them together.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should merge these 2 sections.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep them separate for now. The symptoms are different (but similar) and if I recall correctly users had experienced and asked about both problems already in the past.


## <a id="android-app-broken-after-app-update"></a> Android app unable to parse JSON after app update

**Symptom:** You released a new version of your Android app and it fails to parse JSON data created by the previous version of your app

**Reason:** You probably have not configured ProGuard / R8 correctly; probably the field names are being obfuscated and their naming changed between the versions of your app

**Solution:** Make sure you have configured ProGuard / R8 correctly to preserve the names of your fields. See the [Android example](examples/android-proguard-example/README.md) for more information.
**Solution:** Make sure you have configured ProGuard / R8 correctly to preserve the names of your fields.
See the [ProGuard / R8](#proguard--r8) section for more information.

If you want to preserve backward compatibility for you app you can use [`@SerializedName`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/annotations/SerializedName.html) on the fields to specify the obfuscated name as alternate, for example: `@SerializedName(value = "myprop", alternate = "a")`

Expand Down Expand Up @@ -346,7 +349,7 @@ For older Gson versions a `RuntimeException` with message 'Missing type paramete
-keep class * extends com.google.gson.reflect.TypeToken
```

See also the [Android example](examples/android-proguard-example/README.md) for more information.
See the [ProGuard / R8](#proguard--r8) section for more information.

Note: For newer Gson versions these rules might be applied automatically; make sure you are using the latest Gson version and the latest version of the code shrinking tool.

Expand Down Expand Up @@ -393,3 +396,11 @@ For backward compatibility it is possible to restore Gson's old behavior of allo

- This does not solve any of the type-safety problems mentioned above; in the long term you should prefer one of the other solutions listed above. This system property might be removed in future Gson versions.
- You should only ever set the property to `"true"`, but never to any other value or manually clear it. Otherwise this might counteract any libraries you are using which might have deliberately set the system property because they rely on its behavior.

## ProGuard / R8

If you use Gson as a dependency in an Android project which uses R8 or ProGuard as code shrinking and obfuscation tool you don't have to do anything.
The specific rules are [already bundled](gson/src/main/resources/META-INF/proguard/gson.pro) (from Gson 2.11.0) into
the JAR which can be interpreted by R8 automatically. For users who still use older Gson versions, you may need to
copy the rules from the [`gson.pro`](gson/src/main/resources/META-INF/proguard/gson.pro) file into your own ProGuard
configuration file.
21 changes: 0 additions & 21 deletions examples/android-proguard-example/AndroidManifest.xml

This file was deleted.

20 changes: 0 additions & 20 deletions examples/android-proguard-example/README.md

This file was deleted.

12 changes: 0 additions & 12 deletions examples/android-proguard-example/default.properties

This file was deleted.

32 changes: 0 additions & 32 deletions examples/android-proguard-example/proguard.cfg

This file was deleted.

Binary file not shown.
12 changes: 0 additions & 12 deletions examples/android-proguard-example/res/layout/main.xml

This file was deleted.

5 changes: 0 additions & 5 deletions examples/android-proguard-example/res/values/strings.xml

This file was deleted.

This file was deleted.

This file was deleted.

Loading