22

Alright, so I'm trying to implement Data Backup into my application, and have been following this guide. I've implemented my BackupAgentHelper using a SharedPreferencesBackupHelper. I don't get any errors, and I'm being sure to call dataChanged() after all preference changes, but when I test the backup (`adb shell bmgr run) I get this information in LogCat:

07-07 12:29:00.258: V/BackupManagerService(291): Scheduling immediate backup pass
07-07 12:29:00.258: V/BackupManagerService(291): Running a backup pass
07-07 12:29:00.258: V/BackupManagerService(291): clearing pending backups
07-07 12:29:00.258: V/PerformBackupTask(291): Beginning backup of 1 targets
07-07 12:29:00.289: V/BackupServiceBinder(291): doBackup() invoked
07-07 12:29:00.289: D/PerformBackupTask(291): invokeAgentForBackup on @pm@
07-07 12:29:00.297: I/PerformBackupTask(291): no backup data written; not calling transport

So for reference, in my manifest I've added:

<application
        android:allowBackup="true"
        android:backupAgent="com.kcoppock.sudoku.SudokuBackupAgent"

as well as

<meta-data
        android:name="com.google.android.backup.api_key"
        android:value="my_key_goes_here" />

and my BackupAgentHelper is implemented like so:

public class SudokuBackupAgent extends BackupAgentHelper {
    static final String SCORES = "SCORES";
    static final String BACKUP_ID = "sudoku_backup";

    @Override
    public void onCreate() {
        SharedPreferencesBackupHelper backupHelper = 
                new SharedPreferencesBackupHelper(this, SCORES);
        addHelper(BACKUP_ID, backupHelper);
    }
}

and finally, in my main activity, I call for a data backup like this:

edit.putString(id + "_values", valueCache.toString());
edit.putString(id + "_hints", hintCache.toString());
edit.commit();
BackupManager backup = new BackupManager(this);
backup.dataChanged();

I've tried debugging, and it seems my onCreate() in SudokuBackupAgent is never called. Or at least it's never reached from the debugger. It seems it isn't finding any updated data, and I have double checked to ENSURE there is data to be backed up. Is there something I'm missing here?

EDIT: I should add, I'm testing on a device (Galaxy Nexus), and I've even tried using an exported release APK for testing purposes.

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • Are you sure that you use write adb command? In guide, that you linked there are other commands. – Jin35 Jul 09 '12 at 18:05
  • Yes, I'm sure. There's also `adb shell bmgr start com.kcoppock.sudoku` which forces a backup even if `dataChanged()` is never called in code, which I have also tried in conjunction with `adb shell bmgr run com.kcoppock.sudoku` but it makes no difference. – Kevin Coppock Jul 09 '12 at 18:06
  • `start` should have been `backup` in the comment above. My typo. :) – Kevin Coppock Jul 09 '12 at 18:20
  • Try uninstalling your app, and then installing a fresh build again and running it. – Raghav Sood Jul 09 '12 at 18:35
  • Tried that multiple times, but since I've made some new changes recently and it's been a couple of days, I'll try that again once I get home. – Kevin Coppock Jul 09 '12 at 18:45
  • I am testing on 2 ICS devices. And none of the solutions below work. Did you get onBackup to be called? How did you do it? – coolcool1994 Dec 17 '14 at 23:53
  • 1
    @coolcool1994 Never did have any luck, I stopped trying. – Kevin Coppock Dec 18 '14 at 00:33
  • 1
    I am about to stop working on the backup too. I spent 5 days none stop. I read every single stack overflow posts about backup and so many blogs. The thing that made me keep trying was that at one point he back up was working. But I may also have to give up. In addition, I could say Android sucks for developers. iOS is the place where great apps are created. – coolcool1994 Dec 18 '14 at 00:48
  • @coolcool1994 any luck in the end? – StuStirling Jan 07 '15 at 13:42
  • 1
    Nope, there is no solution to this problem. I gave up, and you probably should too. You can't backup files using backup manager as of1/17/2015. The system is inconsistent and it fails as you keep doing it. – coolcool1994 Jan 17 '15 at 20:32
  • 1
    can anyone has found the answer? – abh22ishek Apr 12 '15 at 14:46

3 Answers3

3

1) Put Log.i() into your onCreate, to see if it's called.

2) Logcat indicates that your function didn't write anything. Check if you have shared_prefs/SCORES file in your app's private folder (assumes using appropriate file manager on rooted device). This is the file you're attempting to have in backup. Probably your preferences file is something else than this file, in such case fix your String SCORES to reflect real preferences file.

3) I tried to debug BackupAgentHelper.onCreate in my app, and it can be debugged after invoking adb shell bmgt... so it is possible to step here in debugger.

Pointer Null
  • 39,597
  • 13
  • 90
  • 111
  • Thanks, I'll give these suggestions a try when I get home. The SCORES file definitely seems like it should be correct, as I access it quite frequently with getSharedPreferences(). – Kevin Coppock Jul 10 '12 at 13:48
  • This is baffling me. So as per #1, I've logged onCreate(), and it *is* reached. #2, I've checked and SCORES.xml *is* there in the shared_prefs/ folder and contains values. – Kevin Coppock Jul 10 '12 at 22:54
2

I had the same problem today with Android SDK 4.1. Using 2.3.3 versions, however, helped:

07-15 13:59:56.459: V/LocalTransport(61): performBackup() pkg=com........
07-15 13:59:56.469: V/LocalTransport(61): Got change set key=filehelper:../databases/database.db size=8208 key64=ZmlsZWhlbHBlcjouLi8kYXRhYmFzZXMvZXhwZW5zZXIuZGI=
07-15 13:59:56.469: V/LocalTransport(61):   data size 8208
07-15 13:59:56.469: V/LocalTransport(61): finishBackup()
Kamen
  • 3,575
  • 23
  • 35
  • 1
    Do you mean you changed your target SDK to 2.3.3? Or you reverted to an older version of the SDK tools? – Kevin Coppock Jul 15 '12 at 16:47
  • 1
    Also, I just realized yours is backing up to the local transport, not the Android cloud backup transport. – Kevin Coppock Jul 15 '12 at 23:51
  • 1
    I changed the execution AVD to be 2.3.3. The target is still API-16. I am not sure as to how the Galaxy Nexus will handle the backup transport. I guess you have the "Backup & reset" stuff set on the phone. But it shouldn't matter if it is local or cloud. After all - that's device-specific (as Google notes in the DataBackup docs) and the important thing is to make the backup/restore go through regardless of the execution method - this means configure backup agent as well as place the dataChanged() calls on the proper places (which was the harder part for me). – Kamen Jul 16 '12 at 11:15
  • It could be a Jelly Bean bug -- and by bug I mean not really a bug since I'm running a totally unofficial build -- as that is what mine is running. I'll do a backup later and flash stock ICS and see if the problem continues. Thanks! – Kevin Coppock Jul 16 '12 at 15:40
  • Still seems to be doing the same on ICS. I have no idea what the deal is but I'm going to ahead and award the bounty to you anyway, as it seems to just be a bug on my end. – Kevin Coppock Jul 17 '12 at 04:22
  • Hi, kcoppock, have you confirmed that this is a bug in Jelly bean? I am trying the BackupRestore sample from the SDK and it does not work for JB but works fine for GB and ICS. I am using emulators for all my testing. Did you get backupagent working for JB? Any workaround? Thanks! – Safecoder Sep 09 '12 at 16:26
  • 1
    Same for us, there seem to be a bug in JB. But works fine in ICS and previous versions. – Snicolas Sep 28 '12 at 07:43
2

If you do not change your preferences data it will just back up once but not subsequently.

https://developer.android.com/reference/android/app/backup/SharedPreferencesBackupHelper.html

"Whenever a backup is performed, it will back up all named shared preferences that have changed since the last backup operation."

As other poster said make sure to have a log statement in your onCreate.

You can force a backup by:

// bmgr wipe TRANSPORT PACKAGE
bmgr wipe com.google.android.backup/.BackupTransportService com.kcoppock.sudoku

bmgr run

The default is com.google.android.backup/.BackupTransportService, but best to check for yourself with bmgr list transports.

You'll be much happier running against the local transport, not the default Google Transport. Check the dev docs on this.

Saran
  • 3,845
  • 3
  • 37
  • 59
Hayes Haugen
  • 822
  • 1
  • 7
  • 7