Any easy way to get into Android NDK code?

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

I am looking for a solution that is as simple as using the print function in C and viewing it in a DDMS log or something similar. Does anyone have experience with this?

+59
c android eclipse logging android-ndk jni
Jan 07 2018-11-11T00:
source share
6 answers

You can use the Android logging tools:

#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 to the logging library in your Android.mk file:

  LOCAL_LDLIBS := -llog 
+113
Jan 07 2018-11-11T00:
source share

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

Enter these 3 commands on the command line:

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

You can then view the output of your printf () statements by looking at the Eclipse debugger's LogCat window or by typing it at the command line:

 adb logcat 

Just keep in mind that since data is buffered before being transferred from the emulator or device, you must definitely flush the stdout buffer, for example:

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

Then you should see a log message starting with "I / stdout:"

+12
Aug 11 '11 at 6:32
source share

An alternative solution (using a debugger) is explained here:

How can I effectively debug C code that is wrapped by JNI in Eclipse? (Android Dev)

+2
Feb 09 '11 at 8:05
source share

ADT 20 includes an NDK plugin that provides support for creating and debugging NDK projects in Eclipse. This document describes how to install and use the NDK plugin. The instructions are quite simple and consist of just a few steps.

This is the simplest solution I found and it worked for me.

Note. If you use the ADT package, you only need to install the C development tools with the installation of new software (see the screenshot), and you can immediately go to the "Using the NDK plugin" section.

c dev tools install

Edit: It seems that the CDT issue in eclipse juno http://code.google.com/p/android/issues/detail?id=33788 causes the eclipse debugger cannot find breakpoints. The workaround that I used was to start the application in debug mode (not debugging as my own application, but β€œregular” debugging), and then at the command line I went to my project and typed ndk-gdb (this creates a file gdb.setup in obj/local/armeabi ). After that, the control points worked as usual.

In the comments related to the link question above, they suggest some other workarounds, but I have not tried them as they seemed to require more effort than that.

+2
May 03 '13 at 11:19
source share

No one has published information about the various levels of the magazine so far. The answer is an attempt to make the "picture" of logging complete.

 #include <android/log.h> #define TAG "MY_TAG" #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__) #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__) #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__) 

Using:

 char err[] = "wrong"; LOGE("Something went %s", err); 

Link the Android Log Library as shown below.

Android.mk :

 LOCAL_LDLIBS := -llog 

CMakeLists.txt :

 find_library( log-lib log ) target_link_libraries( ${log-lib} ) 

Further reading: registration

+2
Nov 10 '18 at 14:26
source share

You can also recycle a little

 #include <android/log.h> #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-lib", __VA_ARGS__)) 

Using:

 std::string hello = "Hello from C++"; int a = 1; LOGI("int %d, string: %s", a, hello.c_str()); 
+1
Nov 05 '18 at 12:36
source share



All Articles