Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

EncryptedSharedPreferences

class EncryptedSharedPreferences : SharedPreferences
kotlin.Any
   ↳ androidx.security.crypto.EncryptedSharedPreferences

An implementation of SharedPreferences that encrypts keys and values.

String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
 
   SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
       "secret_shared_prefs",
       masterKeyAlias,
       context,
       EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
       EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
   );
 
   // use the shared preferences and editor as you normally would
   SharedPreferences.Editor editor = sharedPreferences.edit();
  

Summary

Nested classes

The encryption scheme to encrypt keys.

The encryption scheme to encrypt values.

Public methods

Boolean
contains(@Nullable key: String?)

static SharedPreferences
create(@NonNull fileName: String, @NonNull masterKeyAlias: String, @NonNull context: Context, @NonNull prefKeyEncryptionScheme: EncryptedSharedPreferences.PrefKeyEncryptionScheme, @NonNull prefValueEncryptionScheme: EncryptedSharedPreferences.PrefValueEncryptionScheme)

Opens an instance of encrypted SharedPreferences

Editor

MutableMap<String!, *>

Boolean
getBoolean(@Nullable key: String?, defValue: Boolean)

Float
getFloat(@Nullable key: String?, defValue: Float)

Int
getInt(@Nullable key: String?, defValue: Int)

Long
getLong(@Nullable key: String?, defValue: Long)

String?
getString(@Nullable key: String?, @Nullable defValue: String?)

MutableSet<String!>?
getStringSet(@Nullable key: String?, @Nullable defValues: MutableSet<String!>?)

Unit

Unit

Public methods

contains

fun contains(@Nullable key: String?): Boolean

create

@NonNull static fun create(@NonNull fileName: String, @NonNull masterKeyAlias: String, @NonNull context: Context, @NonNull prefKeyEncryptionScheme: EncryptedSharedPreferences.PrefKeyEncryptionScheme, @NonNull prefValueEncryptionScheme: EncryptedSharedPreferences.PrefValueEncryptionScheme): SharedPreferences

Opens an instance of encrypted SharedPreferences

Parameters
fileName String: The name of the file to open; can not contain path separators.
Return
SharedPreferences: The SharedPreferences instance that encrypts all data.
Exceptions
GeneralSecurityException when a bad master key or keyset has been attempted
IOException when fileName can not be used

edit

@NonNull fun edit(): Editor

getAll

@NonNull fun getAll(): MutableMap<String!, *>

getBoolean

fun getBoolean(@Nullable key: String?, defValue: Boolean): Boolean

getFloat

fun getFloat(@Nullable key: String?, defValue: Float): Float

getInt

fun getInt(@Nullable key: String?, defValue: Int): Int

getLong

fun getLong(@Nullable key: String?, defValue: Long): Long

getString

@Nullable fun getString(@Nullable key: String?, @Nullable defValue: String?): String?

getStringSet

@Nullable fun getStringSet(@Nullable key: String?, @Nullable defValues: MutableSet<String!>?): MutableSet<String!>?

registerOnSharedPreferenceChangeListener

fun registerOnSharedPreferenceChangeListener(@NonNull listener: OnSharedPreferenceChangeListener): Unit

unregisterOnSharedPreferenceChangeListener

fun unregisterOnSharedPreferenceChangeListener(@NonNull listener: OnSharedPreferenceChangeListener): Unit