# Target is more debuggable and adbd is on by default
ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1
# Enable Dalvik lock contention logging.
ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.lockprof.threshold=500
# Include the debugging/testing OTA keys in this build.
INCLUDE_TEST_OTA_KEYS := true
else # !enable_target_debugging
# Target is less debuggable and adbd is off by default
ADDITIONAL_DEFAULT_PROPERTIES += ro.debuggable=1//0-->1
endif # !enable_target_debugging
Add Log printing method in jni:
Reference: https://blog.csdn.net/yf210yf/article/details/9305623
1. Import the log header file
in the .c/.cpp file you use
Import the log.h header file
#include<android/log.h>
2. In Android.mk
plus
LOCAL_LDLIBS: = - interest
Note that there is a line include $(CLEAR_VARS) in Android.mk
Must put LOCAL_LDLIBS :=-llog after it to be useful,
Otherwise, it is equivalent to not writing.
3. Define the LOG function
First define a global variable, and then define some output LOG functions:
- #define TAG "myDemo-jni" // This is the logo of the custom LOG
- #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__) // define LOGD type
- #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG ,__VA_ARGS__) // define LOGI type
- #define LOGW(...) __android_log_print(ANDROID_LOG_WARN,TAG ,__VA_ARGS__) // define LOGW type
- #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG ,__VA_ARGS__) // define LOGE type
- #define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,TAG ,__VA_ARGS__) // define LOGF type
The function defined in the above code
Corresponding to Android's Java code respectively
Log.d(), Log.i(), Log.w(),Log.e(), Log.f()等方法.
4. Examples
- #include <jni.h>
- #include <string.h>
- #include <android/log.h>
- #define TAG "myhello-jni-test" // 这个是自定义的LOG的标识
- #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__) // 定义LOGD类型
- extern "C" {
- JNIEXPORT jstring JNICALL Java_com_snail_helloworld_MainActivity_myhello(JNIEnv * env, jobject obj);
- };
- JNIEXPORT jstring JNICALL Java_com_snail_helloworld_MainActivity_myhello(JNIEnv * env, jobject obj)
- {
- int i = 0;
- LOGD("########## i = %d", i);
- return env->NewStringUTF("Hello From CPP");
- }
Android.mk
- LOCAL_PATH := $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_MODULE := hello
- LOCAL_SRC_FILES := hello.cpp
- LOCAL_LDLIBS :=-llog
- include $(BUILD_SHARED_LIBRARY)
实际使用情况如下:
#define LOG_TAG "SysPropJNI"
#include "cutils/properties.h"
#include "utils/misc.h"
#include <utils/Log.h>
#include "jni.h"
#include "core_jni_helpers.h"
#include <nativehelper/JNIHelp.h>
#include <android/log.h>
#define TAG "SysPropJNI" // 这个是自定义的LOG的标识
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__) // 定义LOGD类型
namespace android
{
static jstring SystemProperties_getSS(JNIEnv *env, jobject clazz,
jstring keyJ, jstring defJ)
{
int len;
const char* key;
char buf[PROPERTY_VALUE_MAX];
jstring rvJ = NULL;
if (keyJ == NULL) {
jniThrowNullPointerException(env, "key must not be null.");
goto error;
}
key = env->GetStringUTFChars(keyJ, NULL);
LOGD("lucky SystemProperties_getSS");
LOGD("lucky SystemProperties_getSS 22key = [%s]", key);
len = property_get(key, buf, "");
LOGD("lucky SystemProperties_getSS len = [%d],[%s]", len,buf);
if ((len <= 0) && (defJ != NULL)) {
rvJ = defJ;
} else if (len >= 0) {
rvJ = env->NewStringUTF(buf);
} else {
rvJ = env->NewStringUTF("");
}
env->ReleaseStringUTFChars(keyJ, key);
error:
return rvJ;
}
....
result:
logcat |grep lucky
04-01 20:01:28.240 D/SysPropJNI( 5610): lucky SystemProperties_getSS04-01 20:01:28.240 D/SysPropJNI( 5610): lucky SystemProperties_getSS 22key = [ro.serialno]04-01 20:01:28.240 D/SysPropJNI( 5610): lucky SystemProperties_getSS len = [19],[ABC0M0A1S19000008]