The new android build is in a special branch. It's as up to date as it can be with trunk.
- the official ndk with standalone toolchain
- this is for the cmake toolchain for android
sudo apt-get install mercurial
- opencv -- experimental branch
svn co https://code.ros.org/svn/opencv/branches/android-experimental
Quick NDK Setup (ubuntu and bash)
create some working directory:
WORK=$HOME/android_dev cd $WORK
now get the android-cmake project with mercurial
hg clone https://android-cmake.googlecode.com/hg/ android-cmake
there is a convenience script in there for pulling down and setting up the android ndk as a standalone toolchain
cd android-cmake/scripts ./get_ndk_toolchain_linux.sh $WORK
Add the cmake toolchain location to your bashrc or otherwise export it to your env.
echo export ANDTOOLCHAIN=$WORK/android-cmake/toolchain/android.toolchain.cmake >> $HOME/.bashrc
A useful alias to define is:
alias android-cmake='cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN '
With that alias you may run cmake for android in general with
cd myproject mkdir build cd build android-cmake ..
Quick opencv build (ubuntu and bash)
Make sure you either source your bashrc or otherwise export the ANDTOOLCHAIN variable.
There is a script in the android folder for running cmake with the proper cache variables set. It is recommended that you use this to setup a smake build directory.
cd opencv/android sh ./cmake_android.sh
You should now see a build directory, that is ready to be made.
cd build make -j8
That will build most of the opencv modules, except for those that don't make sense on android - gpu, etc..
Building the android-opencv project
The android-opencv project is the shared android library for opencv. It contains some java bindings and a java/jni camera client. It is required to build all of the samples.
cd android/android-opencv mkdir build cd build android-cmake .. make
If the android-cmake errors out, be sure to read the errors. Use ccmake, or cmake-gui to set the path to the build directory for opencv (the one for andorid, might be in android/build).
Building a sample OpenCV_SAMPLE
cd android/apps/OpenCV_SAMPLE mkdir build cd build android-cmake .. make
Again you may need to point the cmake cache to the android build of opencv, and the android-opencv/build directory. It's looking for the paths containing AndroidOpenCVConfig.cmake and OpenCVConfig.cmake
Now you are ready to build the android project and run it on your device. You should have the android sdk setup on your machine, in your path, and also have ant for command line compilation of android apps.
Plug in your device and do the following
cd android/apps/OpenCV_SAMPLE sh project_create.sh ant compile ant install
If installing to your device or emulator fails with "[INSTALL_FAILED_INVALID_APK]" be sure to check the device/emulator log with:
If you see errors related to an ABI mismatch, such as "Native ABI mismatch from package file", you may need to edit the CMake include file pointed to in ANDTOOLCHAIN, "android.toolchain.cmake".
In "android.toolchain.cmake" change the ARM_TARGETS variable to "armeabi" (default is "armeabi-v7a"), then recompile everything.
#Re-compile opencv cd opencv/android mv build build-v7a sh ./cmake_android.sh make -j8 #Re-compile android-opencv cd ../android-opencv mv build build-v7a mkdir build cd build android-cmake .. make #Re-compile sample code cd ../../apps/OpenCV_SAMPLE/ mv build build-v7a mkdir build cd build android-cmake .. make #Install to device/emulator cd .. sh project_create.sh ant clean ant compile ant install
If you are questioning if the libraries all made it into the apk, open up the apk in an archive manager and peak in the libs directory. You should see libOpenCV_SAMPLE.so and libandroid-opencv.so in there.
Using opencv in your own cmake projects
Use the cmake find script for opencv:
Then when you run cmake, use:
cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN ..
And direct your cmake cache for OpenCV_Dir to the path where you built opencv for android. something like : opencv/android/build
To avoid setting the cmake cache for OpenCV_Dir, you can just "install" opencv to your android toolchain. Run the following from the opencv/android/build path: make install
You may wish to build android for multiple hardware targets.
Just change the cmake cache ARM_TARGETS to either:
"armeabi" "armeab-v7a" "armeab-v7a with NEON"
You may install each of these to the toolchain, and they should be linked against properly via way of the android-cmake toolchain.