GSON not able to parse on a release build
See original GitHub issueWhen I generate release build, GSON is not able to parse my API response.
I am using retrofit and gson Parser. com.squareup.retrofit2:retrofit com.squareup.retrofit2:converter-gson Retrofit version 2.6.0
implementation “com.google.code.gson:gson:$rootProject.gsonVersion” Version : ‘2.8.5’
My proguard for GSON
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# For using GSON @Expose annotation
-keepattributes EnclosingMethod
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
-dontwarn com.google.gson.internal.UnsafeAllocator
# Gson specific classes
-dontwarn sun.misc.**
# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
Proguard for retrofit2
# Retrofit 2.X
## https://square.github.io/retrofit/ ##
# Retrofit does reflection on generic parameters. InnerClasses is required to use Signature and
# EnclosingMethod is required to use InnerClasses.
-keepattributes Signature, InnerClasses, EnclosingMethod
# Retrofit does reflection on method and parameter annotations.
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations
# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface * {
@retrofit2.http.* <methods>;
}
# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
# Ignore JSR 305 annotations for embedding nullability information.
-dontwarn javax.annotation.**
# Guarded by a NoClassDefFoundError try/catch and only used when on the classpath.
-dontwarn kotlin.Unit
# Top-level functions that can only be used by Kotlin.
-dontwarn retrofit2.KotlinExtensions
-dontwarn retrofit2.KotlinExtensions$*
# With R8 full mode, it sees no subtypes of Retrofit interfaces since they are created with a Proxy
# and replaces all potential values with null. Explicitly keeping the interfaces prevents this.
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface <1>
I am using R8 enabled compilation
# Enables R8 for Android Library modules only.
android.enableR8.libraries = true
# Enables R8 for all modules.
android.enableR8 = true
But when I include my models in proguard, the parsing seems to work
# Application classes from libraries that will be serialized/deserialized over Gson
-keep class com.mindvalley.loginmodule.model.** { <fields>; }
-keep class com.mindvalley.module_videoplayer.model.** { <fields>; }
-keep class com.mindvalley.mva.model.** { <fields>; }
Can someone suggest how can I make parsing work without including models inside proguard?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5
Top Results From Across the Web
Gson Not mapping data in Production Mode APK Android
As has been said, the problem arises from Proguard (when minifyEnabledtrue ). Proguard gets rid of Type which GSON needs for parsing.
Read more >Leveraging the Gson Library | CodePath Android Cliffnotes
This library helps to avoid needing to write boilerplate code to parse JSON ... create Java objects that will be able to parse...
Read more >Parsing between JSON and Kotlin Object with Google Gson ...
Gson is capable of parsing to and from an array or a list automatically into json object and vice versa. See the below...
Read more >Java Gson - JSON serialization and deserialization ... - ZetCode
Gson by default does not serialize fields with null values to JSON. If a field in a Java object is null , Gson...
Read more >AGP 4.1-rc01: exception during AS sync [165343136]
Unable to parse settings file /Users/cpuerta/.android/analytics.settings: com.google.gson.JsonSyntaxException: Jun 23, 2020 8:14:09 AM AnalyticsSettings call ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi ! I had same issue, after 2 days investigating I discovered it is related with minify true in your graddle.
If is your case, you have to add all classes you use on the parse of GSON to your proguard-rules.txt
-keepattributes Signature -keep class sun.misc.Unsafe { *; } -keep class com.your.Package.yourclases.** { *; }
Hope it helps. Kind regards@marcosybarraa yes added classes to proguard rules work. Is there a more cleaner way to do this without adding the classes? Adding classes manually can induce errors. Any other way GSON team is aware of?