Skip to content

Instantly share code, notes, and snippets.

@pthom
Last active May 13, 2020 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pthom/07a130b9f45abb1ddb14bf76b935482b to your computer and use it in GitHub Desktop.
Save pthom/07a130b9f45abb1ddb14bf76b935482b to your computer and use it in GitHub Desktop.
vcpkg / opencv4: instructions to test the build on android

These instructions were tested on a vanilla linux installation.

  1. Download Android ndk
mkdir ~/Android
cd ~/Android
wget https://dl.google.com/android/repository/android-ndk-r21b-linux-x86_64.zip
unzip android-ndk-r21b-linux-x86_64.zip
# edit the path below if needed
export ANDROID_NDK_HOME=~/Android/android-ndk-r21b
  1. Install at least one android triplet
# edit the path below!
export VCPKG_ROOT=/your/path/to/vcpkg
cd $VCPKG_ROOT
# this will create the triplet community/arm-android.cmake
echo "
set(VCPKG_TARGET_ARCHITECTURE arm)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic)
set(VCPKG_CMAKE_SYSTEM_NAME Android)
" > triplets/community/arm-android.cmake
  1. Create a simple cpp + cmake file using opencv
mkdir test && cd test

# a. create a simple cpp file that uses opencv
echo "
#include <opencv2/core/core.hpp>
void foo() {}
" > hello.cpp

# b. create a simple cmakelist that uses opencv
echo "
cmake_minimum_required(VERSION 3.0)
project(test)
find_package(OpenCV CONFIG REQUIRED)
add_library(hello hello.cpp)
target_link_libraries(hello opencv_core)
" > CMakeLists.txt

#
# c.Create a simple compile.sh file that will compile using android + vcpkg toolchain
# c.1. needed env variables
export vcpkg_toolchain_file=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
export android_toolchain_file=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake
export android_abi=armeabi-v7a
export vcpkg_target_triplet=arm-android
# c.2. create the compile.sh script
echo "
rm -rf build
mkdir build
cd build 
cmake .. \
  -DCMAKE_TOOLCHAIN_FILE=$vcpkg_toolchain_file \
  -DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=$android_toolchain_file \
  -DVCPKG_TARGET_TRIPLET=$vcpkg_target_triplet \
  -DANDROID_ABI=$android_abi \
  -DANDROID_NATIVE_API_LEVEL=21
make
" > compile.sh
chmod +x compile.sh
  1. Install opencv for android using vcpkg
cd $VCPKG_ROOT
./vcpkg install "opencv4[core,jpeg,opengl,tiff,webp]" --triplet arm-android
  1. Run the compile script.
cd test
./compile.sh

You will get the following output:

CMake Error at /root/vcpkg/installed/arm-android/share/opencv/abi-armeabi-v7a/OpenCVModules.cmake:157 (message):
  The imported target "opencv_core" references the file
     "/root/vcpkg/installed/arm-android/share/debug/lib/libopencv_cored.so"
  but this file does not exist.

Optional:

If you do not want to use your own PC, you can use Docker in order to start a fresh linux (based on the official gcc images)

docker run --name test_android -it gcc /bin/bash

And run this to re-enter the docker machine once you exited it

docker start test_android && docker attach test_android
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment