72

My current Android application targets 12 and higher.

I do not want to allow backup of any type and currently have these manifest settings:

android:allowBackup="false"
android:fullBackupContent="false"

However the android:allowBackup="false" setting gives the following warning now:

The attribute android:allowBackup is deprecated from Android 12 and higher and may be removed in future versions. Consider adding the attribute android:dataExtractionRules specifying an @xml resource which configures cloud backups and device transfers on Android 12 and higher.

I've looked at the examples for android:dataExtractionRules xml and none of them show how to configure the equivalent of allowBackup="false".

What am I missing?

Is it possible to achieve allowBackup="false" with the use of android:dataExtractionRules xml?

Vadik Sirekanyan
  • 3,332
  • 1
  • 22
  • 29
Hector
  • 4,016
  • 21
  • 112
  • 211

2 Answers2

119

Add dataExtractionRules attribute to your AndroidManifest.xml file with a reference to data_extraction_rules.xml file:

<application
    android:allowBackup="false"
    android:fullBackupContent="false"
    android:dataExtractionRules="@xml/data_extraction_rules"
    ...>

Then, exclude all possible domains for cloud backups and d2d transfers, update or create a file app/src/main/res/xml/data_extraction_rules.xml:

<?xml version="1.0" encoding="utf-8"?>
<data-extraction-rules>
    <cloud-backup>
        <exclude domain="root" />
        <exclude domain="file" />
        <exclude domain="database" />
        <exclude domain="sharedpref" />
        <exclude domain="external" />
    </cloud-backup>
    <device-transfer>
        <exclude domain="root" />
        <exclude domain="file" />
        <exclude domain="database" />
        <exclude domain="sharedpref" />
        <exclude domain="external" />
    </device-transfer>
</data-extraction-rules>

The dataExtractionRules attribute is available for API 31 (Android 12) and higher. Keep allowBackup and fullBackupContent attributes for Android versions before API 31.

Note to maybe silence "Attribute dataExtractionRules is only used in API level 31 and higher (current min is 19)" warning, with tools:targetApi="s" attribute as well (because older platforms simply ignore manifest-attributes they don't support, and the warning is useless).

Vadik Sirekanyan
  • 3,332
  • 1
  • 22
  • 29
  • 2
    [The docs](https://developer.android.com/guide/topics/data/autobackup#xml-include-exclude) say "If you specify an `` element, the system no longer includes any files by default and backs up only the files specified". Did anybody try smth like ``? – gmk57 Jul 20 '22 at 12:49
  • *`domain` — Specifies the location of resource. Valid values for this attribute include the following: `root`, `file`, `database`, `sharedpref`, `external`.* I am not sure the empty value is a valid one. [The docs](https://developer.android.com/guide/topics/data/autobackup#xml-include-exclude) don't specify behavior for an empty value. – Vadik Sirekanyan Aug 03 '22 at 23:19
  • We don't need to specify the exact path?? for example, "???? If we don't need to specify the exact path, does it exclude everything in that domain? – CodingBruceLee Aug 10 '22 at 05:05
  • 2
    @CodingBruceLee this is a great question-- there's nothing said about what happens w/o a path, so would a better answer look like: – fattire Aug 11 '22 at 21:39
  • 3
    Also, I don't see anywhere in the docs that "false" is legit for fullBackupContent. It is meant to point to an xml file, but from a quick look at the source may still work if "false" (or any string not pointing to parsable xml resource file) is put there. See https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/backup/FullBackup.java#495 – fattire Aug 11 '22 at 21:51
  • 1
    @fattire, good point, there is a [reference](https://developer.android.com/reference/android/R.attr#fullBackupContent) that says it can be boolean. IDE also shows possible values as `boolean` or `reference` (when I open Quick Documentation, Ctrl + Q). – Vadik Sirekanyan Aug 11 '22 at 23:01
  • @vadik good catch. I didn't see that somehow, but looks like "false" is valid. now I wonder if the path is needed too in the rules... guess I could look at the source for that too but would be best to comply with the dox if it's specified. I kist don't see path as being listed as optional (ie, not in [brackets]) – fattire Aug 12 '22 at 00:29
  • After adding data extraction rules file, I was getting an error in Android studio about missing resource in base folder (or something). To resolve, I had to restart Android studio. – lenooh Sep 08 '22 at 11:59
  • You fixed it for me. The Android docs say write it `android:dataExtractionRules="backup_rules.xml"`, but that kept failing to build. Your way of writing it, `android:dataExtractionRules="@xml/backup_rules"` with the `"@xml/` in the front works – Matthew Sisinni Nov 17 '22 at 01:08
1

An important question raised in the comments section of the great answer given by @Vadik Sirekanyan was whether or not using an <exclude> element with only the domain property would exclude everything in that domain.

Having read through the docs myself, an important point is mentioned in the XML config syntax section which states:

Each <include> and <exclude> element must include the following two attributes:

domain
...

path


So, I wouldn't omit the path just to be safe.

However, an important point in the path section is given which I think answers the discussion point raised in the above comments:

  • If you specify a directory, then the rule applies to all files in the directory and recursive subdirectories.

Therefore, I think the following setup for the data extraction rules file would mimic the behaviour of allowBackup=false, by excluding everything in the domain's root and subdirectories from being backed-up:

<?xml version="1.0" encoding="utf-8"?>
<data-extraction-rules>
<cloud-backup>
   <exclude domain="root" path="." />
   <exclude domain="file" path="." />
   <exclude domain="database" path="." />
   <exclude domain="sharedpref" path="." />
   <exclude domain="external" path="."/>
</cloud-backup>
<device-transfer>
   <exclude domain="root" path="."/>
   <exclude domain="file" path="."/>
   <exclude domain="database" path="."/>
   <exclude domain="sharedpref" path="."/>
   <exclude domain="external" path="."/>
</device-transfer>
</data-extraction-rules>


I hope this additional info helps support the above answer :)

Scott Barbour
  • 374
  • 3
  • 14