ranjithkumards
ranjithkumards

Reputation: 11

Is it possible to debug native C code (an ndk-build project) with Android Studio?

I need to debug native C code (ndk-build) on the Android platform.

Is it possible to debug it in Android Studio, and if so how can I do it ?

Upvotes: 1

Views: 164

Answers (3)

LaurentY
LaurentY

Reputation: 7653

In your C source code, you could add logs with this function:

__android_log_print(ANDROID_LOG_INFO, "MyTag", "The value is %d", some_variable);

Need this define:

#define <android/log.h>

Logs will be print in LogCat of Android device.

You don't have "step by step" debugger as in Android application.

Upvotes: 1

Engineer
Engineer

Reputation: 8847

  1. Open Android Studio
  2. Connect your device and select it from the dropdown in the "Android" tab view in Studio
  3. Go to shell/terminal/CLI and build your app using ndk-build, make or whatever
  4. Set up your usual logcat filters in Studio, and debug away!

Works for me with the lastest Studio version (1.3 RC3). You don't have to debug via CLI / logcat.

Upvotes: 0

Paulo Roberto
Paulo Roberto

Reputation: 3

As the LaurentY response, In your header file, you could define some logs like this:

#define <android/log.h>

#define LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGD(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

Once in your .c/.cpp code you set the TAG as:

#define LOG_TAG "sample.cpp"

And call your logs using:

LOGI("Yes!!, %s, %s", some, some2);

Upvotes: 0

Related Questions