A Kotlin SharedPreferences wrapper
Switch branches/tags
Clone or download
zsmb13 Merge branch 'dev' into 'master'
Added license, updated README

See merge request AutLibs/Krate!6
Latest commit 8583a36 Sep 26, 2018
Failed to load latest commit information.
app Added initial version of example app Sep 24, 2018
gradle/wrapper Dependency updates Sep 24, 2018
krate Version bump to 0.0.3 (3) Sep 26, 2018
.gitlab-ci.yml Use RELEASE tags instead of SNAPSHOT Sep 26, 2018
README.md Added license, updated README Sep 26, 2018
build.gradle Refactored publication scripts, added Bintray Sep 26, 2018
gradle.properties Updated Gradle version to 4.9 Aug 6, 2018
gradlew Created project Aug 6, 2018
gradlew.bat Created project Aug 6, 2018
publish-artifactory.gradle Publish as release to artifactory Sep 26, 2018
publish-bintray.gradle Enable auto publishing on Bintray Sep 26, 2018



Krate is a SharedPreferences wrapper library that uses delegated properties for convenient access to SharedPreferences values.

Here's what its basic usage looks like, extending the SimpleKrate class:

class UserSettings(context: Context) : SimpleKrate(context) {

    var notificationsEnabled by booleanPref("notifications_enabled", false)
    var loginCount by intPref("login_count", 0)
    var nickname by stringPref("nickname", "")


val settings = UserSettings(context)
settings.loginCount = 10
Log.d("LOGIN_COUNT", "Count: ${settings.loginCount}")


You can include Krate from the jcenter repository, like so:

implementation 'hu.autsoft:krate:0.0.3'

Optionals vs defaults

Each property can be declared with or without a default value, here are the differences:

Optional values:

A property declared with the one-argument delegate function will have a nullable type. It will have a null value if no value has been set for this property yet, and its current value can be erased from SharedPreferences completely by setting it to null.

var username: String? by stringPref("username")

Default values:

A property declared with the two-argument delegate function takes its default value as the second argument, and it will have a non-nullable type. Reading from this property will return either the value it was last set to or the default value. Setting this property will update the value stored in SharedPreferences. Note that there's no way to remove these values from SharedPreferences (although you could set it explicitly to the default value).

var username: String by stringPref("username", defaultValue = "admin")

Custom Krate implementations

You can usually get away with extending SimpleKrate, as it does allow you to pass in a custom name for the SharedPreferences to be used to store your values in its constructor as an optional parameter. (If you pass in no name parameter to its constructor, it will default to using the instance returned by PreferenceManager.getDefaultSharedPreferences(context).)

However, you can also implement the Krate interface directly if you want to manage the SharedPreferences instance yourself for whatever reason - all this interface requires is a property that holds a SharedPreferences instance. With that, you can use the delegate functions the same way as shown above:

class ExampleCustomKrate(context: Context) : Krate {

    override val sharedPreferences: SharedPreferences

    init {
        sharedPreferences = context.applicationContext.getSharedPreferences("custom_krate_prefs", Context.MODE_PRIVATE)

    var exampleBoolean by booleanPref("exampleBoolean", false)


Copyright 2018 AutSoft

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at


Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.