attribute, which enables/disables backup, defaults to <code>true</code> if omitted.
To avoid confusion, we recommend you explicitly set the attribute in the <code><application></code>
element of your <code>AndroidManifest.xml</code>. For example:
<pre class="prettyprint"><application ...
android:allowBackup="true">
</app></pre>
<p>To disable Auto Backup, add either of the following attributes to the
application element in your manifest file:
<ul>
<li>set <code>android:allowBackup</code> to <code>false</code>. This completely disables data
backup. You may want to disable backups when your app can recreate its state
through some other mechanism or when your app deals with sensitive
information that should not be backed up.</li>
<li>set <code>android:allowBackup</code> to <code>true</code> and
<code>android:fullBackupOnly</code> to <code>false</code>. With these settings,
your app always participates in Key/Value Backup, even when running on devices that
support Auto Backup.</li></ul>
<h2 id="IncludingFiles">Including and excluding files</h2>
<p>By default, the system backs up almost all app data. For more information,
see <a href="#Files">Files that are backed up</a>. This section shows you how to
define custom XML rules to control what gets backed up.
<ol>
<li>In <code>AndroidManifest.xml</code>, add the <a href="{@docRoot}reference/android/R.attr.html#fullBackupContent">android:fullBackupContent</a> attribute to the
<code><application></code> element. This attribute points to an XML file that contains backup
rules. For example:
<pre class="prettyprint"><application ...
android:fullBackupContent="@xml/my_backup_rules">
</app></pre></li>
<li>Create an XML file called <code>my_backup_rules.xml</code> in the <code>res/xml/</code> directory. Inside the file, add rules with the <code><include></code> and <code><exclude></code> elements. The following sample backs up all shared preferences except <code>device.xml</code>:
<p>Inside the <code><full-backup-content></code> tag, you can define <code><include></code> and <code><exclude></code>
elements:
<ul>
<li><code><include></code> - Specifies a file or folder to backup. By default, Auto Backup
includes almost all app files. If you specify an <include> element, the system
no longer includes any files by default and backs up <em>only the files
specified</em>. To include multiple files, use multiple <include> elements.
<p>note: Files in directories returned by <code>getCacheDir()</code>, <code>getCodeCacheDir()</code>, or
<code>getNoBackupFilesDir()</code> are always excluded even if you try to include them.</li>
<li><code><exclude></code> - Specifies a file or folder to exclude during backup. Here are
some files that are typically excluded from backup: <ul>
<li>Files that have device specific identifiers, either issued by a server or
generated on the device. For example, <a href="https://developers.google.com/cloud-messaging/android/start">Google Cloud Messaging (GCM)</a> needs to
generate a registration token every time a user installs your app on a new
device. If the old registration token is restored, the app may behave
unexpectedly.
<li>Account credentials or other sensitive information. Consider asking the
user to reauthenticate the first time they launch a restored app rather than
allowing for storage of such information in the backup.
<li>Files related to app debugging, such as <a href="{@docRoot}studio/run/index.html#instant-run">instant run files</a>. To exclude instant run files, add the rule <code><exclude
domain="file" path="instant-run"/></code>
<li>Large files that cause the app to exceed the 25MB backup quota.</li> </ul>
</li> </ul>
<p class="note"><strong>Note:</strong> If your configuration file specifies both elements, then the
backup contains everything captured by the <code><include></code> elements minus the
resources named in the <code><exclude></code> elements. In other words,
<code><exclude></code> takes precedence.
<p>Each element must include the following two attributes:
<ul>
<li><code>domain</code> - specifies the location of resource. Valid values for this attribute
include the following: <ul>
<li><code>root</code> - the directory on the filesystem where all private files belonging to
this app are stored.
<li><code>file</code> - directories returned by {@link android.content.Context#getFilesDir()}.
<li><code>database</code> - directories returned by {@link android.content.Context#getDatabasePath(String) getDatabasePath()}.
Databases created with {@link android.database.sqlite.SQLiteOpenHelper}
are stored here.
<li><code>sharedpref</code> - the directory where {@link android.content.SharedPreferences}
are stored.
<li><code>external</code> the directory returned by {@link android.content.Context#getExternalFilesDir(String) getExternalFilesDir()}
<p>Note: You cannot backup files outside of these locations.</li></ul>
<li><code>path</code>: Specifies a file or folder to include in or exclude from backup. Note
that: <ul>
<li>This attribute does not support wildcard or regex syntax.
<li>You can use <code>.</code> to reference the current directory, however, you cannot
reference the parent directory <code>..</code> for security reasons.
<li>If you specify a directory, then the rule applies to all files in the
directory and recursive sub-directories.</li></ul></li></ul>
<p>Apps that implement Auto Backup do not need to implement {@link android.app.backup.BackupAgent}. However, you can optionally implement a custom {@link android.app.backup.BackupAgent}. Typically, there are two reasons for doing this:
<ul>
<li>You want to receive notification of backup events such as,
{@link android.app.backup.BackupAgent#onRestoreFinished()} or {@link android.app.backup.BackupAgent#onQuotaExceeded(long,long)}. These callback methods are executed
even if the app is not running.
<li>You can't easily express the set of files you want to backup with XML rules.
In these very rare cases, you can implement a BackupAgent that overrides {@link android.app.backup.BackupAgent#onFullBackup(FullBackupDataOutput)} to
store what you want. To retain the system's default implementation, call the
corresponding method on the superclass with <code>super.onFullBackup()</code>.</li></ul>
<p class="note"><strong>Note:</strong> Your <code>BackupAgent</code> must