CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-tencent--mmkv-shared

MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application.

Pending
Overview
Eval results
Files

initialization.mddocs/

Initialization and Setup

Core initialization functionality for setting up MMKV in Android applications. MMKV must be initialized once during application startup before creating any MMKV instances.

Capabilities

Basic Initialization

Initialize MMKV with default settings using application context.

/**
 * Initialize MMKV with default configuration.
 * You must call one of the initialize() methods on App startup process before using MMKV.
 * @param context The context of Android App, usually from Application
 * @return The root folder of MMKV, defaults to $(FilesDir)/mmkv
 */
public static String initialize(Context context);

Usage Example:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        
        // Initialize MMKV with default settings
        String rootDir = MMKV.initialize(this);
        Log.d("MMKV", "MMKV initialized with root: " + rootDir);
    }
}

Initialization with Log Level

Initialize MMKV with custom log level for debugging or production builds.

/**
 * Initialize MMKV with customize log level.
 * @param context The context of Android App, usually from Application
 * @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:

// Debug build with verbose logging
String rootDir = MMKV.initialize(this, MMKVLogLevel.LevelDebug);

// Production build with minimal logging
String rootDir = MMKV.initialize(this, MMKVLogLevel.LevelError);

Initialization with Custom Root Directory

Initialize MMKV with a custom storage directory.

/**
 * Initialize MMKV with customize root folder.
 * @param context The context of Android App, usually from Application
 * @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 context of Android App, usually from Application
 * @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");
String rootDir = MMKV.initialize(this, customDir.getAbsolutePath());

Initialization with Library Loader

Initialize MMKV with a custom library loader (e.g., ReLinker) for enhanced native library loading reliability.

/**
 * Initialize MMKV with a 3rd library loader.
 * @param context The context of Android App, usually from Application
 * @param loader The 3rd library loader (for example, the 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 context of Android App, usually from Application
 * @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);

Usage Example:

// Using ReLinker for safer native library loading
MMKV.LibLoader relinkerLoader = new MMKV.LibLoader() {
    @Override
    public void loadLibrary(String libName) {
        ReLinker.loadLibrary(MyApplication.this, libName);
    }
};
String rootDir = MMKV.initialize(this, relinkerLoader, MMKVLogLevel.LevelInfo);

Full Initialization

Complete initialization with all customization options including error handling callbacks.

/**
 * Initialize MMKV with customize settings.
 * @param context The context of Android App, usually from Application
 * @param rootDir The root folder of MMKV
 * @param loader The 3rd library loader (optional)
 * @param logLevel The log level of MMKV
 * @param handler The callback handler for error recovery and logging (optional)
 * @return The root folder of MMKV
 */
public static String initialize(Context context, String rootDir, LibLoader loader, MMKVLogLevel logLevel, MMKVHandler handler);

Usage Example:

MMKVHandler customHandler = 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.OnErrorRecover;
    }
    
    @Override
    public boolean wantLogRedirecting() {
        return true; // Redirect MMKV logs to custom handler
    }
    
    @Override
    public void mmkvLog(MMKVLogLevel level, String file, int line, String function, String message) {
        Log.d("MMKV-Custom", message);
    }
};

String rootDir = MMKV.initialize(
    this,
    getFilesDir().getAbsolutePath() + "/mmkv",
    null,
    MMKVLogLevel.LevelInfo,
    customHandler
);

Global Settings

Static methods for managing global MMKV settings.

/**
 * Set the log level of MMKV.
 * @param level Defaults to MMKVLogLevel.LevelInfo
 */
public static void setLogLevel(MMKVLogLevel level);

/**
 * Get the root folder of MMKV.
 * @return The root folder of MMKV, defaults to $(FilesDir)/mmkv
 */
public static String getRootDir();

/**
 * Get the device's memory page size.
 * @return The device's memory page size
 */
public static int pageSize();

/**
 * Get the version of MMKV.
 * @return The version of MMKV
 */
public static String version();

/**
 * Notify MMKV that App is about to exit. It's totally fine not calling it at all.
 */
public static void onExit();

Usage Example:

// Change log level at runtime
MMKV.setLogLevel(MMKVLogLevel.LevelDebug);

// Get version information
String version = MMKV.version();
Log.i("MMKV", "Using MMKV version: " + version);

// Get system page size
int pageSize = MMKV.pageSize();
Log.i("MMKV", "System page size: " + pageSize + " bytes");

// Cleanup on app exit (optional)
@Override
protected void onDestroy() {
    super.onDestroy();
    MMKV.onExit();
}

Process Mode Management

Control process mode checking for debugging multi-process access patterns.

/**
 * Manually enable the process mode checker.
 * By default, it's automatically enabled in DEBUG build, and disabled in RELEASE build.
 */
public static void enableProcessModeChecker();

/**
 * Manually disable the process mode checker.
 * By default, it's automatically enabled in DEBUG build, and disabled in RELEASE build.
 */
public static void disableProcessModeChecker();

Usage Example:

// Enable process mode checking in production for debugging
if (BuildConfig.DEBUG || isDebuggingMultiProcess) {
    MMKV.enableProcessModeChecker();
} else {
    MMKV.disableProcessModeChecker();
}

Types

public interface LibLoader {
    void loadLibrary(String libName);
}

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 MMKVLogLevel {
    LevelDebug,    // Debug level. Not available for release/production build
    LevelInfo,     // Info level. The default level
    LevelWarning,  // Warning level
    LevelError,    // Error level
    LevelNone      // Special level for disabling all logging
}

public enum MMKVRecoverStrategic {
    OnErrorDiscard,  // The default strategic is to discard everything on errors
    OnErrorRecover   // The recover strategic will try to recover as much data as possible
}

Install with Tessl CLI

npx tessl i tessl/maven-com-tencent--mmkv-shared

docs

advanced-features.md

data-management.md

encryption.md

index.md

initialization.md

instance-management.md

multi-process.md

storage-operations.md

tile.json