完整备份内容 xml 文件应该为空还是根本不添加以包含所有内容?

Should full backup content xml file be empty or not added at all to include all?

所以我在我的应用程序清单中添加了一些东西,我看到我的应用程序标签上有一个警告:

On SDK version 23 and up, your app data will be automatically backed up and restored on app install. Consider adding the attribute android:fullBackupContent to specify an @xml resource which configures which files to backup.

然后我搜索了那个。显然只有 2 个标签:<include><exclude>。我不想从备份中排除任何文件,因为我没有任何依赖于本地的文件,而且我不需要任何 <include> 标签,因为

<include>: Specifies a set of resources to back up, instead of having the system back up all data in your app by default.

当我看到如果我不放任何<include>标签,那么系统会back up all data in your app by default,这正是我想要的。

现在我有这个问题:我应该添加 backup_content.xml 文件,但默认设置是空的,还是根本不添加文件? (在这种情况下 Android Studio 会投诉)

我认为当您不定义任何排除或包含时它会进行完整备份。

: Specifies a set of resources to back up, instead of having the system back up all data in your app by default. If you specify an element, the system backs up only the resources specified with this element. You can specify multiple sets of resources to back up by using multiple elements

如果您想消除警告,但不需要从备份中排除任何文件,您也可以将 tools:ignore="AllowBackup" 添加到 application 标签中 AndroidManifest.xml 取消警告(或使用查看警告详细信息时获得的 Suppress 按钮)。

这是我发现的一些花絮 here

The default behaviour on Marshmallow and later for apps that target 23+ is to back everything up

如果想要此行为,我认为这是禁止警告的原因。

Auto Backup will by default not restore a backup that was created from a newer version of your app, because it assumes it may not work for you. This might be a problem if your user is migrating to a new device that has an older version of your app preinstalled, because it would mean that nothing is restored.

如果您有信心可以处理向前兼容性,您可以使用 android:restoreAnyVersion="true" 来覆盖该行为。

您可以添加 android:fullBackupContent="true" 在你的 androidmanifest.xml

快速解决方案:

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:fullBackupContent="true"
    ...
    ...
    ...
</application>

有关详细信息,请参阅:https://developer.android.com/guide/topics/data/autobackup

我也遇到了同样的问题,找不到答案。因此,为了全部备份,我尝试制作一个 backup.xml 文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
   <full-backup-content>

   </full-backup-content>

而且效果很好。

编辑 21 年 6 月 4 日

刚从Google那里得到官方答复 IssueTracker:

The answer to your question appears at the beginning of the section titled Include and exclude files: "By default, the system backs up almost all app data". In order to back up all supported data, don't include android:fullBackupContent in your manifest file.

正确答案:

警告是因为缺少 <full-backup-content> 资源。

为此,我们在应用程序标记中有 android:fullBackupContent,它指向包含自动备份的完整备份规则的 XML 文件。这些规则决定备份哪些文件。

示例:

<application ...
    android:fullBackupContent="@xml/my_backup_rules">
</application>

res/xml/ 目录中创建一个名为 my_backup_rules.xml 的 XML 文件。在文件中,使用 <include><exclude> 元素添加规则。以下示例备份除 device.xml 之外的所有共享首选项:

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
    <include domain="sharedpref" path="."/>
    <exclude domain="sharedpref" path="device.xml"/>
</full-backup-content>

更多信息参考:

https://developer.android.com/guide/topics/data/autobackup https://betterprogramming.pub/androids-attribute-android-allowbackup-demystified-114b88087e3b

<application
        android:allowBackup="true"
        android:fullBackupOnly="true">
</application>