0

In an Android application,
I strangely see Gson conversion to Json string is different from debug build to release build
The problem is there is a to z items as following but I need another item which I've named z2
The related class is as following:

public class M4_RawData_DataClass {
    String a;//M4ProductM2;
    String b;//M4ProductM3;
    String c;//M4ProductBareCode;
    String d;//M4ProductEnable;
    String e;//M4ProductName;
    String f;//M4ProductPrice;
    String g;//M4ProductDiscount;
    String h;//M4ProductImage1;
    String i;//M4ProductImage2;
    String j;//M4ProductImage3;
    String k;//M4ProductImage4;
    String l;//M4ProductImage5;
    String m;//M4ProductImage6;
    String n;//M4ProductImage7;
    String o;//M4ProductImage8;
    String p;//M4ProductImage9;
    String q;//M4ProductImage10;
    String r;//M4ProductA;
    String s;//M4ProductB;
    String t;//M4ProductC;
    String u;//M4ProductD;
    String v;//M4ProductE;
    String w;//M4ProductF;
    String x;//M4ProductG;
    String y;//M4ProductH;
    String z;//M4ProductI;
    String z2;//M4ProductJ;

    public M4_RawData_DataClass()
    {
    }

    public String get_M4ProductM2() {
        return a;
    }
    public void set_M4ProductM2(String name) {
        this.a = name;
    }

    public String get_M4ProductM3() {
        return b;
    }
    public void set_M4ProductM3(String name) {
        this.b = name;
    }

    public String get_M4ProductBareCode() {
        return c;
    }
    public void set_M4ProductBareCode(String name) {
        this.c = name;
    }

    public String get_M4ProductEnable() {
        return d;
    }
    public void set_M4ProductEnable(String name) {
        this.d = name;
    }

    public String get_M4ProductName() {
        return e;
    }
    public void set_M4ProductName(String name) {
        this.e = name;
    }

    public String get_M4ProductPrice() {
        return f;
    }
    public void set_M4ProductPrice(String name) {
        this.f = name;
    }

    public String get_M4ProductDiscount() {
        return g;
    }
    public void set_M4ProductDiscount(String name) {
        this.g = name;
    }

    public String get_M4ProductImage1() {
        return h;
    }
    public void set_M4ProductImage1(String name) {
        this.h = name;
    }

    public String get_M4ProductImage2() {
        return i;
    }
    public void set_M4ProductImage2(String name) {
        this.i = name;
    }

    public String get_M4ProductImage3() {
        return j;
    }
    public void set_M4ProductImage3(String name) {
        this.j = name;
    }

    public String get_M4ProductImage4() {
        return k;
    }
    public void set_M4ProductImage4(String name) {
        this.k = name;
    }

    public String get_M4ProductImage5() {
        return l;
    }
    public void set_M4ProductImage5(String name) {
        this.l = name;
    }

    public String get_M4ProductImage6() {
        return m;
    }
    public void set_M4ProductImage6(String name) {
        this.m = name;
    }

    public String get_M4ProductImage7() {
        return n;
    }
    public void set_M4ProductImage7(String name) {
        this.n = name;
    }

    public String get_M4ProductImage8() {
        return o;
    }
    public void set_M4ProductImage8(String name) {
        this.o = name;
    }

    public String get_M4ProductImage9() {
        return p;
    }
    public void set_M4ProductImage9(String name) {
        this.p = name;
    }

    public String get_M4ProductImage10() {
        return q;
    }
    public void set_M4ProductImage10(String name) {
        this.q = name;
    }

    public String get_M4ProductA() {
        return r;
    }
    public void set_M4ProductA(String name) {
        this.r = name;
    }

    public String get_M4ProductB() {
        return s;
    }
    public void set_M4ProductB(String name) {
        this.s = name;
    }

    public String get_M4ProductC() {
        return t;
    }
    public void set_M4ProductC(String name) {
        this.t = name;
    }

    public String get_M4ProductD() {
        return u;
    }
    public void set_M4ProductD(String name) {
        this.u = name;
    }

    public String get_M4ProductE() {
        return v;
    }
    public void set_M4ProductE(String name) {
        this.v = name;
    }

    public String get_M4ProductF() {
        return w;
    }
    public void set_M4ProductF(String name) {
        this.w = name;
    }

    public String get_M4ProductG() {
        return x;
    }
    public void set_M4ProductG(String name) {
        this.x = name;
    }

    public String get_M4ProductH() {
        return y;
    }
    public void set_M4ProductH(String name) {
        this.y = name;
    }

    public String get_M4ProductI() {
            return z;
    }
    public void set_M4ProductI(String name) {
        this.z = name;
    }

    public String get_M4ProductJ() {
        return z2;
    }
    public void set_M4ProductJ(String name) {
        this.z2 = name;
    }
}

In debug build, there is no problem and the result string contains z2
In release build, there is A instead of z2 in the result string and not z2
The conversion function is as following:

String mJSONString = new Gson().toJson(mArrayList);

where

ArrayList<M4_RawData_DataClass> mArrayList =  = new ArrayList<>();

And the result example in release build:

[{"A":"","a":"02","b":"002","c":"000w","d":"Y","e":"00680068","f":"","g":"","h":"","i":"","j":"","k":"","l":"","m":"","n":"","o":"","p":"","q":"","r":"","s":"","t":"","u":"","v":"","w":"","x":"","y":"","z":""}]

Where there is A instead of z2

AndroidVH
  • 11
  • 6
  • Is there a reason why you are naming the fields yourself `a`, `b`, ...? Understanding or debugging the produced JSON will be pretty difficult. If you just did this to work around the ProGuard / R8 field renaming, then also the proper solution is the question linked above; you can give your fields then the name you want. In case you have already released a version of your app with these 'random' field names, see the [Gson troubleshooting guide](https://github.com/google/gson/blob/master/Troubleshooting.md#-android-app-unable-to-parse-json-after-app-update). – Marcono1234 Apr 15 '23 at 22:28
  • The reason of such naming is I send the Json string to a server and I wanted to have minimum string size, I just added @SerializedName and the issue is resolved – AndroidVH Apr 17 '23 at 04:55
  • Did you add `@SerializedName` to all fields or only to `z2`? You should probably add it to all to avoid relying on potential implementation details of how ProGuard / R8 renames (or in your use case rather preserves) field names. That would also allow you to give them more meaningful names, at least in the Java code. – Marcono1234 Apr 17 '23 at 19:26

0 Answers0