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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With