Any simple way to log in Android NDK code?


I'm looking for a way to easily debug C code in an Android NDK application using Eclipse. I've read ways to debug the app using gdb or something similar but what I want is a way to push messages to Eclipse somehow.

I'm looking for a solution that's as simple as using a print function in C and seeing it in the DDMS Log or anything similar. Does anyone have any experience doing this?

11/29/2018 5:20:40 PM

Accepted Answer

You can use the Android logging facilities:

#include <android/log.h>

#define APPNAME "MyApp"

__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "The value of 1 + 1 is %d", 1+1);

Make sure you also link against the logging library, in your file:

  LOCAL_LDLIBS := -llog
1/7/2011 8:03:01 PM

The easiest way is probably to redirect printf() statements to the system log (based on the "Viewing stdout and stderr" section of the official ADB reference manual.

Type these 3 commands on a command line:

adb shell stop
adb shell setprop log.redirect-stdio true
adb shell start

Then you can view the output of your "printf()" statements by looking at the "LogCat" window of Eclipse Debugger, or by typing this on a command line:

adb logcat

Just be aware that since the data is buffered before transferring from the emulator or device, you should definitely flush the stdout buffer, eg:

printf("Hello, I am %d years old!\n", 30);

You should then see a log message starting with "I/stdout:"

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow