94

I've stored user settings in the SharedPreferences in my App. What will happen with the SharedPreferences when I update the App via Google Play Store to a new version of the App?

Will the SharedPrefernces still be there after the update or will they be deleted?

So far I haven't found an answer on the web or Stackoverflow.

Can you point me to some links they describe this process?

Edit: Meanwhile I found an other answer as well: SharedPreferences behaviour on Update/Uninstall

Edit 2: Time past quite a bit when I first asked this question I recently learned that since Android 6.0 (API 23) it is also possible to use the auto backup functionality to store your shared preferences as described by Google here.

Just add the <application android:allowBackup="true" ... > in your AndroidManifest.xml file.

3 Answers 3

77

Cristian here says: your Application data will be remain when user install updates.

But it must be with the same package name to detect as an update of previous App.

EboMike in Warning Android user that app update could lead to losing data from old app version? says:

Quite frankly, losing data due to an upgrade is unacceptable.

Edit:

Normally, the SharedPreferences(as well as other user data) will be kept during the update process, but sometimes, due to some "unknown" problem, the data may get lost, and I guess it is out of your control. So, you can simply believe that the SharedPreferences will be kept(see here).

So,if you want to avoid clearing user's data in upgrading progress,you have to save main data in external storage(this can be a removable storage media,such as an SD card or an internal,non-removable, storage.) and not private for your App.Or at least put away for user to backup data before upgrading .Then in first run of your (upgraded) App,check that is there any backup file in external storage or no.

If you like to know What things must/can happen on upgrading an App?,I did not any good description for this.It is complicated and relative with Android Security,Application signing,copy protection and other topics.I mean that if you change state of your App in any above fields,it causes different result.
For example if you CHANGED COPY PROTECTION FROM ON to OFF OR OFF to ON,your App will be updated but causes all your shared preferences to get lost,file access be impossible and ... .
You although have to be care in about conditions cause your new App being considered as an update for previous App(see Things That Cannot Change).

Also you have to be care in about your code,it may be caused deleting data of your databases(see update app with preloaded SQLite).

But ultimately, if be careful,you can say:

The update process only replaces the apk file(and so what is in it for example drawables,...) and does not alter databases,sharedpreferences and any other files that generated in run time(probably in this case,new App is installed with the UID that is equal to UID of previous App).

You can see these pages for more details:

Help!? Updating our applicatoin on the market deletes the saved SharedPreferences.
Market copy protection totally breaks file access after updating
Can someone explain the App update process?

5
  • Thanks, it seems to me that the SharedPreferences SHOULD be kept ok. But is there no official description of this process? Commented Sep 28, 2012 at 10:54
  • @viperbone which process?clareing sharedpreferences?or upgrading App? Commented Sep 28, 2012 at 10:57
  • Upgrading Apps process. What things must/can happen on upgrading an App. Commented Sep 28, 2012 at 11:02
  • @viperbone Please see my answer again.I added more details. Commented Sep 28, 2012 at 13:06
  • Another note: make sure the shared preference name is the SAME between upgrades! I had different names between versions, so the new version wasn't picking up the saved preferences from previous version... wasted an entire morning to debug this... :-(
    – henrykodev
    Commented Mar 23, 2016 at 3:16
9

After debugging for over 4 hours i found out that i was saving a model as string by serializing it. A serializable class has a unique id by name serialVersionUID, which is set by default at runtime and the id is calculated by the name of class , interfaces and the variable names as well. I found out i changed the model class, added a variable and then updated the app. Since the class is now changed so a new serialVersionUID was set and hence on update , it wasn't able to deserialize the string and create the model and was giving java.io.InvalidClassException

Explicitly set serialVersionUID to avoid this issue

static final long serialVersionUID = 42L;
1
  • 1
    But make sure that your changes are compatible or you could get wierd bugs.
    – mjaggard
    Commented Mar 26, 2018 at 21:58
6

I think when I updated my application last time via Google Play, the sharedPreferences were not affected.
I was using them to automatically log in, and after update it did so.
It was a month ago, my memory could get fuzzy so it is better to hear other people opinions too.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.