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:
adb install old.apk
adb shell "su -c 'ls /data/data/com.pkg.name/shared_prefs'"
: CRC.xmladb install -r new.apk
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!