google / google/gson

GSON not able to parse on a release build

Open
#1,632 5 comments 0 reactions 0 assignees View on GitHub
proguard-r8
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

When 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 ;
```

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.* ;
}

# 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.* ; }
-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.** { ; }
-keep class com.mindvalley.module_videoplayer.model.** { ; }
-keep class com.mindvalley.mva.model.** { ; }
```

Can someone suggest how can I make parsing work without including models inside proguard?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.