High-performance, cross-platform key-value storage framework with static C++ linking for Android applications
—
Supporting interfaces, exceptions, and utility classes used by MMKV for advanced functionality and error handling.
Interface for receiving inter-process content change notifications.
/**
* Inter-process content change notification.
* Triggered by any method call, such as getXXX() or setXXX() or checkContentChangedByOuterProcess().
*/
interface MMKVContentChangeNotification {
/**
* Inter-process content change notification callback
* @param mmapID The unique ID of the changed MMKV instance
*/
void onContentChangedByOuterProcess(String mmapID);
}Usage Example:
// Register for content change notifications
MMKV.registerContentChangeNotify(new MMKVContentChangeNotification() {
@Override
public void onContentChangedByOuterProcess(String mmapID) {
Log.i("MMKV", "Content changed in: " + mmapID);
// Handle the change
}
});
// Unregister when no longer needed
MMKV.unregisterContentChangeNotify();High-performance native memory wrapper for direct JNI access.
/**
* A native memory wrapper, whose underlying memory can be passed to another JNI method directly.
* Avoiding unnecessary JNI boxing and unboxing.
* Must be destroy manually using MMKV.destroyNativeBuffer().
*/
class NativeBuffer {
/** Pointer to native memory */
long pointer;
/** Size of the buffer in bytes */
int size;
/** Constructor for native buffer */
NativeBuffer(long ptr, int length);
}Usage Example:
// Create native buffer for efficient data transfer
NativeBuffer buffer = MMKV.createNativeBuffer(1024);
if (buffer != null) {
try {
// Write value to native buffer
MMKV kv = MMKV.defaultMMKV();
int written = kv.writeValueToNativeBuffer("key", buffer);
if (written > 0) {
// Use the buffer data in native code
Log.d("MMKV", "Written " + written + " bytes to buffer");
}
} finally {
// Always destroy the buffer to avoid memory leaks
MMKV.destroyNativeBuffer(buffer);
}
}Callback handler for MMKV error recovery and log redirection.
/**
* Callback handler for MMKV.
* Callback is called on the operating thread of the MMKV instance.
*/
interface MMKVHandler {
/**
* Called on CRC32-check failure
* By default MMKV will discard all data on crc32-check failure
* @param mmapID The unique ID of the MMKV instance
* @return Return OnErrorRecover to recover any data on the file
*/
MMKVRecoverStrategic onMMKVCRCCheckFail(String mmapID);
/**
* Called on file length mismatch
* By default MMKV will discard all data on file length mismatch
* @param mmapID The unique ID of the MMKV instance
* @return Return OnErrorRecover to recover any data on the file
*/
MMKVRecoverStrategic onMMKVFileLengthError(String mmapID);
/**
* Whether to redirect MMKV logs to this handler
* @return Return false if you don't want log redirecting
*/
boolean wantLogRedirecting();
/**
* Log redirecting callback
* @param level The level of this log
* @param file The file name of this log
* @param line The line of code of this log
* @param function The function name of this log
* @param message The content of this log
*/
void mmkvLog(MMKVLogLevel level, String file, int line, String function, String message);
}/**
* Exception thrown when trying to use MMKV on unsupported architecture
*/
class UnsupportedArchitectureException extends RuntimeException {
UnsupportedArchitectureException(String message);
}Global MMKV utility methods for configuration and information.
/**
* Get the device's memory page size
* @return The memory page size in bytes
*/
static int pageSize();
/**
* Get the version of MMKV
* @return The version string
*/
static String version();
/**
* Notify MMKV that App is about to exit
* It's totally fine not calling it at all
*/
static void onExit();
/**
* Set the log level of MMKV
* @param level The log level, defaults to LevelInfo
*/
static void setLogLevel(MMKVLogLevel level);
/**
* Get the root folder of MMKV
* @return The root folder path, defaults to $(FilesDir)/mmkv
*/
static String getRootDir();Static methods for creating and managing native buffers.
/**
* Create a native buffer for direct memory access
* An NativeBuffer must be manually destroyed to avoid memory leak
* @param size The size of the underlying memory
* @return NativeBuffer instance or null on failure
*/
static NativeBuffer createNativeBuffer(int size);
/**
* Destroy the native buffer to avoid memory leak
* @param buffer The buffer to destroy
*/
static void destroyNativeBuffer(NativeBuffer buffer);Methods to control process mode validation.
/**
* Manually enable the process mode checker
* By default, it's automatically enabled in DEBUG build, and disabled in RELEASE build
* If enabled, MMKV will throw exceptions when an MMKV instance is created with mismatch process mode
*/
static void enableProcessModeChecker();
/**
* Manually disable the process mode checker
* By default, it's automatically enabled in DEBUG build, and disabled in RELEASE build
*/
static void disableProcessModeChecker();Methods to register and unregister for inter-process content change notifications.
/**
* Register for MMKV inter-process content change notification.
* The notification will trigger only when any method is manually called on the MMKV instance.
* For example checkContentChangedByOuterProcess().
* @param notify The notification handler
*/
static void registerContentChangeNotify(MMKVContentChangeNotification notify);
/**
* Unregister for MMKV inter-process content change notification.
*/
static void unregisterContentChangeNotify();Install with Tessl CLI
npx tessl i tessl/maven-com-tencent--mmkv-static