Monday, November 25, 2013

Getting Android CMake to work....

I recently wanted to get one of my libraries - FTL to build and run on my nexus 7 tablet. Since everything is done via CMake, the obvious question was could I just get the Android NDK toolchain setup and building without any changes to my library? (For those looking to setup it up manually without CMake see this post)

First step.... getting Android CMake to work

There are a number of things spread around the net but nothing conclusive to point out obvious noobie mistakes I came across.... so I documented my findings just in case someone else has the same issues:

1. After setting up the NDK r9 x64 version as:

export NDK=~/Dev/android-ndk-r9
$NDK/build/tools/make-standalone-toolchain.sh --platform=android-5 --install-dir=$HOME/Dev/Install/android-toolchain --toolchain=arm-linux-androideabi-4.8
 
It is very important to put the whole path in and not the './' instead. Otherwise it will complain about the NDK install not being found. The toolchain should match your device. [From Android CMake docs]

2. Next you need Android CMake. The project on Google Code is no longer supported, but the OpenCV version appears to be the latest version, so grab it from here. You just need the .cmake file, so I grabbed the Google Code version and replaced the android.toolchain.cmake file with the latest version. I placed this at ~/Dev/Install/android-cmake.

3. Test it on the Android CMake samples available on the Google Code repository. Setup the environment:

export ANDROID_NDK=~/Dev/android-ndk-r9
export ANDROID_NDK_STANDALONE_TOOLCHAIN=~/Dev/Install/android-toolchain

export ANDROID_NDK_TOOLCHAINS_PATH=$ANDROID_NDK
export ANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.8

export PATH=$ANDROID_NDK_STANDALONE_TOOLCHAIN/bin:$PATH
export ANDROID_CMAKE=~/Dev/Install/android-cmake
export ANDTOOLCHAIN=$ANDROID_CMAKE/toolchain/android.toolchain.cmake

alias android-cmake='ccmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN -DANDROID_NDK=$ANDROID_NDK -DCMAKE_C_COMPILER=arm-linux-androideabi-gcc -DCMAKE_CXX_COMPILER=arm-linux-androideabi-g++ -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.8 -DANDROID_NDK_TOOLCHAINS_PATH=~/Dev/android-ndk-r9'

The last 'alias' command should be on one line or break it up slashes. The tricky bit is the distinction between the ANDROID_NDK and the ANDROID_NDK_STANDALONE_TOOLCHAIN. The latter is good for directly accessing the compilers etc. and the former is needed by Android CMake to find the NDK. If not set correctly, you get the "Could not find any working toolchain in the NDK. Probably your Android NDK is broken" message.

If all goes well then doing the following will work:

mkdir build
cd build
android-cmake ..
make -j 4

At 'android-cmake' line, I used the Curses front-end for CMake. Replace it with your preferred front-end. The rest of the documentation for Hello-CMake example should work fine. More on Android CMake porting of libraries later....

Cheers Shakes - L3mming

No comments:

Post a Comment