What is the NDK?

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'sApplication.mkfile, 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 theDevelopment Toolssection.

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 theNativeActivityclass, 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 arenotstable and may change in future platform versions. Your applications shouldonlymake 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 thecpufeaturesstatic 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:

package里还有相关C库的详细说明

  • OVERVIEW.HTML — provides an overview of the "bionic" C library and the features it offers.

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.
  • 从shared library 里 native method 获取一个字符串,显示在程序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.
  • 动态加载shared library,调用其中的方法。在这个例子里,方法在一个statice library中实现,从shared library导入。
  • san-angeles— a simple application that renders 3D graphics through the native OpenGL ES APIs, while managing activity lifecycle with aGLSurfaceViewobject.
  • 使用natvie OpenGL ES APIs render 3D图形。
  • 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 thecpufeatureslibrary 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.
  • CPU相关
  • bitmap-plasma— a simple application that demonstrates how to access the pixel buffers of AndroidBitmapobjects from native code, and uses this to generate an old-school "plasma" effect.
  • Bitmap相关
  • native-activity— a simple application that demonstrates how to use the native-app-glue static library to create a native activity
  • 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/.

对于每个sample,NDK里提供了相应的C源代码和必要的Android.mk和Application.mk文件。它们位于<ndk>/samples/<name>/,源代码位于<ndk>/samples/<name>/jni/

You can build the shared libraries for the sample apps by going into<ndk>/samples/<name>/then calling thendk-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).

你可以用以下方式编译shared libraries。<ndk>/samples/<name>/后调用ndk-build命令来。
对于ARMv5TE machine code,生成的shared libraries位于<ndk>/samples/<name>/libs/armeabi/,对于ARMv7 machine code,位于<ndk>/samples/<name>/libs/armeabi-v7a/

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

接下来,编译使用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>/apps/<app_name>/project/. Then, set up an AVD, if necessary, and build/run the application in the emulator.
  • 如果你使用Eclipse+ADT,使用向导为每个sample创建一个新的Android工程,使用“Import from Existing Source”选项,从导入。然后,如果有必要的话再安装一个AVD。之后在模拟器中运行
  • If you are developing with Ant, use theandroidtool to create the build file for each of the sample projects at<ndk>/apps/<app_name>/project/. Then set up an AVD, if necessary, build your project in the usual way, and run it in the emulator.
  • 如果是ANT

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

Exploring the hello-jni Sample

The hello-jni sample is a simple demonstration on how to use JNI from an Android application. The HelloJni activity receives a string from a simple C function and displays it in a TextView.

hello-jni sample展示了Android程序如何使用JNI。HelloJni activity 接收了C函数返回的字符串并显示在TextView中。

The main components of the sample include:

sample的主要组件包括:

  • The familiar basic structure of an Android application (anAndroidManifest.xmlfile, asrc/andresdirectories, and a main activity)
  • 基本的Android程序的组成部分,包括AndroidManifest.xml,一个src和res文件夹,一个主activity
  • Ajni/directory that includes the implemented source file for the native code as well as the Android.mk file
  • 一个jni文件夹,含有实现native code的源代码和Android.mk
  • Atests/directory that contains unit test code.
  • 一个test文件夹,包含unit test code
  1. Create a new project in Eclipse from the existing sample source or use theandroidtool to update the project so it generates a build.xml file that you can use to build the sample.
    • In Eclipse:
      1. ClickFile > New Android Project...
      2. Select theCreate project from existing sourceradio button.
      3. Select any API level above Android 1.5.
      4. In theLocationfield, clickBrowse...and select the<ndk-root>/samples/hello-jnidirectory.
      5. ClickFinish.
    • On the command line:
      1. Change to the<ndk-root>/samples/hello-jnidirectory.
      2. Run the following command to generate a build.xml file:
        android update project -p . -s
  2. Compile the native code using thendk-buildcommand.
    cd <ndk-root>/samples/hello-jni
    <ndk_root>/ndk-build
    
  3. Build and install the application as you would a normal Android application. If you are using Eclipse, run the application to build and install it on a device. If you are using Ant, run the following commands from the project directory:
    ant debug
    adb install bin/HelloJni-debug.apk
    

When you run the application on the device, the stringHello JNIshould appear on your device. You can explore the rest of the samples that are located in the<ndk-root>/samplesdirectory for more examples on how to use the JNI.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值