Làm quen với NDK
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.
Bộ phát triển gốc (NDK) là bộ công cụ hỗ trợ bạn sử dụng mã C và C++ với Android, đồng thời cung cấp các thư viện nền tảng mà bạn có thể dùng để quản lý hoạt động gốc và truy cập vào các thành phần của thiết bị thực tế (chẳng hạn như cảm biến và nhập bằng cách chạm). NDK có thể không phù hợp với hầu hết lập trình viên Android chưa có kinh nghiệm chỉ cần sử dụng mã Java và API khung để phát triển ứng dụng. Tuy nhiên, NDK có thể hữu ích cho các trường hợp bạn cần thực hiện một hoặc nhiều việc sau:
- Khai thác thêm hiệu suất của thiết bị để đạt được độ trễ thấp hoặc chạy các ứng dụng nặng về điện toán, chẳng hạn như trò chơi hoặc chương trình mô phỏng vật lý.
- Sử dụng lại thư viện C hoặc C++ của riêng bạn hoặc của nhà phát triển khác.
Khi sử dụng Android Studio 2.2 trở lên, bạn có thể dùng NDK để biên dịch mã C và C++ vào thư viện gốc và đóng gói mã vào APK bằng Gradle, hệ thống xây dựng tích hợp của IDE. Khi đó, mã Java của bạn có thể gọi các hàm trong thư viện gốc thông qua khung Giao diện gốc Java (JNI). Để tìm hiểu thêm về Gradle và hệ thống xây dựng Android, hãy đọc bài viết Định cấu hình bản dựng.
Công cụ xây dựng mặc định của Android Studio để biên dịch thư viện gốc là CMake. Android Studio cũng hỗ trợ ndk-build do hiện có nhiều dự án sử dụng bộ công cụ xây dựng đó. Tuy nhiên, nếu đang tạo một thư viện gốc mới thì bạn nên sử dụng CMake.
Hướng dẫn này cung cấp thông tin mà bạn cần để thiết lập và chạy bằng NDK trên Android Studio. Nếu bạn không có phiên bản Android Studio mới nhất, hãy tải xuống và cài đặt ngay.
Người dùng Gradle thử nghiệm cần chú ý: Cân nhắc việc di chuyển sang trình bổ trợ phiên bản 2.2.0 trở lên và sử dụng CMake hoặc ndk-build để xây dựng thư viện gốc nếu bạn thuộc một trong các trường hợp sau: Dự án gốc của bạn đã sử dụng CMake hoặc ndk-build; bạn muốn sử dụng phiên bản ổn định của hệ thống xây dựng Gradle; hoặc bạn muốn được hỗ trợ cho các công cụ tiện ích bổ sung, chẳng hạn như CCache.
Nếu không, bạn có thể tiếp tục sử dụng phiên bản thử nghiệm của Gradle và Plugin Android.
Tải NDK và các công cụ xuống
Để 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:
- Bộ phát triển gốc (NDK) của Android: một bộ công cụ cho phép bạn sử dụng mã C và mã C++ với Android.
- CMake: một công cụ xây dựng bên ngoài hoạt động cùng Gradle để xây dựng thư viện gốc. Nếu bạn chỉ định sử dụng ndk-build 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.
Để biết thông tin về cách cài đặt các thành phần này, hãy xem Cài đặt và định cấu hình NDK và CMake.
Tạo hoặc nhập dự án gốc
Sau khi thiết lập Android Studio, bạn có thể chỉ cần Tạo dự án mới có hỗ trợ C/C++. Tuy nhiên, nếu muốn thêm hoặc nhập mã gốc vào một dự án Android Studio hiện có, bạn cần thực hiện theo quy trình cơ bản sau:
-
Tạo các tệp nguồn gốc mới và thêm các tệp đó vào dự án Android Studio.
- Bạn có thể bỏ qua bước này nếu đã có mã gốc hoặc muốn nhập một thư viện gốc tạo sẵn.
-
Tạo tập lệnh bản dựng CMake để hướng dẫn CMake cách tạo các nguồn gốc cho một thư viện. Bạn cũng cần có tập lệnh bản dựng này nếu đang nhập và liên kết dựa trên các thư viện nền tảng hoặc tạo sẵn.
- Bạn có thể bỏ qua bước này nếu thư viện gốc hiện có đã có tập lệnh bản dựng
CMakeLists.txt
hoặc sử dụng ndk-build và bao gồm một tập lệnh bản dựng Android.mk
.
-
Liên kết Gradle với thư viện gốc bằng cách cung cấp một đường dẫn đến tệp tập lệnh CMake hoặc ndk-build. Gradle sẽ sử dụng tập lệnh bản dựng để nhập mã nguồn vào dự án Android Studio và đóng gói thư viện gốc (tệp SO) vào APK.
Lưu ý: Nếu dự án hiện tại của bạn sử dụng công cụ ndkCompile
không còn dùng nữa, bạn nên mở tệp build.properties
và xoá dòng mã sau trước khi định cấu hình Gradle để sử dụng CMake hoặc ndk-build:
// Remove this line
android.useDeprecatedNdk = true
-
Tạo và chạy ứng dụng của bạn bằng cách nhấp vào Run (Chạy)
. Gradle sẽ thêm quy trình xây dựng CMake hoặc ndk-build như một phần phụ thuộc để biên dịch, xây dựng và đóng gói thư viện gốc bằng APK.
Một khi ứng dụng đang chạy trên thiết bị thực tế hoặc trình mô phỏng, bạn có thể sử dụng Android Studio để Gỡ lỗi ứng dụng. Nếu không, để tìm hiểu thêm về NDK và các thành phần của NDK, hãy đọc trang Khái niệm.
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-26 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-26 UTC."],[],[],null,["# Get started with the NDK\n\nThe Native Development Kit (NDK) is a set of tools that allows you to use C\nand C++ code with Android, and provides [platform libraries](/ndk/guides/stable_apis) you can use to manage\nnative activities and access physical device components, such as sensors and\ntouch input. The NDK may not be appropriate for most novice Android\nprogrammers who need to use only Java code and framework APIs to develop\ntheir apps. However, the NDK can be useful for cases in which you need to do\none or more of the following:\n\n- Squeeze extra performance out of a device to achieve low latency or run computationally intensive applications, such as games or physics simulations.\n- Reuse your own or other developers' C or C++ libraries.\n\n\nUsing [Android Studio 2.2 and higher](/studio), you can\nuse the NDK to compile C and C++ code into a native library and package it\ninto your APK using Gradle, the IDE's integrated build system. Your Java code\ncan then call functions in your native library through the [Java Native Interface (JNI)](http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html) framework. To learn\nmore about Gradle and the Android build system, read [Configure Your Build](/studio/build).\n\n\nAndroid Studio's default build tool to compile native libraries is [CMake](https://cmake.org/). Android Studio also\nsupports [ndk-build](/ndk/guides/ndk-build) due to the large\nnumber of existing projects that use the build toolkit. However, if you are\ncreating a new native library, you should use CMake.\n\n\nThis guide gives you the information you need to get up and running with the\nNDK on Android Studio. If you don't have the latest version of Android\nStudio, [download and install it now](/studio).\n\n\n**Attention experimental Gradle users:** Consider [migrating to plugin version 2.2.0 or higher](http://tools.android.com/tech-docs/new-build-system/gradle-experimental/migrate-to-stable), and using CMake or ndk-build\nto build your native libraries if any of the following apply to you: Your\nnative project already uses CMake or ndk-build; you would rather use a stable\nversion of the Gradle build system; or you want support for add-on tools,\nsuch as [CCache](https://ccache.samba.org/).\nOtherwise, you can continue to [use\nthe experimental version of Gradle and the Android plugin](http://tools.android.com/tech-docs/new-build-system/gradle-experimental).\n\nDownload the NDK and tools\n--------------------------\n\n\nTo compile and debug native code for your app, you need the following\ncomponents:\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.\n\n\nFor information on installing these components, see [Install and configure the NDK and CMake](/studio/projects/install-ndk).\n\nCreate or import a native project\n---------------------------------\n\n\nOnce you set up Android Studio, you can simply [Create a New Project with\nC/C++ Support](/studio/projects/add-native-code#new-project). However, if you want to add or import native code to an\nexisting Android Studio project, you need to follow this basic process:\n\n1. [Create new\n native source files](/studio/projects/add-native-code#create-sources) and add them to your Android Studio project.\n - You can skip this step if you already have native code or want to import a prebuilt native library.\n2. [Create\n a CMake build script](/studio/projects/configure-cmake#create_script) to tell CMake how to build your native sources into a library. You also require this build script if you are importing and linking against prebuilt or platform libraries.\n - You can skip this step if your existing native library already has a `CMakeLists.txt` build script, or uses ndk-build and includes an [`Android.mk`](/ndk/guides/android_mk) build script.\n3. [Link Gradle to\n your native library](/studio/projects/gradle-external-native-builds) by providing a path to your CMake or ndk-build script file. Gradle uses the build script to import source code into your Android Studio project and package your native library (the SO file) into the APK.\n\n\n **Note:** If your existing project uses the deprecated\n `ndkCompile` tool, you should open your\n `build.properties` file and remove the following line of code\n before configuring Gradle to use CMake or ndk-build: \n\n ```\n // Remove this line\n android.useDeprecatedNdk = true\n ```\n4. [Build and run your app](/studio/run) by clicking **Run** . Gradle adds your CMake or ndk-build process as a dependency to compile, build, and package your native library with your APK.\n\n\nOnce your app is running on a physical device or the emulator, you can use\nAndroid Studio to [Debug your app](/studio/debug).\nOtherwise, to learn more about the NDK and its components, read the [Concepts](/ndk/guides/concepts) page."]]