Hello! I don’t know much about building opencv and I’ve been looking for various guides about it, but I haven’t found any fresh ones. Can you tell me how to build OpenCV with additional modules for Android step by step?
I am also struggling with it, and I think we have same problems.
I’m still trying to find a working way for me. Do you have any success?
I have tried many tutourials for weeks, but all of them failed . I think it maybe success to build opencv with contrib in linux rather than windows.
Luckily! there is another easy way to use opencv with contrib in android studio.
How to Integrate OpenCV with Android Studio
QuickBirdEng / opencv-android
I found two options:
- shell script (reference)
- Install Git Bash and set it in
PATH
(C:\Program Files\Git\cmd
) - Install Cmake 3.24.3 and set it in
PATH
(C:\Program Files\CMake\bin
) - Install java and set it in
PATH
(C:\Program Files\Java\jdk1.8.0_202\bin
), create variableJAVA_HOME
(C:\Program Files\Java\jdk1.8.0_202
) - Install C compiler (mingw or Visual Studio or etc.). If you use
mingw
, then you will need to install:mingw32-base-bin
,
mingw32-gcc-fortran-bin
,
mingw32-gcc-g++-bin
,
mingw32-gcc-objc-bin
,
msys-base-bin
. And then inPATH
addC:\MinGW\bin
- You may also need
WSL for Windows
, but I did it just throughGit Bash
- Create a script in an empty folder and name it
build.sh
:
#!/bin/bash -e
myRepo=$(pwd)
CMAKE_GENERATOR_OPTIONS=-G"Visual Studio 17 2022"
#CMAKE_GENERATOR_OPTIONS=-G"Visual Studio 15 2017 Win64"
#CMAKE_GENERATOR_OPTIONS=(-G"Visual Studio 16 2019" -A x64) # CMake 3.14+ is required
if [ ! -d "$myRepo/gstreamer" ]; then
echo "cloning gstreamer"
git clone https://github.com/GStreamer/gstreamer.git
else
cd gstreamer
git pull --rebase
cd ..
fi
if [ ! -d "$myRepo/openexr" ]; then
echo "cloning openexr"
git clone https://github.com/AcademySoftwareFoundation/openexr.git
else
cd openexr
git pull --rebase
cd ..
fi
if [ ! -d "$myRepo/openblas" ]; then
echo "cloning openblas"
git clone https://github.com/xianyi/OpenBLAS.git
else
cd openblas
git pull --rebase
cd ..
fi
if [ ! -d "$myRepo/eigen" ]; then
echo "cloning eigen"
git clone https://gitlab.com/libeigen/eigen.git
else
cd eigen
git pull --rebase
cd ..
fi
if [ ! -d "$myRepo/opencv" ]; then
echo "cloning opencv"
git clone https://github.com/opencv/opencv.git
else
cd opencv
git pull --rebase
cd ..
fi
if [ ! -d "$myRepo/opencv_contrib" ]; then
echo "cloning opencv_contrib"
git clone https://github.com/opencv/opencv_contrib.git
else
cd opencv_contrib
git pull --rebase
cd ..
fi
if [ ! -d "$myRepo/tesseract" ]; then
echo "cloning tesseract"
git clone https://github.com/tesseract-ocr/tesseract.git
else
cd tesseract
git pull --rebase
cd ..
fi
RepoSource=opencv
mkdir -p build_opencv
pushd build_opencv
CMAKE_OPTIONS=( -DBUILD_opencv_world:BOOL=OFF -DBUILD_JAVA:BOOL=ON -DBUILD_PERF_TESTS:BOOL=OFF -DBUILD_TESTS:BOOL=OFF -DBUILD_DOCS:BOOL=OFF -DWITH_CUDA:BOOL=OFF -DBUILD_EXAMPLES:BOOL=OFF -DINSTALL_CREATE_DISTRIB=ON)
set -x
cmake "${CMAKE_GENERATOR_OPTIONS[@]}" "${CMAKE_OPTIONS[@]}" -DOPENCV_EXTRA_MODULES_PATH="$myRepo"/opencv_contrib/modules -DCMAKE_INSTALL_PREFIX="$myRepo/install/$RepoSource" "$myRepo/$RepoSource"
echo "************************* $Source_DIR -->debug"
cmake --build . --config debug
echo "************************* $Source_DIR -->release"
cmake --build . --config release
cmake --build . --target install --config release
cmake --build . --target install --config debug
popd
-
Open Git Bash, open the folder with our script in it and write
bash build.sh
-
If you have following error
syntax error: unexpected end of file
, then here is the solution -
As result we get opencv_java460.dll and opencv-460.jar
- Cmake with ninja (taken from here, but modified)
Here I will describe what I used to build (it’s not a fact that this is all I need or specific versions, but it worked for me):
-
Install Python and set it in
PATH
(C:\Users\volce\AppData\Local\Programs\Python\Python311\Scripts\
иC:\Users\volce\AppData\Local\Programs\Python\Python311\
) -
Install java 8 and set it in
PATH
and inJAVA_HOME
(see above) -
Install
Cmake 3.24.3
(I haven’t tried it on another version) and set it inPATH
(see above) -
Download NDK r25, unzip and add an environment variable
ANDROID_NDK: C:\Users\volce\AppData\Local\Android\Sdk\ndk\android-ndk-r25b
-
Download SDK tools r25.2.3, platform-tools r25.0.1 and build-tools r25.0.1
-
In the SDK folder, replace the contents of the
tools
,build-tools
andplatform-tools
folders with the downloaded ones -
Also, in
PATH
I added:C:\Users\volce\AppData\Local\Android\Sdk\tools
andC:\Users\volce\AppData\Local\Android\Sdk\platform-tools
-
Adding environment variables:
ANDROID_HOME
andANDROID_SDK_ROOT
, where we specify the path to the SDK (for example,C:\Users\volce\AppData\Local\Android\Sdk
) -
Download ant and set it in
PATH
(C:\apache-ant-1.10.12\bin
иC:\apache-ant-1.10.12\lib
) and createANT_HOME
(C:\apache-ant-1.10.12
) -
Download
MinGw
, install the necessary packages and set it inPATH
(see above) -
Download and install
Visual Studio
What did I do next
-
Created a folder for our task
-
In it we do
git clone https://github.com/opencv/opencv.git
andgit clone https://github.com/opencv/opencv_contrib.git
-
In the folder with
opencv
, go tosamples\android
and in the fileCMakeLists.txt
deletingadd_subdirectory(15-puzzle)
(because of this, I did not build) -
Create a
build
folder in our root folder (which was created for building opencv) -
We go into it and do
git clone https://github.com/ninja-build/ninja.git
-
Open Visual Studio. In it: Tools->Command Line->Developer Command Line. In the command line, write the path to the created folder
build
and writepython configure.py --bootstrap
. This is how we configuredninja
-
Being in our
build
folder in the console, we write the following(don’t forget to change the paths to your own):
cmake -GNinja -DCMAKE_MAKE_PROGRAM=E:\OpenCV\build\ninja.exe -DCMAKE_INSTALL_PREFIX=E:\OpenCV\install -DANDROID_PROJECTS_BUILD_TYPE="ANT" -DBUILD_ANDROID_PROJECTS=ON -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=ON -DANDROID_NDK_HOST_X64=ON -DANDROID_NDK=C:/Users/volce/AppData/Local/Android/Sdk/ndk/android-ndk-r25b/ -DANDROID_SDK=C:/Users/volce/AppData/Local/Android/Sdk -DCMAKE_TOOLCHAIN_FILE=C:/Users/volce/AppData/Local/Android/Sdk/ndk/android-ndk-r25b/build/cmake/android.toolchain.cmake -DANDROID_TOOLCHAIN=clang -DANDROID_STL=c++_static -DANDROID_ARM_NEON=ON -DANDROID_ABI=arm64-v8a -DANDROID_ABI=x86_64 -DANDROID_ABI=x86 -DANDROID_ABI=armeabi-v7a -DANDROID_NDK_HOST_X64=ON -DBUILD_opencv_python3:BOOL=OFF -DBUILD_opencv_python2:BOOL=OFF -DOPENCV_EXTRA_MODULES_PATH=E:/OpenCV/opencv_contrib/modules -DOPENCV_ENABLE_NONFREE=ON -DANDROID_NATIVE_API_LEVEL=25 -DANDROID_TARGET_SDK_VERSION=32 -DANDROID_MIN_SDK_VERSION=21 E:\OpenCV\opencv
-
After execution , write
ninja -j8
in console -
After waiting for the execution of the previous command, we write
ninja install
-
As result we get the OpenCV SDK for Android
On Windows, some months ago I finally managed to make a full built in just some easy and short steps.
Just install MSYS, ANT and Ninja and python
Also Android SDK and NDK bundles are needed.
ANT, SDK and NDK need to be adressed on the Enviroment Variables.
The simplest magic is on “sources\platforms\android\build_sdk.py”
Like this:
python build_sdk.py --ndk_path=“f:\ANDROID\SDK\ndk-bundle” --sdk_path=“f:\ANDROID\SDK\” --extra_modules_path=“c:\LIBs\opencv 4.5.1 contrib\modules” “c:\LIBs\opencvAndroid\” “c:\LIBs\opencv 4.5.1\sources\” --opencl --no_samples_build
But the real work is cmake and several variables. Like this:
f:\ANDROID\SDK\cmake\3.10.2.4988404\bin\cmake -GNinja -DOPENCV_EXTRA_MODULES_PATH=‘c:\LIBs\opencv 4.5.1 contrib\modules’ -DBUILD_TESTS=ON -DINSTALL_TESTS=ON -DCMAKE_TOOLCHAIN_FILE=‘f:\ANDROID\SDK\ndk-bundle\build\cmake\android.toolchain.cmake’ -DINSTALL_CREATE_DISTRIB=‘ON’ -DWITH_OPENCL=‘ON’ -DWITH_IPP=‘OFF’ -DWITH_TBB=‘ON’ -DBUILD_EXAMPLES=‘OFF’ -DBUILD_TESTS=‘OFF’ -DBUILD_PERF_TESTS=‘OFF’ -DBUILD_DOCS=‘OFF’ -DBUILD_ANDROID_EXAMPLES=‘OFF’ -DINSTALL_ANDROID_EXAMPLES=‘OFF’ -DANDROID_STL=‘c++_shared’ -DANDROID_ABI=‘arm64-v8a, armeabi-v7a’ -DANDROID_PLATFORM_ID=‘2’ -DANDROID_TOOLCHAIN=‘clang’ -DANDROID_NATIVE_API_LEVEL=‘21’ -DOPENCV_ENABLE_NONFREE=‘ON’ -DBUILD_opencv_xfeatures2d=‘ON’ -DCMAKE_INSTALL_PREFIX='c:\LIBs\opencvAndroid\o4a' -DOPENCV_JNI_INSTALL_PATH='c:\LIBs\opencvAndroid\o4a\jni' c:\LIBs\opencv 4.5.1\sources
or…
f:\ANDROID\SDK\cmake\3.10.2.4988404\bin\cmake -GNinja -DOPENCV_EXTRA_MODULES_PATH=‘c:\LIBs\opencv 4.5.1 contrib\modules’ -DBUILD_TESTS=ON -DINSTALL_TESTS=ON -DCMAKE_TOOLCHAIN_FILE=‘f:\ANDROID\SDK\ndk-bundle\build\cmake\android.toolchain.cmake’ -DINSTALL_CREATE_DISTRIB=‘ON’ -DWITH_OPENCL=‘ON’ -DWITH_IPP=‘OFF’ -DWITH_TBB=‘ON’ -DBUILD_EXAMPLES=‘OFF’ -DBUILD_TESTS=‘OFF’ -DBUILD_PERF_TESTS=‘OFF’ -DBUILD_DOCS=‘OFF’ -DBUILD_ANDROID_EXAMPLES=‘OFF’ -DINSTALL_ANDROID_EXAMPLES=‘OFF’ -DANDROID_STL=‘c++_shared’ -DANDROID_ABI=‘arm64-v8a’ -DANDROID_PLATFORM_ID=‘2’ -DANDROID_TOOLCHAIN=‘clang’ -DANDROID_NATIVE_API_LEVEL=‘21’ -DOPENCV_ENABLE_NONFREE=‘ON’ -DBUILD_opencv_xfeatures2d=‘ON’ c:\LIBs\opencv 4.5.1\sources
I just change variables values as see fit and then rebuild.
dont know why but I need to force the arm64-v8a ABI on “ndk-18-api-level-21.config.py” when I need to build for this arch.
I have 4.5.1 full built for ARMv7 and ARMv8, Android API Level 21+, including OpenCL enabled. I can send to you the binaries if you want.
Or wait till I write an step by step tutorial.
I’ll wait for the step-by-step guide. Can tou tag me when you post the guide?
sure.
even I need the guide, I forgot some steps
but is easy once everything is set, and is not a lot
Did you finish your guide?