Repost: Speed Up Git (5x to 50x)
updated_css

Build Native Android Programs with CMake and android-cmake

tubo posted @ 2014年10月29日 13:17 in 未分类 , 421 阅读

Using CMake to organize the source code is pretty cool, because you can quickly generate from it Visual Studio solution files on Windows and make files on Linux, and therefore build and test the code on different platforms without maintaining two separate build scripts.

However, CMake does not officially support Android yet. You cannot specify an Android target and ask CMake to generate from your CMakeLists.txt the make files for Android. Luckily some folks have already started to empower us with CMake for Android. One of the efforts is android-cmake. I used the latest version from GitHub android-cmake maintained by taka-no-me.

Below is a step-by-step walkthrough.

  • Let’s assume you have a C or C++ project with a few binary targets (libraries or executables) in a directory <SRC_DIR>. You have created a CMakeLists.txt under <SRC_DIR>, and you are able to use CMake to generate Visual Studio solutions or GCC make files on Linux. Let’s assume this is a C++ utility library or a GUI-less console application (for example, some unit test programs), and nothing else should prevent it form being built on Android.
  • As we know, eventually native Android libraries or applications will be built by Android NDK (Native Development Kit). So the first thing you need to have, is NDK from Google. As a mobile platform, Android’s development activities are mostly carried out on a host system, i.e., Android applications are cross-built. Depending on your host OS of daily work, you’ll need to download the appropriate NDK for your host. For example, I use Android NDK r9c for Windows 64-bit. You only need to extract the files from the zip file to somewhere on your hard drive, say, <NDK_DIR>. No other “installation” is necessary for NDK. Before we can use NDK with CMake and android-cmake, we need to do a few more things.
    • Set the environment variable ANDROID_NDK to point to <NDK_DIR>. The android-cmake will need to access this environment variable to know where NDK is.
    • Find the GNU make.exe under NDK. This is normally under <NDK_DIR>\prebuilt\windowsXXXX\bin. For example, my NDK has it under <NDK_DIR>\prebuilt\windows-x86_64\bin. Let’s call this directory <NDK_PREBUILT_BIN>.
  • The next thing you need to have, is, obviously, CMake. Install it on Windows, and make sure you can invoke cmake.exe from a command line console window.
  • Download android-cmake, i.e., the file android.toolchain.cmake. Put it in a directory, say <ANDROID_CMAKE_DIR>.
  • We are almost there. Now let CMake generate the make files for Android.
    • Launch cmd.exe to get a console window.
    • Change to your build directory, assuming out-of-source build. This directory will house the make files and built binaries. Run:
    • cmake.exe -G"MinGW Makefiles" -DCMAKE_TOOLCHAIN_FILE=<ANDROID_CMAKE_DIR>\android.toolchain.cmake -DCMAKE_BUILD_TOOL=<NDK_PREBUILT_BIN>\make.exe <SRC_DIR>
    • The command above asks CMake to use the MinGW Make file generator (basically the make file will use cmd.exe as shell on Windows), use android.toolchain.cmake as toolchain file for cross building, use the NDK’s prebuilt make.exe as the build tool for generated make files, and start walking from the CMakeLists.txt in the source directory. If there is no error, congratulations, you get the make files for your Android program in your build directory. Now you can build the program, by simply running:
    • cmake.exe –-build .
    • The build command above will invoke the NDK prebuilt make.exe, saving you from typing the lengthy path to that make.exe.

Once you are comfortable with the process above, it is easy to write a simple batch file that would automate most of the steps above, including finding the right make.exe and so on. Note that CMake is quite smart – if there is no change in the CMakeLists.txt files, it will no regenerate the make files. In the end, you probably only need to double click the batch file, and your Android program is cross built on Windows. The process to cross build an Android program on Linux with CMake and android-cmake is very similar.

The android.toolchain.cmake is a toolchain file required by CMake cross building. Normally when you use CMake, it assumes native building. You would see CMake detecting various build environment settings by trying to build some probe programs with the compiler and linker you specified on your host. Obviously, this is impossible for cross building Android programs, because even if CMake tries, it would only detect the build environment for the host system. In cross building, one has to provide all the CMake required build environment settings in a separate toolchain file, such as android.toolchain.cmake, thanks to these guys.

You can read android.toolchain.cmake to get much more insight, and this can help you to write your CMakeLists.txt to be Android aware. For example, this toolchain file will set the CMake variable ANDROID. If you need to add some POST_BUILD commands using adb such that the binary would be pushed to device and run automatically after successful building, you can use if(ANDROID) add_custom_command … to achieve that in your CMakeLists.txt.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter