Debug native code in Android project with NDK in Eclipse
-
Install CDT from Eclipse update site. Help -> Install New Software -> Select Eclipse update -> Programming Language -> C/C++ Development Tools -> Next ... -> Finish
-
Download and extract Android NDK. https://developer.android.com/tools/sdk/ndk/index.html
-
Import your C/C++ source code to Eclipse.
-
Set Android NDK path. Window -> Preferences -> Android -> NDK -> Set path to the NDK
-
Right click on an Android project and select Android Tools -> Add native support. Note that you will not be able to add native support if the project already has C/C++ nature.
-
Right click project -> Properties -> C/C++ Build then add the following line to the "Build command": ndk-build NDK_DEBUG=1
-
Add more line to application tag of the AndroidManifest.xml file: android:debuggable="true"
-
Set breakpoint in your C/C++ code.
-
Right click on your project, select Debug As -> Android Native Application.
KNOWN ISSUES:
-
Error when debugging: Unable to detect application ABI's To fix this, add more information to your Android.mk file, e.g: APP_ABI := armeabi APP_PLATFORM := android-15
-
It is recommended to introduce delay (about 5 seconds) in the application code between loading the native code library and calling any native functions (to be debugged), e.g:
try { System.loadLibrary("some_native_lib.so"); } catch (UnsatisfiedLinkError e) { return -1; }
// Wait for GDB init if ((getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { try { Thread.sleep(5000); } catch (InterruptedException e) { } }
// Start calling native functions here // ...