javaandroidgsonproguarddexguard

Gson Nested classes are null while using proguard


Warning: class 'com.google.gson.internal.bind.ReflectiveTypeAdapterFactory' is calling Class.getDeclaredFields on class.

After enabling proguard found some issues mentioned below:

  1. Nested static classes are null.
  2. List of a custom object in a class is null

Already refer the following bugs, but no luck:

Proguard issue while using GSON Using GSON with proguard enabled


Solution

  • I was able to fix this issue by doing three things to my proguard-rules.pro file:

    First, makes sure ProGuard doesn't change the names of any of your custom classes you serialize with Gson. Let's say that you have a class called Classname. To exempt it, add this to your progaurd-rules.pro:

    -keepclassmembernames class com.your.package.classname { <fields>; }
    

    (Replace com.your.package.classname with your actual package and class name)

    I had to do this for like a dozen classes. Don't forget to exempt any member variables of those classes that are also custom. Exempt inner classes with classname$innerclass instead of classname.

    Second, add the rules that the Gson library recommends. They can be found here. Here they are as of writing:

    ##---------------Begin: proguard configuration for Gson  ----------
    # Gson uses generic type information stored in a class file when working with fields. Proguard
    # removes such information by default, so configure it to keep all of it.
    -keepattributes Signature
    
    # For using GSON @Expose annotation
    -keepattributes *Annotation*
    
    # Gson specific classes
    -dontwarn sun.misc.**
    #-keep class com.google.gson.stream.** { *; }
    
    # Application classes that will be serialized/deserialized over Gson
    -keep class com.google.gson.examples.android.model.** { <fields>; }
    
    # Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
    # JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
    -keep class * extends com.google.gson.TypeAdapter
    -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>;
    }
    
    # Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
    -keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
    -keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken
    
    ##---------------End: proguard configuration for Gson  ----------
    

    Last, add in these two rules:

    -dontwarn java.lang.reflect.**
    -keep class kotlin.** { *; }
    

    These are what fixed the nested null issue I had - Of course, after I had done the above steps.