Is there a step-by-step guide on how to build OpenCV with extra modules for Andoird in 2022?

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 :sob:. 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:

  1. 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 variable JAVA_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,
    msys-base-bin. And then in PATH add C:\MinGW\bin
  • You may also need WSL for Windows, but I did it just through Git Bash
  • Create a script in an empty folder and name it
#!/bin/bash -e
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
    cd gstreamer
    git pull --rebase
    cd ..
if [  ! -d "$myRepo/openexr"  ]; then
    echo "cloning openexr"
    git clone
    cd openexr
    git pull --rebase
    cd ..
if [  ! -d "$myRepo/openblas"  ]; then
    echo "cloning openblas"
    git clone
    cd openblas
    git pull --rebase
    cd ..
if [  ! -d "$myRepo/eigen"  ]; then
    echo "cloning eigen"
    git clone
    cd eigen
    git pull --rebase
    cd ..
if [  ! -d "$myRepo/opencv"  ]; then
    echo "cloning opencv"
    git clone
    cd opencv
    git pull --rebase
    cd ..
if [  ! -d "$myRepo/opencv_contrib"  ]; then
    echo "cloning opencv_contrib"
    git clone
    cd opencv_contrib
    git pull --rebase
    cd ..
if [  ! -d "$myRepo/tesseract"  ]; then
    echo "cloning tesseract"
    git clone
    cd tesseract
    git pull --rebase
    cd ..
mkdir -p build_opencv
pushd build_opencv
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
  • Open Git Bash, open the folder with our script in it and write bash

  • 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

  1. 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 in JAVA_HOME (see above)

  • Install Cmake 3.24.3 (I haven’t tried it on another version) and set it in PATH (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 and platform-tools folders with the downloaded ones

  • Also, in PATH I added: C:\Users\volce\AppData\Local\Android\Sdk\tools and C:\Users\volce\AppData\Local\Android\Sdk\platform-tools

  • Adding environment variables: ANDROID_HOME and ANDROID_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 create ANT_HOME (C:\apache-ant-1.10.12)

  • Download MinGw, install the necessary packages and set it in PATH (see above)

  • Download and install Visual Studio

What did I do next

  • Created a folder for our task

  • In it we do git clone and git clone

  • In the folder with opencv, go to samples\android and in the file CMakeLists.txt deleting add_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

  • Open Visual Studio. In it: Tools->Command Line->Developer Command Line. In the command line, write the path to the created folder build and write python --bootstrap. This is how we configured ninja

  • Being in our build folder in the console, we write the following(don’t forget to change the paths to your own):

  • 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\”
Like this:

python --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:




I just change variables values as see fit and then rebuild.

dont know why but I need to force the arm64-v8a ABI on “” 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?

even I need the guide, I forgot some steps :slight_smile:

but is easy once everything is set, and is not a lot

Did you finish your guide? :point_right: :point_left: