High-performance mobile key-value storage framework with memory mapping, encryption, and multi-process support for Android applications.
—
Comprehensive data storage API supporting primitives, strings, byte arrays, string sets, and Parcelable objects with optional expiration times.
Store and retrieve boolean values with optional expiration.
/**
* Store a boolean value.
* @param key The key for the value
* @param value The boolean value to store
* @return True if successful, false otherwise
*/
public boolean encode(String key, boolean value);
/**
* Store a boolean value with expiration.
* @param key The key for the value
* @param value The boolean value to store
* @param expireDurationInSecond Expiration time in seconds, 0 means never expire
* @return True if successful, false otherwise
*/
public boolean encode(String key, boolean value, int expireDurationInSecond);
/**
* Retrieve a boolean value with default false.
* @param key The key for the value
* @return The boolean value or false if not found
*/
public boolean decodeBool(String key);
/**
* Retrieve a boolean value with custom default.
* @param key The key for the value
* @param defaultValue The default value if key not found
* @return The boolean value or defaultValue if not found
*/
public boolean decodeBool(String key, boolean defaultValue);Usage Example:
MMKV kv = MMKV.defaultMMKV();
// Store boolean values
kv.encode("is_premium", true);
kv.encode("show_tutorial", false);
// Store with expiration (expires in 1 hour)
kv.encode("temp_flag", true, MMKV.ExpireInHour);
// Retrieve boolean values
boolean isPremium = kv.decodeBool("is_premium"); // true
boolean showTutorial = kv.decodeBool("show_tutorial"); // false
boolean unknownFlag = kv.decodeBool("unknown"); // false (default)
boolean unknownFlag2 = kv.decodeBool("unknown", true); // true (custom default)Store and retrieve integer values with optional expiration.
/**
* Store an integer value.
* @param key The key for the value
* @param value The integer value to store
* @return True if successful, false otherwise
*/
public boolean encode(String key, int value);
/**
* Store an integer value with expiration.
* @param key The key for the value
* @param value The integer value to store
* @param expireDurationInSecond Expiration time in seconds
* @return True if successful, false otherwise
*/
public boolean encode(String key, int value, int expireDurationInSecond);
/**
* Retrieve an integer value with default 0.
* @param key The key for the value
* @return The integer value or 0 if not found
*/
public int decodeInt(String key);
/**
* Retrieve an integer value with custom default.
* @param key The key for the value
* @param defaultValue The default value if key not found
* @return The integer value or defaultValue if not found
*/
public int decodeInt(String key, int defaultValue);Usage Example:
// Store integer values
kv.encode("user_id", 12345);
kv.encode("score", 9850);
kv.encode("level", 42);
// Store with expiration (expires in 1 day)
kv.encode("daily_bonus", 100, MMKV.ExpireInDay);
// Retrieve integer values
int userId = kv.decodeInt("user_id"); // 12345
int score = kv.decodeInt("score"); // 9850
int unknown = kv.decodeInt("unknown"); // 0 (default)
int lives = kv.decodeInt("lives", 3); // 3 (custom default)Store and retrieve long values for large numbers or timestamps.
/**
* Store a long value.
* @param key The key for the value
* @param value The long value to store
* @return True if successful, false otherwise
*/
public boolean encode(String key, long value);
/**
* Store a long value with expiration.
* @param key The key for the value
* @param value The long value to store
* @param expireDurationInSecond Expiration time in seconds
* @return True if successful, false otherwise
*/
public boolean encode(String key, long value, int expireDurationInSecond);
/**
* Retrieve a long value with default 0.
* @param key The key for the value
* @return The long value or 0 if not found
*/
public long decodeLong(String key);
/**
* Retrieve a long value with custom default.
* @param key The key for the value
* @param defaultValue The default value if key not found
* @return The long value or defaultValue if not found
*/
public long decodeLong(String key, long defaultValue);Usage Example:
// Store timestamps and large numbers
long currentTime = System.currentTimeMillis();
kv.encode("last_login", currentTime);
kv.encode("total_points", 999999999L);
// Store with expiration
kv.encode("session_start", currentTime, MMKV.ExpireInHour);
// Retrieve long values
long lastLogin = kv.decodeLong("last_login");
long totalPoints = kv.decodeLong("total_points");
long sessionStart = kv.decodeLong("session_start", currentTime);Store and retrieve floating-point values.
/**
* Store a float value.
* @param key The key for the value
* @param value The float value to store
* @return True if successful, false otherwise
*/
public boolean encode(String key, float value);
/**
* Store a float value with expiration.
* @param key The key for the value
* @param value The float value to store
* @param expireDurationInSecond Expiration time in seconds
* @return True if successful, false otherwise
*/
public boolean encode(String key, float value, int expireDurationInSecond);
/**
* Retrieve a float value with default 0.0f.
* @param key The key for the value
* @return The float value or 0.0f if not found
*/
public float decodeFloat(String key);
/**
* Retrieve a float value with custom default.
* @param key The key for the value
* @param defaultValue The default value if key not found
* @return The float value or defaultValue if not found
*/
public float decodeFloat(String key, float defaultValue);Usage Example:
// Store float values
kv.encode("completion_percentage", 85.5f);
kv.encode("volume", 0.8f);
kv.encode("temperature", 23.7f);
// Retrieve float values
float completion = kv.decodeFloat("completion_percentage"); // 85.5f
float volume = kv.decodeFloat("volume"); // 0.8f
float brightness = kv.decodeFloat("brightness", 1.0f); // 1.0f (default)Store and retrieve double-precision floating-point values.
/**
* Store a double value.
* @param key The key for the value
* @param value The double value to store
* @return True if successful, false otherwise
*/
public boolean encode(String key, double value);
/**
* Store a double value with expiration.
* @param key The key for the value
* @param value The double value to store
* @param expireDurationInSecond Expiration time in seconds
* @return True if successful, false otherwise
*/
public boolean encode(String key, double value, int expireDurationInSecond);
/**
* Retrieve a double value with default 0.0.
* @param key The key for the value
* @return The double value or 0.0 if not found
*/
public double decodeDouble(String key);
/**
* Retrieve a double value with custom default.
* @param key The key for the value
* @param defaultValue The default value if key not found
* @return The double value or defaultValue if not found
*/
public double decodeDouble(String key, double defaultValue);Usage Example:
// Store high-precision values
kv.encode("pi", 3.141592653589793);
kv.encode("exchange_rate", 1.2345678901234567);
// Retrieve double values
double pi = kv.decodeDouble("pi");
double exchangeRate = kv.decodeDouble("exchange_rate");
double defaultRate = kv.decodeDouble("unknown_rate", 1.0);Store and retrieve string values with null support.
/**
* Store a string value.
* @param key The key for the value
* @param value The string value to store (can be null)
* @return True if successful, false otherwise
*/
public boolean encode(String key, String value);
/**
* Store a string value with expiration.
* @param key The key for the value
* @param value The string value to store
* @param expireDurationInSecond Expiration time in seconds
* @return True if successful, false otherwise
*/
public boolean encode(String key, String value, int expireDurationInSecond);
/**
* Retrieve a string value with default null.
* @param key The key for the value
* @return The string value or null if not found
*/
public String decodeString(String key);
/**
* Retrieve a string value with custom default.
* @param key The key for the value
* @param defaultValue The default value if key not found
* @return The string value or defaultValue if not found
*/
public String decodeString(String key, String defaultValue);Usage Example:
// Store string values
kv.encode("username", "john_doe");
kv.encode("email", "john@example.com");
kv.encode("nickname", null); // Store null value
// Store with expiration (expires in 1 week)
kv.encode("temp_token", "abc123", 7 * MMKV.ExpireInDay);
// Retrieve string values
String username = kv.decodeString("username"); // "john_doe"
String email = kv.decodeString("email"); // "john@example.com"
String nickname = kv.decodeString("nickname"); // null
String theme = kv.decodeString("theme", "default"); // "default" (custom default)Store and retrieve collections of strings.
/**
* Store a string set.
* @param key The key for the value
* @param value The string set to store (can be null)
* @return True if successful, false otherwise
*/
public boolean encode(String key, Set<String> value);
/**
* Store a string set with expiration.
* @param key The key for the value
* @param value The string set to store
* @param expireDurationInSecond Expiration time in seconds
* @return True if successful, false otherwise
*/
public boolean encode(String key, Set<String> value, int expireDurationInSecond);
/**
* Retrieve a string set with default null.
* @param key The key for the value
* @return The string set or null if not found
*/
public Set<String> decodeStringSet(String key);
/**
* Retrieve a string set with custom default.
* @param key The key for the value
* @param defaultValue The default value if key not found
* @return The string set or defaultValue if not found
*/
public Set<String> decodeStringSet(String key, Set<String> defaultValue);
/**
* Retrieve a string set with custom default and set implementation.
* @param key The key for the value
* @param defaultValue The default value if key not found
* @param cls The Set implementation class to use
* @return The string set or defaultValue if not found
*/
public Set<String> decodeStringSet(String key, Set<String> defaultValue, Class<? extends Set> cls);Usage Example:
import java.util.*;
// Store string sets
Set<String> tags = new HashSet<>();
tags.add("android");
tags.add("mobile");
tags.add("development");
kv.encode("project_tags", tags);
Set<String> permissions = new LinkedHashSet<>();
permissions.add("CAMERA");
permissions.add("STORAGE");
kv.encode("granted_permissions", permissions);
// Store with expiration
Set<String> tempCategories = new HashSet<>();
tempCategories.add("featured");
kv.encode("temp_categories", tempCategories, MMKV.ExpireInHour);
// Retrieve string sets
Set<String> projectTags = kv.decodeStringSet("project_tags");
Set<String> grantedPerms = kv.decodeStringSet("granted_permissions");
// With custom default
Set<String> defaultTags = new HashSet<>();
defaultTags.add("default");
Set<String> userTags = kv.decodeStringSet("user_tags", defaultTags);
// With specific Set implementation (LinkedHashSet to preserve order)
Set<String> orderedSet = kv.decodeStringSet("ordered_items", null, LinkedHashSet.class);Store and retrieve binary data as byte arrays.
/**
* Store a byte array.
* @param key The key for the value
* @param value The byte array to store (can be null)
* @return True if successful, false otherwise
*/
public boolean encode(String key, byte[] value);
/**
* Store a byte array with expiration.
* @param key The key for the value
* @param value The byte array to store
* @param expireDurationInSecond Expiration time in seconds
* @return True if successful, false otherwise
*/
public boolean encode(String key, byte[] value, int expireDurationInSecond);
/**
* Retrieve a byte array with default null.
* @param key The key for the value
* @return The byte array or null if not found
*/
public byte[] decodeBytes(String key);
/**
* Retrieve a byte array with custom default.
* @param key The key for the value
* @param defaultValue The default value if key not found
* @return The byte array or defaultValue if not found
*/
public byte[] decodeBytes(String key, byte[] defaultValue);Usage Example:
// Store binary data
String text = "Hello, MMKV!";
byte[] textBytes = text.getBytes("UTF-8");
kv.encode("text_data", textBytes);
// Store image or file data
byte[] imageData = loadImageFromAssets();
kv.encode("cached_image", imageData);
// Store with expiration (cache for 1 hour)
byte[] tempData = generateTempData();
kv.encode("temp_cache", tempData, MMKV.ExpireInHour);
// Retrieve binary data
byte[] storedText = kv.decodeBytes("text_data");
if (storedText != null) {
String restoredText = new String(storedText, "UTF-8"); // "Hello, MMKV!"
}
byte[] cachedImage = kv.decodeBytes("cached_image");
byte[] defaultBytes = new byte[]{0, 1, 2};
byte[] someData = kv.decodeBytes("unknown_data", defaultBytes);Store and retrieve Android Parcelable objects.
/**
* Store a Parcelable object.
* @param key The key for the value
* @param value The Parcelable object to store (can be null)
* @return True if successful, false otherwise
*/
public boolean encode(String key, Parcelable value);
/**
* Store a Parcelable object with expiration.
* @param key The key for the value
* @param value The Parcelable object to store
* @param expireDurationInSecond Expiration time in seconds
* @return True if successful, false otherwise
*/
public boolean encode(String key, Parcelable value, int expireDurationInSecond);
/**
* Retrieve a Parcelable object with default null.
* @param key The key for the value
* @param tClass The class of the Parcelable object
* @return The Parcelable object or null if not found
*/
public <T extends Parcelable> T decodeParcelable(String key, Class<T> tClass);
/**
* Retrieve a Parcelable object with custom default.
* @param key The key for the value
* @param tClass The class of the Parcelable object
* @param defaultValue The default value if key not found
* @return The Parcelable object or defaultValue if not found
*/
public <T extends Parcelable> T decodeParcelable(String key, Class<T> tClass, T defaultValue);Usage Example:
import android.graphics.Rect;
import android.graphics.Point;
// Store Parcelable objects
Rect bounds = new Rect(0, 0, 100, 200);
kv.encode("window_bounds", bounds);
Point location = new Point(50, 75);
kv.encode("cursor_position", location);
// Store with expiration
Bundle tempBundle = new Bundle();
tempBundle.putString("temp_key", "temp_value");
kv.encode("temp_bundle", tempBundle, MMKV.ExpireInMinute);
// Retrieve Parcelable objects
Rect storedBounds = kv.decodeParcelable("window_bounds", Rect.class);
if (storedBounds != null) {
int width = storedBounds.width(); // 100
int height = storedBounds.height(); // 200
}
Point cursorPos = kv.decodeParcelable("cursor_position", Point.class);
// With default value
Point defaultPoint = new Point(0, 0);
Point somePoint = kv.decodeParcelable("unknown_point", Point.class, defaultPoint);MMKV implements the SharedPreferences interface for easy migration from Android SharedPreferences.
// SharedPreferences.Editor methods
public Editor putString(String key, String value);
public Editor putStringSet(String key, Set<String> values);
public Editor putInt(String key, int value);
public Editor putLong(String key, long value);
public Editor putFloat(String key, float value);
public Editor putBoolean(String key, boolean value);
public Editor putBytes(String key, byte[] bytes); // MMKV extension
public Editor remove(String key);
public Editor clear();
// With expiration support (MMKV extensions)
public Editor putString(String key, String value, int expireDurationInSecond);
public Editor putStringSet(String key, Set<String> values, int expireDurationInSecond);
public Editor putInt(String key, int value, int expireDurationInSecond);
public Editor putLong(String key, long value, int expireDurationInSecond);
public Editor putFloat(String key, float value, int expireDurationInSecond);
public Editor putBoolean(String key, boolean value, int expireDurationInSecond);
public Editor putBytes(String key, byte[] bytes, int expireDurationInSecond);
// SharedPreferences methods
public String getString(String key, String defValue);
public Set<String> getStringSet(String key, Set<String> defValues);
public int getInt(String key, int defValue);
public long getLong(String key, long defValue);
public float getFloat(String key, float defValue);
public boolean getBoolean(String key, boolean defValue);
public byte[] getBytes(String key, byte[] defValue); // MMKV extension
public boolean contains(String key);
public Editor edit();
// Deprecated SharedPreferences methods (compatibility only)
public boolean commit(); // Use sync() instead
public void apply(); // Use async() insteadUsage Example:
// Easy migration from SharedPreferences
MMKV kv = MMKV.defaultMMKV();
// SharedPreferences-style usage
kv.edit()
.putString("name", "John")
.putInt("age", 25)
.putBoolean("premium", true)
.apply(); // or commit()
// Retrieve values
String name = kv.getString("name", "");
int age = kv.getInt("age", 0);
boolean isPremium = kv.getBoolean("premium", false);
// MMKV extensions with expiration
kv.edit()
.putString("session_token", "abc123", MMKV.ExpireInHour)
.putInt("daily_count", 5, MMKV.ExpireInDay)
.apply();Advanced operations using native memory buffers for high-performance scenarios.
/**
* Create a native buffer for direct JNI memory operations.
* @param size The size of the underlying memory
* @return NativeBuffer instance or null if creation failed
*/
public static NativeBuffer createNativeBuffer(int size);
/**
* Destroy a native buffer to avoid memory leaks.
* @param buffer The NativeBuffer to destroy
*/
public static void destroyNativeBuffer(NativeBuffer buffer);
/**
* Write the value of a key to the native buffer.
* @param key The key of the value to write
* @param buffer The native buffer to write to
* @return The size written, or -1 on error
*/
public int writeValueToNativeBuffer(String key, NativeBuffer buffer);Usage Example:
// Create a native buffer for high-performance operations
NativeBuffer buffer = MMKV.createNativeBuffer(1024); // 1KB buffer
if (buffer != null) {
try {
MMKV kv = MMKV.defaultMMKV();
kv.encode("test_data", "Hello, native buffer!");
// Write value directly to native memory
int bytesWritten = kv.writeValueToNativeBuffer("test_data", buffer);
if (bytesWritten > 0) {
Log.d("MMKV", "Wrote " + bytesWritten + " bytes to native buffer");
// Use the buffer for JNI operations...
}
} finally {
// Always destroy the buffer to prevent memory leaks
MMKV.destroyNativeBuffer(buffer);
}
}Advanced features for automatic key expiration to manage data lifecycle.
/**
* Enable auto key expiration with default duration.
* @param expireDurationInSecond Default expire duration for all keys, 0 means no default
* @return True if successful
*/
public boolean enableAutoKeyExpire(int expireDurationInSecond);
/**
* Disable auto key expiration.
* @return True if successful
*/
public boolean disableAutoKeyExpire();Usage Example:
MMKV kv = MMKV.defaultMMKV();
// Enable auto-expiration with 1 hour default for all new keys
boolean enabled = kv.enableAutoKeyExpire(MMKV.ExpireInHour);
if (enabled) {
// All new keys will expire in 1 hour unless overridden
kv.encode("auto_expire_key", "This will expire in 1 hour");
// Override with custom expiration
kv.encode("custom_expire_key", "This expires in 1 day", MMKV.ExpireInDay);
Log.d("MMKV", "Auto-expiration enabled");
}
// Later, disable auto-expiration
kv.disableAutoKeyExpire();Features for optimizing MMKV performance in specific scenarios.
/**
* Enable data compare before set for better performance.
* Use when data for key seldom changes.
* Invalid when encryption or expiration is enabled.
*/
public void enableCompareBeforeSet();
/**
* Disable data compare before set.
*/
public void disableCompareBeforeSet();Usage Example:
MMKV kv = MMKV.defaultMMKV();
// Enable compare-before-set for performance optimization
// Only works with non-encrypted instances without expiration
try {
kv.enableCompareBeforeSet();
// Now MMKV will compare values before writing
// If the value is the same, it won't write to disk
kv.encode("stable_config", "value1");
kv.encode("stable_config", "value1"); // No actual write occurs
kv.encode("stable_config", "value2"); // Write occurs due to change
Log.d("MMKV", "Compare-before-set enabled for performance");
} catch (RuntimeException e) {
Log.e("MMKV", "Cannot enable compare-before-set: " + e.getMessage());
// This happens if encryption or expiration is enabled
}
// Disable when no longer needed
kv.disableCompareBeforeSet();// Expiration time constants
public static final int ExpireNever = 0;
public static final int ExpireInMinute = 60;
public static final int ExpireInHour = 3600;
public static final int ExpireInDay = 86400;
public static final int ExpireInMonth = 2592000;
public static final int ExpireInYear = 946080000;Install with Tessl CLI
npx tessl i tessl/maven-com-tencent--mmkv