What is the Log API to invoke the Android JNI program?

I would like to debug a JNI C application by inserting log messages into logcat. What is the C API that does this?

+42
c android android-emulator android-ndk
Mar 28 2018-11-21T00:
source share
3 answers

Like this:

#include <android/log.h> __android_log_write(ANDROID_LOG_ERROR, "Tag", "Error here");//Or ANDROID_LOG_INFO, ... 

Add it to your makefile as follows:

 LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 
+84
Mar 28 '11 at 23:01
source share

Below is a snippet of code that you should include in your own code.

 #include <android/log.h> __android_log_write(ANDROID_LOG_ERROR, "Tag", "Error msg");//Or ANDROID_LOG_INFO, ... 

To use the above API, we need to link the appropriate library.

We can link the shared library in Android in three ways. In the below 3 cases, the specified lines should be added to Android.mk

So, here are three ways.

 1. LOCAL_LDLIBS way LOCAL_LDLIBS := -llog 

For some reason, if 1 doesn't work (it didn't work for me), you can try less than two ways

 2. LOCAL_LDFLAGS way LOCAL_LDFLAGS := -llog 3. LOCAL_SHARED_LIBRARIES way LOCAL_SHARED_LIBRARIES += liblog 
+6
Mar 31 '14 at 9:21
source share

syslog

This POSIX function also outputs to logcat.

The advantage is that it is more portable on systems other than Android than __android_log_write , and it automatically adds the application package to the log.

Tested with this sample application: https://github.com/cirosantilli/android-cheat/tree/a080f5c370c1f06e74a8300fb4a2e93369861047/gradle/NdkSyslog NDK source:

 #include <jni.h> #include <string> #include <syslog.h> extern "C" JNIEXPORT jstring JNICALL Java_com_cirosantilli_android_1cheat_ndksyslog_MainActivity_stringFromJNI( JNIEnv* env, jobject /* this */) { syslog(LOG_CRIT, "hello syslog"); return env->NewStringUTF("Check adb logcat"); } 

And now logcat contains:

 01-14 15:39:07.582 3633 3633 E com.cirosantilli.android_cheat.ndksyslog: hello syslog 

Tested on Android O, HiKey 960.

+1
Nov 06 '17 at 9:45
source share



All Articles