c0dehunter
c0dehunter

Reputation: 6160

SharedPreferences lost after app update

I've been researching this issue for the whole day. Here are key points:


8 hours later

For test I changed SharedPrefs filename in new.apk (SP2.xml) and upon updating, the old SharedPrefs file from old.apk (SP.xml) got deleted! Here is adb shell output:

  1. adb install old.apk

  2. adb shell "su -c 'ls /data/data/com.pkg.name/shared_prefs'": CRC.xml

  3. adb install -r new.apk

  4. adb shell "su -c 'ls /data/data/com.pkg.name/shared_prefs'": CRC2.xml (CRC.xml missing!)

My SharedPreferences singleton class (init: SharedPrefs.init(getApplicationContext());):

public final class SharedPrefs {
    private static SharedPrefs sp;

    private SharedPrefs() {
    }

    public static void init(Context context) {
        if (sp == null)
            sp = context.getSharedPreferences("CRC2", Context.MODE_PRIVATE);
    }

    public static void saveString(String name, String value) {
        sp.edit().putString(name, value).apply();
    }

    public static String getString(String key, String defaultValue) {
      sp.getString(key, defaultValue);
    }
    ...
}

So basically I am loosing SharedPreferences and I have no clue why. Please help, any hint welcome!

Upvotes: 9

Views: 6192

Answers (1)

user3407133
user3407133

Reputation: 176

If you changed a property in the application section of the manifest file, this error will occur and 90% of the time, the shared pref data will be reset. This is what I found from my test installing the signed apk on top of my play store app. Not sure what will happen if the app was installed from the play store as an update, but am pretty sure the data would be lost in that case as well.

EDIT- I republished the application and tested multiple times. This in fact is the issue.

Upvotes: 14

Related Questions