Search code examples
androidsharedpreferencesandroid-backup-serviceencrypted-shared-preference

Will migrating to a new device work when using EncryptedSharedPreferences?


I know that EncryptedSharedPreferences use the android keystore to encrypt preferences securely and that the android keystore is tied to the devices hardware I also read that the keystore cannot be backed up like preferences and databases are. My question is what this means when I am trying to restore my apps settings onto a new device:

Are preferences still backed up in a decrypted manner?

Are they still encrypted and thus unusable on the new device?

should I exclude EncryptedSharedPreferences from backups?


Solution

  • If you locate the .xml file that serves as your Shared Preferences file, you'll observe that both the key and value are encrypted. (The key here means the key you use when putting Shared Preferences, not MasterKey for encyrpting). Therefore, if you back up the file, you will get its encrypted version. Since the new device and the key are encrypted, you will not be able to access anything and you probably will reach the default state of all Shared Preferences. You should exclude Encrypted Shared Preferences from the backup. If included, there won't be a MasterKey available to decrypt the values on the new device. In conclusion this implies that you won't be able to access any data when installing on a new device.

    You can review the warning here.

    Encrypted Shared Preferences