Proper way of implementing BackupAgentHelper to backup multiple files

543 views Asked by At

For two days I'm trying to get the BackupAgent/BackupAgentHelper to work on Android version below 6.0. My goal is to backup all files in the /data/data/com.package/ folder, either via the com.google.android.backup/.BackupTransportService or the android/com.android.internal.backup.LocalTransport.

Is there somewhere a tutorial on a default implementation of a BackupAgentHelper which stores multiple files? I have tried using the FileBackupHelper with every snippet I could find on the internet, but the files just don't get restored.

My current BackupAgentHelper:

public class CustomBackupAgent extends BackupAgentHelper {

    private static final String TAG = "FullBackupAgent";
    private static final boolean DEBUG = true;

    public void addFileHelper(String files[])
    {
        FileBackupHelper aHelper = new CustomFileHelper(this, files);
        addHelper("userfiles", aHelper);
    }

    @Override
    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
                         ParcelFileDescriptor newState) throws IOException {
        addFileHelper(getBackupDirs());
        super.onBackup(oldState, data, newState);
    }


    private String[] getBackupDirs() {
        LinkedList<File> dirsToScan = new LinkedList<>();
        ArrayList<String> allFiles = new ArrayList<>();

        // build the list of files in the app's /data/data tree
        File file = new File(getFilesDir().getParentFile().getAbsolutePath());
        dirsToScan.add(file);
        Log.e(TAG, "Backing up dir tree @ " + file.getAbsolutePath() + " :");
        while (dirsToScan.size() > 0) {
            File dir = dirsToScan.removeFirst();
            File[] contents = dir.listFiles();
            if (contents != null) {
                for (File f : contents) {
                    if (f.isDirectory()) {
                        dirsToScan.add(f);
                    } else if (f.isFile()) {
                        Log.e(TAG, "    " + f.getAbsolutePath());
                        allFiles.add(f.getAbsolutePath());
                    }
                }
            }
        }

        return allFiles.toArray(new String[allFiles.size()]);
    }

}

When I provoke a backup with "adb shell bgmr run" I get the following logcat output:

08-31 16:09:36.534 E/FullBackupAgent: Backing up dir tree @ /data/data/package :
08-31 16:09:36.542 E/FullBackupAgent:     /data/data/package/cache/com.android.opengl.shaders_cache
08-31 16:09:36.542 E/FullBackupAgent:     /data/data/package/shared_prefs/multidex.version.xml
08-31 16:09:36.550 E/FullBackupAgent:     /data/data/package/code_cache/secondary-dexes/package.apk.classes2.dex
08-31 16:09:36.550 E/FullBackupAgent:     /data/data/package/code_cache/secondary-dexes/package.apk.classes2.zip
08-31 16:09:36.550 D/BackupHelperDispatcher: handling existing helper 'userfiles' package.CustomFileHelper@41515378
08-31 16:09:36.557 I/PerformBackupTask: Backup pass finished.
08-31 16:11:38.776 V/BackupManagerService: Backup requested but nothing pending

And when I reinstall the app I get the following logcar output:

08-31 16:30:13.932 D/BackupManagerService: MSG_RUN_RESTORE observer=android.app.backup.IRestoreObserver$Stub$Proxy@41b003c8
08-31 16:30:13.972 I/dalvikvm: Could not find method android.content.Context.getNoBackupFilesDir, referenced from method android.support.v4.content.ContextCompat.getNoBackupFilesDir
08-31 16:30:13.972 W/dalvikvm: VFY: unable to resolve virtual method 470: Landroid/content/Context;.getNoBackupFilesDir ()Ljava/io/File;
08-31 16:30:13.979 V/LocalTransport:     ... key=com.android.sharedstoragebackup size=1107
08-31 16:30:14.065 V/BackupServiceBinder: doRestore() invoked
08-31 16:30:14.073 I/BackupManagerService: Restore complete.

But none of the files I backupped get restored. I have also tried to log or set breakpoints in available methods I can override from BackupHelperAgent but the doesn't stop or log in any of them.

The AndroidManifest.xml looks like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="package">

    <application android:allowBackup="true" android:label="@string/app_name"
        android:backupAgent="CustomBackupAgent"
        android:restoreAnyVersion="true"
        android:supportsRtl="true">

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

    </application>
</manifest>

If you ever used the BackupAgent to backup whole directories please help :)

Thanks in advance!

0

There are 0 answers