Install and configure the NDK and CMake

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

  • The Android Native Development Kit (NDK): a set of tools that allows you to use C and C++ code with Android.
  • 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.
  • LLDB: the debugger Android Studio uses to debug native code. By default, LLDB will be installed alongside Android Studio.

This page describes how to use Android Studio or the sdkmanager tool to download and install these components.

Install the NDK and CMake

When you install the NDK, Android Studio selects the latest available NDK. For most projects, installing this default version of the NDK is sufficient. If your project needs one or more specific versions of the NDK, though, you can download and configure specific versions. Doing so helps you ensure reproducible builds across projects that each depend on a specific version of the NDK. Android Studio installs all versions of the NDK in the android-sdk/ndk/ directory.

To install CMake and the default NDK in Android Studio, do the following:

  1. With a project open, click Tools > SDK Manager.

  2. Click the SDK Tools tab.

  3. Select the NDK (Side by side) and CMake checkboxes.

    Image of SDK Manager Figure 1: The SDK Tools window showing the NDK (Side by side) option

  4. Click OK.

    A dialog box tells you how much space the NDK package consumes on disk.

  5. Click OK.

  6. When the installation is complete, click Finish.

  7. Your project automatically syncs the build file and performs a build. Resolve any errors that occur.

Install a specific version of the NDK

To install a specific version of the NDK, do the following:

  1. With a project open, click Tools > SDK Manager.

  2. Click the SDK Tools tab.

  3. Select the Show Package Details checkbox.

  4. Select the NDK (Side by side) checkbox and the checkboxes below it that correspond to the NDK versions you want to install. Android Studio installs all versions of the NDK in the android-sdk/ndk/ directory.

    Image of SDK Tools window Figure 2: The SDK Tools window showing the NDK (Side by side) options

  5. Click OK.

    A dialog box tells you how much space the NDK package(s) consumes.

  6. Click OK.

  7. When the installation is complete, click Finish.

  8. Your project automatically syncs the build file and performs a build. Resolve any errors that occur.

  9. Configure each module with the version of the NDK you want it to use.

Install the NDK from the command line

To install one or more versions of the NDK from the command line, do the following:

  1. Use the sdkmanager tool to view versions of CMake and NDK packages that are available:

    sdkmanager --list
    
  2. Pass the sdkmanager the strings for the packages that you want to install. For example, to install CMake and the NDK, use the following syntax:

    sdkmanager --install
              "ndk;major.minor.build"
              "cmake;major.minor.micro.build"
    
  3. Configure each module in your project with the version of the NDK that you want it to use.

Configure specific versions of the NDK in your project

If you install a specific version of the NDK and want to use it in a module, you specify it using the android.ndkVersion property in the module's build.gradle file, as shown in the following code sample.

android {
    ndkVersion "major.minor.build"
}