Migration from SharedPreferences to EncryptedSharedPreferences in Android

Jay Patel
Mar 3, 2021

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

implementation "androidx.security:security-crypto:1.0.0-rc03"

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.

PreferenceManager.getInstance(getContext()).migrateToEncryptedSharedPreferences(getContext());

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.

--

--