网上有很多关于搭建android NDK开发环境的文章,发现很多都不能用,大部分都是过时的,比如安装什么Cygwin,在eclipse里面新建什么Builder之类的,有的只有部分内容。经过一天的摸索终于搭建好了android NDK开发环境,包括C/C++的调试等,废话不多说,开始我们的搭建流程。
首先、下载android NDK,得到一个android-ndk-r10d-windows-x86_64.exe的安装文件,双击解压得到一个android-ndk-r10d的文件夹。安装android NDK,前提是你必须是安装android sdk,能够看到这儿,应该是已经安装android sdk了的,点击菜单Window-->Preferences-->Android-->NDK,在NDK Location中输入之前android-ndk-r10d的全路径,点击OK,截图如下:
到此为止,我们的android NDK就安装完成了!!!,如果展开android这个节点,发现没有NDK这个选项,请参看:eclipse adt开发android ndk没有NDK选项问题的解决方案
其次、从NDK的案例中导入一个来测试,把android-ndk-r10d\samples下的hello-jni导入到eclipse 中,然后将项目Add Native Support,截图如下:
在弹出的对话框中,点击finish,这样就为我们的项目增加了NDK的支持。
然后、在C/C++的Perspective,选中项目,点击一个锤子模样的菜单,来Build Project,如果能看到在控制台打印类似20:50:59 Build Finished (took 1m:11s.298ms)的字样就说明Build成功了,截图如下:
运行项目,我们能看到在模拟器上显示出从当前CUP的类型,截图如下:
我们NDK的环境就算搭建好了!!!
最后、我们来看一下怎样来调试我们的NDK C/C++代码,打开jni/hello-jni.c文件,我们看到一打开就报错了,没关系,解决方法请看Android NDK *** could not be resolved,错误消失后,在方法Java_com_example_hellojni_HelloJni_stringFromJNI的最后一行代码上打上断点,debug android Native Application,截图如下:
发现没有Debug起来,控制台的错误如下:
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni] Android
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni] NDK:
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni] WARNING:
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni] APP_PLATFORM
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni] android-9
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni] is
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni] larger
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni] than
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni] android:minSdkVersion
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni] 3
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni] in
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni] ./AndroidManifest.xml
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni]
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni]
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni]
[2015-01-28 21:06:01 - HelloJni] Unknown Application ABI:
[2015-01-28 21:06:01 - HelloJni]
all
[2015-01-28 21:06:01 - HelloJni] Unable to detect application ABI's
这是由于android:minSdkVersion和项目的编译版本不一致,造成的,我们改成一致,都是10,再次运行,发现又报了一个错误:
[2015-01-28 21:13:32 - HelloJni] gdbserver output:
[2015-01-28 21:13:32 - HelloJni] run-as: exec failed for lib/gdbserver Error:No such file or directory
[2015-01-28 21:13:32 - HelloJni] Verify if the application was built with NDK_DEBUG=1
build command中输入:ndk-build NDK_DEBUG=1,截图如下:
再次Debug我们的项目,发现没有进我们打的断点,但是我们发现了Android Native Application gdb运行起来了,如下:
同时在控制台上打印出如下的错误代码:
warning: Could not load shared library symbols for 88 libraries, e.g. /system/bin/linker.
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
我试过,这个错误信息对我们的影响不大,我们按模拟器的返回键退出程序,再次进入,我们发现,进入断点了,可以调试了,截图如下:
到此,我们调试就成功了!!!
还有两个遗留问题
1.为什么android:minSdkVersion要跟项目的编译版本一致
2.调试第一次为什么不进入断点,而要退出再次进入才会进入断点和控制台报出的
warning: Could not load shared library symbols for 88 libraries, e.g. /system/bin/linker.
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
有知道的网友欢迎给我留言,也欢迎有不懂的地方向我提问,谢谢大家!!!