Gson mapping to pojo returns null when running release variant with proguad enabled, even with @SerializedName added

Viewed 388

Please note that some might suggest i add the model class to the proguard -keep, i emphasize that i don't want to do that, due to security restriction guidelines imposed by my employer, i'm looking for a solution that doesn't involve editing proguard rules

Using Gson to parse the a Json formatted string into an array mapped to a Pojo, everything works fine is debug variant but when in release build with proguard enabled, the properties of the mapped Pojo objects return null causing the application to crash, below is the model/pojo class:

package com.xx.xxxxxxx


import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;

public class BeneficiaryModel implements Serializable {

    @SerializedName("beneficiaryId")
    @Expose
    private String beneficiaryId;

    @SerializedName("beneficiaryName")
    @Expose
    private String beneficiaryName;

    @SerializedName("beneficiaryType")
    @Expose
    private String beneficiaryType;

    @SerializedName("status")
    @Expose
    private String status;


    public String getBeneficiaryId() {
        return beneficiaryId;
    }


    public void setBeneficiaryId(String beneficiaryId) {
        this.beneficiaryId = beneficiaryId;
    }


    public String getBeneficiaryName() {
        return beneficiaryName;
    }


    public void setBeneficiaryName(String beneficiaryName) {
        this.beneficiaryName = beneficiaryName;
    }


    public String getBeneficiaryType() {
        return beneficiaryType;
    }


    public void setBeneficiaryType(String beneficiaryType) {
        this.beneficiaryType = beneficiaryType;
    }


    public String getStatus() {
        return status;
    }


    public void setStatus(String status) {
        this.status = status;
    }
}

and this is how i serilaize the json string to array of objects

 Gson gson = new Gson();
clientes = gson.fromJson(beneficiaryStringJsonArray, BeneficiaryModel[].class);
models =  new ArrayList<BeneficiaryModel>(Arrays.asList(clientes));

Log.e("TAG", models.get(0).getBeneficiaryId() + "," +  models.get(0).getBeneficiaryName());
// Log returns  null , null

Here's the portion of the Json string response needs to be serialized:

[{"beneficiaryId":"XYZ54646464646","beneficiaryName":"xxxxxxxxxxxxxx1","beneficiaryType":"","status":"ACTIVE"},{"beneficiaryId":"XYZ5468524654","beneficiaryName":"xxxxxxxxxxxxx2","beneficiaryType":"","status":"ACTIVE"}]

( as i mentioned before everything works fine in debug mode )

1 Answers

The only solution i found that worked for me was using jackson library, i only had to add a few rules to my proguard without adding the models or any other files in my app. here's the rules you'll need for jackson

# Proguard configuration for Jackson 2.x
-keep class com.fasterxml.jackson.databind.ObjectMapper {
    public <methods>;
    protected <methods>;
}

-keep class com.fasterxml.jackson.databind.ObjectWriter {
    public ** writeValueAsString(**);
}
-keepnames class com.fasterxml.jackson.** { *; }
-dontwarn com.fasterxml.jackson.databind.**

Of course i'm still open to suggestion regarding how to solve issue using Gson, appreciate any suggestions

Related