Skip to content

Commit

Permalink
support custom library loader
Browse files Browse the repository at this point in the history
  • Loading branch information
lingol committed Jan 22, 2019
1 parent 969b784 commit c1fc247
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
27 changes: 19 additions & 8 deletions Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import android.support.annotation.Nullable;
import android.util.Log;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.EnumMap;
Expand Down Expand Up @@ -59,26 +58,38 @@ public class MMKV implements SharedPreferences, SharedPreferences.Editor {
index2LogLevel = new MMKVLogLevel[] {MMKVLogLevel.LevelDebug, MMKVLogLevel.LevelInfo,
MMKVLogLevel.LevelWarning, MMKVLogLevel.LevelError,
MMKVLogLevel.LevelNone};

if (BuildConfig.FLAVOR.equals("SharedCpp")) {
System.loadLibrary("c++_shared");
}
System.loadLibrary("mmkv");
}

public interface LibLoader { public void loadLibrary(String libName); }

// call on program start
public static String initialize(Context context) {
String root = context.getFilesDir().getAbsolutePath() + "/mmkv";
return initialize(root);
return initialize(root, null);
}

static private String rootDir = null;
public static String initialize(String rootDir) {
return initialize(rootDir, null);
}

public static String initialize(String rootDir, LibLoader loader) {
if (loader != null) {
if (BuildConfig.FLAVOR.equals("SharedCpp")) {
loader.loadLibrary("c++_shared");
}
loader.loadLibrary("mmkv");
} else {
if (BuildConfig.FLAVOR.equals("SharedCpp")) {
System.loadLibrary("c++_shared");
}
System.loadLibrary("mmkv");
}
MMKV.rootDir = rootDir;
jniInitialize(MMKV.rootDir);
return rootDir;
}

static private String rootDir = null;
public static String getRootDir() {
return rootDir;
}
Expand Down
1 change: 1 addition & 0 deletions Android/MMKV/mmkvdemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.getkeepsafe.relinker:relinker:1.3.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.tencent.mmkv.MMKVHandler;
import com.tencent.mmkv.MMKVRecoverStrategic;
import com.tencent.mmkv.MMKVLogLevel;
import com.getkeepsafe.relinker.ReLinker;

import org.jetbrains.annotations.Nullable;

Expand All @@ -54,7 +55,12 @@ protected void onCreate(Bundle savedInstanceState) {
// set root dir
// String rootDir = "mmkv root: " + MMKV.initialize(this);
String dir = getFilesDir().getAbsolutePath() + "/mmkv_2";
String rootDir = "mmkv root: " + MMKV.initialize(dir);
String rootDir = "mmkv root: " + MMKV.initialize(dir, new MMKV.LibLoader() {
@Override
public void loadLibrary(String libName) {
ReLinker.loadLibrary(MainActivity.this, libName);
}
});
Log.i("MMKV", rootDir);

// set log level
Expand Down

0 comments on commit c1fc247

Please sign in to comment.