Lorixen.
EN

Android Auto Backup restoring stale logins and tokens?

Short answer: Android's Auto Backup quietly copies most of your app's files, databases and shared preferences to the user's Google account, then restores them when they set up a new phone. That's helpful for settings, but it can restore things that only made sense on the old device: a push notification token, a device-bound login token, or files encrypted with a key that never left the old phone. The result is an app that looks signed in but fails, crashes on launch or never gets notifications. Fix it with backup rules that exclude device-specific data, and a first-launch check that recovers cleanly.

What Auto Backup does by default

According to Android's documentation, Auto Backup applies to apps targeting and running on Android 6.0 or higher. It uploads app data to the user's Google Drive, protected by their Google account, and on Android 9 or higher the backup is end-to-end encrypted using the device's screen lock. There's a per-app size limit and no charge.

By default it includes:

  • shared preferences files,
  • files in your app's internal storage,
  • databases, including those created with SQLiteOpenHelper,
  • some files on external storage in your app's directories.

Files in the cache, code cache and the special no-backup directory are always excluded. Backups happen automatically when backup is enabled, the device is idle and on Wi-Fi, and enough time has passed since the last one. The default value of allowBackup is true, so unless your app says otherwise, it's taking part.

What goes wrong after a restore

Android's own guidance lists data that's typically excluded: files with device-specific identifiers, whether issued by a server or generated on the device. Its example is the Firebase Cloud Messaging registration token. FCM needs a new token on each install on a new device, and if the old one is restored, Android notes the app might behave unexpectedly.

The same pattern shows up with other data:

  • Device-bound login tokens. If your server ties a refresh token to the device it was issued to, the restored copy will be rejected, and an app that doesn't handle that may loop or show a blank screen.
  • Encrypted local data. Apps often encrypt files or preferences with a key held in the Android Keystore. The encrypted files can be restored to the new phone, but the key generally isn't, so decryption fails. Without error handling, that's a crash on first launch.
  • Install or device IDs. Analytics, fraud checks and licensing logic that assume an ID is unique to one phone will see two devices claiming the same ID.
  • Caches of server data. Old cached data in a database may be months out of date by the time it's restored.

Write backup rules

Android lets you include and exclude specific files. For apps targeting Android 12 or higher, point the dataExtractionRules attribute on the <application> element to an XML file in res/xml/. It has separate sections for cloud backup and device-to-device transfer, so you can treat the two differently. For older Android versions, the fullBackupContent attribute uses a similar file. Supporting both means providing both.

Typical rules for a business app:

  • Exclude the preferences file or database table holding push tokens, session tokens and device IDs.
  • Exclude encrypted files whose keys live in the Keystore, unless your app can re-derive or re-fetch the key.
  • Exclude large caches and anything that can be downloaded again.
  • Include user settings and preferences that are worth keeping, such as notification choices or a preferred branch or language.

Android also supports a requireFlags condition so that certain files are only backed up when client-side encryption is in place, or only included in device-to-device transfers.

Where to keep credentials instead

Android's documentation suggests not storing user credentials and authentication tokens in shared preferences or a plain file, and recommends its Block Store APIs for credentials that should be securely stored and restored with other app data. Whether you use it depends on your sign-in design, but the principle is clear: decide deliberately what should follow a user to a new phone.

Should you just turn backup off?

You can set android:allowBackup="false", and Android says that may make sense if your app can recreate its state another way or handles sensitive information. Two cautions:

  • Users lose legitimately useful settings when they change phones.
  • Android notes that for apps targeting Android 12 or higher, on some manufacturers' devices allowBackup="false" disables cloud backup but doesn't stop device-to-device transfers.

For most apps, precise rules are better than switching everything off.

Add a recovery path on first launch

Even with good rules, plan for restored state that doesn't work:

  1. On launch, detect a new install or a changed device, for example by storing a marker in the no-backup directory and checking whether it exists.
  2. If it's missing, request a fresh push token and send it to your server.
  3. Validate any stored session with your server, and if it's rejected, clear it and show the sign-in screen with a friendly message.
  4. Catch decryption failures, delete the unreadable data and fetch it again from the server.

This turns a crash into a one-time sign-in.

Testing it

Test backup and restore on real devices or emulators before release, including a restore to a second device. Check notifications arrive on the new device, sign-in behaves sensibly, and nothing crashes when restored data can't be used.

An illustrative example

A pharmacy chain in Ottawa has an app for prescription refill reminders. Customers who switched phones reported the app opening to a blank screen and never sending reminders. The team finds the old FCM token and an encrypted session file being restored without the Keystore key. They exclude both in their backup rules, keep the notification preferences included, add a first-launch check using a marker in the no-backup directory, and handle decryption failures by signing the user out cleanly.

FAQ

Does Auto Backup include my app's database?

By default, yes. Exclude tables or files you don't want restored.

Can users turn off backup themselves?

Yes. Backup must be enabled on the device for automatic backups to run.

Is backup data visible to the user in their Drive?

It's stored with their Google account and encrypted on recent Android versions. Treat it as user data and follow your privacy notice.

Do iOS backups cause similar problems?

They can, with different rules. Check each platform's backup behaviour separately.

If customers' apps break after moving to a new phone and you want backup and restore handled properly, tell us what your app stores on the device.

Sources

WhatsAppBook a call