4

I wonder if it's possible to backup and restore a DataStore via the backup feature on Android.

Let's say I have some AppDataStore class which provides a logic to keep some user-related preferences via the jetpack DataStore:

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")

I use the dataStore object above to write and read some primitive data and everything works well. The file is created and stays alive. However, I can't find a way to make backup logic work (actually backup logic worked flawlessly with the shared prefs previously).

To have backup working I have these changes in my manifest file:

 <application
        ...
        android:fullBackupContent="@xml/backup_rules"
        android:allowBackup="true">

And here is the backup_rules.xml:

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
    <include
        domain="file"
        path="datastore/settings.preferences_pb" />
</full-backup-content>

I tried different paths, even following the exact folders structure, but still nothing.

Has anyone faced this issue before? Any ideas maybe?

1 Answer 1

3

I was able do setup backup for Datastore using similar settings to yours. I setup backup for everything in the datastore directory, I don't think that is the issue though. Just make sure that your Google account is setup on the device you are testing with and that backup is enabled.

AndroidManifest.xml:

<application
    android:allowBackup="true"
    android:fullBackupContent="@xml/backup_rules"
    ... >

backup_rules.xml:

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
    <include domain="file" path="datastore/."/>
</full-backup-content>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.