BackupAgent: "unable to restore package ..."

I have implemented the BackupAgent as described at Data Backup, registered an API key and declared the BackupAgent in my Manifest. The Backup part work's quite well, I think; When I run adb shell bmgr run in command line, the following output appears in LogCat:

01-11 22:23:09.002: DEBUG/PerformBackupThread(97): starting agent for backup of BackupRequest{app=ApplicationInfo{4547c5b8 com.meins.nightclock} full=false}
01-11 22:23:09.002: DEBUG/BackupManagerService(97): awaiting agent for ApplicationInfo{4547c5b8 com.meins.nightclock}
01-11 22:23:09.013: DEBUG/BackupManagerService(97): agentConnected pkg=com.meins.nightclock agent=android.os.BinderProxy@4536a7f8
01-11 22:23:09.032: DEBUG/BackupHelperDispatcher(9122): handling existing helper 'alarms' android.app.backup.FileBackupHelper@44e197b0
01-11 22:23:09.032: DEBUG/BackupHelperDispatcher(9122): handling existing helper 'prefs' android.app.backup.SharedPreferencesBackupHelper@44e19478
01-11 22:23:09.032: VERBOSE/LocalTransport(97): performBackup() pkg=com.meins.nightclock
01-11 22:23:09.032: VERBOSE/LocalTransport(97): Got change set key=alarms:alarms size=16 key64=YWxhcm1zOmFsYXJtcw==
01-11 22:23:09.042: VERBOSE/LocalTransport(97):   data size 16
01-11 22:23:09.062: VERBOSE/LocalTransport(97): Got change set key=prefs:com.meins.nightclock_preferences size=265 key64=cHJlZnM6Y29tLm1laW5zLm5pZ2h0Y2xvY2tfcHJlZmVyZW5jZXM=
01-11 22:23:09.072: VERBOSE/LocalTransport(97):   data size 265
01-11 22:23:09.072: VERBOSE/LocalTransport(97): finishBackup()

The restore part on the other hand doesn't work at all, on onReceive() method is not called. When I reinstall my app, the only output which may (?) refer to the BackupAgent is

01-11 22:14:01.042: DEBUG/vending(7426): [100] LocalAssetCache.updateOnePackage(): No local info for com.meins.nightclock

When I run adb shell bmgr restore com.meins.nightclock it simply states Unable to restore package com.meins.nightclock .


I am using the following implementation of BackupAgentHelper

package com.meins.nightclock;

import java.io.IOException;

import android.app.backup.BackupAgentHelper;
import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.app.backup.FileBackupHelper;
import android.app.backup.SharedPreferencesBackupHelper;
import android.os.ParcelFileDescriptor;
import android.util.Log;

public class MyBackupAgent extends BackupAgentHelper {

    public static final Object[] DATA_LOCK = new Object[0];

    private static final String PREFS_BACKUP_KEY = "prefs";
    private static final String ALARMS_BACKUP_KEY = "alarms";

    private DataLayerAlarms d;

    @Override
    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
            ParcelFileDescriptor newState) throws IOException {

        if (d.backup() != 0)
            return;

        synchronized (DATA_LOCK) {
            super.onBackup(oldState, data, newState);
        }
    }

    @Override
    public void onCreate() {
        SharedPreferencesBackupHelper preferencesHelper = new SharedPreferencesBackupHelper(this,
                getPackageName() + "_preferences");
        addHelper(PREFS_BACKUP_KEY, preferencesHelper);

        FileBackupHelper fileHelper = new FileBackupHelper(this, DataLayerAlarms.BACKUP_FILE);
        addHelper(ALARMS_BACKUP_KEY, fileHelper);

        d = new DataLayerAlarms(this);
    }

    @Override
    public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
            throws IOException {
        Log.d(toString(), "onRestore()");
        synchronized (DATA_LOCK) {
            super.onRestore(data, appVersionCode, newState);
        }

        d.restore();
    }

}

For the sake of completeness the relevant Manifest part:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:debuggable="true"
    android:backupAgent="MyBackupAgent">
    <meta-data
        android:name="com.google.android.backup.api_key"
        android:value="AEdPqrEAAAAI4MsUOC-[...]" />

    ...

</application>

Does anyone know, why the BackupManager is unable to restore this package?


Try adding a dot before your backup class name, as in:

android:backupAgent=".MyBackupAgent"

Or if your backup class isn't in your root package, add the path to it:

android:backupAgent=".package.MyBackupAgent"

android:backupAgent="MyBackupAgent"更改为android:backupAgent=".MyBackupAgent"也适用于我。


This problem happened when I changed the package name, and then other problems subsequently appeared. It took me a couple hours to figure out.

Disable Instant Run. It messed up things for me.

Check the transports list, make sure Google is set:

adb shell bmgr list transports
    android/com.android.internal.backup.LocalTransport
  * com.google.android.gms/.backup.BackupTransportService

Make sure the BackupAgentHelper package name and the meta-data package name have the same name. The name conflicts could cause a problem.

android:backupAgent The name of the class that implement's the application's backup agent, a subclass of BackupAgent. The attribute value should be a fully qualified class name (such as, "com.example.project.MyBackupAgent"). However, as a shorthand, if the first character of the name is a period (for example, ".MyBackupAgent"), it is appended to the package name specified in the element. There is no default. The name must be specified.

链接地址: http://www.djcxy.com/p/4340.html

上一篇: 在javascript中选择最丰富多彩的颜色

下一篇: BackupAgent:“无法恢复软件包...”