Delegates to create encrypted DataStores (#24)
Delegates encryptedDataStore
and encryptedPreferencesDataStore
were added to simplify DataStore creation.
If you have the following code:
val dataStore = DataStoreFactory.createEncrypted(serializer) {
EncryptedFile.Builder(
context.dataStoreFile("filename"),
context,
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build()
}
You can simplify it using delegate for DataStore creation.
// 1. Move the field to top level of you Kotlin file and turn it to an extension on Context
// 2. Replace `DataStoreFactory.createEncrypted` with `encryptedDataStore`
val Context.dataStore by encryptedDataStore(
fileName = "filename", // Keep file the same
serializer = serializer,
)
Note
This only will be interchangeable if you used context.dataStoreFile(...)
to create datastore file.
In case you have custom logic for master key creation, pass the created master key as a parameter masterKey
to the delegate.
Dependencies
- Target JVM
1.8
→11
- Kotlin
1.7.21
→1.9.23
- Tink
1.7.0
→1.13.0
Housekeeping
- Update Gradle to 8.7
- Update AGP to 8.3.2
- Move
PreferenceDataStoreHack.java
intoencrypted-datastore-preferences
module (#25)
Full Changelog: v1.0.0-beta01...v1.0.0