Skip to content

android_setup

guoling edited this page Dec 13, 2024 · 32 revisions

MMKV for Android

MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application. It's currently available on both Android, iOS/macOS, Windows and POSIX.

Getting Started

Prerequisites

  • API level 23 and above are supported. If you want API level 21~22 support, use 1.3.x LTS series.
  • ARM64 required. If you want 32-bit support, use 1.3.x LTS series.
  • NDK r26d and above (should you choose to build MMKV from source)

Installation

  • Via Maven
    Starting from v1.2.8, MMKV has been migrated to Maven Central. Older versions (<= v1.2.7) are still available on JCenter.
    Starting from v2.0.0, MMKV no longer supports 32-bit arch and API level 22 or 21, if you want 32-bit or API level 21~22, use v1.3.x LTS series.

    1. Add the following lines to build.gradle on your app module:

      dependencies {
          implementation 'com.tencent:mmkv:2.0.1'
          // replace "2.0.1" with any available version
      }

      This will cause Gradle to download the AAR package while building your application.

    2. By default, MMKV links libc++ with static library. The libc++ will consume about 2 MB size unpacked if shared linked. If you really care about bundle size and your project already includes libc++_shared.so by other JNI libs. You can use MMKV with shared linking of libc++, by adding this line to your Gradle:

      dependencies {
           implementation 'com.tencent:mmkv-shared:2.0.1'
           // replace "2.0.1" with any available version
      }
  • Build from Source
    Starting from v1.2.9, MMKV no longer supports the armeabi arch officially. And armv7 is droped from (v1.3.5~v1.3.6) v2.0+, along with the API level been bumped to API level 23. If you need to support armeabi, armv7 or lower API level, you should build MMKV from the source.

    1. Getting source code from the git repository:

      git clone https://github.com/Tencent/MMKV.git
      cd mmkv
    2. Install the latest Android NDK from Android Studio's SDK Manager.
      Note: Starting from NDK r17, ARMv5 (armeabi) is no longer supported. If you need to support the armeabi arch, you should install NDK r16b instead, and uncomment these lines inside Android/MMKV/mmkv/build.gradle:

      // uncomment this line to support armv7, add 'armeabi' to support armeabi
      // abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
      ...
      // uncomment this line to support armeabi by using android-ndk-r16b
      // ndkVersion = '16.1.4479499'

      If you need to support lower API level, change the root build.gradle's minSdkVersion variable:

      ext {
          minSdkVersion = 21 // change this to whatever you like
          ....
      }
    3. Open Android/MMKV project in Android Studio, select the 'MMKV:mmkv [buildAndPublishToLocalMaven]' gradle task, click the Run button to start building. The resulting package should be found in your $HOME/.m2/repository/com/tencent/mmkv/$version directory. You can add this to your App's build.gradle to verify that.

      repositories {
          mavenLocal()
          ....
      }
    4. If you are pretty sure that encryption is not needed, you can turn on the preprocess directive MMKV_DISABLE_CRYPT in Core/MMKVPredef.h to save some binary size.

    5. If you are pretty sure that MMKV for Flutter is not needed, you can turn on the preprocess directive MMKV_DISABLE_FLUTTER in Core/MMKVPredef.h to save some binary size.

What's next