How To Use The Jetpack Preference DataStore With Kotlin Multiplatform
Easy key-value store solution for Android and iOS
Since DataStore version 1.1.0, the library does not only support native Android but also Kotlin Multiplatform. In this article, you will learn how to use the Preferences DataStore as a key-value store solution in the common code of our Kotlin Multiplatform project.
Setup
First, we need to make sure to include the library in our project’s build.gradle.kts
file.
With version catalogs, before proceeding, declare the following in your libs.versions.toml
:
[versions]
dataStorePreferences = "1.1.1"
[libraries]
datastore-preferences = { group = "androidx.datastore", name = "datastore-preferences-core", version.ref = "dataStorePreferences" }
Afterward, depending on your case, add the following to your build.gradle.kts
file:
kotlin {
// ..
sourceSets {
// ..
commonMain.dependencies {
// With Version Catalog:
implementation(libs.datastore.preferences)
// With regular Gradle dependency declaration
implementation("androidx.datastore:datastore-preferences-core:1.1.0")
}
}
}