MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application.
npx @tessl/cli install tessl/maven-com-tencent--mmkv-shared@2.2.0MMKV is an efficient, high-performance key-value storage framework developed by Tencent for Android applications. It uses memory-mapped files for fast read/write operations and Protocol Buffers for serialization, providing superior performance compared to SharedPreferences while maintaining full compatibility with the SharedPreferences interface.
implementation 'com.tencent:mmkv-shared:2.2.2'import com.tencent.mmkv.MMKV;
import com.tencent.mmkv.MMKVLogLevel;import com.tencent.mmkv.MMKV;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize MMKV (call once in Application.onCreate)
String rootDir = MMKV.initialize(this);
// Get default MMKV instance
MMKV kv = MMKV.defaultMMKV();
// Store values
kv.encode("user_name", "Alice");
kv.encode("user_age", 25);
kv.encode("is_premium", true);
// Retrieve values
String name = kv.decodeString("user_name", "");
int age = kv.decodeInt("user_age", 0);
boolean isPremium = kv.decodeBool("is_premium", false);
// Use as SharedPreferences drop-in replacement
SharedPreferences.Editor editor = kv.edit();
editor.putString("settings", "value").apply();
}
}MMKV is built around several key components:
Core initialization functionality for setting up MMKV in Android applications. Must be called before using any MMKV instances.
public static String initialize(Context context);
public static String initialize(Context context, MMKVLogLevel logLevel);
public static String initialize(Context context, String rootDir);
public static String initialize(Context context, String rootDir, MMKVLogLevel logLevel);
public static String initialize(Context context, String rootDir, LibLoader loader, MMKVLogLevel logLevel, MMKVHandler handler);MMKV instance creation with various configuration options including encryption, multi-process modes, and custom storage locations.
public static MMKV defaultMMKV();
public static MMKV mmkvWithID(String mmapID);
public static MMKV mmkvWithID(String mmapID, int mode);
public static MMKV mmkvWithID(String mmapID, int mode, String cryptKey);
public static MMKV mmkvWithID(String mmapID, int mode, String cryptKey, String rootPath, long expectedCapacity);Core storage operations for all supported data types including primitives, strings, byte arrays, and Parcelable objects, with optional expiration support.
public boolean encode(String key, boolean value);
public boolean encode(String key, int value);
public boolean encode(String key, long value);
public boolean encode(String key, String value);
public boolean encode(String key, byte[] value);
public boolean decodeBool(String key, boolean defaultValue);
public int decodeInt(String key, int defaultValue);
public String decodeString(String key, String defaultValue);Advanced multi-process functionality for safe concurrent access across Android processes, including inter-process locking and content change notifications.
public void lock();
public void unlock();
public boolean tryLock();
public void checkContentChangedByOuterProcess();
public boolean isMultiProcess();Encryption capabilities for protecting sensitive data with AES encryption, key management, and secure key rotation.
public String cryptKey();
public boolean reKey(String cryptKey);
public void checkReSetCryptKey(String cryptKey);Comprehensive data management including backup/restore operations, file validation, storage cleanup, and performance optimization.
public void clearAll();
public void clearAllWithKeepingSpace();
public void trim();
public long totalSize();
public long actualSize();
public boolean removeStorage(String mmapID);
public boolean isFileValid(String mmapID);Advanced functionality including key expiration, native buffer operations, Anonymous Shared Memory support, and performance optimizations.
public boolean enableAutoKeyExpire(int expireDurationInSecond);
public boolean disableAutoKeyExpire();
public void enableCompareBeforeSet();
public NativeBuffer createNativeBuffer(int size);
public MMKV mmkvWithAshmemID(Context context, String mmapID, int size, int mode, String cryptKey);// Process modes
public static final int SINGLE_PROCESS_MODE = 1 << 0;
public static final int MULTI_PROCESS_MODE = 1 << 1;
public static final int READ_ONLY_MODE = 1 << 5;
// Expiration constants
public static final int ExpireNever = 0;
public static final int ExpireInMinute = 60;
public static final int ExpireInHour = 60 * 60;
public static final int ExpireInDay = 24 * 60 * 60;
public static final int ExpireInMonth = 30 * 24 * 60 * 60;
public static final int ExpireInYear = 365 * 30 * 24 * 60 * 60;// Core enums
public enum MMKVLogLevel {
LevelDebug, LevelInfo, LevelWarning, LevelError, LevelNone
}
public enum MMKVRecoverStrategic {
OnErrorDiscard, OnErrorRecover
}
// Callback interfaces
public interface MMKVHandler {
MMKVRecoverStrategic onMMKVCRCCheckFail(String mmapID);
MMKVRecoverStrategic onMMKVFileLengthError(String mmapID);
boolean wantLogRedirecting();
void mmkvLog(MMKVLogLevel level, String file, int line, String function, String message);
}
public interface MMKVContentChangeNotification {
void onContentChangedByOuterProcess(String mmapID);
}
// Native buffer support
public final class NativeBuffer {
public long pointer;
public int size;
public NativeBuffer(long ptr, int length);
}
// Library loader interface
public interface LibLoader {
void loadLibrary(String libName);
}
// Exception classes
public class UnsupportedArchitectureException extends RuntimeException {
public UnsupportedArchitectureException(String message);
}