更新 Android 应用程序时 SharedPreferences 会发生什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12637737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:16:36  来源:igfitidea点击:

What will happen to the SharedPreferences on update an Android App?

androidsharedpreferencesupdates

提问by Bruno Bieri

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?

我已经在我的应用程序的 SharedPreferences 中存储了用户设置。当我通过 Google Play 商店将应用程序更新到应用程序的新版本时,SharedPreferences 会发生什么?

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

更新后 SharedPrefernces 是否仍然存在还是会被删除?

So far I haven't found an answer on the web or Stackoverflow (may I search with wrong keywords?).

到目前为止,我还没有在网络或 Stackoverflow 上找到答案(我可以用错误的关键字搜索吗?)。

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

你能指出一些他们描述这个过程的链接吗?

Edit:Meanwhile I found a other answer too: SharedPreferences behaviour on Update/Uninstall

编辑:同时我也找到了另一个答案:更新/卸载时的 SharedPreferences 行为

Edit 2:Since 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 secureyour shared preferences as described by Google here. Just add the allowBackup="true"in your AndroidManifest.xmlfile.

编辑 2:自从我第一次问这个问题时已经过去了很久,我最近了解到,自 Android 6.0 (API 23) 以来,还可以使用自动备份功能来保护您的共享首选项,如谷歌此处所述。只需allowBackup="true"在您的AndroidManifest.xml文件中添加。

采纳答案by hasanghaforian

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

克里斯蒂安在这里说:当用户安装更新时,您的应用程序数据将保留。

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

但必须与package name之前的应用程序更新检测相同。

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

EboMike警告 Android 用户应用更新可能导致旧应用版本丢失数据?说:

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).

通常情况下,SharedPreferences(以及其他用户数据)会在更新过程中保留,但有时,由于某些“未知”问题,数据可能会丢失,我猜这超出了您的控制。因此,您可以简单地相信 SharedPreferences 将被保留(请参阅此处)。

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.

因此,如果您想避免在升级过程中清除用户数据,您必须将主要数据保存在外部存储中(可以是可移动存储介质,例如 SD 卡或内部不可移动存储。)而不是为您的应用程序私有。或者至少在升级前为用户备份数据。然后在您(升级后的)应用程序的第一次运行时,检查外部存储中是否有任何备份文件或没有。

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 protectionand 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 consideredas an update for previous App(see Things That Cannot Change).

如果您想知道升级应用程序时必须/可能发生哪些事情?,我没有什么好介绍的this.It是复杂的和相对有Android SecurityApplication signingcopy protection和其他topics.I意味着,如果你在任何上述领域改变你的应用程序的状态,它会导致不同的结果。
例如,如果您CHANGED COPY PROTECTION FROM ON to OFF OR OFF to ON的应用程序将被更新,但会导致您的所有共享首选项丢失,文件访问将无法进行以及......。
您虽然必须注意条件导致您的新应用程序being considered作为以前应用程序的更新(请参阅无法更改的事情)。

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).

此外,您必须注意您的代码,这可能会导致删除您的数据库数据(请参阅使用预加载的 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).

更新过程仅替换 apk 文件(以及其中的内容,例如可绘制对象,...),并且不会更改数据库、sharedpreferences 和运行时生成的任何其他文件(可能在这种情况下,新应用程序安装在与之前 App 的 UID 相同的 UID)。

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?

帮助!?在市场上更新我们的应用程序会删除保存的 SharedPreferences。
更新后市场复制保护完全中断文件访问
有人可以解释应用程序更新过程吗?

回答by abhishek

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 serialVersionUIDwas set and hence on update , it wasn't able to deserialize the string and create the model and was giving java.io.InvalidClassException

调试超过 4 个小时后,我发现我通过序列化将模型保存为字符串。一个可序列化的类有一个名为serialVersionUID的唯一 id ,它是在运行时默认设置的,id 也是由类的名称、接口和变量名称计算出来的。我发现我更改了模型类,添加了一个变量,然后更新了应用程序。由于类现在已更改,因此设置了新的serialVersionUID,因此在 update 时,它​​无法反序列化字符串并创建模型并给出 java.io.InvalidClassException

Explicitly set serialVersionUID to avoid this issue

显式设置 serialVersionUID 以避免此问题

static final long serialVersionUID = 42L;

回答by Lazy Ninja

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.

我想当我上次通过 Google Play 更新我的应用程序时,sharedPreferences 没有受到影响。
我使用它们自动登录,更新后它这样做了。
那是一个月前,我的记忆会变得模糊,所以最好也听听别人的意见。