Android 23+ – Exclude GCM Registration ID from backup

After further study from @Simon answer, I think we would still need to replace the file name specified in the Path when the backup descriptor xml file is auto-generated from the quickfix in Android Studio. I check the shared_prefs directory in one of my app which has implemented the Firebase Cloud Messaging and found the following GCM settings preferences files:

enter image description here

So the backup_descriptor.xml would be:

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
    <!-- Exclude the shared preferences file that contains the GCM registrationId -->
    <exclude
        domain="sharedpref"
        path="com.google.android.gms.appid.xml"/>
    <exclude
        domain="sharedpref"
        path="com.google.android.gms.measurement.prefs.xml"/>
</full-backup-content>

I faced same issue and I was going to follow accepting answer, but turns out it seems it is not what should be done.

What I did is that I set the fullBackupContentProperty in my manifest before creating the backup_scheme.xml file. So of course, Android Studio complained with a warning but provided me a quickfix to automatically generate that file.

Applying the quickfix generated the following file:

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
    <!-- Remove the following "exclude" elements to make them a part of the auto backup -->
    <exclude
        domain="database"
        path="attendee.db" />
    <exclude
        domain="database"
        path="whova_messages.db" />
    <exclude
        domain="database"
        path="photo.db" />
    <exclude
        domain="database"
        path="agenda.db" />
    <exclude
        domain="database"
        path="ebb.db" />
    <exclude
        domain="database"
        path="vertical.db" />
    <exclude
        domain="database"
        path="whova.db" />
    <!-- Exclude the shared preferences file that contains the GCM registrationId -->
    <exclude
        domain="sharedpref"
        path="WhovaMessagePrefFile.xml" />
    <exclude
        domain="sharedpref"
        path="APP_RATER.xml" />
    <exclude
        domain="sharedpref"
        path="WhovaPrefFile.xml" />
</full-backup-content>

Note the <!-- Exclude the shared preferences file that contains the GCM registrationId -->

So my guess is that you have to exclude the entire Shared Preference file containing the GCM ID, and not just the key.

Too bad the documentation does not make it super clear and also too bad we have to exclude and entire file from the backup.

So I figured it out. In my RegistrationIntentService.java file, I store the users registrationID in the shared preferences under the string “registrationID”. So use the following in the backup_scheme.xml file…

<exclude domain="sharedpref" path="registrationID"/>

Leave a Comment