MMKV Key-Value Storage Framework

repository·master·Indexed 12 days ago

https://github.com/tencent/mmkv

A high-performance key-value storage framework designed for mobile applications, providing fast data persistence and support for primitives and byte arrays. It offers official support for Flutter (v2.4.1) and Kotlin Multiplatform (v2.4.1), with platform-specific implementations for Android, iOS, Linux, Windows, and OpenHarmony (OHOS). Features include data encryption via cryptKey, customizable storage locations, and a common platform interface for extensibility.

Tokens
12.9K
Snippets
44
Records
57
Agent score
97%

What's inside MMKV

  1. MMKV for Python (on POSIX & Windows) Overview

    master
    MMKV is an efficient, small, and easy-to-use key-value storage framework originally developed for the WeChat application. The Python implementation supports both POSIX and Windows environments.
  2. Avoid duplicate symbols on iOS

    master
    When using mmkv-kmp on iOS, do not link the native MMKV CocoaPod or Swift Package Manager (SPM) product into the same binary. mmkv-kmp already embeds MMKV Core through a C bridge. Linking both will result in duplicate native symbols.
  3. Run Flutter against a local MMKV source

    master

    If you are developing or testing against a local checkout of the MMKV source instead of the tagged Swift package, you can point the build process to your local directory by setting the MMKV_LOCAL_PACKAGE_PATH environment variable to the root of your MMKV checkout.

    MMKV_LOCAL_PACKAGE_PATH=/path/to/MMKV flutter run
  4. Use the mmkv_example project as a starting point

    master
    The mmkv_example project serves as a demonstration of how to integrate and use the MMKV plugin within a Flutter application. It is designed to be used as a template or reference for developers looking to implement MMKV's high-performance key-value storage in their own Flutter projects.
  5. How to use mmkv_win32

    master

    The mmkv_win32 package provides the Windows-specific FFI implementation for MMKV.

    Important: Do not use this package alone. It is not a complete set of MMKV functionality; it is a supporting implementation detail.

    Recommended Usage: Because this is an [endorsed][2] package, you should use the main mmkv package instead. When you add mmkv to your project, mmkv_win32 will be automatically included as a dependency for Windows platforms. You do not need to manually add mmkv_win32 to your pubspec.yaml for standard usage.

    Direct Usage: If you intend to import this package to access its specific APIs directly, you must add it to your pubspec.yaml manually.

  6. How to use mmkv_ios in a Flutter project

    master

    The mmkv_ios package provides the FFI (Foreign Function Interface) implementation required for MMKV to function on iOS.

    Important: Do not use mmkv_ios as your primary interface. It is not a complete set of MMKV functionality. Instead, use the main mmkv package. Because mmkv_ios is an endorsed federated plugin, it will be automatically included in your project when you add mmkv to your dependencies. You do not need to add mmkv_ios to your pubspec.yaml unless you intend to import its APIs directly.

  7. Upload Android and KMP artifacts to Maven Central

    master

    To release MMKV, upload the Android artifacts first, followed by the KMP artifacts. The KMP command publishes the root metadata artifact and all target-specific artifacts (Android, iOS, etc.) in a single operation.

    Upload Commands

    # 1. Upload Android
    cd Android/MMKV
    ./gradlew :mmkv:publishAllPublicationsToMavenCentralRepository
    
    # 2. Upload KMP
    cd ../../KMP
    ./gradlew :mmkv:publishAllPublicationsToSonatypeRepository \
      -PMMKV_USE_MAVEN_LOCAL=true

    Central Portal Handoff

    After the Maven-style upload, the build automatically performs a handoff to the Central Portal. The default behavior is user_managed, which transfers the deployment for final review. You can override this using -PCENTRAL_PORTAL_PUBLISHING_TYPE:

    • automatic: Transfer and automatically release after validation.
    • portal_api: Transfer only; continue through the Publisher API.

    Configuration Properties

    • CENTRAL_PORTAL_NAMESPACE: e.g., com.tencent
    • CENTRAL_PORTAL_STAGING_API_URL: https://ossrh-staging-api.central.sonatype.com
    • CENTRAL_PORTAL_DRY_RUN: Set to true for testing.
    # Example: Automatic release after validation
    ./gradlew :mmkv:publishAllPublicationsToSonatypeRepository \
      -PMMKV_USE_MAVEN_LOCAL=true \
      -PCENTRAL_PORTAL_PUBLISHING_TYPE=automatic
  8. Test KMP publications locally

    master

    Run local tests for all KMP targets (Android, iOS X64, and iOS Arm64) and publish them to a local test repository to verify the build before a real release.

    To run the common smoke suite on a connected Android device, use connectedAndroidDeviceTest after ensuring your device is visible via adb devices -l.

    # Test all targets and publish to local test repository
    KMP/gradlew -p KMP \
      :mmkv:assembleAndroidDeviceTest \
      :mmkv:iosX64Test \
      :mmkv:linkDebugTestIosArm64 \
      :mmkv:linkDebugTestIosSimulatorArm64 \
      :mmkv:publishAllPublicationsToLocalTestRepository \
      -PMMKV_LOCAL_REPOSITORY="$LOCAL_REPO" \
      --project-cache-dir "$PWD/KMP/build/gradle-project-cache"
    
    # Run smoke suite on connected Android device
    KMP/gradlew -p KMP :mmkv:connectedAndroidDeviceTest \
      -PMMKV_LOCAL_REPOSITORY="$LOCAL_REPO" \
      --project-cache-dir "$PWD/KMP/build/gradle-project-cache"
  9. Build MMKV KMP from source

    master

    To build the KMP project and its iOS targets, you need macOS, Xcode command-line tools, CMake, and JDK 11 or newer. Use the following command from the KMP directory:

    Note: Use -PMMKV_USE_MAVEN_LOCAL=true if the matching Android artifact is only available in your Maven Local repository.

    cd KMP
    ./gradlew :mmkv:assemble -PMMKV_USE_MAVEN_LOCAL=true
  10. Install MMKV for Flutter

    master

    Add mmkv to your pubspec.yaml dependencies and run flutter pub get.

    Android Migration/Conflict Resolution: If your app uses com.tencent.mmkv-static or com.tencent.mmkv-shared, you should migrate to com.tencent.mmkv. If third-party SDKs cause conflicts, use the following build.gradle configuration to replace them:

        dependencies {
            ...
    
            modules {
                module("com.tencent:mmkv-static") {
                    replacedBy("com.tencent:mmkv", "Using mmkv for flutter")
                }
                module("com.tencent:mmkv-shared") {
                    replacedBy("com.tencent:mmkv", "Using mmkv for flutter")
                }
            }
        }

    iOS Migration: Starting from v1.3.5, you no longer need to rename the plugin to mmkvflutter. Remove any fix_mmkv_plugin_name() scripts from your Podfile.

    dependencies:
      mmkv: "^2.4.1"
  11. Publish the Android dependency locally for KMP development

    master

    The KMP Android target depends on com.tencent:mmkv:<version>. To develop KMP locally, you must first publish the Android dependency to a workspace-local repository. This allows both the Android dependency and the KMP publications to share the same local repository.

    Use the following commands to clean and publish the Android dependency to a local Maven directory:

    cd <MMKV checkout>
    LOCAL_REPO="$PWD/KMP/build/local-maven"
    
    KMP/gradlew -p KMP :mmkv:clean \
      --project-cache-dir "$PWD/KMP/build/gradle-project-cache"
    
    Android/MMKV/gradlew -p Android/MMKV \
      :mmkv:clean \
      :mmkv:publishDefaultCppReleasePublicationToMavenLocal \
      -Dmaven.repo.local="$LOCAL_REPO" \
      --project-cache-dir "$PWD/KMP/build/android-project-cache"