CMake for Android

Last time, we’ve seen how to install and test the standalone toolchain of the NDK to build a native shared library with a standard Makefile & make command under Linux.

CMake LogoBut nowadays, many open source libraries and projects does not directly come with a Makefile, instead using a build system to automate its generation. CMake is becoming more and more used for those kind of jobs, so being able to use it for Android is really interesting. And easy!

A CMake configuration file for Android (android.toolchain.cmake) is provided by the android-cmake Google project hosted on Mercurial (see my previous post on Mercurial). Let’s clone its repository in the home directory with the following commands:
hg clone android-cmake
Then, add to your .bashrc the ANDROID_NDK_TOOLCHAIN_ROOT and ANDTOOLCHAIN environment variables pointing to NDK and to the cmake config file. I also added a simple alias for later use:



alias cmake-android='cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN'

Ok, let’s try to compile the “hello-cmake” sample provided by the Google project (see hello-cmake documentation):

cd ~/android-cmake/samples/hello-cmake/
mkdir build
cd build

We can simplify the last command by using our alias, so next type we will use “cmake-android ..” instead.
At this stage, CMake has generated the Makefile for you. Use it to build the sample :
Here you are, the shared library has been build and is ready for integration in an Application. You can find it here:
Now we want to invoke this native library from a Java Application, using JNI. This is documented in the next provided sample, “hello-android-cmake“, which is the Java counterpart of the C application we’ve build.

Copy the above “libs” directory to ~/android-cmake/samples/hello-android-cmake. Then open Eclipse and start a “File->New…->Project…->Android Project”. Select “create project from existing source” and give it the ~/android-cmake/samples/hello-android-cmake path.

Build the application, and launch it on an emulator or an actual device. You should see an activity showing the multi-line text starting by “JNI — pi = 3.14159…” coming from our shared library !

Hello Android CMake

Here you are!

7 Replies to “CMake for Android”

  1. Bonjour,

    Je me permets de poster en français, vu que vous avez commencé ce blog dans la langue de Molière.

    Je vous consulte afin de vous demander conseil. J’essaie actuellement d’automatiser la création de librairie statique pour android à l’aide du CMake.
    J’ai ainsi suivi les indications de votre blog, seulement je suis confronté à un soucis de link avec la STD. J’ai cherché des solutions, mais actuellement je stagne.

    J’aimerais savoir si vous aviez eu les mêmes soucis afin de faire le lien avec la STD, et sinon que pourriez vous me conseiller.

    Badoual Grégoire

    1. I didn’t try yet to use the STL with CMake, but this is an interesting problematic I will try to discuss later.
      Je n’ai pas encore tenté d’utiliser la STL avec CMake, mais la problématique est intéressante et mérite que je m’y penche : avez vous tenté de modifier le sample hello-android-cmake pour y ajouter l’utilisation de la STL ? Avez vous des problèmes avec les containers simples (std::vector) ou seulement dans une utilisation plus avancée de la bibliothèque standard ?
      Tenez-moi au courant.


      1. Je suis actuellement sur la création de la librairie statique via l’exemple du “hello-cmake”.
        Je ne suis actuellement pas dans l’intégration de la librairie au sein d’un projet android.

        J’ai des erreur uniquement avec les conteneur simple, que vous utilisez également dans votre example (std::string, std::iostream, std::sstream, std::stdexcept ).

        Mon erreur se résume par : error: string: No such file or directory

        Pourriez vous m’éclairer afin de savoir comment vous avez pu linker de manière efficace votre cmake à la librairie STL.

        Merci beaucoup.

  2. Hi,

    Very impressive blog about cmake.

    I am doing a project about Android, right now it can only run on VirtualBox, because I don’t have hardware board. But I need cmake on Android. Do you know how to install cmake on Android?

    Thank you.

    1. Not sure of what you mean by “how to install cmake on Android?” : this article is about how to install cmake on your computer to develop a Native application for Android.

      If you really want to install cmake on Android, and then use it to compile some C/C++ code under Android, I do not have any idea about how to begin with, sorry.


Leave a Reply

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