Skip to content

android_tutorial

guoling edited this page Apr 19, 2024 · 5 revisions

MMKV for Android

MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application. It's currently available on both Android, iOS/macOS, Windows and POSIX.

Tutorial

You can use MMKV as you go. All changes are saved immediately, no sync, no apply calls needed.

Configuration

  • Setup MMKV on App startup, say your Application class, add these code:

    public void onCreate() {
        super.onCreate();
    
        String rootDir = MMKV.initialize(this);
        System.out.println("mmkv root: " + rootDir);
    }

CRUD Operations

  • MMKV has a global instance, you can use it directly:

    import com.tencent.mmkv.MMKV;
    ...
    MMKV kv = MMKV.defaultMMKV();
    
    kv.encode("bool", true);
    System.out.println("bool: " + kv.decodeBool("bool"));
    
    kv.encode("int", Integer.MIN_VALUE);
    System.out.println("int: " + kv.decodeInt("int"));
    
    kv.encode("long", Long.MAX_VALUE);
    System.out.println("long: " + kv.decodeLong("long"));
    
    kv.encode("float", -3.14f);
    System.out.println("float: " + kv.decodeFloat("float"));
    
    kv.encode("double", Double.MIN_VALUE);
    System.out.println("double: " + kv.decodeDouble("double"));
    
    kv.encode("string", "Hello from mmkv");
    System.out.println("string: " + kv.decodeString("string"));
    
    byte[] bytes = {'m', 'm', 'k', 'v'};
    kv.encode("bytes", bytes);
    System.out.println("bytes: " + new String(kv.decodeBytes("bytes")));

    As you can see, MMKV is quite easy to use.

  • Deleting & Querying:

    MMKV kv = MMKV.defaultMMKV();
    
    kv.removeValueForKey("bool");
    System.out.println("bool: " + kv.decodeBool("bool"));
        
    kv.removeValuesForKeys(new String[]{"int", "long"});
    System.out.println("allKeys: " + Arrays.toString(kv.allKeys()));
    
    boolean hasBool = kv.containsKey("bool");
  • If different modules/logics need isolated storage, you can also create your own MMKV instance separately:

    MMKV kv = MMKV.mmkvWithID("MyID");
    kv.encode("bool", true);
  • If multi-process accessing is needed, you can set MMKV.MULTI_PROCESS_MODE on MMKV initialization:

    MMKV kv = MMKV.mmkvWithID("InterProcessKV", MMKV.MULTI_PROCESS_MODE);
    kv.encode("bool", true);

Supported Types

  • Primitive Types:

    • boolean, int, long, float, double, byte[]
  • Classes & Collections:

    • String, Set<String>
    • Any class that implements Parcelable

Migrating from SharedPreferences

  • MMKV provides importFromSharedPreferences(), you can migrate from SharedPreferences with one line of code;

  • MMKV implements SharedPreferences & SharedPreferences.Editor. You can just change initialization & declaration to MMKV, and leave those CRUD codes untouched.

    private void testImportSharedPreferences() {
        //SharedPreferences preferences = getSharedPreferences("myData", MODE_PRIVATE);
        MMKV preferences = MMKV.mmkvWithID("myData");
        // migration
        {
            SharedPreferences old_man = getSharedPreferences("myData", MODE_PRIVATE);
            preferences.importFromSharedPreferences(old_man);
            old_man.edit().clear().commit();
        }
        // just use it as before
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("bool", true);
        editor.putInt("int", Integer.MIN_VALUE);
        editor.putLong("long", Long.MAX_VALUE);
        editor.putFloat("float", -3.14f);
        editor.putString("string", "hello, imported");
        HashSet<String> set = new HashSet<String>();
        set.add("W"); set.add("e"); set.add("C"); set.add("h"); set.add("a"); set.add("t");
        editor.putStringSet("string-set", set);
        // commit() is not needed any more
        //editor.commit();
    }

What's Next

Clone this wiki locally