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 未分类 , 1474 阅读

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.

Bihar Board 7th Clas 说:
2022年9月05日 23:49

Bihar Board Model Paper 2023 Class 7 Pdf Download with Answers for English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. Bihar Board 7th Class Model Paper New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.

Alyssa 说:
2022年12月30日 23:47

CMake is a cross-platform build system that can be used to build, test, and Benefits Of CBD Hand Sanitizer package software. android-cmake is a CMake toolchain file that allows you to build native Android programs using CMake. The step-by-step process on build a native Android program with CMake and android-cmake is described here. Keep sharing more details here.

meidir 说:
2023年1月26日 04:56

Youre so cool! I dont suppose Ive read anything similar to this prior to. So nice to seek out somebody by original ideas on this subject. realy thank you for starting this up. this web site can be something that is needed over the internet, an individual after a little originality. beneficial purpose of bringing new things towards the web! Buy Original ECFMG Certificate Without Exam

meidir 说:
2023年1月31日 22:08

This is actually exciting, You’re an enormously knowledgeable blogger. I have enrolled with your feed and also anticipate witnessing the useful write-ups. At the same time, I’ve shared your site in our social networks. what is bullet knit fabric

 

 

 

=====================

 

 

Way cool, some valid points! I appreciate you making this article available, the rest of the site is also high quality. Have a fun. printed bullet fabric

meidir 说:
2023年2月03日 04:34

Hello there I am so excited I found your web site, I really found you by error, while I was looking on Aol for something else, Nonetheless I am here now and would just like to say thank you for a tremendous post and a all round entertaining blog (I also love the theme/design), I don’t have time to look over it all at the moment but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the great job. crear sitio web

 

 

=====================

 

 

Excellent post. I simply stumbled upon your blog and also wanted to claim that I’ve really appreciated exploring your blog articles. Regardless I will be subscribing to the nourish and i also hope you write again shortly! Mejores monitores

meidir 说:
2023年2月05日 04:44

This is a great blog” and i want to visit this every day of the week .   software licensing attorney

Matthew Wade 说:
2024年12月07日 23:10

A really fascinating read, I may well not agree totally, but you do make some quite legitimate factors. ECサイト セキュリティ

Matthew Wade 说:
2024年12月12日 03:07

I am thankful that I noticed this web blog , just the right info that I was searching for! . 妻を抱かせる夫達

Matthew Wade 说:
2024年12月12日 03:07

I thought you were going to chip in with some decisive insght at the end there, not leave it with ‘we leave it to you to decide’. カッティングシート

Matthew Wade 说:
2024年12月12日 03:07

Youre so cool! I dont suppose Ive read anything like this before. So nice to search out any person with some original thoughts on this subject. realy thank you for starting this up. this website is something that’s wanted on the net, someone with somewhat originality. useful job for bringing something new to the web! 大人動画

Matthew Wade 说:
2024年12月14日 05:02

i wish to have some diamond necklace but they are quite expensive,, 【フル映像】

Matthew Wade 说:
2024年12月14日 05:03

Spot i’ll carry on with this write-up, I really believe this amazing site needs a great deal more consideration. I’ll more likely once again to learn to read much more, many thanks for that info. 裏街さんぽ

Matthew Wade 说:
2024年12月14日 05:03

We love watching Dancing with the stars, the actresses and actors that joins it are nice; フル動画のエロ動画

Matthew Wade 说:
2024年12月20日 04:09

Super-Duper website! I am loving it!! Will be back later to read some more. I am bookmarking your feeds also ギャル

Matthew Wade 说:
2024年12月20日 04:09

Aw, it was a very good post. In idea I would like to devote writing such as this furthermore,?¡ìC spending time and specific work to produce a great article?- nonetheless so what can I say?- I waste time alot and never at all seem to obtain one thing completed. 乱視用カラコン

Matthew Wade 说:
2024年12月20日 04:10

Really good post, i certainly love this site, keep on it Japanese Pokemon Card


登录 *


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