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

An Android app remembers its data after uninstall and reinstall

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

该应用旨在存储其在第一个屏幕中询问的详细信息。 在OS4.4.4版本再次卸载安装后,APP提示用户填写数据,正常。但是在 6.0 版中,相同的 install/uninstall 序列会返回最初输入的数据。

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

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

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

因为Android6有自动备份。如果您不想备份数据或者想要包含或排除某些资源,则需要调整清单 <application> 标记中的 android:allowBackupandroid:fullBackupContent。这不是错误。

有关 Android here.

上自动备份的更多信息

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 上查看图像作为三星设备的参考。其他设备和版本可能有不同的屏幕。见下图。

Automatic Restore Setting under Backup and Reset

我最近需要利用这些功能,我找到了相关文档,经过大量测试,我得出以下结论:

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

Android:fullBackupContent - 与 Google 的备份恢复一起使用 api 并且可以通过 xml 文件控制以指定要备份的内容,以及您可以实现的 BackupManager class 以进一步控制流程。

但是文档指出,并且我已经通过测试确认,只有当设备被恢复并且恢复应用程序数据进程被触发时才会发生恢复。或者当应用程序通过 adb 旁加载时它也会恢复,这就是我们 运行 通过 Android Studio 在我们的设备上测试或调试应用程序时所做的。请注意,如果您设置 android:allowbackup 但未使用 Google api 代码配置 android:fullBackupContent,则应用程序数据仅存储在本地,而如果您正确配置它,那么如果您的应用程序已备份,您获得了新设备应用程序数据存储在云中,因此可以在新设备上恢复。

只需将android:allowBackup="true"更改为manifiest.xml中的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>

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

Here 转到 android 使用自动备份备份用户数据的文档

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

添加答案:

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

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

如果您的目标是 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 is a new manifest setting (I’m guessing on ). “If true the user is prompted to keep the app’s data on uninstall”. This seems ripe for abuse, but I can see where it might be useful for some apps.

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(用于从设备清除存储在 Google 驱动器中的现有应用备份数据:设置 > 备份 > 切换 Google 一个备份关闭然后重新打开,然后再次尝试 uninstall/reinstall )

作为旁注,其他一些答案建议 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> 更改仅影响未来应用程序备份的创建(或不创建);任何现有的应用程序备份数据将仍然存在并被使用,直到被覆盖或清除(请参阅上文了解清除设备数据的说明)。