Using GSON with proguard enabled

前端 未结 5 1852
太阳男子
太阳男子 2020-12-01 03:45

My code works perfectly without proguard, but GSON doesn\'t work when proguard is enabled.

This is the part of code where it doesn\'t works

JSONArray         


        
5条回答
  •  旧巷少年郎
    2020-12-01 04:10

    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 { ; }
    # You can apply the rule to all the affected classes also
    # -keepclassmembers class com.example.apps.android.model.** { ; }
    

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

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

    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;
    

提交回复
热议问题