Android Studio CMake

Android Studio 2.2 Preview 1 launched at Google I/O 2016. Android Studio allows you to develop with the latest Android APIs and features as mentioned here. UI layout editor is great but i really excited about CMake plugin. Using previous version, i was able to develop simple image scanner app that using NDK features. Now I’ll try develop simple native app using CMake rather than ndk-build.

Create project, just like usual…but with cmake plugin

But remember to create project using Android Studio 2.2 Preview 1, you can download it here. If you already have previous version don’t worry because you can install it side-by-side. When you’re done, just create project like usual because the differences about enabling CMake features are when we use native code (C/C++).

android-studio-project-folder-structureThat’s typical folder structure in Android Studio. Next we create directory cpp inside main folder, and then create CMakeLists.txt. If you’re not familiar with CMakeLists.txt find out more here and here.

  1.   cmake_minimum_required(VERSION 2.8.7) means we need minimum version 2.8.7 of cmake. It depends on what you need in your project. by default Android Studio built in CMake version is 3.4.1
  2. add_library(hello-jni SHARED hello-jni.c) means we create shared library named hello-jni with hello-jni.c as  source.
  3. target_link_libraries(hello-jni log android) means we link library needed by hello-jni, in this case log and android.\

Next create hello-jni.c as follow:

This code for checking ABI on device. Next right click on module name and then click “Link C++ Project with Gradle“. For first time would be pop-up to install cmake plugin.

android studio cmake plugin installation

android studio cmake plugin installation

When installation done, point CMakeLists.txt to “Project Path”.

cmake project path

cmake project path

Since we still need NDK Framework to get cmake plugin works, make sure we already download NDK framework and set ndk.dir in or by Project Structure >> SDK Location >>Android NDK Location.

ndk-location pathAfter syncing Gradle and no error reported, we can start invoking native method provide by hello-jni.c from activity. In this example print out ABI to textview. Here’s my layout

And here’s my activity

Highlighted rows are invoking native method to activity, remember that we load library named hello-jni as we declared in CMakeLists.txt. Next try to build and run it to real device or emulator and should looks like this

hello-jni cmake sample appI think it’s pretty straightforward to implementing cmake plugin with ndk rather than using as earlier native build configuration because it gives us flexibility when using a lot of dependencies. But this is experimental anyway, until i post this, Android Studio 2.2 Preview 2 is available to download. So, i appreciate any suggestion by leaving comment below, see you next post.

[UPDATE July, 20th 2016]

Android Studio 2.2 Preview 6 available on canary channel. This update address bugs on Preview 5 see release notes for details. For me, i found bugs on Preview 5 that is Android Studio no longer recognize cmake plugins, cannot resolve c/c++ and bad performance.

Gradle DSL method not found: ‘cmake()’  error solve

By updating gradle plugin to 2.2.0.alpha6 you have to change cmake config at  defaultConfig


remember to update gradle plugin to 2.2.0.alpha 6, there are two ways to update, first let android studio fix it by clicking “Fix and sync project” when error comes up on messages gradle build. or by updating root  build.gradle (not module build.gradle)





8 thoughts on “Android Studio CMake

  1. Great post.
    Do you know how to select the abi to be generative from native code ?
    By default, all ABI are generated (armv7, arm64, mips, x86, x86_64, mips64). Do you know if it is possible to only build one ABI ?

    1. you can add abiFilters cmake config at defaultConfig in build.gradle, like

      defaultConfig{    cmake{      abiFilters 'x86','x86_64'    }}

      1. Hi Oliver,

        It seems android studio 2.2 preview 6 available, on Preview 5 there are some bugs affect to cmake plugin. As i mentioned on previous reply that you need to add abiFilters at defaultConfig, this cause error if you update to preview 6 or even update LLDB (i’m not sure about LLDB). You can solve it by little edit
        defaultConfig{   externalNativeBuild{      cmake{        abiFilters 'x86',        // some other config      }   }}

        As you can see externalNativeBuild added. see this link

  2. I use the following CMakelist.txt but my third party library isn’t packaged into apk. so anyting wrong?


    1. hi Tony,

      In order to use third party libs, you should include it by find_libray(<lib_name>), but before cmake can “find” the libs, you should include your third party INCLUDE_DIR by calling include_directories(<dir_path>).

      include_directories docs

      hope this helps

Leave a Reply

Your email address will not be published. Required fields are marked *