Migrate from SharedPreferences to EncryptedSharedPreferences

Paolo Montalto
3 min readMar 13, 2023

Android Developers have always been used to save little pieces of data inside their apps with SharedPreferences.

At the same time we’ve always struggled with securing information saved inside our apps because there was no standard way to do so and third party solutions have always been buggy or not sufficiently secure.

Fortunately, with the advent of Android Jetpack, Security library finally came in to help.

Among the several features provided by the Security library we can find the new EncryptedSharedPreferences class.

It comes as a replacement for the old SharedPreferences in most cases, it means there’s some caveat:

  • Version 1.0.0 of the Security library is supported on devices running Android 6.0 (API 23) or higher (though there is an alpha version of the library working on Android 5.0 and 5.1)
  • The preference file should not be backed up with Auto Backup (when restoring the file it is likely the key used to encrypt it will no longer be present)

Being that said, EncryptedSharedPreferences are easy and straightforward to implement in a new app.

You just need to add Security library dependency to your Gradle build file

implementation…

--

--

Paolo Montalto

Android Engineer, freelance, mobile developer, software craftsman, guitar strummer, husband, father, humble.