NDK 使用入門
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Native Development Kit (NDK) 是一套工具,可讓您在 Android 系統中使用 C 和 C ++ 程式碼,並提供各種平台程式庫,方便您管理原生活動和存取權實體裝置元件,例如感應器和觸控輸入。NDK 可能不適合大多數 Android 程式設計新手,因為他們只需要使用 Java 程式碼和架構 API 來開發應用程式。不過,如果您需要進行以下一項或多項操作,NDK 就能派上用場:
- 進一步提升裝置效能,以降低延遲或執行遊戲、物理模擬等需要大量運算的應用程式。
- 重複使用您自己或其他開發人員的 C 或 C++ 程式庫。
您可以在 Android Studio 2.2 以上版本中使用 NDK 將 C 和 C++ 程式碼編譯為原生資料庫,然後使用 IDE 的整合建構系統 Gradle 將原生資料庫封裝至 APK。您的 Java 程式碼便可透過 Java 原生介面 (JNI) 架構呼叫原生資料庫中的函式。如要進一步瞭解 Gradle 和 Android 建構系統,請參閱設定您的建構。
Android Studio 編譯原生資料庫的預設建構工具是 CMake。此外,由於有許多現有專案都使用 ndk-build 建構工具包,因此 Android Studio 也支援 ndk-build。不過,如要建立新的原生資料庫,建議您使用 CMake。
本指南提供一切所需資訊,說明如何在 Android Studio 上設定和執行 NDK。如果尚未安裝最新版 Android Studio,請立即下載並安裝。
Gradle 實驗版使用者請注意:請考慮遷移至 2.2.0 以上版本的外掛程式,並使用 CMake 或 ndk-build 版的原生程式庫(如果下列適用於您的情況:您的原生專案已使用 CMake 或 ndk-build);建議使用穩定版的 Gradle 建構系統,或是建議取得 CCache 等外掛程式工具的支援。或者,您可以繼續使用 Gradle 和 Android 外掛程式的實驗版本。
下載 NDK 和工具
如要針對應用程式編譯及偵錯原生程式碼,您需要下列元件:
- Android Native Development Kit (NDK):這套工具
以便透過 Android 使用 C 和 C++ 程式碼
- CMake:與 Gradle 搭配使用的外部建構工具,以建構原生資料庫。如果您只打算使用 ndk-build,則不需要這個元件。
-
LLDB:Android Studio 用來偵錯原生程式碼的偵錯工具。
如需瞭解如何安裝這些元件,請參閱安裝及設定 NDK 和 CMake。
建立或匯入原生專案
Android Studio 設定完成後,便可直接建立支援 C/C++ 的新專案。但如果要在現有 Android Studio 專案中新增或匯入原生程式碼,則需要按照下列基本程序操作:
-
建立新的原生來源檔案,並將這些檔案新增至 Android Studio 專案。
- 如果您已有原生程式碼,或想要匯入預先建立的原生資料庫,請略過這個步驟。
-
建立
CMake 建構指令碼,指示 CMake 如何建構原生來源
到程式庫中如要匯入並連結預建程式庫或平台程式庫,您也需要這個建構指令碼。
- 如果現有的原生資料庫已有
CMakeLists.txt
建構指令碼,或使用 ndk-build 並包含 Android.mk
建構指令碼,則可略過這個步驟。
-
提供指向 CMake 或 ndk-build 指令碼檔案的路徑,將 Gradle 連結至原生資料庫。Gradle 會使用建構指令碼將原始碼匯入您的 Android Studio 專案,並將原生資料庫 (SO 檔案) 封裝至 APK。
注意:如果現有專案使用的是已淘汰的 ndkCompile
工具,請先開啟 build.properties
檔案,並移除以下這行程式碼,然後再設定 Gradle 以使用 CMake 或 ndk-build:
// Remove this line
android.useDeprecatedNdk = true
-
按一下這裡,即可建構並執行應用程式
執行
。Gradle 新增
CMake 或 ndk-build 程序做為依附元件,以便編譯、建構
將您的原生資料庫與 APK 一併封裝
應用程式在實體裝置或模擬器上執行後,您可以使用 Android Studio 偵錯應用程式。如要進一步瞭解 NDK 及其元件,請參閱概念頁面。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","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-26 (世界標準時間)。"],[],[],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."]]