/KMPrefs

Kotlin Multiplatform Preferences system

Primary LanguageKotlinApache License 2.0Apache-2.0

KMPrefs

Maven Central

Static Badge Static Badge Static Badge Static Badge

v1.0.1

The Kotlin Multiplatform Pref(erence)s system allows you to store, retrieve, and remove data locally on each platform. It leverages the native mechanisms provided by each platform.

Implementation

Version catalog

  • libs.version.toml
[versions]
kmprefs = "1.0.1"

[libraries]
kmprefs = { module = "io.github.n7ghtm4r3:KMPrefs", version.ref = "kmprefs" }
  • build.gradle.kts
dependencies {
    implementation(libs.kmprefs)
}

Gradle (Kotlin)

dependencies {
    implementation("io.github.n7ghtm4r3:KMPrefs:1.0.1")
}

Gradle (Groovy)

dependencies {
    implementation 'io.github.n7ghtm4r3:KMPrefs:1.0.1'
}

Core functionality

Supported types

Type Description
Boolean Boolean type (true/false)
BooleanArray Array of Boolean
Byte 8-bit signed integer
ByteArray Array of Byte
UByte 8-bit unsigned integer
UByteArray Array of UByte
Short 16-bit signed integer
ShortArray Array of Short
UShort 16-bit unsigned integer
UShortArray Array of UShort
Int 32-bit signed integer
IntArray Array of Int
UInt 32-bit unsigned integer
UIntArray Array of UInt
Float 32-bit floating-point number
FloatArray Array of Float
Double 64-bit floating-point number
DoubleArray Array of Double
Long 64-bit signed integer
LongArray Array of Long
ULong 64-bit unsigned integer
ULongArray Array of ULong
String String of characters
Serializable Custom serializable objects

Store values

Same procedure for all the types

Primitives and String

val kmPrefs = KMPrefs("your_storage_path") // create an instance

// for example store a Double value
kmPrefs.storeDouble(
    key = "constant", // the key of the double value to store
    value = 3.14159265359 // the double value to store
)

Custom serializable objects

Under the hood the KMPrefs works with the kotlinx-serialization library so it is required to import both the library and the plugin to correctly store and retrieve custom objects

Create the @Serializable object
@Serializable // required
data class Car(
    val plate: String,
    val hp: Int
)
Store the object
val kmPrefs = KMPrefs("your_storage_path") // create an instance

// create the instance to store
val carToStore = Car(
    plate = "AA000AA",
    hp = 450
)

// store the instance created
kmPrefs.storeCustomObject(
    key = "your_key",
    value = carToStore
) 

Retrieve values

Same procedure for all the types

Primitives and String

// for example retrive a Double value
val constant = kmPrefs.retrieveDouble(
    key = "constant",
    defValue = 1.6180339887 // a default value to use if the searched one is not stored yet
)

Custom serializable objects

// retrieve the car
val carToStore: Car = kmPrefs.retrieveCustomObject(
    key = "your_key",
    deserializer = ,// custom deserializer to use during the retrieve if the type is not explicit
    defValue = Car(
        plate = "not_found",
        hp = 0
    ) // a default value to use if the searched one is not stored yet
)

Remove values

// same method for all the types
kmPrefs.removeValue(
    key = "constant"
)  

Check whether a key has been stored

// same method for all the types
kmPrefs.hasKey(
    key = "your_key"
)

Check whether a value matches with a specified one

Primitives and String

// same method for all the types
kmPrefs.valueMatchesTo(
    key = "your_key",
    matcher = // the matcher value such "Hello World!", 22, doubleArrayOf(8, 1) etc...
)

Custom serializable objects

kmPrefs.customObjectMatchesTo(
    key = "your_key",
    deserializer =,// custom deserializer to use when the matcher value is null 
    matcher = carToStore// the matcher custom object
)

Support

If you need help using the library or encounter any problems or bugs, please contact us via the following links:

Thank you for your help!

Donations

If you want support project and developer

Crypto Address Network
3H3jyCzcRmnxroHthuXh22GXXSmizin2yp Bitcoin
0x1b45bc41efeb3ed655b078f95086f25fc83345c4 Ethereum
AtPjUnxYFHw3a6Si9HinQtyPTqsdbfdKX3dJ1xiDjbrL Solana

If you want support project and developer with PayPal

Copyright © 2025 Tecknobit