Using GSON with proguard enabled

Variable names will be obfuscated with proguard, leaving you with something like

private String a;

Instead of

private String descripcionCategoria;

You can add proguard rules so some classes don’t get obfuscated. I got away with it using these:

-keepattributes Signature
# POJOs used with GSON
# The variable names are JSON key values and should not be obfuscated
-keepclassmembers class com.example.apps.android.Categorias { <fields>; }
# You can apply the rule to all the affected classes also
# -keepclassmembers class com.example.apps.android.model.** { <fields>; }

If your POJO class name is also used for parsing then you should also add the rule

-keep class com.example.apps.android.model.** { <fields>; }

In your case, annotations are not used, you would need this if you do

# Keep the annotations
-keepattributes *Annotation*

Another way to solve this problem is to use the SerializedName annotation and let the class get obfuscated. For this you will still need the -keepattributes *Annotation* rule.

import com.google.gson.annotations.SerializedName

@SerializedName("descripcionCategoria")
private String descripcionCategoria;

Leave a Comment