Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Android M Auto Backup for Apps support

David Eriksson edited this page May 16, 2017 · 7 revisions

The new Android M Auto Backup for Apps feature backups all app data automatically when an app is installed on an Android M Device. This can cause big problems when unwanted data gets restored from a backup, such as a device specific GCM registration ID.

From the Android Documentation:

Known Issues

The following are known issues with the automatic backup service:

  • Google Cloud Messaging - For apps that use Google Cloud Messaging for push notifications, there is a known issue where backing up the registration ID returned by Google Cloud Messaging registration can break push notifications for the restored app. It is important to query the API for a new registration ID after being installed on a new device, which is not be the case if the old registration ID was backed up. To avoid this, exclude the registration id from the set of backed up files.

Excluding Tray modules or specific keys from a backup wasn't possible with v0.9.2 and below. Everything was saved in a single database. Starting with Tray v1.0.0 the database was split up into two, one for modules with user specific data and one for device specific data. Following the recommendation from Google, device specific data shouldn't be part of a backup. To do this, every project including Tray has to define auto backup rules.

Configuration

Add a reference to a backup definition reference with fullBackupContent to your <application/> tag in the AndroidManifest.xml:

<manifest
    ...
    xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:fullBackupContent="@xml/mybackupscheme" <---- reference your full-backup-content xml
        android:icon="@mipmap/ic_launcher"
        ...>
        <activity .../>
    </application>
</manifest>

Add one (you cannot use both!) of the Tray rules to your /res/xml/mybackupscheme.xml, either exclude or include:

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

    <!--exclude device specific tray data from backup-->
    <exclude
        domain="database"
        path="tray_backup_excluded.db" />

    <!--OR-->
    <!--include user specific tray data into the backup-->
    <include
        domain="database"
        path="tray.db" />

</full-backup-content>
tag description
<include> the system backs up only the resources specified with this element.
<exclude> The system backs up all data in your app, except for resources specified with this element.
You cannot use both!

Implementation

You can define modules to be user or device specific in the constructor as 4th parameter.

Use TrayStorage.Type.USER to backup the preferences saved in this module and TrayStorage.Type.DEVICE to exclude those preferences from the backup. TrayStorage.Type.USER is default

private class MyPreference extends TrayPreferences {

    public MyPreference(@NonNull final Context context) {
        super(context, "myModule", VERSION, TrayStorage.Type.USER);
    }
}
Clone this wiki locally