ndk-build 脚本
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
ndk-build
脚本使用 NDK 的基于 Make 的构建系统构建项目。我们针对 ndk-build
使用的 Android.mk 和 Application.mk 配置提供了更具体的文档。
内部原理
运行 ndk-build
脚本相当于运行以下命令:
$GNUMAKE -f <ndk>/build/core/build-local.mk
<parameters>
$GNUMAKE
指向 GNU Make 3.81 或更高版本,<ndk>
则指向 NDK 安装目录。您可以根据这些信息从其他 Shell 脚本(甚至是您自己的 Make 文件)中调用 ndk-build。
从命令行调用
ndk-build
脚本位于 NDK 安装目录顶层。如需从命令行运行该脚本,请在应用项目目录或其子目录中进行调用。例如:
$ cd <project>
$ <ndk>/ndk-build
在此示例中,<project>
指向项目的根目录,<ndk>
则是您安装 NDK 的目录。
选项
ndk-build 的所有参数都将直接传递到运行 NDK 构建脚本的底层 GNU make
命令。请将 ndk-build
和表单 ndk-build <option>
中的选项结合使用。例如:
$ ndk-build clean
您可以使用以下选项:
-
clean
-
移除之前生成的所有二进制文件。
注意:在 Mac OS X 上,运行 ndk-build clean
时若有大量并行执行,可能会导致出现包含以下消息的构建错误:
rm: fts_read: No such file or directory
为了避免出现此问题,请考虑不使用 -jN
修饰符,或为 N
选择较小的值(例如 2)。
-
V=1
-
启动构建,并显示构建命令。
-
-B
-
强制执行完整的重新构建。
-
-B V=1
-
强制执行完整的重新构建,并显示构建命令。
-
NDK_LOG=1
-
显示内部 NDK 日志消息(用于调试 NDK 本身)。
-
NDK_DEBUG=1
-
强制执行可调试 build(详见表 1)。
-
NDK_DEBUG=0
-
强制执行发布 build(详见表 1)。
-
NDK_HOST_32BIT=1
-
始终使用 32 位模式下的工具链。
-
NDK_APPLICATION_MK=<file>
-
使用
NDK_APPLICATION_MK
变量指向的特定 Application.mk
文件进行构建。
-
-C <project>
-
构建位于
<project>
的项目路径的原生代码。如果您不想在终端通过 cd
切换到该路径,此选项会非常有用。
可调试 build 与发布 build
使用 NDK_DEBUG
选项(在特定情况下结合 AndroidManifest.xml
)指定调试 build 或发布 build、与优化相关的行为以及是否包含符号。表 1 显示了每个可能的设置组合的结果。
表 1. NDK_DEBUG
(命令行)和 android:debuggable
(清单)组合的结果。
清单设置 | NDK_DEBUG=0 | NDK_DEBUG=1 | 未指定 NDK_DEBUG
|
android:debuggable="true" | 调试;符号;已优化*1
| 调试;符号;未优化*2 | (与 NDK_DEBUG=1 相同)
|
android:debuggable="false" | 发布;符号;已优化
| 发布;符号;未优化 | 发布;无符号;已优化*3 |
*1:有助于进行剖析。
*2:用于运行 ndk-gdb
的默认值。
*3:默认模式。
注意:“NDK_DEBUG=0”相当于“APP_OPTIM=release”,并用“-O2”编译。“NDK_DEBUG=1”相当于“Application.mk”中的“APP_OPTIM=debug”,并用“-O0”编译。如需详细了解“APP_OPTIM”,请参阅 Application.mk。
例如,命令行上的语法如下:
$ ndk-build NDK_DEBUG=1
要求
一般来说,您需要使用 GNU Make 4 才能使用 ndk-build 或 NDK。NDK 包含其自己的 GNU Make 副本,除非您将 $GNUMAKE
环境变量设置为指向不适当的 Make,否则 NDK 将使用该副本。
JSON 编译数据库
在 NDK r18 及更高版本中,ndk-build 可以生成 JSON 编译数据库。
您可以使用 ndk-build compile_commands.json
生成数据库而不构建代码,也可以使用 ndk-build GEN_COMPILE_COMMANDS_DB=true
来构建代码,同时生成数据库。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# The ndk-build script\n\nThe `ndk-build` script builds projects that use the NDK's Make-based build\nsystem. There is more specific documentation for the\n[Android.mk](/ndk/guides/android_mk) and\n[Application.mk](/ndk/guides/application_mk) configuration used\nby `ndk-build`.\n\nInternals\n---------\n\nRunning the `ndk-build` script is equivalent to running the following command: \n\n```\n$GNUMAKE -f \u003cndk\u003e/build/core/build-local.mk\n\u003cparameters\u003e\n```\n\n`$GNUMAKE` points to GNU Make 3.81 or later, and\n`\u003cndk\u003e` points to your NDK installation directory. You can use\nthis information to invoke ndk-build from other shell scripts, or even your own\nmake files.\n\nInvoke from the command line\n----------------------------\n\nThe `ndk-build` script lives in the top level NDK installation directory. To run it\nfrom the command line, invoke it while in or under your application project directory.\nFor example: \n\n```\n$ cd \u003cproject\u003e\n$ \u003cndk\u003e/ndk-build\n```\n\nIn this example, `\u003cproject\u003e` points to your\nproject's root directory, and `\u003cndk\u003e` is the directory where\nyou installed the NDK.\n\n### Options\n\nAll parameters to ndk-build are passed directly to the underlying GNU `make`\ncommand that runs the NDK build scripts. Combine `ndk-build` and\noptions in the form `ndk-build \u003coption\u003e`. For example: \n\n```\n$ ndk-build clean\n```\n\n\nThe following options are available:\n\n\n`clean`\n\n: Remove any previously generated binaries.\n **Note:** On Mac OS X, running `ndk-build clean` with a\n high number of [parallel executions](https://www.gnu.org/software/make/manual/html_node/Parallel.html) may result in a build error that\n includes the following message:\n\n ```\n rm: fts_read: No such file or directory\n ```\n\n\n To avoid this issue, consider not using the `-j`\u003cvar translate=\"no\"\u003eN\u003c/var\u003e\n modifier or selecting a smaller value for \u003cvar translate=\"no\"\u003eN\u003c/var\u003e, such as 2.\n\n\n`V=1`\n:\n Launch build, and display build commands.\n\n\n`-B`\n:\n Force a complete rebuild.\n\n\n`-B V=1`\n:\n Force a complete rebuild, and display build commands.\n\n\n`NDK_LOG=1`\n:\n Display internal NDK log messages (used for debugging the NDK itself).\n\n\n`NDK_DEBUG=1`\n:\n Force a debuggable build (see [table 1](#dvr)).\n\n\n`NDK_DEBUG=0`\n:\n Force a release build (see [table 1](#dvr)).\n\n\n`NDK_HOST_32BIT=1`\n:\n Always use the toolchain in 32-bit mode.\n\n\n`NDK_APPLICATION_MK=\u003cfile\u003e`\n:\n Build, using a specific `Application.mk` file pointed to by the\n `NDK_APPLICATION_MK` variable.\n\n\n`-C \u003cproject\u003e`\n:\n Build the native code for the project path located at\n `\u003cproject\u003e`. Useful if you don't want to `cd`\n to it in your terminal.\n\n### Debuggable versus release builds\n\nUse the `NDK_DEBUG` option and, in certain cases,\n`AndroidManifest.xml` to specify debug or release build,\noptimization-related behavior, and inclusion of symbols. Table 1 shows the\nresults of each possible combination of settings.\n\n**Table 1.** Results of `NDK_DEBUG` (command line) and\n`android:debuggable` (manifest) combinations.\n\n| Manifest Setting | NDK_DEBUG=0 | NDK_DEBUG=1 | NDK_DEBUG not specified |\n|----------------------------|------------------------------|----------------------------------|-----------------------------------|\n| android:debuggable=\"true\" | Debug; Symbols; Optimized\\*1 | Debug; Symbols; Not optimized\\*2 | (same as NDK_DEBUG=1) |\n| android:debuggable=\"false\" | Release; Symbols; Optimized | Release; Symbols; Not optimized | Release; No symbols; Optimized\\*3 |\n\n\\*1: Useful for profiling. \n\\*2: Default for running [`ndk-gdb`](/ndk/guides/ndk-gdb). \n\\*3: Default mode. \n\n**Note:** \\`NDK_DEBUG=0\\` is the equivalent of\n\\`APP_OPTIM=release\\`, and compiles with \\`-O2\\`. \\`NDK_DEBUG=1\\` is the equivalent of\n\\`APP_OPTIM=debug\\` in \\`Application.mk\\`, and compiles with \\`-O0\\`.\nFor more information about \\`APP_OPTIM\\`, see\n[Application.mk](/ndk/guides/application_mk).\n\nThe syntax on the command line is, for example: \n\n```\n$ ndk-build NDK_DEBUG=1\n```\n\nRequirements\n------------\n\nYou need GNU Make 4 to use ndk-build or the NDK in general. The NDK includes its\nown copy of GNU Make and will use that unless you've set the `$GNUMAKE`\nenvironment variable to point to an unsuitable make.\n\nJSON compilation databases\n--------------------------\n\nIn NDK r18 and newer, ndk-build can generate a\n[JSON compilation database](https://clang.llvm.org/docs/JSONCompilationDatabase.html).\n\nYou can either use `ndk-build compile_commands.json` to generate the database\nwithout building your code, or `ndk-build GEN_COMPILE_COMMANDS_DB=true` if you\nwant to build and generate the database as a side-effect."]]