1page.title=Configuring Auto Backup for Apps 2page.tags=backup, marshmallow, androidm 3page.keywords=backup, autobackup 4page.image=images/cards/card-auto-backup_2x.png 5 6@jd:body 7 8<div id="tb-wrapper"> 9<div id="tb"> 10<h2>This lesson teaches you to</h2> 11<ol> 12 <li><a href="#configuring">Configure Data Backup</a></li> 13 <li><a href="#previous-androids">Support Lower Versions of Android</a></li> 14 <li><a href="#testing">Test Backup Configuration</a></li> 15 <li><a href="#issues">Handle Google Cloud Messaging</a></li> 16</ol> 17 <h2>You should also read</h2> 18 <ul> 19 <li><a href="{@docRoot}guide/topics/data/backup.html">Data Backup</a></li> 20 <li><a href="{@docRoot}training/backup/backupapi.html">Using the Backup API</a> 21 </li> 22 </ul> 23 24</div> 25</div> 26 27<p> 28 Users frequently invest time and effort to configure apps just the way they like them. Switching 29 to a new device can cancel out all that careful configuration. For apps whose <a href= 30 "{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">target SDK version</a> 31 is Android 6.0 (API level 23) and higher, devices running Android 6.0 and higher automatically 32 back up app data to the cloud. The system performs this automatic backup 33 for nearly all app data by default, and does so without your having to write any additional app 34 code. 35</p> 36 37<p class="note"> 38<strong>Note:</strong> To protect user privacy, the device user must have opted in to Google 39services for Auto Backup to work. The Google services opt-in dialog appears when the user goes 40through the Setup Wizard or configures the first Google account on the device. 41</p> 42 43<p> 44 When a user installs your app on 45 a new device, or reinstalls your app on one (for example, after a factory reset), the system 46 automatically restores the app data from the cloud. This lesson provides information about how to 47 configure the Auto Backup for Apps feature, explaining its default behavior and how to 48 exclude data that you don't want the system to back up. 49</p> 50 51<p> 52 The automatic backup feature preserves the data your app creates on a user device by uploading it 53 to the user’s Google Drive account and encrypting it. There is no charge to you or the user for 54 data storage, and the saved data does not count towards the user's personal Google Drive quota. 55 Each app can store up to 25MB. Once its backed-up data reaches 25MB, the app no longer sends 56 data to the cloud. If the system performs a data restore, it uses the last data snapshot that 57 the app had sent to the cloud. 58</p> 59 60<p>Automatic backups occur when the following conditions are met:</p> 61 <ul> 62 <li>The device is idle.</li> 63 <li>The device is charging.</li> 64 <li>The device is connected to a Wi-Fi network.</li> 65 <li>At least 24 hours have elapsed since the last backup.</li> 66 </ul> 67</p> 68 69<h2 id="configuring">Configure Data Backup</h2> 70 71<p> 72 On devices running Android 6.0 (API level 23) or higher, the default system behavior is to back up 73 almost all data that an app creates. The exception is <a href="#auto-exclude"> 74 automatically excluded data files</a>. This section explains how you can use settings in 75 your app <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a> to further 76 limit and configure what data the system backs up. 77</p> 78 79<h3 id="include-exclude">Including or excluding data</h3> 80 81<p> 82 Depending on what data your app needs and how you save it, you may need to set specific 83 rules for including or excluding certain files or directories. Auto Backup for Apps 84 lets you set these backup rules through the app manifest, in which you specify a backup scheme 85 configuration XML file. For example: 86</p> 87 88<pre> 89<?xml version="1.0" encoding="utf-8"?> 90<manifest xmlns:android="http://schemas.android.com/apk/res/android" 91 xmlns:tools="http://schemas.android.com/tools" 92 package="com.my.appexample"> 93 <uses-sdk android:minSdkVersion="23"/> 94 <uses-sdk android:targetSdkVersion="23"/> 95 <application ... 96<strong> android:fullBackupContent="@xml/mybackupscheme"></strong> 97 </app> 98 ... 99</manifest> 100</pre> 101 102<p> 103 In this example, the <code>android:fullBackupContent</code> attribute specifies an XML file 104 called {@code mybackupscheme.xml}, which resides in the <code>res/xml/</code> directory of your 105 app development project. This configuration file contains rules controlling which files are backed 106 up. The following example code shows a configuration file that excludes a specific file, 107 {@code device_info.db}: 108</p> 109 110<pre> 111<?xml version="1.0" encoding="utf-8"?> 112<full-backup-content> 113 <exclude domain="database" path="device_info.db"/> 114</full-backup-content> 115</pre> 116 117<h3 id="auto-exclude">Automatically excluded data files</h3> 118 119<p> 120 Most apps do not need to, and in fact should not, back up all data. For example, the system 121 should not back up temporary files and caches. For this reason, the automatic backup 122 service excludes certain data files by default: 123</p> 124 125<ul> 126 <li>Files in the directories to which the 127 {@link android.content.Context#getCacheDir getCacheDir()} and 128 {@link android.content.Context#getCodeCacheDir getCodeCacheDir()} methods refer. 129 </li> 130 131 <li>Files located on external storage, unless they reside in the directory to which the 132 {@link android.content.Context#getExternalFilesDir getExternalFilesDir()} method refers. 133 </li> 134 135 <li>Files located in the directory to which the 136 {@link android.content.Context#getNoBackupFilesDir getNoBackupFilesDir()} method refers. 137 </li> 138</ul> 139<h3>Backup Configuration Syntax</h3> 140 141<p> 142 The backup service configuration allows you to specify what files to include or exclude from 143 backup. The syntax for the data backup configuration XML file is as follows: 144</p> 145 146<pre> 147<full-backup-content> 148 <include domain=["file" | "database" | "sharedpref" | "external" | "root"] 149 path="string" /> 150 <exclude domain=["file" | "database" | "sharedpref" | "external" | "root"] 151 path="string" /> 152</full-backup-content> 153</pre> 154 155<p> 156 The following elements and attributes allow you to specify the files to include in, and exclude 157 from, backup: 158</p> 159 160<ul> 161 <li> 162 <code><include></code>: Specifies a set of resources to 163 back up, instead of having the system back up all data in your app by default. If you specify 164 an <code><include></code> element, the system backs up <em>only the resources specified</em> 165 with this element. You can specify multiple sets of resources to back up by using multiple 166 <code><include></code> elements 167 </li> 168 169 <li> 170 <code><exclude></code>: Specifies any data you want the system to exclude 171 when it does a full backup. If you target the same set of resources with both the 172 <code><include></code> and <code><exclude></code> elements, 173 <code><exclude></code> takes precedence. 174 </li> 175 176 <li> 177 <code>domain</code>: Specifies the type of resource you want to include in, 178 or exclude from, backup. Valid values for this attribute include: 179 180 181 182 <ul> 183 <li> 184 <code>root</code>: Specifies that the resource is in the app’s root directory. 185 </li> 186 187 <li> 188 <code>file</code>: Specifies a resource in the directory returned by the 189 {@link android.content.Context#getFilesDir getFilesDir()} method. 190 </li> 191 192 <li> 193 <code>database</code>: Specifies a database that the 194 {@link android.content.Context#getDatabasePath getDatabasePath()} method returns, or that 195 the app interacts with via the {@link android.database.sqlite.SQLiteOpenHelper} class. 196 </li> 197 198 <li> 199 <code>sharedpref</code>: Specifies a {@link android.content.SharedPreferences} object 200 that the {@link android.content.Context#getSharedPreferences getSharedPreferences()} 201 method returns. 202 </li> 203 204 <li> 205 <code>external</code>: Specifies that the resource is in external storage, and corresponds 206 to a file in the directory that the 207 {@link android.content.Context#getExternalFilesDir getExternalFilesDir()} method returns. 208 </li> 209 </ul> 210 </li> 211 <li> 212 <code>path</code>: Specifies the file path to a resource that you want to include in, or 213 exclude from, backup. 214 </li> 215 216 </li> 217</ul> 218 219 220<h3 id="disabling">Disabling data backups</h3> 221 222<p> 223 You can choose to prevent automatic backups of any of your app data by setting the 224 <code>android:allowBackup</code> attribute to <code>false</code> in the {@code app} element of 225 your manifest. This setting is illustrated in the following example: 226</p> 227 228<pre> 229<?xml version="1.0" encoding="utf-8"?> 230<manifest xmlns:android="http://schemas.android.com/apk/res/android" 231 xmlns:tools="http://schemas.android.com/tools" 232 package="com.my.appexample"> 233 <uses-sdk android:minSdkVersion="23"/> 234 <uses-sdk android:targetSdkVersion="23"/> 235 <application ... 236<strong> android:allowBackup="false"></strong> 237 </application> 238 ... 239</manifest> 240</pre> 241 242<h2 id="previous-androids">Support Lower Versions of Android</h2> 243 244<p>There are two scenarios in which you may also need to support versions of Android lower 245than 6.0 (API level 23): You may be updating your existing app to take advantage of the 246new auto backup functionality in Android 6.0, while wanting 247to continue supporting earlier versions of Android. Or you may be releasing a new app, but 248want to make sure devices running on versions of Android predating 6.0 also have backup 249functionality.</p> 250 251<h3 id="updating">Updating an existing app to support auto backup</h3> 252 253<p>Earlier versions of Android supported a key/value-pair-based backup mechanism, in which the app 254defines a subclass of {@link android.app.backup.BackupAgent} and sets 255<a href="{@docRoot}guide/topics/manifest/application-element.html#agent"> 256{@code android:backupAgent}</a> in its 257<a href="{@docRoot}guide/topics/manifest/application-element.html">app manifest</a>. If your app 258used this legacy approach, you can transition to full-data backups by adding the 259{@code android:fullBackupOnly="true"} attribute to the 260<a href="{@docRoot}guide/topics/manifest/application-element.html">{@code <application/>}</a> 261element in the manifest. When running on a device with Android 5.1 262(API level 22) or lower, your app ignores this value in the manifest, and continues performing 263backups in the previous manner.</p> 264 265<p>Even if you’re not using key/value backups, you can still use the approach described above to do 266any custom processing in {@link android.app.Activity#onCreate(android.os.Bundle) onCreate()} 267or {@link android.app.backup.BackupAgent#onFullBackup onFullBackup()}. You can also use that 268approach to receive a notification when a restore operation happens in 269{@link android.app.backup.BackupAgent#onRestoreFinished onRestoreFinished()}. If you want to retain 270the system's default implementation of 271<a href="#include-exclude">XML include/exclude rules handling</a>, call 272{@link android.app.backup.BackupAgent#onFullBackup super.onFullBackup()}.</p> 273 274<h3 id="lower-versions">Giving your new app support for lower versions of Android</h3> 275 276<p>If you are creating a new app that targets Android 6.0, but you also want to enable cloud backup 277for devices running on Android 5.1 (API level 22) and lower, you must also 278<a href="{@docRoot}training/backup/backupapi.html">implement the Backup API</a>.</p> 279 280<h2 id="testing">Test Backup Configuration</h2> 281 282<p> 283 Once you have created a backup configuration, you should test it to make sure your app saves data 284 and can restore it properly. 285</p> 286 287 288<h3>Enabling Backup Logging</h3> 289 290<p> 291 To help determine how the backup feature is parsing your XML file, enable logging before 292 performing a test backup: 293</p> 294 295<pre class="no-pretty-print"> 296$ adb shell setprop log.tag.BackupXmlParserLogging VERBOSE 297</pre> 298 299<h3>Testing Backup</h3> 300 301<p>To manually run a backup, first initialize the Backup Manager by executing the following 302 command: 303</p> 304 305<pre class="no-pretty-print"> 306$ adb shell bmgr run 307</pre> 308 309<p> 310 Next, manually back up your application using the following command. Use the 311 <code><PACKAGE></code> parameter to specify the package name for your app: 312</p> 313 314<pre class="no-pretty-print"> 315$ adb shell bmgr fullbackup <PACKAGE></pre> 316 317 318<h3>Testing restore</h3> 319 320<p> 321 To manually initiate a restore after the system has backed up your app data, execute the following 322 command, using the <code><PACKAGE></code> parameter to specify the package name for your 323 app: 324</p> 325 326<pre class="noprettyprint"> 327$ adb shell bmgr restore <PACKAGE> 328</pre> 329 330<p class="warning"> 331 <b>Warning:</b> This action stops your app and wipes its data before performing the restore 332 operation. 333</p> 334 335<p> 336 You can test automatic restore for your app by uninstalling and reinstalling your app. The app 337 data is automatically restored from the cloud once the app installation is complete. 338</p> 339 340 341<h3>Troubleshooting backups</h3> 342 343<p> 344 If backup fails, you can clear the backup data and associated metadata either by turning backup 345 off and on in <strong>Settings > Backup</strong>, factory-resetting the device, or 346 executing this command: 347</p> 348 349<pre>$ adb shell bmgr wipe <TRANSPORT> <PACKAGE></pre> 350 351<p> 352 You must prepend <code>com.google.android.gms</code> to the {@code <TRANSPORT>} value. 353 To get the list of <a href="{@docRoot}google/backup/index.html">transports</a>, execute the 354 following command: 355</p> 356 357<pre>$ adb shell bmgr list transports</pre> 358 359<h2 id="gcm">Handle Google Cloud Messaging</h2> 360 361 <p> 362 For apps that use <a href="https://developers.google.com/cloud-messaging/gcm">Google Cloud 363 Messaging</a> (GCM) for push notifications, backing up the registration 364 token that Google Cloud Messaging registration returned can cause unexpected behavior in 365 notifications for the restored app. This is because when a user installs your app on a new device, 366 the app must <a href="https://developers.google.com/cloud-messaging/android/client#sample-register"> 367 query the GCM API for a new registration token</a>. If the old registration is present, because the 368 system had backed it up and restored it, the app doesn't seek the new token. To prevent this issue 369 from arising, exclude the registration token from the set of backed-up files. 370 </p> 371