This article was reproduced from: http://blog.csdn.net/zengraoli/article/details/11644815
1. import Log header file
In the. c/. cpp file that you are using
Import Log.h Header File
#include <android/log.h>
2. In the ANDROID.MK
Plus
Local_ldlibs: =-llog
Notice there's a line in Android.mk. Include $ (clear_vars)
Local_ldlibs: =-llog must be put behind it to be used,
Otherwise the equivalent is not written.
3. Define the Log function
Define a global variable, and then define some output log functions:
[CPP]View Plaincopyprint?
- #define TAG "Mydemo-jni"//This is the identity 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
functions defined in the preceding code
corresponding to the Java code in Android, respectively.
LOG.D (), LOG.I (), LOG.W (), LOG.E (), LOG.F () and other methods.
4. For example
[CPP]View Plaincopyprint?
- #include <jni.h>
- #include <string.h>
- #include <android/log.h>
- #define TAG "Myhello-jni-test"//This is the identity of the custom log
- #define LOGD (...) __android_log_print (android_log_debug,tag,__va_args__)//define LOGD type
- 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
[CPP]View Plaincopyprint?
- Local_path: = $ (call My-dir)
- Include $ (clear_vars)
- Local_module: = Hello
- Local_src_files: = Hello.cpp
- Local_ldlibs: =-llog
- Include $ (build_shared_library)
Log print "Go" in the-----Jni of Android