{"id":15067319,"url":"https://github.com/ctripcorp/mmkv-kotlin","last_synced_at":"2025-04-05T07:02:18.364Z","repository":{"id":37348373,"uuid":"481907762","full_name":"ctripcorp/mmkv-kotlin","owner":"ctripcorp","description":"A Kotlin Multiplatform porting for MMKV.","archived":false,"fork":false,"pushed_at":"2025-02-06T05:20:55.000Z","size":226,"stargazers_count":148,"open_issues_count":2,"forks_count":11,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-03-29T06:01:45.226Z","etag":null,"topics":["android","ios","kotlin","kotlin-multiplatform","kotlin-multiplatform-mobile","kotlin-native"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ctripcorp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-15T09:30:58.000Z","updated_at":"2025-03-28T13:45:49.000Z","dependencies_parsed_at":"2023-11-28T04:29:45.624Z","dependency_job_id":"ca90dcba-5d8f-44a6-9e1c-41618eb09911","html_url":"https://github.com/ctripcorp/mmkv-kotlin","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctripcorp%2Fmmkv-kotlin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctripcorp%2Fmmkv-kotlin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctripcorp%2Fmmkv-kotlin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctripcorp%2Fmmkv-kotlin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ctripcorp","download_url":"https://codeload.github.com/ctripcorp/mmkv-kotlin/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247299829,"owners_count":20916190,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","ios","kotlin","kotlin-multiplatform","kotlin-multiplatform-mobile","kotlin-native"],"created_at":"2024-09-25T01:19:38.114Z","updated_at":"2025-04-05T07:02:18.341Z","avatar_url":"https://github.com/ctripcorp.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MMKV for Kotlin Multiplatform\n\n中文版本请参看[这里](README_CN.md)\n\nMMKV-Kotlin is a porting of [MMKV](https://github.com/Tencent/MMKV) to Kotlin Multiplatform. Currently, Android/iOS/macOS are supported.\n\n## Tutorial\n\n### Installation Via Maven in Gradle\n\nKotlin Multiplatform Common (kts):\n\n```kotlin\ndependencies {     \n    implementation(\"com.ctrip.flight.mmkv:mmkv-kotlin:1.2.16\")\n}\n```\n\nCurrent version is based on `Kotlin 2.1.10` and `MMKV 2.0.2`.\n\nPure Android platform (kts):\n\n```kotlin\ndependencies {     \n    implementation(\"com.ctrip.flight.mmkv:mmkv-kotlin-android:1.2.16\")\n}\n```\n\nKotlin/Native on macOS：\n\n```kotlin\ndependencies { \n    // Intel Chip\n    implementation(\"com.ctrip.flight.mmkv:mmkv-kotlin-macosx64:1.2.16\")\n    \n    // M1\u0026M2 Chip\n    implementation(\"com.ctrip.flight.mmkv:mmkv-kotlin-macosarm64:1.2.16\")\n}\n```\nNote, 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：\n\n```kotlin\nkotlin {\n    macosX64 {\n        binaries {\n            // ......\n            all {\n                val moduleName = \"mmkv_operator\"\n                val mmkvPath = \"${buildDir.absolutePath}/cocoapods/synthetic/OSX/$moduleName/build/Release/MMKV\"\n                val mmkvCorePath = \"${buildDir.absolutePath}/cocoapods/synthetic/OSX/$moduleName//build/Release/MMKVCore\"\n                linkerOpts += listOf(\n                    \"-F$mmkvPath\",\n                    \"-rpath\", mmkvPath,\n                    \"-framework\", \"MMKV\",\n                    \"-F$mmkvCorePath\",\n                    \"-rpath\", mmkvCorePath,\n                    \"-framework\", \"MMKVCore\"\n                )\n            }\n        }\n    }\n    cocoapods {\n        // ......\n        pod(name = \"MMKV\") {\n            version = \"2.0.2\"\n            moduleName = \"MMKV\"\n        }\n    }\n    // ......\n}\n```\n\n### Initialization and Configure Root Path\n\nYou can initialize MMKV when your app or the process initialization. [MMKV-Android](https://github.com/Tencent/MMKV/tree/master/Android/MMKV) initialization API depends on [Context](https://developer.android.com/reference/android/content/Context). So, Android's initialization API is different with iOS.\n\nAndroid:\n\n```kotlin\nimport com.ctrip.flight.mmkv.initialize\n\n// In Android source set\nfun initializeMMKV(context: Context) {\n    val rootDir = initialize(context)\n    Log.d(\"MMKV Path\", rootDir)\n}\n```\n\niOS:\n\n```kotlin\nimport com.ctrip.flight.mmkv.initialize\n\n// In iOS source set\nfun initializeMMKV(rootDir: String) {\n    initialize(rootDir)\n    println(\"MMKV Path: $rootDir\")\n}\n```\n\nYou also could call [MMKV-Android](https://github.com/Tencent/MMKV/tree/master/Android/MMKV) (Java) or [MMKV-iOS](https://github.com/Tencent/MMKV/tree/master/iOS) (Objective-C) initialization API in your Android or iOS app project.\n\n### CRUD Operations\n\n- MMKV has a **global instance**, you can use it directly:\n\n```kotlin\nimport com.ctrip.flight.mmkv.defaultMMKV\n\nfun demo() {\n    val kv = defaultMMKV()\n\n    kv.set(\"Boolean\", true)\n    println(\"Boolean: ${kv.takeBoolean(\"Boolean\")}\")\n\n    kv.set(\"Int\", Int.MIN_VALUE)\n    println(\"Int: ${kv.takeInt(\"Int\")}\")\n\n    kv.set(\"Long\", Long.MAX_VALUE)\n    println(\"Long: ${kv.takeLong(\"Long\")}\")\n\n    kv.set(\"Float\", -3.14f)\n    println(\"Float: ${kv.takeFloat(\"Float\")}\")\n\n    kv.set(\"Double\", Double.MIN_VALUE)\n    println(\"Double: ${kv.takeDouble(\"Double\")}\")\n\n    kv.set(\"String\", \"Hello from mmkv\")\n    println(\"String: ${kv.takeString(\"String\")}\")\n\n    val bytes = byteArrayOf(\n        'm'.code.toByte(), \n        'm'.code.toByte(), \n        'k'.code.toByte(), \n        'v'.code.toByte(),\n    )\n    kv.set(\"ByteArray\", bytes)\n    println(\"ByteArray: ${kv.takeByteArray(\"ByteArray\")?.toString()}\")\n}\n```\n\n- **Deleting \u0026 Judging (Whether the key is existed):**\n\n```kotlin\nkv.removeValueForKey(\"Boolean\")\nprintln(\"Boolean: ${kv.takeBoolean(\"Boolean\")}\")\n\nkv.removeValuesForKeys(listOf(\"Int\", \"Long\"))\nprintln(\"allKeys: ${kv.allKeys()}\")\n\nval hasBoolean = kv.containsKey(\"Boolean\")\n```\n\n- If a different modules/logics need **isolated storage**, you can also create your own MMKV instance separately:\n\n```kotlin\nimport com.ctrip.flight.mmkv.mmkvWithID\n...\nval kvWithMyId = mmkvWithID(\"MyID\")\nkvWithMyId.set(\"Boolean\", true)\n```\n\n- If **multi-process accessing** is needed, you can set `MMKVMode.MULTI_PROCESS` when MMKV initialization:\n\n```kotlin\nimport com.ctrip.flight.mmkv.mmkvWithID\nimport com.ctrip.flight.mmkv.MMKVModel\n\n...\nval kvMultiProcess = mmkvWithID(\"InterProcessKV\", MMKVModel.MULTI_PROCESS)\nkvMultiProcess.set(\"Boolean\", true)\n```\n\n## Supported Types\n\n* Supported Kotlin types in Kotlin Multiplatform common source set：\n  * `Boolean, Int, Long, Float, Double, String, UInt, ULong, ByteArray, Set\u003cString\u003e`\n \n\n* The following types are additionally supported in the Android source set:\n  * `Any class that implements Parcelable`\n\n\n* The following types are additionally supported in the Apple source set:\n  * `NSDate and any class that implements NSCoding protocol`\n\n\n## Note\n \n- MMKV-Kotlin currently does not support migrating old data from SharedPreferences and NSUserDefaults.\n\n## License\n\nDistributed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).\n\nSee [LICENSE](LICENSE.txt) for more information.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fctripcorp%2Fmmkv-kotlin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fctripcorp%2Fmmkv-kotlin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fctripcorp%2Fmmkv-kotlin/lists"}