http://developer.android.com/sdk/ndk/overview.html
The Android NDK is a toolset that lets you embed components that make use of native code in your Android applications.
NDK可以让你在程序中使用native code开发的组件
Android applications run in the Dalvik virtual machine. The NDK allows you to implement parts of your applications using native-code languages such as C and C++. This can provide benefits to certain classes of applications, in the form of reuse of existing code and in some cases increased speed.
Android在 Dalvik 虚拟机上运行。NDK允许你使用natvie code,如C和C++实现你程序的某些部分。这样做对提高速度、重用代码有利。
The NDK provides:
- A set of tools and build files used to generate native code libraries from C and C++ sources
- 生成C和C++的库的工具
- A way to embed the corresponding native libraries into an application package file (
.apk
) that can be deployed on Android devices - 把native库嵌入application package的途径
- A set of native system headers and libraries that will be supported in all future versions of the Android platform, starting from Android 1.5. Applications that use native activities must be run on Android 2.3 or later.
- Documentation, samples, and tutorials
The latest release of the NDK supports these ARM instruction sets:
- ARMv5TE (including Thumb-1 instructions)
- ARMv7-A (including Thumb-2 and VFPv3-D16 instructions, with optional support for NEON/VFPv3-D32 instructions)
- x86 instructions (see CPU-ARCH-ABIS.HTML for more information)
ARMv5TE machine code will run on all ARM-based Android devices. ARMv7-A will run only on devices such as the Verizon Droid or Google Nexus One that have a compatible CPU. The main difference between the two instruction sets is that ARMv7-A supports hardware FPU, Thumb-2, and NEON instructions. You can target either or both of the instruction sets — ARMv5TE is the default, but switching to ARMv7-A is as easy as adding a single line to the application's Application.mk
file, without needing to change anything else in the file. You can also build for both architectures at the same time and have everything stored in the final .apk
. Complete information is provided in the CPU-ARCH-ABIS.HTML in the NDK package.
ARMv5TE机器代码会在所有以ARM为基础的Android设备上运行。ARMv7-A只能在Verizon Droid和Google Nexus One上运行。这两种指令集的主要区别是ARMv7-A支持hardware FPU, Thumb-2, and NEON instructions。你可以面向这两种指令或者其中之一------缺省支持ARMv5TE,但是可以通过在程序的Appliation.mk中加上一行代码就切换到ARMv7-A。
The NDK provides stable headers for libc (the C library), libm (the Math library), OpenGL ES (3D graphics library), the JNI interface, and other libraries, as listed in the Development Tools section.
When to Develop in Native Code
The NDK will not benefit most applications. As a developer, you need to balance its benefits against its drawbacks; notably, using native code does not result in an automatic performance increase, but always increases application complexity. In general, you should only use native code if it is essential to your application, not just because you prefer to program in C/C++.
NDK对多数程序并没有好处。作为开发者,你需要权衡利弊:使用native code不能自动改进性能,但是多数情况下会增加程序的复杂度。通常,你应该只在它对你非常重要时使用,而不是你习惯于用C++开发。
Typical good candidates for the NDK are self-contained, CPU-intensive operations that don't allocate much memory, such as signal processing, physics simulation, and so on. Simply re-coding a method to run in C usually does not result in a large performance increase. When examining whether or not you should develop in native code, think about your requirements and see if the Android framework APIs provide the functionality that you need. The NDK can, however, can be an effective way to reuse a large corpus of existing C/C++ code.
通常适用于NDK的是这样一些程序:self-contained, CPU密集操作但是不申请很多内存。比如signal processing,physics simulation。简单地把一个方法用C改写并不会产生很显著的性能提升。当权衡是否用native code开发的时候,考虑一下你的需求,看看Android framework API是否已经提供了相应的功能。不过,NDK可能是一种重用大量已有C++代码的有效的途径。
The Android framework provides two ways to use native code:
Android framework提供了两种使用native code的方式:
- Write your application using the Android framework and use JNI to access the APIs provided by the Android NDK. This technique allows you to take advantage of the convenience of the Android framework, but still allows you to write native code when necessary. You can install applications that use native code through the JNI on devices that run Android 1.5 or later.
- 用Android framework写你的程序,使用JNI访问Android NDK提供的API。这种办法允许你享受Android framework带来的便利,但是仍允许你在必要的时候写native code。你可以在Android 1.5以上的设备上安装这类程序。
- Write a native activity, which allows you to implement the lifecycle callbacks in native code. The Android SDK provides the
NativeActivity
class, which is a convenience class that notifies your native code of any activity lifecycle callbacks (onCreate()
,onPause()
,onResume()
, etc). You can implement the callbacks in your native code to handle these events when they occur. Applications that use native activities must be run on Android 2.3 (API Level 9) or later. - 写一个natvie 的activity,which allows you to implement the lifecycle callbacks in native code. Android SDK提供了类 NativeActivity,which is a convenience class that notifies your native code of any activity lifecycle callbacks (
onCreate()
,onPause()
,onResume()
, etc). 你可以在你的native code中实现这些回调来处理事件。(适用于Android 2.3以后版本)
You cannot access features such as Services and Content Providers natively, so if you want to use them or any other framework API, you can still write JNI code to do so.
你不能用native的方式访问Service和Content Provider之类的功能。so if you want to use them or any other framework API, you can still write JNI code to do so.
Contents of the NDK
The NDK contains the APIs, documentation, and sample applications that help you write your native code.Development tools
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.
NDK还提供了build系统。你创建一个简短的build文件,描述哪个源文件要编译,哪个Android应用要使用它们。buld系统会编译源文件并把shared libraries直接嵌入你的程序工程。
Important: With the exception of the libraries listed above, native system libraries in the Android platform are not stable and may change in future platform versions. Your applications should only make use of the stable native system libraries provided in this NDK. (上述列表以外的native库未来可能会发生变化,建议使用这些)
Documentation
The NDK package includes a set of documentation that describes the capabilities of the NDK and how to use it to create shared libraries for your Android applications. In this release, the documentation is provided only in the downloadable NDK package. You can find the documentation in the<ndk>/docs/
directory. Included are these files:
NDK包含有一系列文档,它们描述了NDK的能力,如何为你的Android程序创建shared libraries。目前,文档仅在可以下载的NDK package,你可以在 <ndk>/docs中找到,包括以下文件
- INSTALL.HTML — describes how to install the NDK and configure it for your host system
- 如何安装NDK,如何在你的host system上配置
- OVERVIEW.HTML — provides an overview of the NDK capabilities and usage
- NDK 能力和使用的概览
- ANDROID-MK.HTML — describes the use of the Android.mk file, which defines the native sources you want to compile
- Android.mk文件的使用,它定义了你要编译的native source
- APPLICATION-MK.HTML — describes the use of the Application.mk file, which describes the native sources required by your Android application
- 如何使用Application.mk文件
- CPLUSPLUS-SUPPORT.HTML — describes the C++ support provided in the Android NDK
- NDK支持的C++
- CPU-ARCH-ABIS.HTML — a description of supported CPU architectures and how to target them.
- 支持的CPU架构以及如何target它们
- CPU-FEATURES.HTML — a description of the
cpufeatures
static library that lets your application code detect the target device's CPU family and the optional features at runtime. - cpufeatures 静态库描述。可以让程序在运行时检测目标设备的CPU家族和optional features
- CPU-ARM-NEON.HTML — a description of how to build with optional ARM NEON / VFPv3-D32 instructions.
- 如何使用optional ARM NEON/VFPv3-D32 指令编译
- CHANGES.HTML — a complete list of changes to the NDK across all releases.
- 变更记录
- DEVELOPMENT.HTML — describes how to modify the NDK and generate release packages for it
- 如何修改NDK并产生release package
- HOWTO.HTML — information about common tasks associated with NDK development
- NDK通常的任务
- IMPORT-MODULE.HTML — describes how to share and reuse modules
- 如何share和reuse模块
- LICENSES.HTML — information about the various open source licenses that govern the Android NDK
- 许可
- NATIVE-ACTIVITY.HTML — describes how to implement native activities
- 如何实现native activities
- NDK-BUILD.HTML — describes the usage of the ndk-build script
- ndk-build脚本的使用
- NDK-GDB.HTML — describes how to use the native code debugger
- 如何使用native code debugger
- PREBUILTS.HTML — information about how shared and static prebuilt libraries work
- shared和statuic prebuilit libraries如何工作
- STANDALONE-TOOLCHAIN.HTML — describes how to use Android NDK toolchain as a standalone compiler (still in beta).
- SYSTEM-ISSUES.HTML — known issues in the Android system images that you should be aware of, if you are developing using the NDK.
- 已知问题
- STABLE-APIS.HTML — a complete list of the stable APIs exposed by headers in the NDK.
- NDK中稳定的API
Additionally, the package includes detailed information about the "bionic" C library provided with the Android platform that you should be aware of, if you are developing using the NDK. You can find the documentation in the <ndk>/docs/system/libc/
directory:
- OVERVIEW.HTML — provides an overview of the "bionic" C library and the features it offers.