-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ProGuard / R8 integration tests & add default ProGuard rules (#2397)
* Add code shrinking tools integration test * Keep no-args constructor of classes usable with JsonAdapter * Add library ProGuard rules for Gson They are automatically applied for all users of Gson, see https://developer.android.com/build/shrink-code#configuration-files * Skip japicmp-maven-plugin for shrinker-test * Add more tests for JsonAdapter, add tests for generic classes * Extend default constructor test * Add Troubleshooting Guide entry for TypeToken
- Loading branch information
1 parent
4c65a82
commit 43396e4
Showing
30 changed files
with
1,218 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
### Gson ProGuard and R8 rules which are relevant for all users | ||
### This file is automatically recognized by ProGuard and R8, see https://developer.android.com/build/shrink-code#configuration-files | ||
### | ||
### IMPORTANT: | ||
### - These rules are additive; don't include anything here which is not specific to Gson (such as completely | ||
### disabling obfuscation for all classes); the user would be unable to disable that then | ||
### - These rules are not complete; users will most likely have to add additional rules for their specific | ||
### classes, for example to disable obfuscation for certain fields or to keep no-args constructors | ||
### | ||
|
||
# Keep generic signatures; needed for correct type resolution | ||
-keepattributes Signature | ||
|
||
# Keep Gson annotations | ||
# Note: Cannot perform finer selection here to only cover Gson annotations, see also https://stackoverflow.com/q/47515093 | ||
-keepattributes *Annotation* | ||
|
||
|
||
### The following rules are needed for R8 in "full mode" which only adheres to `-keepattribtues` if | ||
### the corresponding class or field is matches by a `-keep` rule as well, see | ||
### https://r8.googlesource.com/r8/+/refs/heads/master/compatibility-faq.md#r8-full-mode | ||
|
||
# Keep class TypeToken (respectively its generic signature) | ||
-keep class com.google.gson.reflect.TypeToken { *; } | ||
|
||
# Keep any (anonymous) classes extending TypeToken | ||
-keep class * extends com.google.gson.reflect.TypeToken | ||
|
||
# Keep classes with @JsonAdapter annotation | ||
-keep @com.google.gson.annotations.JsonAdapter class * | ||
|
||
# Keep fields with @SerializedName annotation, but allow obfuscation of their names | ||
-keepclassmembers,allowobfuscation class * { | ||
@com.google.gson.annotations.SerializedName <fields>; | ||
} | ||
|
||
# Keep fields with any other Gson annotation | ||
-keepclassmembers class * { | ||
@com.google.gson.annotations.Expose <fields>; | ||
@com.google.gson.annotations.JsonAdapter <fields>; | ||
@com.google.gson.annotations.Since <fields>; | ||
@com.google.gson.annotations.Until <fields>; | ||
} | ||
|
||
# Keep no-args constructor of classes which can be used with @JsonAdapter | ||
# By default their no-args constructor is invoked to create an adapter instance | ||
-keep class * extends com.google.gson.TypeAdapter { | ||
<init>(); | ||
} | ||
-keep class * implements com.google.gson.TypeAdapterFactory { | ||
<init>(); | ||
} | ||
-keep class * implements com.google.gson.JsonSerializer { | ||
<init>(); | ||
} | ||
-keep class * implements com.google.gson.JsonDeserializer { | ||
<init>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# shrinker-test | ||
|
||
This Maven module contains integration tests which check the behavior of Gson when used in combination with code shrinking and obfuscation tools, such as ProGuard or R8. | ||
|
||
The code which is shrunken is under `src/main/java`; it should not contain any important assertions in case the code shrinking tools affect these assertions in any way. The test code under `src/test/java` executes the shrunken and obfuscated JAR and verifies that it behaves as expected. | ||
|
||
The tests might be a bit brittle, especially the R8 test setup. Future ProGuard and R8 versions might cause the tests to behave differently. In case tests fail the ProGuard and R8 mapping files created in the `target` directory can help with debugging. If necessary rewrite tests or even remove them if they cannot be implemented anymore for newer ProGuard or R8 versions. | ||
|
||
**Important:** Because execution of the code shrinking tools is performed during the Maven build, trying to directly run the integration tests from the IDE might not work, or might use stale results if you changed the configuration in between. Run `mvn clean verify` before trying to run the integration tests from the IDE. |
Oops, something went wrong.