Skip to content

Instantly share code, notes, and snippets.

@salehjg
Last active March 10, 2023 19:58
Show Gist options
  • Save salehjg/2ce2ef90071eaa3ba7f3404f4094ae12 to your computer and use it in GitHub Desktop.
Save salehjg/2ce2ef90071eaa3ba7f3404f4094ae12 to your computer and use it in GitHub Desktop.
How to build OpenCV for NDK from source.
ANDROID_SDK_PATH=/home/foo/Android/Sdk/
ANDROID_NDK_PATH=/home/foo/Android/Sdk/ndk/21.4.7075529
ANDROID_NDK_TOOLCHAIN_CMAKE_MODULE_PATH=/home/foo/Android/Sdk/ndk/21.4.7075529/build/cmake/android.toolchain.cmake
OPENCV_INSTALL_PATH=../01_ndk_outputs
OPENCV_CONTRIB_PATH=../opencv_contrib-3.4.16/modules
OPENCV_CONTRIB_PATH_RESOLVED=$(builtin cd $OPENCV_CONTRIB_PATH; pwd)
OPENCV_INSTALL_PATH_RESOLVED=$(builtin cd $OPENCV_INSTALL_PATH; pwd)
ANT_EXEC=/usr/bin/ant
cmake \
-DCMAKE_INSTALL_PREFIX:PATH=${OPENCV_INSTALL_PATH_RESOLVED} \
-DANT_EXECUTABLE=${ANT_EXEC} \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_opencv_androidcamera=ON \
-DBUILD_OPENCV_NATIVECAMERA=ON \
-DBUILD_ZLIB=ON \
-DBUILD_ANDROID_EXAMPLES=OFF \
-DBUILD_EXAMPLES:BOOL=OFF \
-DBUILD_TESTS:BOOL=OFF \
-DBUILD_PERF_TESTS:BOOL=OFF \
-DBUILD_JAVA=ON \
-DBUILD_opencv_java=ON \
-DBUILD_SHARED_LIBS:BOOL=OFF \
-DBUILD_FAT_JAVA_LIB=ON \
-DBUILD_PYTHON:BOOL=OFF \
-DINSTALL_ANDROID_EXAMPLES:BOOL=OFF \
-DANDROID_EXAMPLES_WITH_LIBS:BOOL=OFF \
-DBUILD_DOCS:BOOL=OFF \
-DWITH_OPENCL=OFF \
-DANDROID_NDK_HOST_X64=ON \
-DANDROID_SDK=${ANDROID_SDK_PATH} \
-DANDROID_NDK=${ANDROID_NDK_PATH} \
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_TOOLCHAIN_CMAKE_MODULE_PATH} \
-DANDROID_TOOLCHAIN=clang \
-DANDROID_STL=c++_static \
-DANDROID_ARM_NEON=ON \
-DANDROID_ABI='arm64-v8a' \
-DBUILD_opencv_python3:BOOL=OFF \
-DBUILD_opencv_python2:BOOL=OFF \
-DOPENCV_ENABLE_NONFREE=ON \
-DANDROID_NATIVE_API_LEVEL=25 \
-DANDROID_SDK_BUILD_TOOLS_VERSION=25.0.3 \
-DANDROID_SDK_TARGET=25 \
-DOPENCV_FP16_DISABLE=ON \
-DBUILD_ANDROID_PROJECTS=ON \
-DANDROID_PROJECTS_BUILD_TYPE="ANT" \
-DOPENCV_EXTRA_MODULES_PATH=${OPENCV_CONTRIB_PATH_RESOLVED} \
..

Pre Requisities

  1. Use a Linux OS. It is assumed that Arch Linux is used.
  2. Install the latest version of Android Studio.
  3. Use SDK Manger of Android Studio to install your SDK 25, NDK 21.4.7075529, and BuildTools 25.0.3 of your choice.
  4. Install the packages that we are going to need on your linux: sudo pacman -S cmake git aria2 ant unzip ant
  5. Make sure you have a suitable python2, python3, and jdk

OpenCV Versions

If you need the cv1 interface of OpenCV, you should use version < 4. Versions>=4 only have the cv2 interface. We are going to use OpenCV 3.4.16 .

Android SDK Tools

The android executable of Android SDK located at sdk/tools/android which is used by OpenCV's CMake scripts to setup the java module project is depricated by Google. Unfortunately, OpenCV, even the latest versions of it, still use this depricated executable. The workaround is to download an older version of this file from Google and use it instead. (as described here)

Downgrade Sdk/tools

$ cd ~/Downloads
$ aria2 -x8 http://dl-ssl.google.com/android/repository/tools_r25.2.5-linux.zip
$ mv ~/Android/Sdk/tools ~/Android/Sdk/tools.ORIG
$ unzip tools_r25.2.5-linux.zip -d ~/Android/Sdk/

Get OpenCV Source

$ cd ~
$ mkdir opencv_repo
$ cd opencv_repo
$ aria2 -x8 https://github.com/opencv/opencv/archive/refs/tags/3.4.16.zip
$ aria2 -x8 https://github.com/opencv/opencv_contrib/archive/refs/tags/3.4.16.zip
$ unzip opencv-3.4.16.zip
$ unzip opencv_contrib-3.4.16.zip
$ cd opencv-3.4.16
$ aria2 https://gist.githubusercontent.com/salehjg/2ce2ef90071eaa3ba7f3404f4094ae12/raw/41f1533729283affb1c02c00af4fd81e9d57aa2b/cmake_conf.sh

Now open cmake_conf.sh and edit the path lines. Save it and continue.

$ mkdir 00_build
$ mkdir 01_ndk_outputs
$ cd 00_build
$ bash ../cmake_conf.sh
$ make all -j8
$ make install

The NDK version of OpenCV should be available in 01_ndk_outputs now.

Android SDK Tools

Now that we are finished with the old android executable, we have to restore the original version of the sdk/tools directory.

Resoring the original Sdk/tools

$ rm -rf ~/Android/Sdk/tools
$ mv ~/Android/Sdk/tools.ORIG ~/Android/Sdk/tools

Link: https://forum.unity.com/threads/solved-android-command-deprecated-error-unable-to-list-target-platforms.458814/

SDK Tools Release Notes: https://developer.android.com/studio/releases/sdk-tools.html SDK Platform Tools Release Notes: https://developer.android.com/studio/releases/platforms.html

To download a manual version of SDK Tools, fill in the following url with a version from the above:

https://dl-ssl.google.com/android/repository/tools_r-windows.zip https://dl-ssl.google.com/android/repository/tools_r-macosx.zip

For me downgrading to https://dl-ssl.google.com/android/repository/tools_r25.2.5-macosx.zip worked just fine. Once you download an older version the tools, just rename the tools directory at your Android SDK install and replace with the downloaded version.

I didn't need to downgrade Platform Tools, but if you need to, here are those links as well:

https://dl-ssl.google.com/android/repository/platform-tools_r-windows.zip https://dl-ssl.google.com/android/repository/platform-tools_r-macosx.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment