android ndk环境配置(包括CDT环境设置)
一、关于NDK
Android NDK全称:Native Development Kit,即本地开发包。
1、NDK是一系列工具的集合。
NDK提供了一系列的工具,这些工具对开发者的帮助是巨大的。
它们能帮助开发者快速开发C(或C++)的动态库,并能自动将so库和java应用一起打包成apk。
NDK集成了交叉编译器,并提供了相应的mk文件隔离CPU、平台、ABI等差异,
开发人员只需要简单修改mk文件(指出“哪些文件需要编译”、“编译特性要求”等),就可以创建出so库。
NDK可以自动地将so和Java应用一起打包,极大地减轻了开发人员的打包工作。
2、NDK提供了一份稳定、功能有限的API头文件声明。
Google明确声明该API是稳定的,在后续所有版本中都稳定支持当前发布的API。
从该版本的NDK中看出,这些API支持的功能非常有限,包含有:
C标准库(libc)、标准数学库(libm)、压缩库(libz)、Log库(liblog)。
二、NDK实例的实现
对于Windows环境下NDK的开发,如果使用的NDK是r7之前的版本,必须要安装Cygwin才能使用NDK;
从r7开始,Google的提供了一个ndk-build.cmd的脚本,可以直接用它编译,而不需要使用Cygwin了。
只需要为Eclipse Android工程添加一个Builder,就能让Eclipse自动编译NDK。
本文是讲述NDK-r8e下的实现实例。
下面是在windows下为NDK配置自动编译的builder的过程(对于Linux,只需将ndk-build.cmd修改为ndk-build就可以了)。
(1)先下载安装NDK
下载地址:http://developer.android.com/sdk/ndk/index.html
下载后解压缩就可以用了。
(2)新建工程和jni文件夹
打开Eclipse,新建或导入一个Android工程(我的取名为NdkTest),在工程目录下新建jni文件夹;
该jni文件夹就用来保存NDK需要编译的文件代码等。
(3)新建并配置一个Builder:
(a)Project->Properties->Builders->New,新建一个Builder。
(b)在弹出的【Choose configuration type】对话框,选择【Program】,点击【OK】:
(c)在弹出的【Edit Configuration】对话框中,配置选项卡【Main】。
在“Name“中输入新builders的名称(我取名为Ndk_Builder)。
在“Location”中输入nkd-build.cmd的路径。
(我的是E:\adt-bundle-windows-x86-20130522\ndk-r8e\ndk-build.cmd, 路径不能有空格
根据各自的ndk路径设置,也可以点击“Browser File System…”来选取这个路径)。
在“Working Diretcoty”中输入项目工程的路径${workspace_loc:/project}
(我的是${workspace_loc:/NdkTest},也可以点击“Browse Workspace”来选取本工程目录)。
(d)【Edit Configuration】对话框中,配置选项卡【Refresh】。
勾选“Refresh resources upon completion”,
勾选“The entire workspace”,
勾选“Recuresively include sub-folders”。
(e)【Edit Configuration】对话框中,配置选项卡【Build options】。
勾选“After a “Clean””,
勾选“During manual builds”,
勾选“During auto builds”,
勾选“Specify working set of relevant resources”。
点击“Specify Resources…”
勾选本工程的“jni“目录,点击”finish“。
点击“OK“,完成配置。到这里Eclipse就能自动调用NDK编译jni目录下的C/C++代码了。
出现的提示信息如下:
Multiple markers at this line
- Syntax error
- Type 'JNIEnv' could not be resolved
- Type 'JNICALL' could not be resolved
是由于没有将jni.h导入的缘故,而这个文件在ndk的目录下面。所以,参照以下步骤:
Project Properties -> C/C++ General -> Path and Symbols
选择include标签,Add -> $Android_NDK_HOME/platforms/android-14/arch-arm/usr/include
且选中All languages.
最后Apply -> OK
这样错误就解决了。
5) Press Ctrl+n (or choose File->New->Other… from main menu) and selectConvert to a C/C++ Project.
This will convert your project into a mixed Java & C/C++ project rather than into pure C/C++ project (the name of the function is misleading).
Click Next. Then choose your project and below choose Makefile project and – Other Toolchain –. Click Finish.
After doing this Eclipse will ask you if you want to switch to C/C++ perspective. ChooseYes because otherwise you wouldn’t be able to set C/C++ build preferences.
6) Click on your project with right button and select Properties or press Alt+Enter
Properties windows will appear. Here you have to configure use of ndk-build instead of make all command and set proper include paths.
7) Choose C/C++ Build and configure ndk-build as a build command
In Builder settings fill ndk-build into Build command entry. You have to uncheck Use default build command. You also need to have ndk-build script in your PATH.
In Behaviour setting uncheck clean (ndk-build cleans project automatically on build and does not support separate clean command) and clear all text from build (ndk-build does not accept all as a parameter.
Click Apply to save settings.
8) Choose C/C++ General->Paths and Symbols and configure include path
In Includes tab choose GNU C or GNU C++ and click Add… button. Add path to include directory which is located in platforms/android-4/arch/arm/usr/include subdirectory of place where you’ve unpacked Android ndk. Include path depends on target for which you are compiling (android-4 in my case — i.e. Android 1.6).
Finally click Apply and OK and that is all. Now you can use all Eclipse power for editing your C/C++ sources. If you click Run or Debug Eclipse will compile C/C++ code as well as Java code and run it on device/emulator. However you will not be able to debug C/C++ code.
In Eclipse:
- right click the project,
- click properties
- Expand "C/C++ general" the item in the left hand tree view by clicking the arror, (just clicking the item itself does not expand the suboptions)
- From the suboptions select "Preprocessor Include Paths, Macros etc."
- Click the tab "Providers" and check the box next to "CDT GCC Built-in Compiler Settings [ Shared ]".
转自:http://blog.csdn.net/gaojinshan/article/details/9464195
http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/