Setting up Android NDK Development and Debugging inside Eclipse

To follow the steps, you should have an up and running Android SDK environment. If you don't, you can follow this guide.

  1. Download the latest Android NDK from:
    http://developer.android.com/tools/sdk/ndk/index.html
    You should match the instruction sets of your SDK, JDK and NDK. In example, if you have an x86 Android SDK you should download the x86 NDK.
  2. Extract the contents to a folder of your liking, ex:
    C:\SDK\android\android-ndk-r8e-windows-x86\
  3. Install the NDK plugin through ADT
    1. In eclipse click, Help -> Install New Software
    2. In the Work with field select
      Android Developer Tools Update Site - http://dl-ssl.google.com/android/eclipse/
      from the dropdown menu and hit enter.
    3. Check the NDK Plugins box, and click next.
    4. ADT should automatically download and install the plugins
  4. Setting NDK path
    1. In eclipse, Window -> Preferences
    2. From the list on the left, expand Android and select NDK.
    3. Enter the folder you extracted the NDK files, to NDK Location field.

At this point everything is set up and should be working, however there are some additional steps to be able to build and debug an NDK project.

To convert a sample project to Native project:

  1. In Eclipse, File -> import; then expand Android and select Existing Android Code Into Workspace
  2. Browse for a simple sample project, ex:
    C:\SDK\android\android-ndk-r8e\samples\hello-jni
  3. Click Finish.
  4. Right click the project name, go to Android Tools and click Add native support
  5. Don't change the lib name, and click finish.

To debug native code:

  • In eclipse, double click on hello-jni.c which is under jni folder.
  • You should see the following code:
jstring  
Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
  • insert a break point to return line
  • however you may also need to add the following:
jstring  
Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv* env,
jobject thiz )
{
sleep(5);
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
  • This enables the gdb_server to catch up. Since it takes a few seconds for native debugging to start working, sleeping here for 5 seconds allows us to catch the break point in the return line.
  • Right click the project and hover on debug as then click Android Native Application
  • The debugger should kick in and the execution would stop at the breakpoint.