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

**URL:** <https://forum.opencv.org/t/is-there-a-step-by-step-guide-on-how-to-build-opencv-with-extra-modules-for-andoird-in-2022/10973>\
**Category:** Android/Java\
**Tags:** android, build\
**Created:** [November 15, 2022, 8:05pm UTC](https://forum.opencv.org/t/is-there-a-step-by-step-guide-on-how-to-build-opencv-with-extra-modules-for-andoird-in-2022/10973 "2022-11-15T20:05:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamada](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@Tamada](https://forum.opencv.org/u/Tamada)\
**Post date:** [November 15, 2022, 8:05pm UTC](https://forum.opencv.org/t/is-there-a-step-by-step-guide-on-how-to-build-opencv-with-extra-modules-for-andoird-in-2022/10973/1 "2022-11-15T20:05:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![HISAK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/hisak/32/6544_2.png) [@HISAK](https://forum.opencv.org/u/HISAK)\
**Post date:** [November 21, 2022, 1:03am UTC](https://forum.opencv.org/t/is-there-a-step-by-step-guide-on-how-to-build-opencv-with-extra-modules-for-andoird-in-2022/10973/2 "2022-11-21T01:03:57Z")

</div>

I am also struggling with it, and I think we have same problems.

---

<div class="post-metadata">

**Author:** ![Tamada](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@Tamada](https://forum.opencv.org/u/Tamada)\
**Post date:** [November 22, 2022, 10:25pm UTC](https://forum.opencv.org/t/is-there-a-step-by-step-guide-on-how-to-build-opencv-with-extra-modules-for-andoird-in-2022/10973/4 "2022-11-22T22:25:07Z")

</div>

I’m still trying to find a working way for me. Do you have any success?

---

<div class="post-metadata">

**Author:** ![HISAK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/hisak/32/6544_2.png) [@HISAK](https://forum.opencv.org/u/HISAK)\
**Post date:** [November 23, 2022, 1:03pm UTC](https://forum.opencv.org/t/is-there-a-step-by-step-guide-on-how-to-build-opencv-with-extra-modules-for-andoird-in-2022/10973/6 "2022-11-23T13:03:14Z")

</div>

> [@Tamada](#):
>
> 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](https://www.youtube.com/watch?v=ECyOmSMgsPI&t=105s)  
[QuickBirdEng / opencv-android](https://github.com/QuickBirdEng/opencv-android)

---

<div class="post-metadata">

**Author:** ![Tamada](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@Tamada](https://forum.opencv.org/u/Tamada)\
**Post date:** [November 24, 2022, 7:26pm UTC](https://forum.opencv.org/t/is-there-a-step-by-step-guide-on-how-to-build-opencv-with-extra-modules-for-andoird-in-2022/10973/10 "2022-11-24T19:26:24Z")

</div>

I found two options:

1. **shell script** ([reference](https://ru.stackoverflow.com/questions/1464972/%D0%9D%D0%B5-%D0%BC%D0%BE%D0%B3%D1%83-%D0%B8%D0%BC%D0%BF%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C-%D0%BD%D0%B5%D0%BA%D0%BE%D1%82%D0%BE%D1%80%D1%8B%D0%B5-%D0%BF%D0%B0%D0%BA%D0%B5%D1%82%D1%8B-javacv#comment2629052_1464972))

- Install [Git Bash](https://gitforwindows.org/) and set it in `PATH` (`C:\Program Files\Git\cmd`)
- Install [Cmake 3.24.3](https://cmake.org/download/) and set it in `PATH` (`C:\Program Files\CMake\bin`)
- Install [java](https://www.oracle.com/java/technologies/downloads/) 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](https://osdn.net/projects/mingw/downloads/68260/mingw-get-setup.exe/) or [Visual Studio](https://visualstudio.microsoft.com/vs/) 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 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 `build.sh`:

```bash
#!/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](https://stackoverflow.com/a/6366607/18750563) is the solution

- As result we get opencv\_java460.dll and opencv-460.jar

1. **Cmake with ninja** (taken from [here](https://youtu.be/C7CiuWyLkr8), 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](https://www.python.org/downloads/) 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](https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html) 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](https://developer.android.com/ndk/downloads), 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](https://dl.google.com/android/repository/tools_r25.2.3-windows.zip), [platform-tools r25.0.1](https://dl.google.com/android/repository/platform-tools_r25.0.1-windows.zip) and [build-tools r25.0.1](https://dl.google.com/android/repository/build-tools_r25.0.1-windows.zip)

- 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](https://ant.apache.org/bindownload.cgi) 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 https://github.com/opencv/opencv.git` and `git clone https://github.com/opencv/opencv_contrib.git`

- 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 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 write `python configure.py --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** ):

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![kinetix](https://avatars.discourse-cdn.com/v4/letter/k/8edcca/32.png) [@kinetix](https://forum.opencv.org/u/kinetix)\
**Post date:** [December 8, 2022, 3:58am UTC](https://forum.opencv.org/t/is-there-a-step-by-step-guide-on-how-to-build-opencv-with-extra-modules-for-andoird-in-2022/10973/11 "2022-12-08T03:58:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tamada](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@Tamada](https://forum.opencv.org/u/Tamada)\
**Post date:** [December 8, 2022, 7:52am UTC](https://forum.opencv.org/t/is-there-a-step-by-step-guide-on-how-to-build-opencv-with-extra-modules-for-andoird-in-2022/10973/12 "2022-12-08T07:52:30Z")

</div>

I’ll wait for the step-by-step guide. Can tou tag me when you post the guide?

---

<div class="post-metadata">

**Author:** ![kinetix](https://avatars.discourse-cdn.com/v4/letter/k/8edcca/32.png) [@kinetix](https://forum.opencv.org/u/kinetix)\
**Post date:** [December 8, 2022, 9:18pm UTC](https://forum.opencv.org/t/is-there-a-step-by-step-guide-on-how-to-build-opencv-with-extra-modules-for-andoird-in-2022/10973/13 "2022-12-08T21:18:11Z")

</div>

sure.  
even I need the guide, I forgot some steps 🙂

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

---

<div class="post-metadata">

**Author:** ![Tamada](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@Tamada](https://forum.opencv.org/u/Tamada)\
**Post date:** [February 9, 2023, 7:28pm UTC](https://forum.opencv.org/t/is-there-a-step-by-step-guide-on-how-to-build-opencv-with-extra-modules-for-andoird-in-2022/10973/14 "2023-02-09T19:28:46Z")

</div>

Did you finish your guide? 👉 👈
