NDK 使用入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
原生开发套件 (NDK) 是一套工具,使您能够在 Android 应用中使用 C 和 C++ 代码,并提供众多平台库,您可使用这些平台库管理原生 activity 和访问实体设备组件,例如传感器和触控输入。NDK 可能不适合大多数 Android 编程初学者,这些初学者只需使用 Java 代码和框架 API 开发应用。然而,如果您需要实现以下一个或多个目标,那么 NDK 就能派上用场:
- 进一步提升设备性能,以降低延迟或运行游戏或物理模拟等计算密集型应用。
- 重复使用您自己或其他开发者的 C 或 C++ 库。
您可以在 Android Studio 2.2 或更高版本中使用 NDK 将 C 和 C++ 代码编译到原生库中,然后使用 Android Studio 的集成构建系统 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 原生开发套件 (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
-
通过点击 Run 图标
构建并运行应用。Gradle 会以依赖项的形式添加 CMake 或 ndk-build 进程,用于编译和构建原生库并将其随 APK 一起打包。
应用在实体设备或模拟器上运行后,您可以使用 Android Studio 调试应用。如需详细了解 NDK 及其组件,请参阅概念页面。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):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."]]