MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application.
—
Core storage operations for all supported data types including primitives, strings, byte arrays, and Parcelable objects. MMKV provides both MMKV-native methods and SharedPreferences-compatible methods for seamless migration.
Store and retrieve boolean values with optional expiration support.
/**
* Store a boolean value.
* @param key The key to store the value under
* @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 custom expiration.
* @param key The key to store the value under
* @param value The boolean value to store
* @param expireDurationInSecond Override the default duration, ExpireNever (0) means never expire
* @return true if successful, false otherwise
*/
public boolean encode(String key, boolean value, int expireDurationInSecond);
/**
* Retrieve a boolean value.
* @param key The key to retrieve the value for
* @return The boolean value, or false if not found
*/
public boolean decodeBool(String key);
/**
* Retrieve a boolean value with default.
* @param key The key to retrieve the value for
* @param defaultValue The default value to return if key is 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_first_launch", true);
kv.encode("notifications_enabled", false);
// Store with expiration (1 hour)
kv.encode("temporary_flag", true, MMKV.ExpireInHour);
// Retrieve boolean values
boolean isFirst = kv.decodeBool("is_first_launch"); // false if not found
boolean notificationsEnabled = kv.decodeBool("notifications_enabled", true); // true if not foundStore and retrieve integer values with optional expiration support.
/**
* Store an integer value.
* @param key The key to store the value under
* @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 custom expiration.
* @param key The key to store the value under
* @param value The integer value to store
* @param expireDurationInSecond Override the default duration, ExpireNever (0) means never expire
* @return true if successful, false otherwise
*/
public boolean encode(String key, int value, int expireDurationInSecond);
/**
* Retrieve an integer value.
* @param key The key to retrieve the value for
* @return The integer value, or 0 if not found
*/
public int decodeInt(String key);
/**
* Retrieve an integer value with default.
* @param key The key to retrieve the value for
* @param defaultValue The default value to return if key is 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("retry_count", 3);
// Store with expiration (24 hours)
kv.encode("daily_login_bonus", 100, MMKV.ExpireInDay);
// Retrieve integer values
int userId = kv.decodeInt("user_id", -1);
int retryCount = kv.decodeInt("retry_count", 0);Store and retrieve long values for large numbers and timestamps.
/**
* Store a long value.
* @param key The key to store the value under
* @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 custom expiration.
* @param key The key to store the value under
* @param value The long value to store
* @param expireDurationInSecond Override the default duration, ExpireNever (0) means never expire
* @return true if successful, false otherwise
*/
public boolean encode(String key, long value, int expireDurationInSecond);
/**
* Retrieve a long value.
* @param key The key to retrieve the value for
* @return The long value, or 0 if not found
*/
public long decodeLong(String key);
/**
* Retrieve a long value with default.
* @param key The key to retrieve the value for
* @param defaultValue The default value to return if key is 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
kv.encode("last_sync_time", System.currentTimeMillis());
kv.encode("total_score", 9876543210L);
// Store with expiration
kv.encode("session_start", System.currentTimeMillis(), MMKV.ExpireInHour);
// Retrieve long values
long lastSync = kv.decodeLong("last_sync_time", 0);
long totalScore = kv.decodeLong("total_score", 0L);Store and retrieve float values for decimal numbers.
/**
* Store a float value.
* @param key The key to store the value under
* @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 custom expiration.
* @param key The key to store the value under
* @param value The float value to store
* @param expireDurationInSecond Override the default duration, ExpireNever (0) means never expire
* @return true if successful, false otherwise
*/
public boolean encode(String key, float value, int expireDurationInSecond);
/**
* Retrieve a float value.
* @param key The key to retrieve the value for
* @return The float value, or 0.0f if not found
*/
public float decodeFloat(String key);
/**
* Retrieve a float value with default.
* @param key The key to retrieve the value for
* @param defaultValue The default value to return if key is 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("user_rating", 4.5f);
kv.encode("progress_percentage", 0.75f);
// Retrieve float values
float rating = kv.decodeFloat("user_rating", 0.0f);
float progress = kv.decodeFloat("progress_percentage", 0.0f);Store and retrieve double values for high-precision decimal numbers.
/**
* Store a double value.
* @param key The key to store the value under
* @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 custom expiration.
* @param key The key to store the value under
* @param value The double value to store
* @param expireDurationInSecond Override the default duration, ExpireNever (0) means never expire
* @return true if successful, false otherwise
*/
public boolean encode(String key, double value, int expireDurationInSecond);
/**
* Retrieve a double value.
* @param key The key to retrieve the value for
* @return The double value, or 0.0 if not found
*/
public double decodeDouble(String key);
/**
* Retrieve a double value with default.
* @param key The key to retrieve the value for
* @param defaultValue The default value to return if key is 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("latitude", 37.7749295);
kv.encode("longitude", -122.4194155);
kv.encode("account_balance", 1234.56789);
// Retrieve double values
double lat = kv.decodeDouble("latitude", 0.0);
double lon = kv.decodeDouble("longitude", 0.0);Store and retrieve string values with full Unicode support.
/**
* Store a string value.
* @param key The key to store the value under
* @param value The string value to store (nullable)
* @return true if successful, false otherwise
*/
public boolean encode(String key, String value);
/**
* Store a string value with custom expiration.
* @param key The key to store the value under
* @param value The string value to store (nullable)
* @param expireDurationInSecond Override the default duration, ExpireNever (0) means never expire
* @return true if successful, false otherwise
*/
public boolean encode(String key, String value, int expireDurationInSecond);
/**
* Retrieve a string value.
* @param key The key to retrieve the value for
* @return The string value, or null if not found
*/
public String decodeString(String key);
/**
* Retrieve a string value with default.
* @param key The key to retrieve the value for
* @param defaultValue The default value to return if key is not found (nullable)
* @return The string value, or defaultValue if not found
*/
public String decodeString(String key, String defaultValue);Usage Example:
// Store string values
kv.encode("username", "alice_smith");
kv.encode("user_email", "alice@example.com");
kv.encode("welcome_message", "Welcome to our app! 🎉");
// Store with expiration
kv.encode("session_token", "abc123xyz", MMKV.ExpireInHour);
// Store null value (removes key)
kv.encode("temp_data", null);
// Retrieve string values
String username = kv.decodeString("username", "guest");
String email = kv.decodeString("user_email"); // null if not foundStore and retrieve sets of strings for collections of unique string values.
/**
* Store a string set value.
* @param key The key to store the value under
* @param value The string set to store (nullable)
* @return true if successful, false otherwise
*/
public boolean encode(String key, Set<String> value);
/**
* Store a string set value with custom expiration.
* @param key The key to store the value under
* @param value The string set to store (nullable)
* @param expireDurationInSecond Override the default duration, ExpireNever (0) means never expire
* @return true if successful, false otherwise
*/
public boolean encode(String key, Set<String> value, int expireDurationInSecond);
/**
* Retrieve a string set value.
* @param key The key to retrieve the value for
* @return The string set, or null if not found
*/
public Set<String> decodeStringSet(String key);
/**
* Retrieve a string set value with default.
* @param key The key to retrieve the value for
* @param defaultValue The default value to return if key is not found (nullable)
* @return The string set, or defaultValue if not found
*/
public Set<String> decodeStringSet(String key, Set<String> defaultValue);
/**
* Retrieve a string set value with default and custom Set implementation.
* @param key The key to retrieve the value for
* @param defaultValue The default value to return if key is not found (nullable)
* @param cls The Set implementation class to use (e.g., HashSet.class, LinkedHashSet.class)
* @return The string set, or defaultValue if not found
*/
public Set<String> decodeStringSet(String key, Set<String> defaultValue, Class<? extends Set> cls);Usage Example:
// Store string sets
Set<String> permissions = new HashSet<>();
permissions.add("camera");
permissions.add("location");
permissions.add("storage");
kv.encode("granted_permissions", permissions);
Set<String> favoriteColors = new LinkedHashSet<>();
favoriteColors.add("blue");
favoriteColors.add("green");
favoriteColors.add("red");
kv.encode("favorite_colors", favoriteColors, MMKV.ExpireInDay);
// Retrieve string sets
Set<String> grantedPerms = kv.decodeStringSet("granted_permissions", new HashSet<>());
Set<String> colors = kv.decodeStringSet("favorite_colors", null, LinkedHashSet.class);Store and retrieve raw byte data for binary content, images, or serialized objects.
/**
* Store a byte array value.
* @param key The key to store the value under
* @param value The byte array to store (nullable)
* @return true if successful, false otherwise
*/
public boolean encode(String key, byte[] value);
/**
* Store a byte array value with custom expiration.
* @param key The key to store the value under
* @param value The byte array to store (nullable)
* @param expireDurationInSecond Override the default duration, ExpireNever (0) means never expire
* @return true if successful, false otherwise
*/
public boolean encode(String key, byte[] value, int expireDurationInSecond);
/**
* Retrieve a byte array value.
* @param key The key to retrieve the value for
* @return The byte array, or null if not found
*/
public byte[] decodeBytes(String key);
/**
* Retrieve a byte array value with default.
* @param key The key to retrieve the value for
* @param defaultValue The default value to return if key is not found (nullable)
* @return The byte array, or defaultValue if not found
*/
public byte[] decodeBytes(String key, byte[] defaultValue);Usage Example:
// Store binary data
String jsonData = "{\"user\":\"alice\",\"id\":123}";
kv.encode("cached_response", jsonData.getBytes(StandardCharsets.UTF_8));
// Store image data
Bitmap bitmap = getBitmapFromResource();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
kv.encode("cached_avatar", stream.toByteArray(), MMKV.ExpireInDay);
// Retrieve binary data
byte[] cachedResponse = kv.decodeBytes("cached_response");
if (cachedResponse != null) {
String json = new String(cachedResponse, StandardCharsets.UTF_8);
}
byte[] avatarData = kv.decodeBytes("cached_avatar", new byte[0]);Store and retrieve Android Parcelable objects for complex data structures.
/**
* Store a Parcelable object.
* @param key The key to store the value under
* @param value The Parcelable object to store (nullable)
* @return true if successful, false otherwise
*/
public boolean encode(String key, Parcelable value);
/**
* Store a Parcelable object with custom expiration.
* @param key The key to store the value under
* @param value The Parcelable object to store (nullable)
* @param expireDurationInSecond Override the default duration, ExpireNever (0) means never expire
* @return true if successful, false otherwise
*/
public boolean encode(String key, Parcelable value, int expireDurationInSecond);
/**
* Retrieve a Parcelable object.
* @param key The key to retrieve the value for
* @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 default.
* @param key The key to retrieve the value for
* @param tClass The class of the Parcelable object
* @param defaultValue The default value to return if key is not found (nullable)
* @return The Parcelable object, or defaultValue if not found
*/
public <T extends Parcelable> T decodeParcelable(String key, Class<T> tClass, T defaultValue);Usage Example:
// Store custom Parcelable objects
User user = new User("Alice", "alice@example.com", 25);
kv.encode("current_user", user);
Location location = new Location("GPS");
location.setLatitude(37.7749);
location.setLongitude(-122.4194);
kv.encode("last_location", location, MMKV.ExpireInHour);
// Retrieve Parcelable objects
User currentUser = kv.decodeParcelable("current_user", User.class);
Location lastLocation = kv.decodeParcelable("last_location", Location.class, null);MMKV provides drop-in replacement methods for SharedPreferences interface.
// SharedPreferences interface 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 boolean contains(String key);
// SharedPreferences.Editor interface methods
public Editor putString(String key, String value);
public Editor putString(String key, String value, int expireDurationInSecond);
public Editor putStringSet(String key, Set<String> values);
public Editor putStringSet(String key, Set<String> values, int expireDurationInSecond);
public Editor putInt(String key, int value);
public Editor putInt(String key, int value, int expireDurationInSecond);
public Editor putLong(String key, long value);
public Editor putLong(String key, long value, int expireDurationInSecond);
public Editor putFloat(String key, float value);
public Editor putFloat(String key, float value, int expireDurationInSecond);
public Editor putBoolean(String key, boolean value);
public Editor putBoolean(String key, boolean value, int expireDurationInSecond);
public Editor putBytes(String key, byte[] bytes);
public Editor putBytes(String key, byte[] bytes, int expireDurationInSecond);
public byte[] getBytes(String key, byte[] defValue);
public Editor remove(String key);
public Editor clear();
public Editor edit();Usage Example:
// Use MMKV as SharedPreferences drop-in replacement
SharedPreferences prefs = MMKV.defaultMMKV();
// Read values using SharedPreferences interface
String username = prefs.getString("username", "guest");
int userId = prefs.getInt("user_id", -1);
boolean isLoggedIn = prefs.getBoolean("is_logged_in", false);
// Write values using Editor interface
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", "alice")
.putInt("user_id", 12345)
.putBoolean("is_logged_in", true)
.apply(); // or commit()
// MMKV-specific extensions with expiration
MMKV mmkv = (MMKV) prefs;
mmkv.putString("session_token", "abc123", MMKV.ExpireInHour);
mmkv.putBytes("cached_data", responseData, MMKV.ExpireInDay);// Expiration constants (in seconds)
public static final int ExpireNever = 0; // Never expire
public static final int ExpireInMinute = 60; // 1 minute
public static final int ExpireInHour = 60 * 60; // 1 hour
public static final int ExpireInDay = 24 * 60 * 60; // 1 day
public static final int ExpireInMonth = 30 * 24 * 60 * 60; // 30 days
public static final int ExpireInYear = 365 * 30 * 24 * 60 * 60; // 365 daysInstall with Tessl CLI
npx tessl i tessl/maven-com-tencent--mmkv-shared