MMKV简介

1
2
MMKV 是基于 mmap 内存映射的 key-value 组件,底层序列化/反序列化使用 protobuf 实现,性能高,稳定性强。从 2015 年中至今在微
信上使用,其性能和稳定性经过了时间的验证。近期也已移植到 Android / macOS / Win32 / POSIX 平台,一并开源。

github地址:https://github.com/Tencent/MMKV

通过github的中文文档查看编译指导:https://github.com/Tencent/MMKV/blob/master/README_CN.md

直接查看POSIX 指南(POSIX是可移植操作系统接口是IEEE为要在各种UNIX操作系统上运行软件,而定义API的一系列互相关联的标准的总称)

POSIX 安装指南原文:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
POSIX 安装指南
基本要求
MMKV 支持 Linux(Ubuntu, Arch Linux, CentOS, Gentoo)、Unix(macOS, FreeBSD, OpenBSD) 等 POSIX 平台;
MMKV 需使用 CMake 3.8.0 或以上进行编译;
C++ 编译器需支持 C++ 17 标准。
通过 CMake 安装引入
获取 MMKV 源码:

git clone https://github.com/Tencent/MMKV.git
打开你项目的 CMakeLists.txt, 添加这几行:

add_subdirectory(mmkv/POSIX/src mmkv)
target_link_libraries(MyApp mmkv)
添加头文件 #include "MMKV.h",就可以愉快地开始你的 MMKV 之旅了。

注意:

你也可以编译运行 demo 工程来测试 MMKV:

cd mmkv/POSIX
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make
cd demo && ./demo
MMKV 依赖 zlib 库。如果你的系统没有安装 zlib1g-dev 库也不用担心,MMKV 会使用内置的精简版(zlib v1.2.11)。

如果你确定不需要加密功能,你可以在Core/MMKVPredef.h 文件中打开宏MMKV_DISABLE_CRYPT,以减小一些二进制大小。

简略编译过程(将mmkv源码引入 ndk编译)

1
2
3
4
5
1. git clone https://github.com/Tencent/MMKV.git
2. 创建一个空的Android 工程
3. 创建一个moudle,选择Android Native Livrary
4. 将mmkv中的Core目录直接复制到新建的moudle中
5. 修改CMakeList.txt

关键部分如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.

# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("mmkv")

# 添加头文件,方便自己的源码中引用Core下的头文件
include_directories(Core)

# 添加mmkv的子CMakeList,在编译时会首先执行子目录的CMakeList
add_subdirectory(Core)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
# is preferred for the same purpose.
#
# In order to load a library into your app from Java/Kotlin, you must call
# System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file.
add_library(${CMAKE_PROJECT_NAME} SHARED
# List C/C++ source files with relative paths to this CMakeLists.txt.
# 添加源文件,这个源文件可加可不加,随意
libmmkv.cpp
mmkv.cpp)

# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
target_link_libraries(${CMAKE_PROJECT_NAME}
mmkv
core
# pthread 这里注意,不能链接pthread,在NDK中使用stdc++
stdc++
# List libraries link to the target library
android
log)

然后直接编译项目即可,具体封装可按照mmkv的api文档进行。详细内容可直接给项目导入Android studio查看