Android应用程序在卸载和重新安装后会记住数据

在开发针对所有4.1以上版本的Android应用程序时,我发现卸载应用程序并重新安装它不会清除其数据。

该应用程序被设计为存储它在第一个屏幕中询问的详细信息。 在操作系统4.4.4版本卸载并重新安装后,app会提示用户填写数据,这是正常的。但是在6.0版本中,相同的安装/卸载顺序将带回最初输入的数据。< / p >

我试图通过访问/data/data/my package folder来确保数据库在卸载后已经消失,并且在卸载期间确实删除了该文件夹。

我试图通过访问设置页面删除该应用程序,通过Titanium Backup,结果是一样的。该设备是运行v6.0的Nexus 5。

这种奇怪行为的原因是什么?

127913 次浏览

这是因为Android 6有自动备份。如果你不想备份数据,或者你想包含或排除一些资源,你需要调优清单<application>标签中的android:allowBackupandroid:fullBackupContent。这不是一个bug。

更多关于Android 在这里上的AutoBackup。

greywolf82的答案是正确的,但我想补充一些信息。

在开发我的Android应用程序时(使用Xamarin),我注意到每当我从Visual Studio重新启动应用程序时,我的数据都会恢复到几个月前的数据。不管我是简单地停止并从VS中重新运行它,还是完全卸载应用程序并重新安装它。

值得注意的是,我们从未明确地告诉应用程序存储备份。

当从Visual Studio启动时,备份似乎也覆盖了更新的数据,我们有用户使用我们的应用程序发布版本的报告,也让更新的数据被备份覆盖。

因为我不知道备份和恢复发生的确切时间,这个特性似乎只会导致问题。

我们修改了AndroidManifest,添加了以下两行代码:

android:allowBackup="false"
android:fullBackupOnly="false"

添加后,我们的AndroidManifest包含以下xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.XXXXXXX" android:versionName="8.0.0" android:installLocation="auto" android:versionCode="439">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="24" />
<application
android:label="@string/appName"
android:icon="@drawable/icon_small"
android:installLocation="internalOnly"
android:largeHeap="true"
android:allowBackup="false"
android:fullBackupOnly="false"
/>
...
</manifest>

一旦显式地将值设置为false,一切似乎都可以工作。我希望这是一个可选择的功能,但是……似乎对于没有指定值的应用程序,默认情况下它是打开的。

您应该检查设备的备份和重置设置,并关闭自动恢复(重新安装应用程序时,将恢复备份的设置和数据)。

关闭自动备份和自动恢复是不同的。如果您认为为您的应用程序启用自动备份会有帮助,那么就这样做。但如果你认为这会让终端用户意识不到他们设备的自动恢复功能已经打开,那就把它关掉吧。

在我的例子中,我关闭了allowBackup功能,但由于我已经在云上备份了以前的版本,它仍然在恢复。

Android 6.0上的三星设备参考图片。其他设备和版本可能有不同的屏幕。见下图。

Backup and Reset下的自动恢复设置

我最近需要利用这些功能,我能够找到文档,经过广泛的测试,我能够推断出以下内容:

Android:allowbackup -将备份它所在设备上的本地应用数据。

Android:fullBackupContent -与谷歌的备份恢复api一起使用,可以通过一个xml文件来控制,以指定备份的内容,以及一个BackupManager类,您可以实现对过程的进一步控制。

然而,文档声明,我已经确认测试,恢复只会发生在设备恢复和恢复应用程序数据过程被触发。或者,当应用程序通过adb侧加载时,它也会恢复,这是我们通过Android Studio在设备上运行应用程序进行测试或调试时所做的事情。请注意,如果你设置了android:allowbackup,但没有配置android:fullBackupContent与谷歌api代码,那么应用程序数据只存储在本地,而如果你配置正确,那么如果你的应用程序被备份,你得到一个新设备,应用程序数据存储在云上,所以它可以恢复到一个新设备。

只需在manifest .xml中将android:allowBackup="true"更改为android:allowBackup="false"即可。它会成功的。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<application
android:allowBackup="false"
android:icon="@mipmap/app_icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
</manifest>

在Manifest文件的应用标签下添加android:allowBackup="false"解决了我的问题。

在这里是用于自动备份用户数据的android文档

除此之外,我们还发现在Android 9(在HMD诺基亚设备上),即使在通过界面和adb删除应用后,资产仍被保留。

添加的答案:

< p > <代码> android: allowBackup =“false” android: fullBackupOnly = " false " < /代码> < / p >

显然,这不是一个新的答案,而是对与我们处于相同位置的人的观察。

如果你的目标是android 10,那么你必须把android:hasFragileUserData="true"放在AndroidManifest.xml的应用程序标签中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:name=".MyApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:allowBackup="true"
android:hasFragileUserData="true">


.....


</application>


</manifest>

android:hasFragileUserData是一个新的清单设置(我猜)。“如果为真,则提示用户在卸载时保留应用程序的数据”。这似乎是被滥用的时机,但我可以看到它在某些应用程序中可能有用的地方。

看到https://commonsware.com/blog/2019/06/06/random-musings-q-beta-4.html

我还补充说:

tools:replace="android:allowBackup"

重写已使用组件中的相同选项

这个答案总结了多个现有的答案,包括Android 12推出的最新细节,并包括清除设备上现有应用备份数据的说明。

更多信息,请参见 https://developer.android.com/guide/topics/data/autobackup#EnablingAutoBackup https://developer.android.com/about/versions/12/behavior-changes-12#backup-restore https://developer.android.com/guide/topics/manifest/application-element http://android-doc.github.io/preview/backup/index.html(用于从设备中清除存储在谷歌驱动器中的现有应用程序备份数据:Settings >备份在

. toggle谷歌一次备份关闭,然后重新启动,然后重试卸载/重新安装)

作为旁注,其他一些答案建议android:fullBackupContent="false",但这似乎不再正确,因为它目前意味着指定一个特定格式的xml文件,而不是true/false。

<application>的这些属性允许禁用或配置Android自动备份功能的细节。

<application
tools:replace="android:label, android:icon, android:allowBackup, '...any other attribute you want to override with a value you set in this file for in case dependencies set them to other values...'"
'...your other attributes set here like android:label and android:icon...'
android:allowBackup="false" '...default is true, and setting this false prevents data backups of any kind (except device to device transfers if your app targets Android 11 (API 30) or higher)...'
android:fullBackupContent="@xml/backup_rules_android_11_and_below" '...optional, for Android 11 and below, referring to a file res/xml/backup_rules_android_11_and_below.xml you need to create...'
android:dataExtractionRules="@xml/backup_rules_android_12_and_above" '...optional, for Android 12 and above (fullBackupContent still needed along with this, assuming you support Android 11 and below), referring to a file res/xml/backup_rules_android_12_and_above.xml you need to create, with a slightly different required xml format...'
android:fullBackupOnly="false" '...optional, and default is false, but if set to true this field description says it enables auto backup on Android 6 (API 23) devices or higher (I am not sure how this matters compared to the more broadly reaching allowBackup)...'
android:hasFragileUserData="false" '...optional, and default is false, but if set to true this field description says it gives the user an option when they uninstall the app whether or not to backup their app data...'
>
'...contents of application element...'
</application>

<application>的变化只影响未来应用备份的创建(或不创建);任何现有的应用程序备份数据将仍然存在并被使用,直到被覆盖或清除(参见上面的说明清除设备的数据)。