0

I have an app on the play store where Shared Preference entries are stored as json strings. I'm trying to update the app but I noticed that I'm unable to load any of the stored shared preference data correctly after the app is updated, meaning all the user's stored preferences will be lost if they update.

So say I have a class like this:

public class BrightnessSetting {
    public int systemAutoBrightness;
    public boolean customAutoBrightness;
    public int brightnessLevel;
    public float adjustmentLevel;
    public boolean playSound;
    public String soundFile;
}

Objects of this class are saved to the app's shared preferences in json format, which just transforms it into a long string. When the release app bundle is generated it shrinks this string down by replacing the variable names with letters because it's using ProGuard. Meaning the end result will be like this: {"a": 0, "b": false, "c": 25, "d": 0.0, "e": false}. After testing I found that without ProGuard everything works fine, but with ProGuard I'm no longer able to retrieve the data after the app is updated. The Shared Preference keys are still correct, and their corresponding json strings are still correct, but when I try to transform the json string back into its original form as a BrightnessSetting object then I get an empty object.

How can I get the original object back from the json string?

  • https://stackoverflow.com/a/12637972/10002974 This might help you. – Piyush Maheswari Jan 01 '21 at 18:05
  • I read the second answer in that post, the one about `serialVersionUID`. I tried searching and couldn't figure out if it was the source of my problem, nor could I find any info on how to get the serialVersionUID of a class nor set it. –  Jan 01 '21 at 18:18
  • Any ideas? .. :) –  Jan 01 '21 at 19:28
  • I've been stuck on this bug for 2 days. Please help –  Jan 01 '21 at 22:53

1 Answers1

0

Add this to your proguard config:

-keepclassmembernames class your.path.to.BrightnessSetting

If you are using Gson to convert your class to and from a string, you need to prevent proguard from obfuscating the class's members (which Gson needs in order to work).

Ideally, you should store your information in another way so as to not have to rely on this method. Any changes to BrightnessSetting between updates could potentially break backwards compatibility.