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 安裝目錄。您可以使用這項資訊從其他殼層指令碼 (或甚至從自己的 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
-
強制執行可進行偵錯的建構 (參閱表 1)。
-
NDK_DEBUG=0
-
強制執行發布子版本 (參閱表 1)。
-
NDK_HOST_32BIT=1
-
一律使用 32 位元模式的工具鏈。
-
NDK_APPLICATION_MK=<file>
-
使用
NDK_APPLICATION_MK
變數指向的特定 Application.mk
檔案進行建構。
-
-C <project>
-
建構位於
<project>
的專案路徑的原生程式碼。如果不想在終端機透過 cd
切換到這個路徑,這項功能將非常實用。
可進行偵錯與發布子版本
使用 NDK_DEBUG
選項 (在特定情況下搭配使用 AndroidManifest.xml
) 來指定偵錯子版本或發布子版本、最佳化相關行為以及是否包含符號。表 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_CLICKIM=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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間: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."]]