Using the NDK plugin
posted Jul 2, 2012, 7:16 PM by Siva Velusamy
ADT 20 includes an NDK plugin that provides support for building and debugging NDK projects in Eclipse. This document describes how to install and use the NDK plugin.
Installation
The NDK plugin currently works with CDT 7.0.2 or CDT 8.0.2.
Using the NDK Plugin
1. First set the path to SDK and NDK: Eclipse -> Window -> Preferences -> Android -> set path to SDK Eclipse -> Window -> Preferences -> Android -> NDK -> set path to the NDK
Note that you will not be able to add native support if the project already has C/C++ nature. At this point, you will be able to build your applications using Project -> Build All. Debugging native applications 1. Update your build config to include “NDK_DEBUG = 1”. Right click project -> properties -> C/C++ Build:
2. Set a breakpoint in your C code.
3. Right click on your project, select Debug As -> Android Native Application Note: There is a delay of a few seconds between when the activity is launched and when native debugging starts. If your code is already executed by that point, then you won’t see the breakpoint being hit. So either put a breakpoint in code that is called repetitively, or make sure that you call JNI code after you see that ndk-gdb has connected. Known Issues 1. Eclipse does not automatically find the include paths to all the NDK headers on Windows. This issue will be fixed in the next update (20.0.1) when it is released. 2. Eclipse does not automatically find the include paths with CDT 8.1.0 (Juno). This issue is tracked in Bug33788. 用上面的方法配置好后,每次编译都会提示错误:make: *** empty variable name. Stop. 查了n久,只是知道这个错误是ndk不识别路径中‘ ’和‘.’符号,但是各种编译路径都修改后,还是会出现。 最后,终于发现是ndk的命令当中有问题,添加调试标志的时候,NDK_DEBUG=1 等号左右也不能有空格,把空格去掉好了~~~ 好了,结局是好的,NDK还是越来越好用了~ 最近一段时间在做native层的开发,把一些经验和技巧记录下来,希望对大家有帮助。本教程以step-by-step的形式给正处入门阶段的native开发新手提供指引。 导言:在进行Android开发的过程中,在一些对性能要求较高的场景,例如图像处理,视音频编解码等,需要使用到native代码以提高运行效率。本教程将在native层进行加法运算和字符串连接,通过这个简单的例子阐述使用eclipse编译运行ndk代码的过程。 注:JNI基础知识不在本文的讨论范围之内,推荐浏览oracle的帮助文档进行系统学习 开发环境: Adt-bundle(ver:21.1.0) 下载地址:https://developer.android.com/sdk/index.html Ndk(ver:r8b) 下载地址:https://developer.android.com/tools/sdk/ndk/index.html 示例工程下载地址:https://github.com/ilzc/Code/tree/master/jni 步骤详解: 1、 配置ndk路径 打开Eclipse后,点击菜单栏的Project->Preferences打开Preferences窗口,点击左侧Android->NDK选项,在右侧NDK Location填入ndk的路径 2、 创建工程并增加native支持 点击菜单栏的File->New->Android Application Project创建Android工程。 创建完毕后,在PackageExplorer中右键点击刚才新建的Android项目,选择Android Tools->Add Native Support,按下图填写,点击确认后,工程目录下会增加jni目录,jni目录下有test.cpp和Android.mk。 3、 编写jave层的jni接口 创建一个Java类,类名为Jni 编写加载库的代码,并添加两个native方法
4、 编写native层的代码
5、 编写Android.mk
6、 编译运行 编译:在Package Explorer视窗,右键点击jni项目,选择Build Project编译项目,编译成功后,在工程libs->armeabi目录下可以看到libtest.so。 运行:在Package Explorer视窗,右键点击jni项目,选择Run As->Android Application运行工程。 最后,我们可以在logcat中可以看到打印结果:
|