Migration from SharedPreferences to EncryptedSharedPreferences in Android

Add the following dependency to your module-level build.gradle

The existing implementation of PreferenceManager.java

The new implementation with EncryptedSharedPreferences

Add below methods to PreferenceManager.java which transfers all the existing prefs to encrypted shared prefs.

Call migrateToEncryptedSharedPreferences() on app start to perform migration from SharedPreferences to EncryptedSharedPreferences.

Make sure once the migration is completed successfully keep some pref which will indicate whether migration is pending or completed in order to avoid executing migration each time app starts.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store