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.
Để biên dịch và gỡ lỗi mã gốc cho ứng dụng, bạn cần có các thành phần sau:
Native Development Kit (NDK) cho Android: một bộ công cụ cho phép bạn
sử dụng mã C và C++ với Android.
CMake: một công cụ bản dựng bên ngoài hoạt động cùng với Gradle để tạo thư viện gốc. Nếu bạn chỉ định sử dụng bản dựng ndk thì không cần đến thành phần này.
LLDB: Trình gỡ lỗi mà Android Studio sử dụng để gỡ lỗi mã gốc. Theo mặc định, LLDB sẽ được cài đặt cùng với Android Studio.
Trang này mô tả cách cài đặt các thành phần này tự động hoặc bằng cách sử dụng
Android Studio hoặc công cụ sdkmanager để tải các thành phần đó theo cách thủ công.
Cài đặt NDK và CMake tự động
Android Gradle Plugin (Trình bổ trợ Android cho Gradle) 4.2.0 trở lên có thể tự động cài đặt thành phần NDK và CMake cần thiết vào lần đầu tiên bạn tạo dự án nếu giấy phép của các thành phần này đã được chấp nhận trước.
Nếu đã đọc và đồng ý với các điều khoản cấp phép, thì bạn có thể chấp nhận trước
các giấy phép trong tập lệnh bằng lệnh sau:
Khi bạn cài đặt NDK, Android Studio sẽ chọn phiên bản NDK mới nhất hiện có. Với
hầu hết các dự án, chỉ cần cài đặt phiên bản NDK mặc định này là đủ.
Tuy nhiên, nếu dự án của bạn cần một hoặc nhiều phiên bản NDK cụ thể, bạn có thể tải và định cấu hình các phiên bản cụ thể. Việc này giúp bạn đảm bảo các bản dựng có thể tái tạo trên các dự án mà mỗi dự án phải phụ thuộc vào một phiên bản NDK cụ thể Android Studio sẽ cài đặt tất cả các phiên bản của NDK trong thư mục
android-sdk/ndk/.
Để cài đặt CMake và NDK mặc định trong Android Studio, hãy làm như sau:
Khi một dự án đang mở, hãy nhấp vào Tools (Công cụ) > SDK Manager (Trình quản lý SDK).
Nhấp vào thẻ SDK Tools (Bộ công cụ SDK).
Đánh dấu vào các ô NDK (Side by side) và CMake.
Hình 1. Cửa sổ SDK Tools (Công cụ SDK) hiển thị tuỳ chọn NDK (Side by side) (NDK (Song song))
Nhấp vào OK.
Một hộp thoại sẽ thông báo cho bạn mức dung lượng mà gói NDK đã tiêu thụ trên ổ đĩa.
Nhấp vào OK.
Khi quá trình cài đặt hoàn tất, hãy nhấp vào Hoàn tất (Finish).
Dự án của bạn sẽ tự động đồng bộ hóa tệp bản dựng và thực hiện một bản dựng.
Giải quyết mọi lỗi xảy ra.
Định cấu hình một phiên bản CMake cụ thể
Trình quản lý SDK bao gồm phiên bản phát triển nhánh 3.6.0 của CMake và phiên bản 3.10.2. Các dự án không đặt phiên bản CMake cụ thể sẽ được dựng bằng CMake 3.10.2. Để đặt phiên bản CMake, hãy thêm nội dung sau vào tệp build.gradle của mô-đun:
Nếu bạn muốn sử dụng một phiên bản CMake không có trong Trình quản lý SDK, hãy thực hiện theo các bước sau:
Tải và cài đặt CMake từ trang web chính thức của CMake.
Chỉ định phiên bản CMake mà bạn muốn Gradle sử dụng trong tệp
build.gradle của mô-đun.
Thêm đường dẫn đến tệp cài đặt CMake vào biến môi trường PATH
hoặc đưa đường dẫn vào tệp local.properties của dự án, như minh hoạ dưới đây. Nếu Gradle không tìm thấy phiên bản CMake mà bạn đã chỉ định trong
tệp build.gradle thì bạn sẽ gặp lỗi bản dựng.
# If you set this property, Gradle no longer uses PATH to find CMake.
cmake.dir = "path-to-cmake"</pre>
Nếu bạn chưa cài đặt hệ thống xây dựng Ninja trên máy trạm, hãy truy cập trang web chính thức của Ninja, rồi tải xuống và cài đặt phiên bản Ninja mới nhất có sẵn cho hệ điều hành của bạn.
Đừng quên thêm đường dẫn đến tệp cài đặt Ninja vào biến môi trường PATH.
Cài đặt một phiên bản NDK cụ thể
Để cài đặt một phiên bản NDK cụ thể, hãy làm như sau:
Khi một dự án đang mở, hãy nhấp vào Tools (Công cụ) > SDK Manager (Trình quản lý SDK).
Nhấp vào thẻ SDK Tools (Bộ công cụ SDK).
Đánh dấu vào ô Show Package Details (Hiển thị chi tiết gói).
Đánh dấu vào ô NDK (Side by side) và các ô bên dưới tương ứng với các phiên bản NDK mà bạn muốn cài đặt. Android Studio sẽ cài đặt
tất cả phiên bản của NDK vào thư mục android-sdk/ndk/.
Hình 2. Cửa sổ SDK Tools (Công cụ SDK) hiển thị tuỳ chọn NDK (Side by side) (NDK (Song song))
Nhấp vào OK.
Một hộp thoại sẽ thông báo cho bạn mức dung lượng mà (các) gói NDK đã tiêu thụ.
Nhấp vào OK.
Khi quá trình cài đặt hoàn tất, hãy nhấp vào Hoàn tất (Finish).
Dự án của bạn sẽ tự động đồng bộ hóa tệp bản dựng và thực hiện một bản dựng.
Giải quyết mọi lỗi xảy ra.
Định cấu hình mỗi mô-đun với phiên bản NDK
bạn muốn sử dụng. Khi sử dụng Android Studio phiên bản 3.6 trở lên, nếu bạn không
chỉ định phiên bản thì trình bổ trợ Android cho Gradle sẽ chọn một phiên bản tương thích đã biết.
Định cấu hình các phiên bản NDK cụ thể trong dự án của bạn
Bạn có thể phải định cấu hình phiên bản NDK trong dự án của mình nếu một trong các điều kiện sau
xảy ra:
Dự án của bạn là dự án kế thừa và bạn cần phải sử dụng các phiên bản NDK
và trình bổ trợ Android cho Gradle (AGP) cụ thể. Để biết thêm thông tin, hãy xem bài viết Định cấu hình NDK cho trình bổ trợ Android cho Gradle.
Bạn đã cài đặt nhiều phiên bản NDK và muốn sử dụng một phiên bản
cụ thể. Trong trường hợp này, hãy chỉ định phiên bản bằng cách sử dụng thuộc tính android.ndkVersion
trong tệp build.gradle của mô-đun, như minh họa trong mã mẫu
sau đây.
Trước khi phát hành, mỗi phiên bản AGP đều được thử nghiệm kỹ lưỡng bằng bản phát hành NDK ổn định mới nhất tại thời điểm đó. Phiên bản NDK này được dùng để tạo dự án của bạn nếu bạn không chỉ định phiên bản NDK trong tệp build.gradle. Phiên bản NDK mặc định cho các phiên bản AGP khác nhau được ghi lại trong ghi chú phát hành AGP và ghi chú phát hành AGP trước đây.
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-08-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-08-27 UTC."],[],[],null,["To compile and debug native code for your app, you need the following components:\n\n- The Android Native Development Kit (NDK): a set of tools that allows you to use C and C++ code with Android.\n- CMake: an external build tool that works alongside Gradle to build your native library. You do not need this component if you only plan to use ndk-build.\n- LLDB: the debugger Android Studio uses to debug native code. By default, LLDB will be installed alongside Android Studio.\n\nThis page describes how to install these components automatically, or by using\nAndroid Studio or the `sdkmanager` tool to download and install them manually.\n\nInstall NDK and CMake automatically\n\nAndroid Gradle Plugin 4.2.0+ can automatically install the required NDK and CMake\nthe first time you build your project if their licenses have been accepted in advance.\nIf you've already read and agree to the license terms, then you can pre-accept\nthe licenses in scripts with the following command: \n\n yes | ${sdk}/cmdline-tools/latest/bin/sdkmanager --licenses\n\nInstall the NDK and CMake\n\nWhen you install the NDK, Android Studio selects the latest available NDK. For\nmost projects, installing this default version of the NDK is sufficient.\nIf your project needs one or more specific versions of the NDK, though, you can\n[download and configure specific versions](#specific-version). Doing so helps\nyou ensure reproducible builds across projects that each depend on a specific\nversion of the NDK. Android Studio installs all versions of the NDK in the\n\u003cvar translate=\"no\"\u003eandroid-sdk\u003c/var\u003e`/ndk/` directory.\n\nTo install CMake and the default NDK in Android Studio, do the following:\n\n1. With a project open, click **Tools \\\u003e SDK Manager**.\n\n2. Click the **SDK Tools** tab.\n\n3. Select the **NDK (Side by side)** and **CMake** checkboxes.\n\n\n **Figure 1.** The **SDK Tools** window showing the **NDK (Side by side)**\n option\n | **Note:** If you have an NDK installed in the `ndk-bundle` folder, it appears in the list with the label **NDK** . If you are using Android Gradle plugin 3.5.0 or later, you can select this checkbox or clear it. Clearing the checkbox uninstalls the NDK, freeing up disk space, and causes the checkbox to disappear from the list. If you uninstall the legacy NDK, remove the `ndk.dir` value, which is now deprecated, from your projects' `local.properties` files.\n4. Click **OK**.\n\n A dialog box tells you how much space the NDK package consumes on disk.\n5. Click **OK**.\n\n6. When the installation is complete, click **Finish**.\n\n7. Your project automatically syncs the build file and performs a build.\n Resolve any errors that occur.\n\nConfigure a specific version of CMake\n\nThe SDK Manager includes the 3.6.0 forked version of\nCMake and version 3.10.2. Projects that don't set a\nspecific CMake version are built with CMake 3.10.2. To\nset the CMake version, add the following to your module's `build.gradle` file: \n\nGroovy \n\n```groovy\nandroid {\n ...\n externalNativeBuild {\n cmake {\n ...\n version \"\u003cvar translate=\"no\"\u003ecmake-version\u003c/var\u003e\"\n }\n }\n}\n```\n\nKotlin \n\n```kotlin\nandroid {\n ...\n externalNativeBuild {\n cmake {\n ...\n version = \"\u003cvar translate=\"no\"\u003ecmake-version\u003c/var\u003e\"\n }\n }\n}\n```\n\nIf you want to use a CMake version that is not included by the\nSDK Manager, follow these steps:\n\n1. Download and install [CMake](https://cmake.org/download/) from the official CMake website.\n2. Specify the CMake version you want Gradle to use in your module's `build.gradle` file.\n3. Either add the path to the CMake installation to your `PATH` environment\n variable or include it in your project's `local.properties` file, as\n shown. If Gradle is unable to find the version of CMake you specified in\n your `build.gradle` file, you get a build error.\n\n # If you set this property, Gradle no longer uses PATH to find CMake.\n cmake.dir = \"\u003cvar translate=\"no\"\u003epath-to-cmake\u003c/var\u003e\"\u003c/pre\u003e\n\n4. If you don't already have the Ninja build system installed on your\n workstation, go to the [official Ninja website](https://ninja-build.org/),\n and download and install the latest version of Ninja available for your OS.\n Make sure to also add the path to the Ninja installation to your\n `PATH` environment variable.\n\nInstall a specific version of the NDK\n\nTo install a specific version of the NDK, do the following:\n\n1. With a project open, click **Tools \\\u003e SDK Manager**.\n\n2. Click the **SDK Tools** tab.\n\n3. Select the **Show Package Details** checkbox.\n\n4. Select the **NDK (Side by side)** checkbox and the checkboxes below it that\n correspond to the NDK versions you want to install. Android Studio installs\n all versions of the NDK in the \u003cvar translate=\"no\"\u003eandroid-sdk\u003c/var\u003e`/ndk/`\n directory.\n\n | **Note:** Preview releases (for example, canary and beta) of the NDK do not show up in this list unless you [change the update channel](/studio/preview/install-preview#change_your_update_channel) for Android Studio. You can [install an Android Studio preview](/studio/preview/install-preview) side-by-side with the stable version.\n\n\n **Figure 2.** The **SDK Tools** window showing the **NDK (Side by side)**\n options\n | **Note:** If you have an NDK installed in the `ndk-bundle` folder, it appears in the list with the label **NDK** . If you are using Gradle version 3.5 or later, you can select this checkbox or clear it. Clearing it uninstalls the NDK installed, freeing up disk space, and cause the checkbox to disappear from the list. If you uninstall the legacy NDK, remove the `ndk.dir` value, which is now deprecated, from your projects' `local.properties` files.\n5. Click **OK**.\n\n A dialog box tells you how much space the NDK package(s) consumes.\n6. Click **OK**.\n\n7. When the installation is complete, click **Finish**.\n\n8. Your project automatically syncs the build file and performs a build.\n Resolve any errors that occur.\n\n9. [Configure each module](#apply-specific-version) with the version of the NDK\n you want it to use. When using Android Studio 3.6 or higher, if you do not\n specify the version, the Android Gradle plugin chooses a version that it is\n known to be compatible with.\n\nConfigure specific versions of the NDK in your project\n\nYou may need to configure the version of the NDK in your project if one of the\nfollowing is true:\n\n- Your project is inherited and you need to use specific versions of the NDK and the Android Gradle plugin (AGP). For more information, see [Configure the NDK for the Android Gradle plugin](/studio/projects/configure-agp-ndk).\n- You have multiple versions of the NDK installed and you want to use a specific\n one. In this case, specify the version using the `android.ndkVersion`\n property in the module's `build.gradle` file, as shown in the following code\n sample.\n\n Groovy \n\n ```groovy\n android {\n ndkVersion \"\u003cvar translate=\"no\"\u003emajor\u003c/var\u003e.\u003cvar translate=\"no\"\u003eminor\u003c/var\u003e.\u003cvar translate=\"no\"\u003ebuild\u003c/var\u003e\" // e.g., ndkVersion \"21.3.6528147\"\n }\n ```\n\n Kotlin \n\n ```kotlin\n android {\n ndkVersion = \"\u003cvar translate=\"no\"\u003emajor\u003c/var\u003e.\u003cvar translate=\"no\"\u003eminor\u003c/var\u003e.\u003cvar translate=\"no\"\u003ebuild\u003c/var\u003e\" // e.g., ndkVersion \"21.3.6528147\"\n }\n ```\n\nDefault NDK version per AGP version\n\nBefore release, each AGP version is thoroughly tested with the latest stable NDK\nrelease at that time. This NDK version is used to build your projects if you\ndon't specify an NDK version in the `build.gradle` file. The default NDK version\nfor different versions of AGP are documented in the\n[AGP release notes](/build/releases/gradle-plugin#compatibility) and\n[AGP past release notes](/build/releases/past-releases)."]]