The android jni instance calls Log to output debugging information.

Source: Internet
Author: User

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
}

 

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.