NDK环境搭建(Native Code的编译,不需要Cygwin)
本文将讲述内容如下:
1. 什么是NDK
2.什么时候要用NDK
3.NDK环境该如何搭建
1. 什么是NDK
NDK,我想应该是Native Development Kit的简称吧。NDK 提供了一系列的工具,
帮助开发者快速开发 C (或 C++ )的动态库,并能自动将 so 和java 应用一起打包
成 apk 。NDK 集成了交叉编译器,并提供了相应的 mk 文件隔离 CPU 、平台、 ABI
等差异,开发人员只需要简单修改 mk 文件(指出 “ 哪些文件需要编译 ” 、 “ 编译特性
要求 ” 等),就可以创建出 so 。
2.什么时候要用NDK
使用NDK,并不会给我们程序提高执行性能,反而会使程序复杂化。因此,尽量避免使用NDK,
除非你真的需要使用本地库(通常是C/C++库),不要因为擅长使用C/C++编程而使用NDK。
Android Framework提供了两种使用本地代码的方式 :
1.通过JNI调用本地代码
2.通过NativeActivity类调用本地代码(从2.3版本开始支持)
3.NDK环境该如何搭建
搜索网上各路资料,会发现都提到了安装Cygwin,以提供支持make 和 gcc的MinGNU环境。
就连官方的网站上,也是这么说:
http://developer.android.com/tools/sdk/ndk/index.html#Contents
System and Software Requirements
Required development tools
- For all development platforms, GNU Make 3.81 or later is required. Earlier versions of GNU Make might work but have not been tested.
- A recent version of awk (either GNU Awk or Nawk) is also required.
- For Windows, Cygwin 1.7 or higher is required. The NDK will not work with Cygwin 1.5 installations.
Cygwin环境了,我们只需要解压,在JNI的build.cm里指定好NDK引用的目录(或者通过ndk-build
去编译)就行了。
这把我坑的真够厉害的。。。。网上写的很多环境搭建指南都是基于比较旧的NDK版本。
Android developers官方居然也没更正过来。。。
顺便再网上找了下,终于找到了一点信息:( http://www.kuwanzu.net/xinwenzixun/xs/9876.html)
将C++源码放入 <project>/jni/... 创建<project>/jni/Android.mk文件,文件中描述C++源码相关的编译配置 使用ndk-build命令进行编译: cd <project> ndk-build 通过SDK对工程进行编译,生成 .apk 文件.
The NDK includes a set of cross-toolchains (compilers, linkers, etc..) that can generate native ARM binaries on Linux, OS X, and Windows (with Cygwin) platforms.
It provides a set of system headers for stable native APIs that are guaranteed to be supported in all later releases of the platform:
- libc (C library) headers
- libm (math library) headers
- JNI interface headers
- libz (Zlib compression) headers
- liblog (Android logging) header
- OpenGL ES 1.1 and OpenGL ES 2.0 (3D graphics libraries) headers
- libjnigraphics (Pixel buffer access) header (for Android 2.2 and above).
- A Minimal set of headers for C++ support
- OpenSL ES native audio libraries
- Android native application APIS
The NDK also provides a build system that lets you work efficiently with your sources, without having to
handle the toolchain/platform/CPU/ABI details. You create very short build files to describe which sources
to compile and which Android application will use them — the build system compiles the sources and places
the shared libraries directly in your application project.