转载自:http://xms.eps.csie.ntut.edu.tw/xms/content/show.php?id=2752 作者: 張玉龍 (05-27 14:06)
Use printf in Android JNI
Problem:
Android can debug by logcat. So Android JNI need it to debug. This method let JNI code can print something we need in logcat.
1. Define LOG in c file
Add the following code in c file
#include <android/log.h>
#define LOG_TAG "D2mce-JNI"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,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__)
2. Add lib link in Android.mk
Add the following code in Android.mk
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
3. How to use
JNIEXPORT jint JNICALL Java_eps_d2mce_D2MCENative_logtest(JNIEnv* env, jobject thiz, jint x, jint y)
{
LOGD("test log in JNI, x is %d, y is %d\n",x,y);
return add(x,y);
}