Android NDK之一:什么是NDK?

来源:互联网 发布:怎么样创造小软件 编辑:程序博客网 时间:2024/06/16 20:11

What is the NDK?


The Android NDK is a toolset that lets you embed components that make use of native code in your Android applications. 

Android NDK是一个工具集,它让我们能够在Android应用程序中使用由本地代码(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让我们可以将应用程序中的某些部分用本地代码(native code)语言(例如C和C++)来实现,这样会对部分应用程序有益处,例如可以代码重用,提高运行速度等。


The NDK provides:

NDK工具集提供的内容包括:

· A set of tools and build files used to generate native code libraries from C and C++ sources

用于将C和C++源代码生成本地代码库(native code libraries)的一套工具和编译文件(build files)


· A way to embed the corresponding native libraries into an application package file (.apk) that can be deployed on Android devices

将相应的本地库(native libraries)嵌入进应用程序包文件(apk)的方法


· 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.

一套本地系统头文件和库,这些头文件和库从Android 1.5版本开始,及其后续所有版本的Android平台中都会支持。使用native activities的应用程序则必须运行在Android 2.3及其以后的版本上。


· Documentation, samples, and tutorials

各类文档,实例程序及各类指导材料。


The latest release of the NDK supports the following instruction sets:

最新release的NDK支持如下的指令集:

· ARMv5TE, including Thumb-1 instructions (see docs/CPU-ARCH-ABIS.html for more information)

· ARMv7-A, including Thumb-2 and VFPv3-D16 instructions, with optional support for NEON/VFPv3-D32 instructions (see docs/CPU-ARM-NEON.html for more information)

· x86 instructions (see docs/CPU-X86.html for more information)

· MIPS instructions (see docs/CPU-MIPS.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等这类拥有兼容CPU(compatible CPU)的设备上运行。这两种指令集之间的主要差别是ARMv7-A支持硬件FPU,Thumb-2和NEON指令。ARMv5TE是默认的指令集,要切换到ARMv7-A也很容易,只需要在应用程序的Application.mk文件中添加一行语句,文件中的其它内容则都不需要修改。我们也可以同时使用这两种指令集,并且都存放进最终的apk文件中。完整的信息请参考NDK包中的CPU-ARCH-ABIS.HTML文件。


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.

NDK为libc,libm,OpenGL ES,JNI接口和其它的一些库提供了稳定的头文件。(全部的库请参考Development Tools章节)


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收益。作为一个开发者,我们需要平衡它能带给我们的好处和它所带来的问题,尤其是,使用本地代码不一定会带来性能上的提升,但是一定会增加程序的复杂度。通常情况下,只有在对应用程序十分必要的情况才应该使用本地代码,而不是仅仅因为喜欢用C或者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-intensive的操作,不需要申请大量的内存,例如信号处理,物理模拟(physics simulation)等。简单的使用C语言重新编码一个函数通常不会带来性能上的提升。在分析是否应该使用本地代码进行开发时,需要考虑我们的要求以及是否Android framework APIs已经提供了我们需要的功能。然而,要复用大量C/C++代码,NDK是一种高效的方式。


The Android framework provides two ways to use native code:

Android framework提供了两种方式使用本地代码:

使用Android framework编写应用程序,然后使用JNI访问Android NDK提供的API。这种方式可以利用Android framework的方便,而且需要的时候依然可以编写本地代码。

编写native activity,这样可以在本地代码中实现生命周期回调函数。Android SDK提供了NativeActivity类。

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. If you use this approach, your application must target specific, minimum Android platform levels, see Android platform compatibility for more information.

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.


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.

不能够在本地代码中使用Service和Content Provider等特性,如果要使用这些特性或者其它任何framework的API,可以编写JNI完成。


Contents of the NDK

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.

NDK包含一套交叉工具链,能够生成运行在Linux,OS X和Windows平台上的本地ARM二进制文件(native ARM binaries)。


It provides a set of system headers for stable native APIs that are guaranteed to be supported in all later releases of the platform:

一套系统头文件,头文件中包含稳定的本地APIs,这些本地APIs在以后的release中都会支持:

· 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提供的编译系统让我们可以高效的编译源码,不需要去处理工具链、平台等细节。我们只需要创建很短的编译文件,描述哪些源文件要编译以及哪些应用程序会使用它们,编译系统就会编译源代码并且将编译生成的共享库(shared libraries)直接放到我们的应用程序中。


Important: With the exception of the libraries listed above, native system libraries in the Android platform are notstable and may change in future platform versions. Your applications should only make use of the stable native system libraries provided in this NDK.

除去上面所列的库外,其它的本地系统库是不稳定的,在以后的版本中可能会改变,因此我们的应用程序应该避免使用不稳定的库,而只使用NDK中提供的稳定的本地系统库。


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 (partial listing):

NDK包包含了一套文档,这些文档描述了NDK的功能,以及如何使用它为Android应用程序创建共享库(shared libraries)。在这次release中,这些文档只在下载的NDK包中提供,可以在docs目录下查看,包括如下的文件:

· INSTALL.HTML — describes how to install the NDK and configure it for your host system

· OVERVIEW.HTML — provides an overview of the NDK capabilities and usage

· ANDROID-MK.HTML — describes the use of the Android.mk file, which defines the native sources you want to compile

描述Android.mk文件的使用,Android.mk文件定义要编译的本地源文件


· APPLICATION-MK.HTML — describes the use of the Application.mk file, which describes the native sources required by your Android application

· CPLUSPLUS-SUPPORT.HTML — describes the C++ support provided in the Android NDK

描述Android NDK对C++的支持


· CPU-ARCH-ABIS.HTML — a description of supported CPU architectures and how to target them.

描述了支持的CPU架构

· 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.

描述了cpufeature静态库,它可以让我们的应用程序代码在运行时探测目标设备的CPU family和其它的特性


· 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

· HOWTO.HTML — information about common tasks associated with NDK development

· IMPORT-MODULE.HTML — describes how to share and reuse modules

· 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-GDB.HTML — describes how to use the native code debugger

· PREBUILTS.HTML — information about how shared and static prebuilt libraries work

共享库和静态库如何工作方面的信息


· 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.

提供了“bionic”的概述,以及它所提供的特性


Sample applications

The NDK includes sample applications that illustrate how to use native code in your Android applications:

· hello-jni — a simple application that loads a string from a native method implemented in a shared library and then displays it in the application UI.

· two-libs — a simple application that loads a shared library dynamically and calls a native method provided by the library. In this case, the method is implemented in a static library imported by the shared library.

· san-angeles — a simple application that renders 3D graphics through the native OpenGL ES APIs, while managing activity lifecycle with a GLSurfaceView object.

· hello-gl2 — a simple application that renders a triangle using OpenGL ES 2.0 vertex and fragment shaders.

· hello-neon — a simple application that shows how to use the cpufeatures library to check CPU capabilities at runtime, then use NEON intrinsics if supported by the CPU. Specifically, the application implements two versions of a tiny benchmark for a FIR filter loop, a C version and a NEON-optimized version for devices that support it.

· bitmap-plasma — a simple application that demonstrates how to access the pixel buffers of Android Bitmap objects from native code, and uses this to generate an old-school "plasma" effect.

· native-activity — a simple application that demonstrates how to use the native-app-glue static library to create a native activity

· native-plasma — a version of bitmap-plasma implemented with a native activity.

For each sample, the NDK includes the corresponding C source code and the necessary Android.mk and Application.mk files. There are located under <ndk>/samples/<name>/ and their source code can be found under<ndk>/samples/<name>/jni/.

You can build the shared libraries for the sample apps by going into <ndk>/samples/<name>/ then calling the ndk-buildcommand. The generated shared libraries will be located under <ndk>/samples/<name>/libs/armeabi/ for (ARMv5TE machine code) and/or <ndk>/samples/<name>/libs/armeabi-v7a/ for (ARMv7 machine code).

Next, build the sample Android applications that use the shared libraries:

· If you are developing in Eclipse with ADT, use the New Project Wizard to create a new Android project for each sample, using the "Import from Existing Source" option and importing the source from <ndk>/samples/<name>/. Then, set up an AVD, if necessary, and build/run the application in the emulator.

· If you are developing with Ant, use the android tool to create the build file for each of the sample projects at<ndk>/samples/<name>/. Then set up an AVD, if necessary, build your project in the usual way, and run it in the emulator.

For more information about developing with the Android SDK tools and what you need to do to create, build, and run your applications, see the Overview section for developing on Android.

Exploring the native-activity Sample Application

The native-activity sample provided with the Android NDK demonstrates how to use the android_native_app_glue static library. This static library makes creating a native activity easier by providing you with an implementation that handles your callbacks in another thread, so you do not have to worry about them blocking your main UI thread. The main parts of the sample are described below:

native-activity实例展示了如何使用android_native_app_glue静态库


· The familiar basic structure of an Android application (an AndroidManifest.xml file, a src/ and res directories). The AndroidManifest.xml declares that the application is native and specifies the .so file of the native activity. SeeNativeActivity for the source or see the <ndk_root>/platforms/samples/native-activity/AndroidManifest.xmlfile.

· A jni/ directory contains the native activity, main.c, which uses the android_native_app_glue.h interface to implement the activity. The Android.mk that describes the native module to the build system also exists here.

原创粉丝点击