Tập lệnh ndk-build
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tập lệnh ndk-build
tạo dự án dùng hệ thống xây dựng dựa trên Make của NDK. Chúng tôi có tài liệu cụ thể hơn cho cấu hình Android.mk và Application.mk mà ndk-build
sử dụng.
Nội bộ
Việc chạy tập lệnh ndk-build
tương đương với chạy lệnh sau:
$GNUMAKE -f <ndk>/build/core/build-local.mk
<parameters>
$GNUMAKE
trỏ đến GNU Make 3.81 trở lên và <ndk>
trỏ tới thư mục cài đặt NDK. Bạn có thể sử dụng thông tin này để gọi ndk-build từ các tập lệnh shell khác hoặc thậm chí là tệp tạo (make) của riêng bạn.
Gọi từ dòng lệnh
Tập lệnh ndk-build
nằm trong thư mục cài đặt NDK cấp cao nhất. Để chạy tập lệnh đó từ dòng lệnh, hãy gọi tập lệnh khi đang ở trong thư mục dự án ứng dụng.
Ví dụ:
$ cd <project>
$ <ndk>/ndk-build
Trong ví dụ này, <project>
trỏ đến thư mục gốc của dự án, còn <ndk>
là thư mục mà bạn đã cài đặt NDK.
Tuỳ chọn
Tất cả tham số gửi đến ndk-build sẽ được truyền trực tiếp đến lệnh make
GNU cơ bản chạy các tập lệnh bản dựng NDK. Kết hợp ndk-build
và các tuỳ chọn trong biểu mẫu ndk-build <option>
. Ví dụ:
$ ndk-build clean
Hiện có các tuỳ chọn sau:
-
clean
- Xoá mọi tệp nhị phân đã tạo trước đó.
Lưu ý: Trên Mac OS X, việc chạy ndk-build clean
có số lượt thực thi song song ở mức cao có thể dẫn đến lỗi bản dựng bao gồm thông báo sau:
rm: fts_read: No such file or directory
Để tránh vấn đề này, hãy cân nhắc không sử dụng công cụ sửa đổi -jN
hoặc chọn một giá trị nhỏ hơn cho N
, chẳng hạn như 2.
-
V=1
-
Chạy bản dựng và hiển thị lệnh tạo.
-
-B
-
Buộc tạo lại toàn bộ.
-
-B V=1
-
Buộc tạo lại toàn bộ và hiển thị lệnh tạo.
-
NDK_LOG=1
-
Hiển thị thông điệp nhật ký NDK nội bộ (dùng để gỡ lỗi cho chính NDK).
-
NDK_DEBUG=1
-
Buộc một bản dựng có thể gỡ lỗi (xem bảng 1).
-
NDK_DEBUG=0
-
Buộc một bản phát hành (xem bảng 1).
-
NDK_HOST_32BIT=1
-
Luôn sử dụng chuỗi công cụ ở chế độ 32 bit.
-
NDK_APPLICATION_MK=<file>
-
Tạo bằng cách sử dụng một tệp
Application.mk
cụ thể được biến NDK_APPLICATION_MK
trỏ đến.
-
-C <project>
-
Tạo mã gốc cho đường dẫn dự án tại
<project>
. Hữu ích nếu bạn không muốn cd
sang vị trí đó trong thiết bị đầu cuối của mình.
Bản dựng có thể gỡ lỗi và bản phát hành
Sử dụng tuỳ chọn NDK_DEBUG
và AndroidManifest.xml
trong một số trường hợp nhất định để chỉ định bản gỡ lỗi hoặc bản phát hành, hành vi liên quan đến tối ưu hoá và thêm ký hiệu. Bảng 1 cho thấy kết quả của mỗi tổ hợp chế độ cài đặt có thể áp dụng.
Bảng 1. Kết quả cho tổ hợp NDK_DEBUG
(dòng lệnh) và android:debuggable
(tệp kê khai).
Cài đặt tệp kê khai | NDK_DEBUG=0 | NDK_DEBUG=1 | NDK_DEBUG chưa được chỉ định
|
android:debuggable="true" | Gỡ lỗi; Ký hiệu; Được tối ưu hoá*1
| Gỡ lỗi; Ký hiệu; Không được tối ưu hoá*2 | (tương tự như NDK_DEBUG=1)
|
android:debuggable="false" | Bản phát hành; Ký hiệu; Được tối ưu hoá
| Bản phát hành; Ký hiệu; Không được tối ưu hoá | Bản phát hành; Không có ký hiệu; Được tối ưu hoá*3 |
*1: Hữu ích khi lập cấu hình.
*2: Mặc định để chạy ndk-gdb
.
*3: Chế độ mặc định.
Lưu ý: `NDK_DEBUG=0` là giá trị tương đương của
`APP_OPTIM=release` và biên dịch bằng `-O2`. `NDK_DEBUG=1` là giá trị tương đương của
`APP_OPTIM=debug` trong `Application.mk` và biên dịch bằng `-O0`.
Để biết thêm thông tin về `APP_OPTIM`, hãy xem
Application.mk.
Ví dụ về cú pháp trên dòng lệnh như sau:
$ ndk-build NDK_DEBUG=1
Yêu cầu
Bạn cần GNU Make 4 để sử dụng ndk-build hoặc NDK nói chung. NDK đi kèm bản sao GNU Make riêng và sẽ sử dụng bản sao đó trừ khi bạn đã đặt biến môi trường $GNUMAKE
để trỏ đến một công cụ tạo (make) không phù hợp.
Cơ sở dữ liệu biên dịch JSON
Trong NDK r18 trở lên, ndk-build có thể tạo cơ sở dữ liệu biên dịch JSON.
Bạn có thể sử dụng ndk-build compile_commands.json
để tạo cơ sở dữ liệu mà không cần tạo mã hoặc sử dụng ndk-build GEN_COMPILE_COMMANDS_DB=true
nếu muốn xây dựng và tạo cơ sở dữ liệu dưới dạng hiệu ứng phụ.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],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."]]