Auto backup of Android Application

Android market has increased tremendously. Every day new mobile phone gets launch, and while uninstalling our app from old phone app’s data gets lost.  As a developer it would always be a great idea backup your user’s app data to their google drive, so when they reinstall the app they can fetch back their data during next installation.

Google has made auto backup possible by uploading the app’s data to user’s google drive private account free of cost (does not include the user’s Google Drive quota)and limitation of data uploading would be 25MB per user. It saves only the most recent data, while the old data would be deleted.

Default autobackup files include:

  • Shared preferences files.
  • Files in the directory returned by getFilesDir().
  • Files in the directory returned by getDatabasePath(String), which also includes files created with the SQLiteOpenHelper class.
  • Files in directories created with getDir(String, int).
  • Files on external storage in the directory returned by getExternalFilesDir(String).

Note: Developer can also include or exclude file as per need.

Enabling and Disabling backup:

To enable backup set android:allowBackup attribute of application element in Manifest file to true, while for disabling we have set it to false

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

 

Including and Excluding the Files for backup:

By default all the above mentioned files data will be backup, but if want to exclude or include some file you can do it by creating a xml file inside res/xml/ directory and add your own set of rules for including and excluding certain file. Below would be syntax and example of the xml file

Syntax:

<full-backup-content>
    <include domain=["file" | "database" | "sharedpref" | "external" | "root"]
    path="string" />
    <exclude domain=["file" | "database" | "sharedpref" | "external" | "root"]
    path="string" />
</full-backup-content>

 

Example:

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

    Note: Here  . refers to current directory, however, you cannot reference the parent directory .. for security reasons.

    Once you have created your set of rules add this to Android Manifest under <application> tag inside android:fullBackupContent attribute

<application ...
    android:fullBackupContent="xml_file_name.xml">
  </app>

 

It would be good if you exclude the files mentioned below

  • Files that have device specific identifiers, either issued by a server or generated on the device. For example, Google Cloud Messaging (GCM)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.
  • 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.
  • Files related to app debugging, such as instant run files. To exclude instant run files, add the rule <exclude domain="file" path="instant-run"/>
  • Large files that cause the app to exceed the 25MB backup quota.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s