Log print "Go" in the-----Jni of Android

Source: Internet
Author: User

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?
  1. #define TAG "Mydemo-jni"//This is the identity of the custom log
  2. #define LOGD (...) __android_log_print (Android_log_debug,tag, __va_args__)//define LOGD type
  3. #define Logi (...) __android_log_print (Android_log_info,tag, __va_args__)//define Logi type
  4. #define LOGW (...) __android_log_print (Android_log_warn,tag, __va_args__)//define LOGW type
  5. #define LOGE (...) __android_log_print (Android_log_error,tag, __va_args__)//define LOGE type
  6. #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?
  1. #include <jni.h>
  2. #include <string.h>
  3. #include <android/log.h>
  4. #define TAG "Myhello-jni-test"//This is the identity of the custom log
  5. #define LOGD (...) __android_log_print (android_log_debug,tag,__va_args__)//define LOGD type
  6. extern "C" {
  7. Jniexport jstring jnicall Java_com_snail_helloworld_mainactivity_myhello (jnienv * env, jobject obj);
  8. };
  9. Jniexport jstring jnicall Java_com_snail_helloworld_mainactivity_myhello (jnienv * env, Jobject obj)
  10. {
  11. int i = 0;
  12. LOGD ("########## i =%d", i);
  13. return Env->newstringutf ("Hello from CPP");
  14. }

Android.mk

[CPP]View Plaincopyprint?
    1. Local_path: = $ (call My-dir)
    2. Include $ (clear_vars)
    3. Local_module: = Hello
    4. Local_src_files: = Hello.cpp
    5. Local_ldlibs: =-llog
    6. Include $ (build_shared_library)


Log print "Go" in the-----Jni of Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.