Android: How to backup DataStore file to keep it through the app reinstall

806 Views Asked by At

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

There are 1 best solutions below

0
Frans Hanyane On

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>