So without further ado, go straight to the code

Integration steps:
  • Add it to the build.gradle file in the project root directory
buildscript {
    repositories {
        mavenCentral()// This line is dependent
    }
}
allprojects {
    repositories {
        mavenCentral()// This line is dependent}}Copy the code
  • Add it to the build.gradle file under the app module of the project
dependencies {
    implementation 'com. Tencent: MMKV -static: 1.2.10'
}
Copy the code

Starting with V1.2.8, MMKV migrated to Maven Central. The old version (<= V1.2.7) is still in JCenter. For more installation instructions, see Android Setup.

Init in Application

Direct initialization:

   MMKV.initialize(this)//(select one of the following, and generally use this one)
Copy the code

By default, MMKV stores files in $(FilesDir)/ MMKV /. You can customize the root directory when MMKV is initialized:

String dir = getFilesDir().getAbsolutePath() + "/mmkv";
String rootDir = MMKV.initialize(dir);
Copy the code

Use in Kotlin:

MMKV object acquisition:

MMKV provides a global instance that can be used directly

import com.tencent.mmkv.MMKV;
/ /...

//1. Get the default global instance (one of the following)
var mmkv: MMKV = MMKV.defaultMMKV()

//2. You can also customize the MMKV object and set your own ID (access instance based on service distinction)
var mmkv: MMKV = MMKV.mmkvWithID("ID")

//3. By default, MMKV supports single process. If services require multi-process access, you need to add the multi-process mode parameter during initialization
var mmkv = MMKV.mmkvWithID("ID", MMKV.MULTI_PROCESS_MODE)  // Multi-process synchronization support

Copy the code
Access method
// Add/update datammkv? .encode(key, value);// Get the data
int value = mmkv.decodeInt(key);
String value = mmkv.decodeString(key);
/ /... Obtain and so on

// Delete the data
mmkv.removeValueForKey(key);

Copy the code

If you need to access an object, you can use the access object JSON string method, the object into JSON storage, take out the JSON back to the object.

SP migration

MMKV can call importFromSharedPreferences method of SP data migration, the example code is as follows: MMKV SharedPreferences is achieved, the Editor two interfaces, so after migration can need not change the operation of the SP code.

val mmkv = MMKV.mmkvWithID("myData")
valolderData = DemoApplication.mContext? .getSharedPreferences("myData", MODE_PRIVATE) mmkv? .importFromSharedPreferences(olderData) olderData? .edit()? .clear()? .apply()Copy the code

Use in JAVA:

MMKV object acquisition:

MMKV provides a global instance that can be used directly

import com.tencent.mmkv.MMKV;
/ /...

//1. Get the default global instance.
MMKV kv = MMKV.defaultMMKV();

//2. You can also customize the MMKV object and set your own ID (access instance based on service distinction)
MMKV kv = MMKV.mmkvWithID("ID");

//3. By default, MMKV supports single process. If services require multi-process access, you need to add the multi-process mode parameter during initialization
MMKV kv = MMKV.mmkvWithID("ID", MMKV.MULTI_PROCESS_MODE); // Multi-process synchronization support

Copy the code
Access method
Add/update data **/
// Save the Boolean type
kv.encode("bool".true);
// Store int
kv.encode("int", Integer.MIN_VALUE);
// Store the string type
kv.encode("string"."MyiSMMKV");


/** Get data **/
// Get Boolean data
boolean bValue = kv.decodeBool("bool");
// Obtain data of type int
int iValue = kv.decodeInt("int");
// Get string data
String str = kv.decodeString("string");
/ /... Etc

// Delete the data
mmkv.removeValueForKey(key);

Copy the code

If you need to access an object, you can use the access object JSON string method, the object into JSON storage, take out the JSON back to the object.

SP migration

MMKV can call importFromSharedPreferences method of SP data migration, the example code is as follows: MMKV SharedPreferences is achieved, the Editor two interfaces, so after migration can need not change the operation of the SP code.

MMKV kv = MMKV.mmkvWithID("myData");
SharedPreferences olderData = App.getInstance().getSharedPreferences("myData", MODE_PRIVATE);
kv.importFromSharedPreferences(olderData);
olderData.edit().clear().apply();

Copy the code

For the advantages and principles of MMKV and SP, please refer to the linkwww.jianshu.com/p/f2d3e7339…

##### summary relative to SP, MMKV both in speed and in the size of the file has more advantages, is a very convenient and easy to use framework.