Migrate to Stable Gradle for NDK Support using CMake and ndk-build

Android Studio 2.2 and higher support building C/C++ components of your Android project with two new options: CMake and ndk-build. Using Android Studio, you can edit and debug your native code while still using either external build system to compile and link the C/C++ sources. For more information, learn how to Add C and C++ Code to Your Project using Android Studio.

While the experimental version of the Android Plugin for Gradle also includes NDK integration for building JNI applications, and is under ongoing development, there are a few reasons you may want to consider using CMake or ndk-build with the stable version of the Gradle plugin:

  • Your project already uses CMake or ndk-build

  • You have a large project that is unable to assume the risk of using an experimental version of the plugin

  • You want to build your C/C++ code for multiple platforms

If none of the above apply to you, and you want to continue building your native code using Gradle, you can choose to keep using the experimental plugin. If want to switch to the stable Gradle plugin because the experimental plugin is unable to meet your current needs, you can follow this guide to migrate your project to use the stable version of Gradle with CMake. Alternatively, to start using stable Gradle with CMake integration right away, you can download Android Studio 2.2 and try out these sample apps.

Note: Support for ndk-build is included due to the large number of legacy projects. However, if you are starting a new project and want to use a non-experimental build system, you should use CMake. CMake is a more effective way to build C/C++ code due to its mature syntax.

Remove the deprecated ndkCompile

If you are using the deprecated ndkCompile in your project, you should start using stable Gradle with CMake integration. Before you use this guide to migrate your project to use CMake, go to your build.properties file and remove the following line:

// Remove this line
android.useDeprecatedNdk = true

If you manually configured your project to build its native code by calling ndk-build, follow this guide to either link Gradle directly to the Android.mk build script or migrate to CMake.

Download the NDK Tools

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

  • The Android Native Development Kit (NDK): a toolset that allows you to use C and C++ code with Android, and provides platform libraries that allow you to manage native activities and access physical device components, such as sensors and touch input.

  • 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.

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 pop-up dialog.

  5. When the installation is finished, click Finish, and then click OK.

Use Gradle Plugin 2.2.0 or higher

In your project-level build.gradle file, change the classpath dependency of the gradle plugin to 2.2.0 or higher:

buildscript {
    repositories {...}
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'

You can check for the latest gradle plugin at https://jcenter.bintray.com/com/android/tools/build/gradle/.

    Switch to Stable Gradle DSL

    You only need to follow the instructions in this section of the guide if you are migrating from experimental Gradle. Since the stable version of Gradle does not use the component model mechanism, you need to change some of the DSL in your module-level build.gradle file:

    1. At the top of your module-level build.gradle file, you need to apply the stable version of the plugin as follows:
      apply plugin: 'com.android.application'

    2. Remove the model{} block, including its opening and closing curly braces. The android{} block should not be enclosed within any other DSL elements.

    3. Remove “.apiLevel” suffix from minSdkVersion and targetSdkVerion:
      minSdkVersion 8
      targetSdkVersion 23

    4. Change the proguardFiles DSL to the following:
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

    5. Remove create(“...”) from your productFlavor configurations. For example, use the following DSL to create a “flavor1” product flavor:
      productFlavors {
        flavor1 {
          applicationId “com.app” }

    6. Only define abiFilters in the ndk{} block--comment out everything else in this block. The stable plugin uses a new externalNativeBuild{} DSL to link Gradle to your CMake or ndk-build project, and set toolchain arguments and compiler flags.

    You learn about the new externalNativeBuild{} DSL and other Gradle configuration you need to perform in the section called "Link Gradle to Your Native Library".

    Create a CMake build script

    To build your native source code into a library, you need to Create a CMake build scriptYou also require this build script if you are importing and linking against prebuilt or platform libraries.
    Note: If you have an existing native library that already has a CMakeLists.txt build script, or uses ndk-build and includes an Android.mk build script, you can skip this step.

    Link Gradle to Your Native Library

    To import source code into your Android Studio project and package your native library (the SO file) into the APK, you need to Link Gradle to your native library. This is the step where you provide Gradle with a path to your CMake or ndk-build script file, and set toolchain arguments, compiler flags, and other build options for your native library project.