ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - ctripcorp/mmkv-kotlin: A Kotlin Multiplatform porting for MMKV.
A Kotlin Multiplatform porting for MMKV. Contribute to ctripcorp/mmkv-kotlin development by creating an account on GitHub.
Visit Site

GitHub - ctripcorp/mmkv-kotlin: A Kotlin Multiplatform porting for MMKV.

GitHub - ctripcorp/mmkv-kotlin: A Kotlin Multiplatform porting for MMKV.

MMKV for Kotlin Multiplatform

中文版本请参看这里

MMKV-Kotlin is a porting of MMKV to Kotlin Multiplatform. Currently, Android/iOS/macOS are supported.

Tutorial

Installation Via Maven in Gradle

Kotlin Multiplatform Common (kts):

dependencies {     
    implementation("com.ctrip.flight.mmkv:mmkv-kotlin:1.2.14")
}

Current version is based on Kotlin 2.0.20 and MMKV 1.3.9.

Pure Android platform (kts):

dependencies {     
    implementation("com.ctrip.flight.mmkv:mmkv-kotlin-android:1.2.14")
}

Kotlin/Native on macOS:

dependencies { 
    // Intel Chip
    implementation("com.ctrip.flight.mmkv:mmkv-kotlin-macosx64:1.2.14")
    
    // M1&M2 Chip
    implementation("com.ctrip.flight.mmkv:mmkv-kotlin-macosarm64:1.2.14")
}

Note, if your project is a Kotlin/Native executable program project of macOS, or it supplies a framework to an iOS application project directly, then you need to manually add the dependency of MMKV, and may need to add linkerOpts for MMKV and MMKVCore:

kotlin {
    macosX64 {
        binaries {
            // ......
            all {
                val moduleName = "mmkv_operator"
                val mmkvPath = "${buildDir.absolutePath}/cocoapods/synthetic/OSX/$moduleName/build/Release/MMKV"
                val mmkvCorePath = "${buildDir.absolutePath}/cocoapods/synthetic/OSX/$moduleName//build/Release/MMKVCore"
                linkerOpts += listOf(
                    "-F$mmkvPath",
                    "-rpath", mmkvPath,
                    "-framework", "MMKV",
                    "-F$mmkvCorePath",
                    "-rpath", mmkvCorePath,
                    "-framework", "MMKVCore"
                )
            }
        }
    }
    cocoapods {
        // ......
        pod(name = "MMKV") {
            version = "1.3.9"
            moduleName = "MMKV"
        }
    }
    // ......
}

Initialization and Configure Root Path

You can initialize MMKV when your app or the process initialization. MMKV-Android initialization API depends on Context. So, Android's initialization API is different with iOS.

Android:

import com.ctrip.flight.mmkv.initialize

// In Android source set
fun initializeMMKV(context: Context) {
    val rootDir = initialize(context)
    Log.d("MMKV Path", rootDir)
}

iOS:

import com.ctrip.flight.mmkv.initialize

// In iOS source set
fun initializeMMKV(rootDir: String) {
    initialize(rootDir)
    println("MMKV Path: $rootDir")
}

You also could call MMKV-Android (Java) or MMKV-iOS (Objective-C) initialization API in your Android or iOS app project.

CRUD Operations

  • MMKV has a global instance, you can use it directly:
import com.ctrip.flight.mmkv.defaultMMKV

fun demo() {
    val kv = defaultMMKV()

    kv.set("Boolean", true)
    println("Boolean: ${kv.takeBoolean("Boolean")}")

    kv.set("Int", Int.MIN_VALUE)
    println("Int: ${kv.takeInt("Int")}")

    kv.set("Long", Long.MAX_VALUE)
    println("Long: ${kv.takeLong("Long")}")

    kv.set("Float", -3.14f)
    println("Float: ${kv.takeFloat("Float")}")

    kv.set("Double", Double.MIN_VALUE)
    println("Double: ${kv.takeDouble("Double")}")

    kv.set("String", "Hello from mmkv")
    println("String: ${kv.takeString("String")}")

    val bytes = byteArrayOf(
        'm'.code.toByte(), 
        'm'.code.toByte(), 
        'k'.code.toByte(), 
        'v'.code.toByte(),
    )
    kv.set("ByteArray", bytes)
    println("ByteArray: ${kv.takeByteArray("ByteArray")?.toString()}")
}
  • Deleting & Judging (Whether the key is existed):
kv.removeValueForKey("Boolean")
println("Boolean: ${kv.takeBoolean("Boolean")}")

kv.removeValuesForKeys(listOf("Int", "Long"))
println("allKeys: ${kv.allKeys()}")

val hasBoolean = kv.containsKey("Boolean")
  • If a different modules/logics need isolated storage, you can also create your own MMKV instance separately:
import com.ctrip.flight.mmkv.mmkvWithID
...
val kvWithMyId = mmkvWithID("MyID")
kvWithMyId.set("Boolean", true)
  • If multi-process accessing is needed, you can set MMKVMode.MULTI_PROCESS when MMKV initialization:
import com.ctrip.flight.mmkv.mmkvWithID
import com.ctrip.flight.mmkv.MMKVModel

...
val kvMultiProcess = mmkvWithID("InterProcessKV", MMKVModel.MULTI_PROCESS)
kvMultiProcess.set("Boolean", true)

Supported Types

  • Supported Kotlin types in Kotlin Multiplatform common source set:

    • Boolean, Int, Long, Float, Double, String, UInt, ULong, ByteArray, Set<String>
  • The following types are additionally supported in the Android source set:

    • Any class that implements Parcelable
  • The following types are additionally supported in the Apple source set:

    • NSDate and any class that implements NSCoding protocol

Note

  • MMKV-Kotlin currently does not support migrating old data from SharedPreferences and NSUserDefaults.

License

Distributed under the Apache License, Version 2.0.

See LICENSE for more information.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory