I am trying to get my room database to persist through reinstall and on using a new phone. I am currently using key-value backups, with this simple code:
class AppBackupAgentHelper: BackupAgentHelper() {
override fun onCreate() {
addHelper("MyDatabase",
DatabaseBackupAgentHelper(
this,
"MyDatabase"
)
)
}
class DatabaseBackupAgentHelper(context: Context, dbName:String): FileBackupHelper(context,context.getDatabasePath(dbName).absolutePath)
My manifest has fullBackupContent = true and allowBackup =true and android:backupAgent="com.example.myapplication3.BackupClasses.AppBackupAgentHelper"
In the app I am calling datachanged at certain points when saving app data. I am then using adb to make the backup manager run, having both tested adb shell bmgr backupnow , and adb bmgr run. Doing adb bmgr run gives me blank, but calls the onBackup methods in my app. Doing backupnow gives me:
Running incremental backup for 1 requested packages. Package @pm@ with result: Success Package com.example.myapplication3 with result: Success Backup finished with result: Success
It does not restore when I reinstall the app, nor does it restore when I use the token and package name and force a restore in adb, instead it gives me:
/adb shell bmgr restore current token package name
Scheduling restore: Google Pixel 2
restoreStarting: 0 packages
restoreFinished: 0
done
I feel like the error must be obvious in the restoreStarting: 0 packages, but I can't find anything online that hints at what would fix this or where I'm going wrong in my code.
The database is also not being restored when I try putting the app on another phone.
Let me know if you need more information,
Thanks for the help
EDIT:
I managed to get it to display restoreStarting 1 restoreFinishing 0. Logs are showing
2021-01-07 15:20:23.375 1393-3162/? D/BackupManagerService: initiateOneRestore packageName=@pm@
2021-01-07 15:20:23.411 1393-3162/? V/BackupManagerService: No more packages; finishing restore
2021-01-07 15:20:23.413 1393-3162/? I/BackupRestoreController: restoreFinished for 0
and on restore is not being called by my app
Edit 2:
It doesn't seem like the data is even being uploaded in the first place as I cannot find it on google drive. Here's the log of the backup manager running for my app.
2021-01-08 00:54:26.279 1393-15498/? D/BackupManagerService: [UserID:0] awaiting agent for ApplicationInfo{e23f643 com.example.myapplication3}
2021-01-08 00:54:26.293 1393-3923/? D/BackupManagerService: [UserID:0] agentConnected pkg=com.example.myapplication3 agent=android.os.BinderProxy@f2665f9
2021-01-08 00:54:26.293 1393-15498/? I/BackupManagerService: [UserID:0] got agent android.app.IBackupAgent$Stub$Proxy@e01793e
2021-01-08 00:54:26.294 1393-15498/? D/KeyValueBackupTask: Invoking agent on com.example.myapplication3
2021-01-08 00:54:26.299 32291-32458/? I/Backup: [CryptoEnableCheck] Should not encrypt backups: user not opted in.
2021-01-08 00:54:26.303 14524-14543/com.example.myapplication3 D/BackupHelperDispatcher: handling existing helper 'StoryDatabase' com.example.myapplication3.BackupClasses.DatabaseBackupAgentHelper@afc78a9
2021-01-08 00:54:26.320 1393-15498/? I/BackupRestoreController: Getting widget state for user: 0
2021-01-08 00:54:26.320 1393-15498/? V/BackupManagerConstants: getBackupFinishedNotificationReceivers(...) returns
Edit 3:
I've used pjv's answer from this post - Android backup/restore: how to backup an internal database?
This definitely creates a backup of my database file to the google drive, but the file still doesn't get restored.
Edit 4:
Turns out this only works on the emulator... the data is still not being uploaded to google drive on my actual physical pixel 2. I also added synchronization to the onRestore and onBackup but this hasn't changed anything. Logging shows that the onRestore method is being called.
This post Remove room database on app uninstall proves that it is possible. I have moved to using auto backup (deleting backup agent) and it works! Will try and make the kv version work now. Still isn't working on physical device.
Edit 5:
Deleting my cloud backup and starting it again made my backups work on my physical device. Auto-backup allows the room database to persist through reinstall from android 23 onwards. I am still working on making key-value backups work for android version below 23.