Technical docs‎ > ‎

Building C++ in Android Studio with CMake or ndk-build

This page is now deprecated. Redirecting to developers.android.com ...



Overview

Starting in 2.2, Android Studio on 64 bit OS supports building C/C++ via CMake and ndk-build through stable gradle.

In both cases, Gradle is configured to point at the external build system.  It interrogates the external build system to determine a list of all C/C++ sources that are being built in the project and uses this list to populate the Studio project, enabling editing and debugging inside Studio while still using the external build system to compile and link the C/C++ sources.

In order to use C++ in Android Studio you need to download and install the Native Development Kit (NDK).

To configure an external build system, use this syntax inside of the android{} block with stable gradle:

externalNativeBuild{
   cmake{
        path "CMakeLists.txt"
    }
}

defaultConfig {
  externalNativeBuild {
    cmake {
      targets "target1", "target2"
      arguments "-DANDROID_TOOLCHAIN=clang"
      cFlags "-DTEST_C_FLAG1", "-DTEST_C_FLAG2"
      cppFlags "-DTEST_CPP_FLAG2", "-DTEST_CPP_FLAG2"
      abiFilters "armeabi-v7a", "armeabi"
    } 
  }
}

or

externalNativeBuild{
   ndkBuild{
        path "Android.mk"
    }
}

defaultConfig {
  externalNativeBuild {
    ndkBuild {
      targets "target1", "target2"
      arguments "NDK_APPLICATION_MK:=Application.mk"
      cFlags "-DTEST_C_FLAG1", "-DTEST_C_FLAG2"
      cppFlags "-DTEST_CPP_FLAG2", "-DTEST_CPP_FLAG2"
      abiFilters "armeabi-v7a", "armeabi"
    } 
  }
}

Note that external build systems are supported by the stable gradle plugin.

FAQ

Q: How do I see the output from my external build system?
A: Add --info to gradle's command line options under File > Other Settings > Default Settings > Compiler (Gradle-based Android Project)

Q: I have a problem!  What do I do?
A:  File a bug!  Instructions for filing bugs are here.

Getting started resources

There is a set of maintained CMake NDK samples here.

Release History

- Respect android.ndk.abiFilters when set in build.gradle.

Android Studio 2.2 Preview 6 (July 22, 2016)
- Report intermediate build progress during build. We report the .so or .a that is being built right now.
- Significantly improve build/deploy/debug cycle by only building the ABI that is needed for the target device or emulator.
- Fixed issue where ndk-build with gcc caused had red symbols due to process exception.
- Fixed issue with clean. We now respect CMake and ndk-build clean and put build files in a separate folder named .externalNativeBuild.
- Added folder .externalNativeBuild to default .gitignore so that people won't be confused about whether these files should be checked in.

Android Studio 2.2 Preview 5 (July 8, 2016)
- BREAKING CHANGE:In build.gradle configuration sections cmake and ndkBuild groups must be under externalNativeBuild group:
defaultConfig {
  externalNativeBuild {
    ndkBuild {
      targets "target1", "target2"
      arguments "NDK_APPLICATION_MK:=Application.mk"
      cFlags "-DTEST_C_FLAG1", "-DTEST_C_FLAG2"
      cppFlags "-DTEST_CPP_FLAG2", "-DTEST_CPP_FLAG2"
      abiFilters "armeabi-v7a", "armeabi"
    } 
  }
}
- Fixed ProcessException issue with clang and ndk-build on Windows.
- Fixed issue with --gcc-toolchain flag with clang and ndk-build on Ubuntu and Mac.
- Fixed issue where ndk-build projects sometimes leave compiler-file####.d files in the main project directory.
- Enhanced logic for selecting platform version for ndk-build and CMake:
    (1) get the set of ABIs based on compileSdkVersion.
    (2) then for each of these ABIs we have decided to build for,
        (a) if platforms/android-[min sdk]/arch-[ABI] exists, then use the min sdk  as platform for that ABI.
        (b) else if there exists platforms/android-[platform]/arch-[ABI] such that   platform < min sdk, use max(platform where platform < min sdk).
        (c) else use min(platform where platforms/android-[platform]/arch-[ABI]  exists).
- Fixed issue in New Project Wizard where the wrong JNI function name was generated.
- Changed CMakeLists.txt created by New Project Wizard to be more representative of best practices.
- Fixed issue in ndk-build projects that use CCache work.

Android Studio 2.2 Preview 4 (June 23, 2016)
- Allow selecting specific targets to build and package.
- Folders will be disallowed under android.externalNativeBuild.cmake.path and android.externalNativeBuild.ndkBuild.path. Only file names will be accepted.

Android Studio 2.2 Preview 3 (June 10, 2016)
- Better build error message reporting.
- Added support for config.cmake.arguments and config.ndkBuild.arguments for passing arguments directly to CMake and ndk-build respectively. For example,
defaultConfig {
  ndkBuild {
    arguments "NDK_APPLICATION_MK:=Application.mk"
  } 
}
- Changed ndk-build integration to automatically pick up Application.mk if it is in the same folder as the Android.mk
- Fixed bug that broke use of GoogleTest library in ndk-build project.
- Fixed bug that left the Android Studio project stale if only build flags changed.

Android Studio 2.2 Preview 2
- First release

Known issues
  • If you don't have the NDK installed then you will get an error message like the one below. This message will be improved in Android Studio preview 7. You can install the NDK by following the instructions here.
Caused by: 
java.lang.NullPointerException
        at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
        at com.android.build.gradle.tasks.ExternalNativeJsonGenerator.create(ExternalNativeJsonGenerator.java:493)
        at com.android.build.gradle.internal.TaskManager.createExternalNativeBuildJsonGenerators(TaskManager.java:1206)
        at com.android.build.gradle.internal.LibraryTaskManager$12.call(LibraryTaskManager.java:304)
        at com.android.build.gradle.internal.LibraryTaskManager$12.call(LibraryTaskManager.java:301)
Comments