windows下调试Android NDK [英] Debugging Android NDK, under windows
问题描述
我正在将一个复杂的 C 应用程序移植到 Android,作为一个 SO 库,包裹着薄 java 层.经过几个小时的麻烦,我在Android下编译了代码,但是当然,应用程序崩溃了:(
I am porting a complex C application to Android, as an SO library, wrapped with thin java layer. After few hours of hassle, i have the code compiling under Android, but of course, the application crashes :(
环顾四周,我了解到在 Android 下调试 C 应用程序的唯一方法是通过 gdb.由于我对 gdb 没有太多经验,有没有人可以分享一些见解?
After looking around, i understand the only way to debug C application under Android is by gdb. Since I don't have a lot of experience with gdb, any one out there can share some insights?
谁有windows下gdb的教程:)?
Anyone has a tutorial for gdb under windows :) ?
谢谢
推荐答案
对于更新版本的 NDK(我使用的是 r7c),您可以通过构建调试版本
for a more recent version of NDK (I am using r7c), you can build debug version by
- 将
android:debuggable="true"
标志添加到AndroidManifest.xml
中的 - 使用
NDK_DEBUG=1
调用ndk-build
(如果使用 unix shell 运行 ndk-build,则不需要 NDK_DEBUG 标志)
标记- add
android:debuggable="true"
flag to<Application>
tag inAndroidManifest.xml
- invoke
ndk-build
withNDK_DEBUG=1
(NDK_DEBUG flag not necessary if running ndk-build with unix shell)
在 Windows 上,事情会变得有点棘手,因为要使用 ndk-gdb
,您仍然需要从 NDK r7c 开始的 bash(或 cygwin),但要运行 ndk-build
如果您使用任何预构建的静态库,在 cygwin bash 中会遇到权限问题
on Windows, things get a bit tricky because to use ndk-gdb
, you still need bash (or cygwin) as of NDK r7c, yet ndk-build
running in cygwin bash will run into permission problem if you ever use any of the pre-built static library
我在 windows 机器上的解决方案是
my solution on windows machine is
- 在
AndroidManifest.xml
中的<application
标签中添加android:debuggable="true"
标志(同上) - 在 cmd(Windows 的命令提示符)中:使用
NDK_DEBUG=1
调用 - 在 cygwin bash 中:运行
ndk-gdb
ndk-build
- add
android:debuggable="true"
flag to<application
tag inAndroidManifest.xml
(same as above) - in cmd (windows' command prompt): invoke
ndk-build
withNDK_DEBUG=1
- in cygwin bash: run
ndk-gdb
为了快速初步调查原生so库,创建一个简单的活动,一键触发库入口功能,活动中的loadLibrary
如下:
for quick initial investigation of native so library, create a simple activity with one button to trigger library entry function and loadLibrary
in the activity like:
class MyActivity extends Activity {
static {
System.loadLibrary("mylibrary");
}
/* other functions like onCreate, etc... */
public native void libfunc();
public void onClick(View v){
libfunc();
}
}
所以当 gdb
启动时,实际加载了相关库,但您仍然可以在程序崩溃之前设置断点等;当你完成调试器的设置后,在 (gdb)
提示符下,输入 continue
(或只是 'c
'),然后点击按钮启动崩溃的程序并愉快地调试......
So when gdb
starts, the library in question is actually loaded, yet you can still have time to set break points, etc before the program crashes; when you finish setting up the debugger, at (gdb)
prompt, type continue
(or just 'c
'), then hit the button to start the crashing program and happy debugging...
这篇关于windows下调试Android NDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!