CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-tencent--mmkv-static

High-performance, cross-platform key-value storage framework with static C++ linking for Android applications

Pending
Overview
Eval results
Files

shared-preferences.mddocs/

SharedPreferences Compatibility

MMKV provides full compatibility with Android's SharedPreferences interface, serving as a drop-in replacement with significantly better performance. The MMKV class implements both SharedPreferences and SharedPreferences.Editor interfaces.

Capabilities

SharedPreferences Interface

MMKV implements the standard SharedPreferences interface for seamless migration from existing code.

/**
 * Retrieve all values. Intentionally not supported by MMKV.
 * Use allKeys() instead.
 * @throws UnsupportedOperationException Always thrown
 */
Map<String, ?> getAll();

/**
 * Retrieve a string value
 * @param key The name of the preference to retrieve
 * @param defValue Value to return if this preference does not exist
 * @return The preference value if it exists, or defValue
 */
String getString(String key, String defValue);

/**
 * Retrieve a string set value
 * @param key The name of the preference to retrieve
 * @param defValues Values to return if this preference does not exist
 * @return The preference value if it exists, or defValues
 */
Set<String> getStringSet(String key, Set<String> defValues);

/**
 * Retrieve an integer value
 * @param key The name of the preference to retrieve
 * @param defValue Value to return if this preference does not exist
 * @return The preference value if it exists, or defValue
 */
int getInt(String key, int defValue);

/**
 * Retrieve a long value
 * @param key The name of the preference to retrieve
 * @param defValue Value to return if this preference does not exist
 * @return The preference value if it exists, or defValue
 */
long getLong(String key, long defValue);

/**
 * Retrieve a float value
 * @param key The name of the preference to retrieve
 * @param defValue Value to return if this preference does not exist
 * @return The preference value if it exists, or defValue
 */
float getFloat(String key, float defValue);

/**
 * Retrieve a boolean value
 * @param key The name of the preference to retrieve
 * @param defValue Value to return if this preference does not exist
 * @return The preference value if it exists, or defValue
 */
boolean getBoolean(String key, boolean defValue);

/**
 * Checks whether the preferences contains a preference
 * @param key The name of the preference to check
 * @return True if the preference exists in the preferences, otherwise false
 */
boolean contains(String key);

/**
 * Create a new Editor for these preferences, through which you can make
 * modifications to the data in the preferences and atomically commit those changes
 * @return Returns an instance of the Editor interface
 */
SharedPreferences.Editor edit();

Usage Examples:

// Replace SharedPreferences with MMKV
// OLD: SharedPreferences prefs = getSharedPreferences("app_prefs", MODE_PRIVATE);
MMKV prefs = MMKV.mmkvWithID("app_prefs");

// Use exactly like SharedPreferences
String username = prefs.getString("username", "guest");
int userId = prefs.getInt("user_id", -1);
boolean isLoggedIn = prefs.getBoolean("logged_in", false);

// Check for key existence
if (prefs.contains("user_settings")) {
    // Key exists
}

SharedPreferences.Editor Interface

MMKV implements the SharedPreferences.Editor interface for familiar data modification patterns.

/**
 * Set a string value in the preferences editor
 * @param key The name of the preference to modify
 * @param value The new value for the preference (can be null)
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putString(String key, String value);

/**
 * Set a string value with expiration
 * @param key The name of the preference to modify
 * @param value The new value for the preference (can be null)
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putString(String key, String value, int expireDurationInSecond);

/**
 * Set a string set value in the preferences editor
 * @param key The name of the preference to modify
 * @param values The new values for the preference (can be null)
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putStringSet(String key, Set<String> values);

/**
 * Set a string set value with expiration
 * @param key The name of the preference to modify
 * @param values The new values for the preference (can be null)
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putStringSet(String key, Set<String> values, int expireDurationInSecond);

/**
 * Set an integer value in the preferences editor
 * @param key The name of the preference to modify
 * @param value The new value for the preference
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putInt(String key, int value);

/**
 * Set an integer value with expiration
 * @param key The name of the preference to modify
 * @param value The new value for the preference
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putInt(String key, int value, int expireDurationInSecond);

/**
 * Set a long value in the preferences editor
 * @param key The name of the preference to modify
 * @param value The new value for the preference
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putLong(String key, long value);

/**
 * Set a long value with expiration
 * @param key The name of the preference to modify
 * @param value The new value for the preference
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putLong(String key, long value, int expireDurationInSecond);

/**
 * Set a float value in the preferences editor
 * @param key The name of the preference to modify
 * @param value The new value for the preference
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putFloat(String key, float value);

/**
 * Set a float value with expiration
 * @param key The name of the preference to modify
 * @param value The new value for the preference
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putFloat(String key, float value, int expireDurationInSecond);

/**
 * Set a boolean value in the preferences editor
 * @param key The name of the preference to modify
 * @param value The new value for the preference
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putBoolean(String key, boolean value);

/**
 * Set a boolean value with expiration
 * @param key The name of the preference to modify
 * @param value The new value for the preference
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putBoolean(String key, boolean value, int expireDurationInSecond);

/**
 * Mark in the editor that a preference value should be removed
 * @param key The name of the preference to remove
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor remove(String key);

/**
 * Mark in the editor to remove all values from the preferences
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor clear();

/**
 * Commit your preferences changes synchronously.
 * DEPRECATED: This method is only for compatibility. MMKV doesn't rely on commit().
 * @deprecated Use sync() or async() instead if you worry about data persistence
 * @return Always returns true
 */
@Deprecated
boolean commit();

/**
 * Commit your preferences changes asynchronously.
 * DEPRECATED: This method is only for compatibility. MMKV doesn't rely on apply().
 * @deprecated Use async() instead if you worry about data persistence
 */
@Deprecated
void apply();

Usage Examples:

// Use editor pattern like SharedPreferences
MMKV prefs = MMKV.mmkvWithID("app_prefs");

// Chain editor operations
prefs.edit()
    .putString("username", "john_doe")
    .putInt("user_id", 12345)
    .putBoolean("logged_in", true)
    .commit(); // Optional - MMKV persists immediately

// Or use without explicit commit (recommended)
prefs.putString("username", "john_doe");
prefs.putInt("user_id", 12345);
prefs.putBoolean("logged_in", true);

// MMKV extensions with expiration
prefs.edit()
    .putString("session_token", "abc123", MMKV.ExpireInHour)
    .putInt("daily_score", 1500, MMKV.ExpireInDay);

MMKV Extensions to SharedPreferences

MMKV provides additional methods beyond the standard SharedPreferences interface.

/**
 * Set a byte array value (MMKV extension)
 * @param key The name of the preference to modify
 * @param bytes The new byte array value (can be null)
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putBytes(String key, byte[] bytes);

/**
 * Set a byte array value with expiration (MMKV extension)
 * @param key The name of the preference to modify
 * @param bytes The new byte array value (can be null)
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return Returns a reference to the same Editor object
 */
SharedPreferences.Editor putBytes(String key, byte[] bytes, int expireDurationInSecond);

/**
 * Get a byte array value (MMKV extension)
 * @param key The name of the preference to retrieve
 * @param defValue Value to return if this preference does not exist
 * @return The preference value if it exists, or defValue
 */
byte[] getBytes(String key, byte[] defValue);

Usage Examples:

// Use MMKV extensions
byte[] imageData = loadImageAsBytes();
prefs.putBytes("profile_image", imageData);

// Retrieve binary data
byte[] storedImage = prefs.getBytes("profile_image", null);

Migration from SharedPreferences

MMKV provides a convenient method for migrating existing SharedPreferences data.

/**
 * Atomically migrate all key-values from an existent SharedPreferences to the MMKV instance.
 * @param preferences The SharedPreferences to import from
 * @return The total count of key-values imported
 */
int importFromSharedPreferences(SharedPreferences preferences);

Usage Examples:

// Migrate from existing SharedPreferences
SharedPreferences oldPrefs = getSharedPreferences("old_prefs", MODE_PRIVATE);
MMKV newPrefs = MMKV.mmkvWithID("new_prefs");

// Import all data
int importedCount = newPrefs.importFromSharedPreferences(oldPrefs);
Log.d("Migration", "Imported " + importedCount + " preferences");

// Clear old preferences after successful migration
if (importedCount > 0) {
    oldPrefs.edit().clear().commit();
}

Unsupported SharedPreferences Methods

MMKV intentionally does not support certain SharedPreferences methods that conflict with its design philosophy.

/**
 * Intentionally not supported by MMKV. We believe it's better not for a storage framework 
 * to notify the change of data. Check registerContentChangeNotify for a potential replacement 
 * on inter-process scene.
 * @throws UnsupportedOperationException Always thrown
 */
void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener);

/**
 * Intentionally not supported by MMKV. We believe it's better not for a storage framework 
 * to notify the change of data.
 * @throws UnsupportedOperationException Always thrown
 */
void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener);

Migration Notes:

// OLD SharedPreferences code with listeners:
sharedPrefs.registerOnSharedPreferenceChangeListener(listener);

// MMKV alternative for inter-process notifications:
MMKV.registerContentChangeNotify(new MMKVContentChangeNotification() {
    @Override
    public void onContentChangedByOuterProcess(String mmapID) {
        // Handle inter-process content changes
    }
});

Performance Comparison

OperationSharedPreferencesMMKVImprovement
Read~100µs~1µs~100x faster
Write~10ms~1µs~10,000x faster
Startup~50ms~1ms~50x faster

Migration Checklist:

  1. Replace getSharedPreferences() calls with MMKV.mmkvWithID()
  2. Remove .commit() and .apply() calls (optional in MMKV)
  3. Replace preference change listeners with MMKV content change notifications
  4. Use importFromSharedPreferences() for data migration
  5. Test with multi-process scenarios if applicable

Install with Tessl CLI

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

docs

advanced-features.md

data-operations.md

index.md

initialization.md

instance-management.md

namespace.md

process-management.md

shared-preferences.md

utility-types.md

tile.json