I'm trying to get my app to auto-backup and restore its shared preferences as per the official docs. Both backup and restore work fine on an Android 6.0 device. However, although the backup works on pre-Android 6 devices, the restore fails. The precise error messages I see in logcat depend on the device being tested. Examples:
Samsung Galaxy S3 Mini, Android 4.1.2
07-02 11:05:00.240 2013-24968/? V/BackupManagerService: beginRestoreSession: pkg=com.my.app transport=null
07-02 11:05:00.260 2013-2024/? V/RestoreSession: restorePackage pkg=com.my.app obs=android.app.backup.IRestoreObserver$Stub$Proxy@42364000
07-02 11:05:00.260 2013-2119/? D/BackupManagerService: MSG_RUN_RESTORE observer=android.app.backup.IRestoreObserver$Stub$Proxy@42364000
07-02 11:05:01.340 2013-2119/? D/BackupManagerService: initiateOneRestore packageName=@pm@
07-02 11:05:01.420 2013-2119/? V/BackupServiceBinder: doRestore() invoked
07-02 11:05:01.500 2013-2119/? V/BackupManagerService: Package com.my.app restore version [24] is compatible with installed version [24]
07-02 11:05:01.530 2013-2119/? D/BackupManagerService: awaiting agent for ApplicationInfo{4240d338 com.my.app}
07-02 11:05:01.651 2013-2022/? D/BackupManagerService: agentConnected pkg=com.my.app agent=android.os.BinderProxy@424b9d48
07-02 11:05:01.651 2013-2119/? I/BackupManagerService: got agent android.app.IBackupAgent$Stub$Proxy@4274be90
07-02 11:05:01.651 2013-2119/? D/BackupManagerService: initiateOneRestore packageName=com.my.app
07-02 11:05:01.661 14846-14857/? V/BackupServiceBinder: doRestore() invoked
07-02 11:05:01.661 14846-14857/? W/BackupHelperDispatcher: Couldn't find helper for: 'prefs:VirtualSavingsPrefs'
07-02 11:05:01.661 14846-14857/? D/backup_data: Chunk header at 40 has invalid type: 0x6144bcbc
07-02 11:05:01.661 14846-14857/? D/BackupServiceBinder: onRestore (com.my.app.PreferencesBackupAgent) threw
java.io.IOException: failed: 0xffffffff
at android.app.backup.BackupDataInput.readNextHeader(BackupDataInput.java:112)
at android.app.backup.BackupHelperDispatcher.performRestore(BackupHelperDispatcher.java:111)
at android.app.backup.BackupAgentHelper.onRestore(BackupAgentHelper.java:75)
at android.app.backup.BackupAgent$BackupServiceBinder.doRestore(BackupAgent.java:527)
at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:106)
at android.os.Binder.execTransact(Binder.java:367)
at dalvik.system.NativeStart.run(Native Method)
07-02 11:05:01.751 2013-2119/? V/BackupManagerService: No next package, finishing restore
Samsung Galaxy A3, Android 5.0.2
07-02 14:44:21.159 1002-1355/? V/BackupManagerService: beginRestoreSession: pkg=com.my.app transport=null
07-02 14:44:21.159 1002-4292/? V/RestoreSession: restorePackage pkg=com.my.app obs=android.app.backup.IRestoreObserver$Stub$Proxy@345efd33
07-02 14:44:21.159 1002-1128/? D/BackupManagerService: MSG_RUN_RESTORE observer=android.app.backup.IRestoreObserver$Stub$Proxy@345efd33
07-02 14:44:21.589 1529-1538/? I/GmsBackupTransport: Http Response Code : 200
07-02 14:44:21.619 1529-1872/? I/GmsBackupTransport: Current restore package : PackageInfo{207b91f @pm@}
07-02 14:44:21.619 1002-1128/? D/BackupManagerService: initiateOneRestore packageName=@pm@
07-02 14:44:21.649 1002-1128/? V/BackupServiceBinder: doRestore() invoked
07-02 14:44:21.659 1529-1871/? I/GmsBackupTransport: Current restore package : PackageInfo{4ab746c com.my.app}
07-02 14:44:21.659 1002-1128/? I/BackupManagerService: Next restore package: RestoreDescription{com.my.app : KEY_VALUE}
07-02 14:44:21.659 1002-1128/? V/BackupManagerService: Package com.my.app restore version [24] is compatible with installed version [24]
07-02 14:44:21.659 1002-1128/? D/BackupManagerService: awaiting agent for ApplicationInfo{2638d65b com.my.app}
07-02 14:44:21.659 1002-1014/? D/BackupManagerService: agentConnected pkg=com.my.app agent=android.os.BinderProxy@1551c6dd
07-02 14:44:21.659 1002-1128/? I/BackupManagerService: got agent android.app.IBackupAgent$Stub$Proxy@2a573252
07-02 14:44:21.659 1002-1128/? D/BackupManagerService: initiateOneRestore packageName=com.my.app
07-02 14:44:21.669 1002-1128/? E/BackupManagerService: SElinux restorecon failed for /cache/com.my.app.stage
07-02 14:44:21.669 17200-17214/? V/BackupServiceBinder: doRestore() invoked
07-02 14:44:21.669 17200-17214/? D/backup_data: SKIP_PADDING FAILED at line 334
07-02 14:44:21.669 1002-1128/? V/BackupManagerService: No more packages; finishing restore
07-02 14:44:21.699 1002-1128/? I/BackupRestoreController: restoreFinished for 0
07-02 14:44:21.699 1002-1128/? I/BackupManagerService: Restore complete.
07-02 14:44:21.699 1002-1128/? E/RestoreSession: Error in finishRestore
java.lang.NullPointerException: Attempt to read from field 'java.util.Set evs.d' on a null object reference
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1493)
at com.android.internal.backup.IBackupTransport$Stub$Proxy.finishRestore(IBackupTransport.java:819)
at com.android.server.backup.BackupManagerService$ActiveRestoreSession$EndRestoreRunnable.run(BackupManagerService.java:9477)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.os.HandlerThread.run(HandlerThread.java:61)
07-02 14:44:21.699 1002-1128/? V/BackupManagerService: Clearing restore session and halting timeout
The log messages tagged with backup_data
suggest some error occurs when trying to read in the backup data. Has anyone seen and know how to fix errors like this? I haven't been able to debug into the backup code to see what's going wrong.
I know the backup works because I can see a correctly timestamped backup entry for my app under each tested device under the Android section of https://www.google.com/settings/dashboard.
To support pre-Android 6 devices I extended BackupAgentHelper
and added a SharedPreferencesBackupHelper
in its onCreate()
method as described here. The one thing different from the docs is that I had to precede the name of my backup agent helper class with a '.' in my manifest to avoid a class-not-found exception when loading the app. i.e.,:
android:backupAgent=".PreferencesBackupAgent"