NDK_OVERVIEW翻译
- Android NDK Overview
- Introduction:
- The Android NDK is a set of tools that allows Android application developers
- to embed native machine code compiled from C and/or C++ source files into
- their application packages.
NDK为程序员提供了一套工具集,帮助程序员将从C/C++源码编译出的库内嵌到安卓应用包中。- IMPORTANT:
- The Android NDK can only be used to target Android system images
- running Cupcake (a.k.a 1.5) or later versions of the platform.
NDK只能被用于1.5版本的安卓系统中。- 1.0 and 1.1 system images are specifically *not* supported due to
- subtle ABI and toolchain changes that happened for the 1.5 release.
- 由于从1.5开始,ABI和toolchain有了轻微的改变,因此不能用于1.0和1.1版本的平台。
- I. Android NDK Goals:
- ---------------------
- The Android VM allows your application's source code to call methods
- implemented in native code through the JNI. In a nutshell, this means that:
- 安卓虚拟机允许应用源码通过jni调用本地代码的方法。这意味着:
- Your application's source code will declare one or more methods- with the 'native' keyword to indicate that they are implemented through
- native code. E.g.:
- 应用源码需通过native关键字来声明一个或者多个本地方法。比如- native byte[] loadFile(String filePath);
- - You must provide a native shared library that contains the
- implementation of these methods, which will be packaged into your
- application's .apk. This library must be named according to standard
- Unix conventions as lib<something>.so, and shall contain a standard JNI
- entry point (more on this later). For example:
- 程序员必须提供一个共享库,库中包含着本地方法的实现,这个库会最终打包到应用的apk包中。库名必须遵守unix的传统命名规范,如libname.so,而且库还要
包含一个标准的jni入口。比如:- libFileLoader.so
- - Your application must explicitly load the library. For example, to load
- it at application start-up, simply add the following to its source code:
- 应用显示加载库。例如,在应用启动时加载库,只需按照以下的方法即可:- static {
- System.loadLibrary("FileLoader");
- }
- Note that you should not use the 'lib' prefix and '.so' suffix here.
记住,代码中显示加载库时,不能写上lib前缀以及so后缀。- The Android NDK is a complement to the Android SDK that helps you to:
- NDK是SDK的补充,NDK可以帮助程序员:
- - Generate JNI-compatible shared libraries that can run on the Android
- 1.5 platform (and later) running on ARM CPUs.
- 产生于jni兼容的共享库,这个库能够运行在1.5之后的版本平台上。当然,平台是要运行在ARM CPU上。- - Copy the generated shared libraries to a proper location of your
- application project path, so they will be automatically added to your
- final (and signed) .apks
- 将产生的共享库复制到应用项目合适的路径,这样这些库将会会自动地添加到apk中。- - In later revisions of the NDK, we intend to provide tools that help
- debug your native code through a remote gdb connection and as much
- source/symbol information as possible.
- 在NDK后续版本中,谷歌官方将会提供工具,来帮助程序员通过远程gdb来调试本地代码。- Moreover, the Android NDK provides:
更多的,NDK还提供了:- - A set of cross-toolchains (compilers, linkers, etc..) that can
- generate native ARM binaries on Linux, OS X and Windows (with Cygwin)
- 一套跨平台的工具集cross-toolchains,包括编译器,链接器等,这些工具将会在Linux,OS X以及cygwin上产生能够在ARM架构上运行的程序。- - A set of system headers corresponding to the list of stable native APIs
- supported by the Android platform. This corresponds to definitions that
- are guaranteed to be supported in all later releases of the platform.
- 一套系统头文件,这些头文件对应着安卓平台支持的本地API。这些头文件在后续的安卓平台版本中都会被支持。
They are documented in the file docs/STABLE-APIS.html
这些系统头文件更多信息可以查阅docs/STABLE-APIS.html- IMPORTANT:
- Keep in mind that most of the native system libraries in Android system
- images are not frozen and might changed drastically, or even deleted,
- in later updates and releases of the platform.
记住,本地系统库将会在后续平台版本中被修改。即,系统头文件会被后续安卓平台支持,但是实现库会改变。- - A build system that allow developers to only write very short build files
- to describe which sources need to be compiled, and how. The build system
- deals with all the hairy toolchain/platform/CPU/ABI specifics. Moreover,
- later updates of the NDK can add support for more toolchains, platforms,
- system interfaces without requiring changes in the developer's build
- files (more on this later).
- 一个编译系统,允许程序员只需写出很短的编译文件,其中指明哪些源码需要编译以及如何编译即可。编译系统会自动完成很多事情。而且,后续的NDK版本会加入更多的工具集,平台
等,而不需要程序员去修改编译文件。- II. Android NDK Non-Goals:
- --------------------------
- The NDK is *not* a good way to write generic native code that runs on Android
- devices. In particular, your applications should still be written in the Java
- programming language, handle Android system events appropriately to avoid the
- "Application Not Responding" dialog or deal with the Android application
- life-cycle.
NDK可不是写本地代码,然后它运行在设备上的好方法。也就是,应用还是尽量使用java语言编写,应用也要合理的处理系统事件,避免出现ANR,应用还要合理的处理安卓应用生命周期。- Note however that is is possible to write a sophisticated application in
- native code with a small "application wrapper" used to start/stop it
- appropriately.
记住,用本地代码编写复杂的应用,然后再用application wrapper包装?
A good understanding of JNI is highly recommended, since many operations- in this environment require specific actions from the developers, that are
- not necessarily common in typical native code. These include:
建议先了解下jni,有一些事情需要注意,包括:- - Not being able to directly access the content of VM objects through
- direct native pointers. E.g. you cannot safely get a pointer to a
- String object's 16-bit char array to iterate over it in a loop.
- 不要通过本地指针去直接访问虚拟机对象的内容。比如,你获取了一个指针,用它指向一个字符串数组对象,再遍历它,这是不安全的。
- Requiring explicit reference management when the native code wants to- keep handles to VM objects between JNI calls.
- 当本地代码需要在jni调用中保持对虚拟机对象的引用,需要显示的引用管理。- The NDK only provides system headers for a very limited set of native
- APIs and libraries supported by the Android platform. While a typical
- Android system image includes many native shared libraries, these should
- be considered an implementation detail that might change drastically between
- updates and releases of the platform.
NDK提供的系统头文件,是针对于安卓平台很有限的本地API和库。当一个典型的安卓系统包括很多的共享库,应当注意,在后续平台的更新中,这些库的实现也会可能改变。
If an Android system library is not explicitly supported by the NDK- headers, then applications should not depend on it being available, or
- they risk breaking after the next over-the-air system update on various
- devices.
Selected system libraries will gradually be added to the set of stable NDK- APIs.
- 未翻译。
- III. NDK development in practice:
- ---------------------------------
- Here's a very rough overview of how you can develop native code with the
- Android NDK:
这部分展示了如何通过NDK开发本地代码。- 1/ Place your native sources under $PROJECT/jni/...
- 2/ Write $PROJECT/jni/Android.mk to describe your sources
- to the NDK build system
- 3/ Optional: write $PROJECT/jni/Application.mk to describe your
- project in more details to the build system. You don't need
- one to get started though, but this allows you to target
- more than one CPU or override compiler/linker flags
- (see docs/APPLICATION-MK.html for all details).
- 4/ Build your native code by running "$NDK/ndk-build" from your
- project directory, or any of its sub-directories.
- The last step will copy, in case of success, the stripped shared libraries
- your application needs to your application's root project directory. You
- will then need to generate your final .apk through the usual means.
- 未翻译
- Now, for a few more details:
- III.1/ Configuring the NDK:
- - - - - - - - - - - - - - -
- Previous releases required that you run the 'build/host-setup.sh'
- script to configure your NDK. This step has been removed completely
- in release 4 (a.k.a. NDK r4).
之前的版本需要程序员运行'build/host-setup.sh'脚本,在版本4中已经被完全删除了。
III.2/ Placing C and C++ sources:- - - - - - - - - - - - - - - - - -
- Place your native sources under the following directory:
- $PROJECT/jni/
- Where $PROJECT corresponds to the path of your Android application
- project.
- You are pretty free to organize the content of 'jni' as you want,
- the directory names and structure here will not influence the final
- generated application packages, so you don't have to use pseudo-unique
- names like com.<mycompany>.<myproject> as is the case for application
- package names.
- Note that C and C++ sources are supported. The default C++ file extensions
- supported by the NDK is '.cpp', but other extensions can be handled as well
- (see docs/ANDROID-MK.html for details).
NDK支持的C++的后缀名是cpp,但是其他的后缀名也能支持。- It is possible to store your sources in a different location by adjusting
- your Android.mk file (see below).
C/C++源码也可以放在其他的地方,不过要在android.mk中注明。- III.3/ Writing an Android.mk build script:
- - - - - - - - - - - - - - - - - - - - - - -
- An Android.mk file is a small build script that you write to describe your
- sources to the NDK build system. Its syntax is described in details in
- the file docs/ANDROID-MK.html.
一个Android.mk是一个编译脚本,在这里程序员可以编写编译选项。语法可以参考相关文档。
In a nutshell, the NDK groups your sources into "modules", where each module- can be one of the following:
在shell中,NDK将源码组成一个模块,每一个模块可以是以下两种中的一种:- - a static library
- - a shared library
- You can define several modules in a single Android.mk, or you can write
- several Android.mk files, each one defining a single module.
可以再Android.mk中定义几个模块,或者编写几个Android.mk,每一个定义一个单独的模块。- Note that a single Android.mk might be parsed several times by the build
- system so don't assume that certain variables are not defined in them.
- By default, the NDK will look for the following build script:
- 记住,一个单独的Android.mk可能会被编译系统解析N次。默认情况下,编译系统会查找以下编译脚本:
$PROJECT/jni/Android.mk- If you want to define Android.mk files in sub-directories, you should
- include them explicitly in your top-level Android.mk. There is even
- a helper function to do that, i.e. use:
如果想要在子目录中定义Android.mk,那么在顶级目录的Android.mk中需要显示的声明他们。这个还可以通过一个帮助功能来做到,比如:- include $(call all-subdir-makefiles)
- This will include all Android.mk files in sub-directories of the current
- build file's path.
这个就包括了当前编译路径的所有子路径的所有Android.mk文件。- III.4/ Writing an Application.mk build file (optional):
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- While an Android.mk file describes your modules to the build system, the
- Application.mk file describes your application itself. See the
- docs/APPLICATION-MK.html document to understand what this file allows you
- to do. This includes, among others:
- - The exact list of modules required by your application.
- - The CPU architecture(s) to generate machine code for.
- - Optional information, like whether you want a release or debug
- build, specific C or C++ compiler flags and others that should
- apply to all modules being built.
- This file is optional: by default the NDK will provide one that simply
- builds *all* the modules listed from your Android.mk (and all the makefiles
- it includes) and target the default CPU ABI (armeabi).
- There are two ways to use an Application.mk:
- - Place it under $PROJECT/jni/Application.mk, and it will be picked
- up automatically by the 'ndk-build' script (more on this later)
- - Place it under $NDK/apps/<name>/Application.mk, where $NDK
- points to your NDK installation path. After that, launch
- "make APP=<name>" from the NDK directory.
- This was the way this file was used before Android NDK r4.
- It is still supported for compatibility reasons, but we strongly
- encourage you to use the first method instead, since it is much
- simpler and doesn't need modifying / changing directories of the
- NDK installation tree.
- Again, see docs/APPLICATION-MK.html for a complete description of its
- content.
- 未翻译
III.5/ Invoke the NDK build system:- - - - - - - - - - - - - - - - - - -
- The preferred way to build machine code with the NDK is to use the
- 'ndk-build' script introduced with Android NDK r4. You can also use
- a second, legacy, method that depends on creating a '$NDK/apps' subdirectory.
一种编译本地代码的建议方法是调用'ndk-build脚本。
In both cases, a successful build will copy the final stripped binary modules- (i.e. shared libraries) required by your application to your application's
- project path (Note that unstripped versions are kept for debugging
- purposes, there is no need to copy unstripped binaries to a device).
编译成功后,会将生成的库放到项目中合适的位置。- 1: Using the 'ndk-build' command:
- ---------------------------------
- The 'ndk-build' script, located at the top of the NDK installation path
- can be invoked directly from your application project directory (i.e. the
- one where your AndroidManifest.xml is located) or any of its sub-directories.
- For example:
- 'ndk-build'可以在项目路径中调用,也可以在项目路径的子路径中调用。
- cd $PROJECT
- $NDK/ndk-build
- This will launch the NDK build scripts, which will automatically probe your
- development system and application project file to determine what to build.
- 'ndk-build'脚本会自动探测开发系统和应用项目文件,已决定什么东西需要编译。
For example:- ndk-build
- ndk-build clean --> clean generated binaries
- ndk-build -B V=1 --> force complete rebuild, showing commands
- By default, it expects an optional file under $PROJECT/jni/Application.mk,
- and a required $PROJECT/jni/Android.mk.
默认地,'ndk-build'会期待在jni目录下有一个可选的Application.mk以及一个必须的Android.mk文件。- On success, this will copy the generated binary modules (i.e. shared
- libraries) to the appropriate location in your project tree. You can later
- rebuild the full Android application package either through the usual
- 'ant' command, or the ADT Eclipse plug-in.
- See docs/NDK-BUILD.html for a more complete description of what this script
- does and which options it can take.
- III.6/ Specifying custom output directories:
- - - - - - - - - - - - - - - - - - - - - - - -
- By default, ndk-build places all intermediate generated files under
- $PROJECT/obj. You can however select a different location by defining
- the NDK_OUT environment variable to point to a different directory.
- When defined, this variable has two effects:
- 1/ It ensures that all files that normally go under $PROJECT/obj are
- stored in $NDK_OUT instead
- 2/ It tells ndk-gdb to look into $NDK_OUT, instead of $PROJECT/obj for
- any symbolized (i.e. unstripped) versions of the generated binaries.
- IV. Rebuild your application package:
- - - - - - - - - - - - - - - - - - - -
- After generating the binaries with the NDK, you need to rebuild your
- Android application package files (.apk) using the normal means, i.e.
- either using the 'ant' command or the ADT Eclipse plug-in.
- See the Android SDK documentation for more details. The new .apk will
- embed your shared libraries, and they will be extracted automatically
- at installation time by the system when you install the package on a
- target device.
- V. Debugging support:
- - - - - - - - - - - -
- The NDK provides a helper script, named 'ndk-gdb' to very easily launch
- a native debugging session of your applications.
- Native debugging can *ONLY* be performed on production devices running
- Android 2.2 or higher, and does not require root or privileged access, as
- long as your application is debuggable.
- For more information, read docs/NDK-GDB.html. In a nutshell, native debugging
- follows this simple scheme:
- 1. Ensure your application is debuggable (e.g. set android:debuggable
- to "true" in your AndroidManifest.xml)
- 2. Build your application with 'ndk-build', then install it on your
- device/emulator.
- 3. Launch your application.
- 4. Run 'ndk-gdb' from your application project directory.
- You will get a gdb prompt. See the GDB User Manual for a list of useful
- commands.
NDK_OVERVIEW翻译的更多相关文章
- 《Django By Example》第五章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者@ucag注:大家好,我是新来的翻译, ...
- 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...
- [翻译]开发文档:android Bitmap的高效使用
内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...
- 【探索】机器指令翻译成 JavaScript
前言 前些时候研究脚本混淆时,打算先学一些「程序流程」相关的概念.为了不因太枯燥而放弃,决定想一个有趣的案例,可以边探索边学. 于是想了一个话题:尝试将机器指令 1:1 翻译 成 JavaScript ...
- 《Django By Example》第三章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第三章滚烫出炉,大家请不要吐槽文中 ...
- 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...
- 《Django By Example》第一章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:本人目前在杭州某家互联网公司工作, ...
- 【翻译】Awesome R资源大全中文版来了,全球最火的R工具包一网打尽,超过300+工具,还在等什么?
0.前言 虽然很早就知道R被微软收购,也很早知道R在统计分析处理方面很强大,开始一直没有行动过...直到 直到12月初在微软技术大会,看到我软的工程师演示R的使用,我就震惊了,然后最近在网上到处了解和 ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点
在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...
随机推荐
- Docker笔记03-docker 网络模式
docker网络模式分为5种 Nat (Network Address Translation) Host other container none overlay 第一种 Nat模式 docker的 ...
- 了解BroadcastRecever
广播分类: 标准广播(Normal broadcasts):完全异步执行的广播,接收没有先后顺序,效率高,无法被接收器被拦截. 有序广播(Ordered broadcasts) :同步执行的广播,有先 ...
- delphi程序向另一个可执行程序发消息(使用GetForegroundWindow; 找出当前操作系统中活动的第一个窗口)
function FindWindowThroughWindowText(WindowText: string): THandle;var hCurrentWindow: THandle; cnt ...
- Windows完成端口编程
Windows完成端口编程目录一 基本概念二 OVERLAPPED数据结构三 完成端口的内部机制创建完成端口完成端口线程的工作原理线程间数据传递线程的安全退出 一 基本概念 设备---wi ...
- 深入浅出RPC——深入篇(转载)
本文转载自这里是原文 <深入篇>我们主要围绕 RPC 的功能目标和实现考量去展开,一个基本的 RPC 框架应该提供什么功能,满足什么要求以及如何去实现它? RPC 功能目标 RPC的主要功 ...
- GetLastError()返回值列表(3259个错误列表)
GetLastError()返回值列表: [0]-操作成功完成. [1]-功能错误. [2]-系统找不到指定的文件. [3]-系统找不到指定的路径. [4]-系统无法打开文件. [5]-拒绝访问. [ ...
- Using VNC on a debian/Ubuntu server with a OS X Mac
I got a brand new MacBook Pro 13" 2016. I used to work on GNU/Linux for decades. I don't want t ...
- Yolov3代码分析与训练自己数据集
现在要针对我们需求引入检测模型,只检测人物,然后是图像能侧立,这样人物在里面占比更多,也更清晰,也不需要检测人占比小的情况,如下是针对这个需求,用的yolov3-tiny模型训练后的效果. Yolov ...
- AcWing 164. 可达性统计
给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量. 输入格式 第一行两个整数N,M,接下来M行每行两个整数x,y,表示从x到y的一条有向边. 输出格式 输出共N行,表示每个点能 ...
- 高并发 Nginx+Lua OpenResty系列(2)——Nginx Lua API
Nginx Lua API 和一般的Web Server类似,我们需要接收请求.处理并输出响应.而对于请求我们需要获取如请求参数.请求头.Body体等信息:而对于处理就是调用相应的Lua代码即可:输出 ...