[Android Pro] Call log output debug information in Android Studio JNI

Source: Internet
Author: User

Reference to:http://www.linuxidc.com/linux/2014-02/96341.htm

Android development, Java can easily use debugging information log.i, LOG.D ..., the JNI layer can use it? The answer is yes.

1 for ease of use, first macro definition:

Eben_hpc_log.h content is as follows:

#ifndef _included_hpc_log
#define _included_hpc_log
#ifdef __cplusplus
extern "C" {
#endif
#include <android/log.h>
Macros define similar Java layer definitions, different levels of log logi, LOGD, LOGW, LOGE, Logf. Yes, log.i log.d in Java.
#define LOG_TAG "HPC-Jnilog"//This is the identity 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 Code in use:

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 Log:java_cn_hpc_cai_jni_jnilogdemo_test () in JNI");
Logi ("Log.i This is Log:java_cn_hpc_cai_jni_jnilogdemo_test () in JNI");

return 0;
}

3 in App/build.gradle

Defaultconfig {        NDK {            ldlibs "log"            modulename "Xal"         //generated so name            abifilters "Armeabi"  // The output specifies the so library under three ABI architectures. Currently dispensable.            //abifilters "Armeabi", "armeabi-v7a", "x86"  //outputs specify so libraries under three ABI architectures. Currently dispensable.        }    }

The next step is the Java layer invocation:

4 Java Calls

public static final String libname = "Xal";
static {
System.loadlibrary (libname);
}

{
Jnilogdemo demo = new Jnilogdemo ();
Demo.test ();
}

Complete.

[Android Pro] Call log output debug information in Android Studio JNI

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.