In Android development, can java easily use the debugging information Log. I, Log. d... and Jni layers? The answer is yes.
1. For ease of use, first define macro:
The content of eben_hpc_log.h is as follows:
[Cpp]
# Ifndef _ Included_hpc_Log
# Define _ Included_hpc_Log
# Ifdef _ cplusplus
Extern "C "{
# Endif
# Include <android/log. h>
// Macro definition is similar to the definition of java layer. Different Levels of Log LOGI, LOGD, LOGW, LOGE, and LOGF are supported. For Log. I log. d in Java
# Define LOG_TAG "hpc -- JNILOG" // This is the ID of the custom LOG
// # Undef LOG // cancel the default LOG
# Define LOGI (...) _ android_log_print (ANDROID_LOG_INFO, LOG_TAG, _ VA_ARGS __)
# Define LOGD (...) _ android_log_print (ANDROID_LOG_DEBUG, LOG_TAG, _ VA_ARGS __)
# Define LOGW (...) _ android_log_print (ANDROID_LOG_WARN, LOG_TAG, _ VA_ARGS __)
# Define LOGE (...) _ android_log_print (ANDROID_LOG_ERROR, LOG_TAG, _ VA_ARGS __)
# Define LOGF (...) _ android_log_print (ANDROID_LOG_FATAL, LOG_TAG, _ VA_ARGS __)
# Ifdef _ cplusplus
}
# Endif
# Endif
2. Use the following code:
[Cpp]
// Demo. c
# Include <jni. h>
# Include "eben_hpc_log.h"
JNIEXPORT jint JNICALL Java_cn_hpc_cai_jni_JniLogDemo_test (JNIEnv * env, jobject jobj)
{
LOGD ("log. d This is the log in Jni: Java_cn_hpc_cai_jni_JniLogDemo_test ()");
LOGI ("Log. I this is the log in Jni: Java_cn_hpc_cai_jni_JniLogDemo_test ()");
Return 0;
}
3 android. mk
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_C_INCLUDES: = $ (LOCAL_PATH)/include
LOCAL_LDLIBS + =-L $ (SYSROOT)/usr/lib-llog // load the Log library during compilation
LOCAL_MODULE: = JniLogDemo
LOCAL_SRC_FILES: = \
Demo. c \
Include $ (BUILD_SHARED_LIBRARY)
Next we will call the Java layer:
4. java call
[Java]
Public static final String libName = "JniLogDemo ";
Static {
System. loadLibrary (libName );
}
{
JniLogDemo demo = new JniLogDemo ();
Demo. test ();
// Output logs in jni
}