Android studio and ndk-gdb to debug a native appPosted: June 25, 2015
I don’t know how good Android Studio support for native apps is nowadays (it changes from week to week!). Up to a few months ago, Gradle, the build system used by AS, had poor support for native development. If you’re having problems, you may find it easier to workaround it completely when it comes to build and debug C/C++ applications.
To debug a native Android application, a binary called gdbserver and its associated gdb.setup must be included in the generated APK file. Including this into the APK can be very painful in Gradle, so here’s a workaround I found:
- Build your stuff the way you normally would (I’m assuming you know already how to build a native app, and if you don’t there are guides online that explain it much better than I could).
- Deploy your application the way you normally would.
- Discover ndk-gdb won’t run. Bang forehead against keyboard a few times.
- After losing some hours looking at logs, figure out there’s no gdbserver included in your apk.
- Lose some more hours trying to figure out how to include it in your apk using Gradle.
- Give up. Bang forehead against keyboard some more.
- find the gdbserver and gdb.setup in your build directory.
- adb push each of these files to the device.
- Using adb shell, move the files you copied to /data/app-lib/com.yourapp/ – you may need to root your device for this.
- Profit! ndk-gdb now works.
Edit: remember you may need to chmod +777 your gdbserver.