diff --git a/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp b/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp index aeeff082..95181da6 100644 --- a/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp +++ b/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp @@ -114,12 +114,14 @@ extern "C" JNIEXPORT JNICALL jint JNI_OnLoad(JavaVM *vm, void *reserved) { namespace mmkv { -MMKV_JNI void jniInitialize(JNIEnv *env, jobject obj, jstring rootDir) { +MMKV_JNI void jniInitialize(JNIEnv *env, jobject obj, jstring rootDir, jint logLevel) { if (!rootDir) { return; } const char *kstr = env->GetStringUTFChars(rootDir, nullptr); if (kstr) { + g_currentLogLevel = (MMKVLogLevel) logLevel; + MMKV::initializeMMKV(kstr); env->ReleaseStringUTFChars(rootDir, kstr); } @@ -778,7 +780,7 @@ static JNINativeMethod g_methods[] = { {"isFileValid", "(Ljava/lang/String;)Z", (void *) mmkv::isFileValid}, {"ashmemFD", "()I", (void *) mmkv::ashmemFD}, {"ashmemMetaFD", "()I", (void *) mmkv::ashmemMetaFD}, - {"jniInitialize", "(Ljava/lang/String;)V", (void *) mmkv::jniInitialize}, + {"jniInitialize", "(Ljava/lang/String;I)V", (void *) mmkv::jniInitialize}, {"getMMKVWithID", "(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)J", (void *) mmkv::getMMKVWithID}, {"getMMKVWithIDAndSize", "(Ljava/lang/String;IILjava/lang/String;)J", diff --git a/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java b/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java index 663c6583..23589e8f 100644 --- a/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java +++ b/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java @@ -64,14 +64,30 @@ public interface LibLoader { void loadLibrary(String libName); } // call on program start public static String initialize(Context context) { String root = context.getFilesDir().getAbsolutePath() + "/mmkv"; - return initialize(root, null); + MMKVLogLevel logLevel = + BuildConfig.DEBUG ? MMKVLogLevel.LevelDebug : MMKVLogLevel.LevelInfo; + return initialize(root, null, logLevel); + } + public static String initialize(Context context, MMKVLogLevel logLevel) { + String root = context.getFilesDir().getAbsolutePath() + "/mmkv"; + return initialize(root, null, logLevel); } public static String initialize(String rootDir) { - return initialize(rootDir, null); + MMKVLogLevel logLevel = + BuildConfig.DEBUG ? MMKVLogLevel.LevelDebug : MMKVLogLevel.LevelInfo; + return initialize(rootDir, null, logLevel); + } + public static String initialize(String rootDir, MMKVLogLevel logLevel) { + return initialize(rootDir, null, logLevel); } public static String initialize(String rootDir, LibLoader loader) { + MMKVLogLevel logLevel = + BuildConfig.DEBUG ? MMKVLogLevel.LevelDebug : MMKVLogLevel.LevelInfo; + return initialize(rootDir, loader, logLevel); + } + public static String initialize(String rootDir, LibLoader loader, MMKVLogLevel logLevel) { if (loader != null) { if (BuildConfig.FLAVOR.equals("SharedCpp")) { loader.loadLibrary("c++_shared"); @@ -84,7 +100,7 @@ public static String initialize(String rootDir, LibLoader loader) { System.loadLibrary("mmkv"); } MMKV.rootDir = rootDir; - jniInitialize(MMKV.rootDir); + jniInitialize(MMKV.rootDir, LogLevel2Int(logLevel)); return rootDir; } @@ -93,7 +109,7 @@ public static String getRootDir() { return rootDir; } - public static void setLogLevel(MMKVLogLevel level) { + private static int LogLevel2Int(MMKVLogLevel level) { int realLevel; switch (level) { case LevelDebug: @@ -115,6 +131,11 @@ public static void setLogLevel(MMKVLogLevel level) { realLevel = 1; break; } + return realLevel; + } + + public static void setLogLevel(MMKVLogLevel level) { + int realLevel = LogLevel2Int(level); setLogLevel(realLevel); } @@ -812,7 +833,7 @@ private MMKV(long handle) { nativeHandle = handle; } - private static native void jniInitialize(String rootDir); + private static native void jniInitialize(String rootDir, int level); private native static long getMMKVWithID(String mmapID, int mode, String cryptKey, String relativePath); diff --git a/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java b/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java index fbcf8d3f..0b72520d 100644 --- a/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java +++ b/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java @@ -61,7 +61,7 @@ protected void onCreate(Bundle savedInstanceState) { public void loadLibrary(String libName) { ReLinker.loadLibrary(MainActivity.this, libName); } - }); + }, MMKVLogLevel.LevelInfo); Log.i("MMKV", "mmkv root: " + rootDir); // set log level