在native c code的project中使用ndk-build指令(前提是你在~/.bashrc文件中已配置好ndk环境变量),就会执行Android.mk文件编译程序。
在分析Android.mk的语法之前,先对ndk-build指令的参数,进行分析:
在一个build.sh文件中有一下的一段ndk-build编译指令,格式如下
eg:
### ---------------- Generic Build Command ----------------
# run NDK build
${NDK_BUILD}/ndk-build
-B
NDK_DEBUG=1
NDK_PROJECT_PATH=Locals/Code
NDK_APPLICATION_MK=Locals/Code/Application.mk
NDK_MODULE_PATH=${t_sdk_root}
NDK_APP_OUT=Out/_build
APP_BUILD_SCRIPT=Locals/Code/Android.mk
APP_OPTIM=$OPTIM
APP_ABI=$APP_ABI
一、使用方法:
The Android NDK r4 introduced a new tiny shell script, named 'ndk-build', to simplify building machine code.
Android NDK r4引入一个新的小型shell脚本,名字是ndk-build,以简化机器码的构建。
The script is located at the top-level directory of the NDK, and shall be invoked from the command-line when in your application project directory, or any of its sub-directories. For example:
脚本位于NDK的顶级目录,并且将在你的应用程序工程目录,或它的任意子目录中的命令行调用。例如:
cd $PROJECT
$NDK/ndk-build
Where $NDK points to your NDK installation path. You can also create an alias or add $NDK to your PATH to avoid typing it every time.
这里$NDK指向你的NDK安装目录。你还可以创建一个别名或者添加$NDK到你的PATH环境变量以避免每次都键入它(注:PATH区分大小写)。
II. Options:
二、选项:
All parameters to 'ndk-build' are passed directly to the underlying GNU Make command that runs the NDK build scripts. Notable uses include:
所有给ndk-build的参数被直接传递到运行NDK构建脚本的底层GNU Make命令。值得注意的使用方法包括:
ndk-build --> rebuild required machine code. ndk-build --> 重新构建所需的机器代码。
ndk-build clean --> clean all generated binaries. ndk-build clean --> 清除所有生成的二进制文件。
ndk-build NDK_DEBUG=1 --> generate debuggable native code. ndk-build NDK_DEBUG=1 --> 生产可调试的本地代码。
ndk-build V=1 --> launch build, displaying build commands. ndk-build V=1 --> 启动构建,显示构建命令。
ndk-build -B --> force a complete rebuild. ndk-build -B --> 强制完全重新构建。
ndk-build -B V=1 --> force a complete rebuild and display build commands. ndk-build -B V=1 --> 强制完全重新构建并且显示构建命令。
ndk-build NDK_LOG=1 --> display internal NDK log messages (used for debugging the NDK itself). ndk-build NDK_LOG=1 --> 显示内部NDK日志消息(用于调试NDK自身)。
ndk-build NDK_DEBUG=1 --> force a debuggable build (see below) ndk-build NDK_DEBUG=1 --> 强制调试版构建(见下)(注:调试版指带调试信息,可以用gdb调试的二进制文件)
ndk-build NDK_DEBUG=0 --> force a release build (see below) ndk-build NDK_DEBUG=0 --> 强制发布版构建(见下)(注:发布版指不带调试信息的二进制文件)
ndk-build NDK_APP_APPLICATION_MK=<file> --> rebuild, using a specific Application.mk pointed to by the NDK_APP_APPLICATION_MK command-line variable.
ndk-build NDK_APP_APPLICATION_MK=<文件名> --> 重新构建,通过NDK_APP_APPLICATION_MK命令行变量指向使用特定的Application.mk.
ndk-build -C <project> --> build the native code for the project path located at <project>. Useful if you don't want to 'cd' to it in your terminal.
ndk-build -C <工程目录> --> 构建位于<工程目录>的工程的本地代码。当你不想在终端上用cd切换到那个目录时有用。
III. Debuggable versus Release builds:
三、调试版和发布版构建:
--------------------------------------
In NDK r5, ndk-build has been modified to make it easier to switch between release and debug builds. This is done by using the NDK_DEBUG variable.
在NDK r5,ndk-build已经被修改成可以更容易地在发布版和调试版构建之间切换。通过使用NDK_DEBUG变量来做到。
For example:
例如:
$NDK/ndk-build NDK_DEBUG=1 => forces the generation of debug binaries
$NDK/ndk-build NDK_DEBUG=1 => 强制生成调试版二进制文件。
$NDK/ndk-build NDK_DEBUG=0 => forces the generation of release binaries
$NDK/ndk-build NDK_DEBUG=0 => 强制生成发布版二进制文件。
If you don't specify NDK_DEBUG, ndk-build will keep its default behaviour, which is to inspect the AndroidManifest.xml, if any, and see if its <application> element has android:debuggable="true".
如果你不指定NDK_DEBUG,ndk-build将保持它的默认行为,即检查AndroidManifest.xml,如果有,检查它的<application>元素是否有android:debuggable="true"属性。
IMPORTANT: If you use the build tools of SDK r8 (or higher), you won't need to touch your AndroidManifest.xml file at all!
重要事项:如果你使用SDK r8(或更高)的构建工具,你将完全不必修改你的AndroidManifest.xml文件!
That's because if you build a debug package (e.g. with "ant debug" or the corresponding option of the ADT plugin), the tool will automatically pick the native debug files generated with NDK_DEBUG=1.
那是因为如果你构建一个调试包(例如,使用“ant debug”或ADT插件相应选项),工具将自动选择用NDK_DEBUG=1生成原生调试版文件。(注:也就是说,Java代码为调试版,则JNI的C代码也自动切换为调试版)
Also, as a convenience, the release and debug object files generated by the NDK are now stored in different directories (e.g. obj/local/<abi>/objs and obj/local/<abi>/objs-debug). This avoids having to recompile all your sources when you switch between these two modes (even when you only modified one or two source files).
同样,方便起见,有NDK生成的发布版和调试版对象文件现在被存放在不同的目录中(例如obj/local/<abi>/objs和obj/local/<abi>/objs-debug)。(注:abi是应用二进制接口的缩写,这里指代某类交叉编译器,例如EABI,嵌入式应用二进制接口)。这避免当你在这两种模式之间切换时必须重新编译所有源代码(即便你仅仅修改一个或两个源文件)。(注:Makefile的策略导致,Makefile认为xxx.c一般只会生成xxx.o,也就是说调试版和发布版的.o不能共存,上面的方法类似Windows,使用不同的目录输出不同版本的.o,以避免覆盖)
IV. Requirements:
四、要求:
You need GNU Make 3.81 or later to use 'ndk-build' or the NDK in general. The build scripts will detect that you're using a non-compliant Make tool and will complain with an error message.
一般你需要GNU Make 3.81或更高以使用ndk-build或NDK(注:因为GNU的make有两种)。构建脚本将检测到你正在使用不兼容的Make工具并且用一个错误消息解释。
If you have GNU Make 3.81 installed, but that it is not launched by the default 'make' command, define GNUMAKE in your environment to point to it before launching 'ndk-build'. For example:
如果你已经安装GNU Make 3.81以上,但不能用默认的make命令启动,可以在运行ndk-build之前在你的环境变量中定义GNUMAKE指向它的路径。例如:
GNUMAKE=/usr/local/bin/gmake ndk-build (注:环境变量的键值对出现在命令之前,常用于临时变更环境变量,等效于下面的export命令)
Or to make the change more permanent:
或更永久地改变:
export GNUMAKE=/usr/local/bin/gmake
ndk-build
Adapt to your shell and GNU Make 3.81 installation location.
请适配你的shell和GNU Make 3.81的安装位置。
V. Internals:
五、内幕:
'ndk-build' itself is a tiny wrapper around GNU Make, its purpose is simply to invoke the right NDK build script, it is equivalent to;
ndk-build本身是对GNU Make的一个小型封装,它的目的是简化正确NDK构建脚本调用,它等效于:
$GNUMAKE -f $NDK/build/core/build-local.mk [parameters]
Where '$GNUMAKE' points to GNU Make 3.81 or later, and $NDK points to your NDK installation directory.
这里$GNUMAKE指向GNU Make 3.81或更新,而$NDK指向你的NDK安装目录。
Use this knowledge if you want to invoke the NDK build script from other shell scripts (or even your own Makefiles).
如果你想从其它shell脚本中调用NDK构建脚本(或者甚至是你自己的Makefile文件)(注:Makefile是make的默认配置文件,类似于Ant的build.xml)时可以使用这种技巧。