¿Qué es la API de registro para llamar desde un programa JNI de Android?


Me gustaría depurar una aplicación JNI C insertando mensajes de registro en logcat. ¿Qué es la API C que hace esto?

Author: hopia, 2011-03-29

3 answers

Así:

#include <android/log.h>


__android_log_write(ANDROID_LOG_ERROR, "Tag", "Error here");//Or ANDROID_LOG_INFO, ...  

Agrégalo a tu makefile así:

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 
 93
Author: Ryan Reeves,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-19 03:22:53

A continuación se muestra el fragmento de código que debe incluir en su código nativo.

#include <android/log.h>


__android_log_write(ANDROID_LOG_ERROR, "Tag", "Error msg");//Or ANDROID_LOG_INFO, ...  

Para usar la API anterior, necesitamos vincular la biblioteca correspondiente.

Podemos vincular una biblioteca compartida en Android de 3 maneras. En los 3 casos siguientes, las líneas mencionadas deben añadirse en Android.mk

Así que aquí están las tres maneras.

1. LOCAL_LDLIBS way
LOCAL_LDLIBS := -llog

Por alguna razón si 1 no funciona (no funcionó para mí), puede probar a continuación 2 maneras

2. LOCAL_LDFLAGS way
LOCAL_LDFLAGS := -llog

3. LOCAL_SHARED_LIBRARIES way
LOCAL_SHARED_LIBRARIES += liblog
 6
Author: mk..,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-20 09:11:39

syslog

Esta función POSIX también emite a logcat.

Tiene la ventaja de ser más portátil en sistemas no Android que __android_log_write y agrega automáticamente el paquete de la aplicación al registro.

Probado con esta aplicación de ejemplo: https://github.com/cirosantilli/android-cheat/tree/a080f5c370c1f06e74a8300fb4a2e93369861047/gradle/NdkSyslog la fuente NDK es:

#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");
}

Y logcat ahora contiene:

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

Probado en Android O, HiKey 960.

 2
Author: Ciro Santilli 新疆改造中心 六四事件 法轮功,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-06 09:50:56