Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharedPreferences lost after app update

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

  • SharedPreferences should be persistent when user does app update
  • in my case, after updating the app, they are lost
  • the issue is reproducable every time (I install old APK from Play Store and then adb install -r new.apk with the new (updated, signed, versionCode incremented) APK)

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!

like image 759
c0dehunter Avatar asked Mar 05 '23 21:03

c0dehunter


1 Answers

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.

like image 83
user3407133 Avatar answered Mar 16 '23 10:03

user3407133