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 https://android-cmake.googlecode.com/hg/ 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:

PATH=$PATH:$HOME/android-sdk-linux_x86/tools
PATH=$PATH:$HOME/android-sdk-linux_x86/platform-tools
PATH=$PATH:$HOME/android-ndk-r5b/standalone-toolchain-api5/bin/

ANDROID_NDK_TOOLCHAIN_ROOT=$HOME/android-ndk-r5b/standalone-toolchain-api5
ANDTOOLCHAIN=$HOME/android-cmake/toolchain/android.toolchain.cmake

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
export ANDROID_NDK_TOOLCHAIN_ROOT
cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN ..

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 :
make
Here you are, the shared library has been build and is ready for integration in an Application. You can find it here:
~/android-cmake/samples/hello-cmake/libs/armeabi-v7a/libhello-cmake.so
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!

This entry was posted in How-to, Links, NDK, Sample, Tools and tagged . Bookmark the permalink.

7 Responses to CMake for Android

  1. Badoual Grégoire says:

    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
    contact@gregoire-badoual.fr

    • srombauts says:

      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.

      SRombauts

      • Badoual Grégoire says:

        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. Pingback: Objectives of this blog | SRombauts.fr

  3. Pingback: Standalone toolchain | SRombauts.fr

  4. Erik Bai says:

    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.

    • srombauts says:

      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.

      SRombauts

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>