High-performance mobile key-value storage framework with memory mapping, encryption, and multi-process support for Android applications.
—
Core initialization methods for setting up MMKV with various configuration options including custom directories, log levels, library loaders, and error handlers.
Initialize MMKV with default configuration. Must be called once during app startup before using any MMKV instances.
/**
* Initialize MMKV with default configuration.
* @param context The Android app context, usually from Application
* @return The root folder of MMKV, defaults to $(FilesDir)/mmkv
*/
public static String initialize(Context context);Usage Example:
import com.tencent.mmkv.MMKV;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Initialize MMKV once during app startup
String rootDir = MMKV.initialize(this);
Log.d("MMKV", "MMKV root directory: " + rootDir);
}
}Initialize MMKV with a custom log level for debugging and production environments.
/**
* Initialize MMKV with customize log level.
* @param context The Android app context
* @param logLevel The log level of MMKV, defaults to MMKVLogLevel.LevelInfo
* @return The root folder of MMKV
*/
public static String initialize(Context context, MMKVLogLevel logLevel);Usage Example:
import com.tencent.mmkv.MMKV;
import com.tencent.mmkv.MMKVLogLevel;
// Initialize with debug logging for development
MMKV.initialize(this, MMKVLogLevel.LevelDebug);
// Initialize with error-only logging for production
MMKV.initialize(this, MMKVLogLevel.LevelError);Initialize MMKV with a custom root directory for storing data files.
/**
* Initialize MMKV with customize root folder.
* @param context The Android app context
* @param rootDir The root folder of MMKV, defaults to $(FilesDir)/mmkv
* @return The root folder of MMKV
*/
public static String initialize(Context context, String rootDir);
/**
* Initialize MMKV with customize root folder and log level.
* @param context The Android app context
* @param rootDir The root folder of MMKV
* @param logLevel The log level of MMKV
* @return The root folder of MMKV
*/
public static String initialize(Context context, String rootDir, MMKVLogLevel logLevel);Usage Example:
// Use custom directory on external storage
File customDir = new File(getExternalFilesDir(null), "custom_mmkv");
MMKV.initialize(this, customDir.getAbsolutePath());
// With custom log level
MMKV.initialize(this, customDir.getAbsolutePath(), MMKVLogLevel.LevelWarning);Initialize MMKV with a third-party library loader such as ReLinker for improved native library loading reliability.
/**
* Initialize MMKV with a 3rd library loader.
* @param context The Android app context
* @param loader The 3rd library loader (e.g., ReLinker)
* @return The root folder of MMKV
*/
public static String initialize(Context context, LibLoader loader);
/**
* Initialize MMKV with a 3rd library loader and customize log level.
* @param context The Android app context
* @param loader The 3rd library loader
* @param logLevel The log level of MMKV
* @return The root folder of MMKV
*/
public static String initialize(Context context, LibLoader loader, MMKVLogLevel logLevel);
/**
* Initialize MMKV with customize root folder and a 3rd library loader.
* @param context The Android app context
* @param rootDir The root folder of MMKV
* @param loader The 3rd library loader
* @return The root folder of MMKV
*/
public static String initialize(Context context, String rootDir, LibLoader loader);Usage Example:
import com.getkeepsafe.relinker.ReLinker;
import com.tencent.mmkv.MMKV;
// Create a ReLinker-based loader for better reliability
MMKV.LibLoader reLinkerLoader = new MMKV.LibLoader() {
@Override
public void loadLibrary(String libName) {
ReLinker.loadLibrary(MyApplication.this, libName);
}
};
// Initialize with ReLinker
MMKV.initialize(this, reLinkerLoader);Complete initialization with all customization options including error handlers.
/**
* Initialize MMKV with all customize settings.
* @param context The Android app context
* @param rootDir The root folder of MMKV
* @param loader The 3rd library loader
* @param logLevel The log level of MMKV
* @param handler The error and log handler
* @return The root folder of MMKV
*/
public static String initialize(Context context, String rootDir, LibLoader loader,
MMKVLogLevel logLevel, MMKVHandler handler);Usage Example:
import com.tencent.mmkv.*;
MMKVHandler handler = new MMKVHandler() {
@Override
public MMKVRecoverStrategic onMMKVCRCCheckFail(String mmapID) {
Log.e("MMKV", "CRC check failed for " + mmapID);
return MMKVRecoverStrategic.OnErrorRecover; // Try to recover data
}
@Override
public MMKVRecoverStrategic onMMKVFileLengthError(String mmapID) {
Log.e("MMKV", "File length error for " + mmapID);
return MMKVRecoverStrategic.OnErrorDiscard; // Discard corrupted data
}
@Override
public boolean wantLogRedirecting() {
return true; // Redirect MMKV logs to this handler
}
@Override
public void mmkvLog(MMKVLogLevel level, String file, int line, String function, String message) {
Log.d("MMKV", String.format("[%s:%d] %s - %s", file, line, function, message));
}
};
// Full initialization
MMKV.initialize(this, customRootDir, reLinkerLoader, MMKVLogLevel.LevelInfo, handler);Methods for managing global MMKV settings after initialization.
/**
* Get the root folder of MMKV.
* @return The root folder path
*/
public static String getRootDir();
/**
* Set the log level of MMKV.
* @param level The new log level
*/
public static void setLogLevel(MMKVLogLevel level);
/**
* Get the device's memory page size.
* @return The page size in bytes
*/
public static native int pageSize();
/**
* Get the version of MMKV.
* @return The version string
*/
public static native String version();
/**
* Notify MMKV that App is about to exit.
* Optional - helps with cleanup but not required.
*/
public static native void onExit();Usage Example:
// Get configuration info
String rootDir = MMKV.getRootDir();
int pageSize = MMKV.pageSize();
String version = MMKV.version();
// Change log level at runtime
MMKV.setLogLevel(MMKVLogLevel.LevelError);
// Optional cleanup on app exit
@Override
protected void onDestroy() {
super.onDestroy();
MMKV.onExit();
}Methods for controlling process mode validation to prevent incorrect usage patterns.
/**
* Manually enable the process mode checker.
* Automatically enabled in DEBUG build, disabled in RELEASE build.
*/
public static void enableProcessModeChecker();
/**
* Manually disable the process mode checker.
* When enabled, MMKV throws exceptions for process mode mismatches.
*/
public static void disableProcessModeChecker();Usage Example:
if (BuildConfig.DEBUG) {
// Enable strict checking in debug builds
MMKV.enableProcessModeChecker();
} else {
// Disable checking in release for performance
MMKV.disableProcessModeChecker();
}Legacy methods for registering MMKV handlers. Use the initialize() method with MMKVHandler parameter instead.
/**
* Register a handler for MMKV log redirecting and error handling.
* @deprecated Use initialize() with MMKVHandler parameter instead
* @param handler The error and log handler
*/
@Deprecated
public static void registerHandler(MMKVHandler handler);
/**
* Unregister the current MMKV handler.
*/
public static void unregisterHandler();Usage Example:
// Legacy handler registration (deprecated)
MMKVHandler legacyHandler = new MMKVHandler() {
@Override
public MMKVRecoverStrategic onMMKVCRCCheckFail(String mmapID) {
Log.w("MMKV", "CRC check failed for " + mmapID);
return MMKVRecoverStrategic.OnErrorRecover;
}
@Override
public MMKVRecoverStrategic onMMKVFileLengthError(String mmapID) {
Log.w("MMKV", "File length error for " + mmapID);
return MMKVRecoverStrategic.OnErrorDiscard;
}
@Override
public boolean wantLogRedirecting() {
return true;
}
@Override
public void mmkvLog(MMKVLogLevel level, String file, int line, String function, String message) {
Log.d("MMKV", String.format("[%s:%d] %s", file, line, message));
}
};
// Register handler (deprecated approach)
MMKV.registerHandler(legacyHandler);
// Later, unregister
MMKV.unregisterHandler();
// Preferred approach: use initialize() with handler parameter
MMKV.initialize(context, rootDir, loader, logLevel, legacyHandler);public interface LibLoader {
/**
* Load a native library by name
* @param libName The name of the library to load
*/
void loadLibrary(String libName);
}
public enum MMKVLogLevel {
LevelDebug, // Debug level (not available for release/production build)
LevelInfo, // Info level (default)
LevelWarning, // Warning level
LevelError, // Error level
LevelNone // Disable all logging (not recommended)
}
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 enum MMKVRecoverStrategic {
OnErrorDiscard, // Discard everything on errors (default)
OnErrorRecover // Try to recover as much data as possible
}Install with Tessl CLI
npx tessl i tessl/maven-com-tencent--mmkv