Skip to content

Most visited

Recently visited

navigation

Getting Started with the NDK

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input. The NDK may not be appropriate for most novice Android programmers who need to use only Java code and framework APIs to develop their apps. However, the NDK can be useful for cases in which you need to do one or more of the following:

Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI) framework. To learn more about Gradle and the Android build system, read Configure Your Build.

Android Studio's default build tool to compile native libraries is CMake. Android Studio also supports ndk-build due to the large number of existing projects that use the build toolkit. However, if you are creating a new native library, you should use CMake.

This guide gives you the information you need to get up and running with the NDK on Android Studio. If you don't have the latest version of Android Studio, download and install it now.

Attention experimental Gradle users: Consider migrating to plugin version 2.2.0 or higher, and using CMake or ndk-build to build your native libraries if any of the following apply to you: Your native project already uses CMake or ndk-build; you would rather use a stable version of the Gradle build system; or you want support for add-on tools, such as CCache. Otherwise, you can continue to use the experimental version of Gradle and the Android plugin.

Download the NDK and Tools

To compile and debug native code for your app, you need the following components:

You can install these components using the SDK Manager:

  1. From an open project, select Tools > Android > SDK Manager from the main menu.
  2. Click the SDK Tools tab.
  3. Check the boxes next to LLDB, CMake, and NDK, as shown in figure 1.

    Figure 1. Installing LLDB, CMake, and the NDK from the SDK Manager.

  4. Click Apply, and then click OK in the next dialog.
  5. When the installation is complete, click Finish, and then click OK.

Create or Import a Native Project

Once you set up Android Studio, you can simply Create a New Project with C/C++ Support. However, if you want to add or import native code to an existing Android Studio project, you need to follow this basic process:

  1. Create new native source files and add them to your Android Studio project.
    • You can skip this step if you already have native code or want to import a prebuilt native library.
  2. Create a CMake build 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.
    • 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 build script.
  3. Link Gradle to your native library 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.

    Note: If your existing project uses the deprecated ndkCompile tool, you should open your build.properties file and remove the following line of code before configuring Gradle to use CMake or ndk-build:

    // Remove this line
    android.useDeprecatedNdk = true
    
  4. Build and run your app by clicking Run run then run app from the main menu. Gradle adds your CMake or ndk-build process as a dependency to compile, build, and package your native library with your APK.

Once your app is running on a physical device or the emulator, you can use Android Studio to Debug Your App. Otherwise, to learn more about the NDK and its components, read the Concepts page.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)