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:
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 ndkCompileIf 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 ToolsTo compile and debug native code for your app, you need the following components:
You can install these components using the SDK Manager:
Use Gradle Plugin 2.2.0 or higherIn 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 DSLYou 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:
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 scriptTo build your native source code into a library, you need to Create a CMake build script. You 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 LibraryTo 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. |