A lightweight wrapper for Android's SharedPreferences -- written in Kotlin.
This library is a fork of EasyPrefs by Pixplicity. For the original Java library, click here.
With this library you can initialize a global instance of SharedPreferences inside your Application class.
class YourApplication : Application() {
override fun onCreate() {
super.onCreate()
// Initialize Effortless Prefs
Prefs.Builder()
.setContext(this)
.setPrefsName(packageName)
.setUseDefaultSharedPreference(true)
.build()
}
}
After initialization, you can use simple one-line methods to save values to SharedPreferences anywhere in your app, such as:
Prefs.putString(key, string)
Prefs.putLong(key, long)
Prefs.putBoolean(key, boolean)
Retrieving data from SharedPreferences can be as simple as:
val data = Prefs.getString(key, defaultValue)
If SharedPreferences contains the key, the String will be obtained, otherwise the default value is returned.
Grab the latest dependency through Gradle:
dependencies {
implementation "io.milkcan:effortless-prefs:1.1.0"
// Optional serialization helpers
implementation "io.milkcan:effortless-prefs-gson:1.1.0"
implementation "io.milkcan:effortless-prefs-moshi:1.1.0"
}
For older versions, see Bintray.
This project is licensed under Apache 2.0 -- see the full license here.