简体   繁体   中英

Release build issues in converting json to list

I have a json file in main/assets folder -

[
  {
    "topic": "Studies",
    "subMessage": "....",
    "people": [
      ....
    ]
  },
  {
    "topic": "....",
    "subMessage": "...",
    "people": []
  }
]

I use this method below to parse the json and return a custom list of mine -

final List<MyList> readJson() {
        try {
            InputStream raw = context.getAssets().open(context.getResources().getString(R.string.read_the_json)); 
            Reader reader = new InputStreamReader(raw, "UTF-8);
            return new Gson().fromJson(reader, new TypeToken<List<MyList>>() {
            }.getType());
        } catch (Exception e) {
            Log.e(TAG, "Error : " + e.getMessage());
            return null;
        }
    }

For Release build, I have minifyEnabled true . But for debug build it is false.

What I found is if I set minifyEnabled to false in release build it works but I need proguard for release builds.

The problem is in Release build the method returns me [][] . But in debug build, the method returns me the values from JSON.

What could be the problem?

It happened because the proguard tool modified your class. You will need to stop proguard from obfuscating the model classes.

Not so good but easiest way:

  • Make all your model classes such as MyList implement the interface Serializable .

  • Then in the file proguard-rules.pro , add the following line:

    -keepclassmembers class * implements java.io.Serializable { *; }

Better Way:

  • Directly include your model class in the file proguard-rules.pro file. For eg.

    -keepclassmembers class com.example.MyList { *; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM