1

I have to to build opencv from source due to opencv_contrib. Although I have tried many tutorials, I faild to build opencv from source.

The code I used:

cmake -G"MinGW Makefiles" -DANDROID_NDK=D:/androidNDK/android-ndk-r22b -DANDROID_SDK_ROOT=D:/android-sdk -DCMAKE_TOOLCHAIN_FILE=D:/androidNDK/android-ndk-r22b/build/cmake/android.toolchain.cmake -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF D:/OpenCV455/opencv-4.5.5

General configuration:

-- General configuration for OpenCV 4.5.5 =====================================
--   Version control:               unknown
--
--   Platform:
--     Timestamp:                   2022-11-19T00:58:08Z
--     Host:                        Windows 10.0.19043 AMD64
--     Target:                      Android 1 armv7-a
--     CMake:                       3.21.3
--     CMake generator:             MinGW Makefiles
--     CMake build tool:            D:/MinGW1/mingw64/bin/mingw32-make.exe
--     Configuration:               Release
--
--   CPU/HW features:
--     Baseline:                    NEON
--       requested:                 DETECT
--
--   C/C++:
--     Built as dynamic libs?:      NO
--     C++ standard:                11
--     C++ Compiler:                D:/androidNDK/android-ndk-r22b/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe  (ver 11.0.5)
--     C++ flags (Release):         -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security     -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -Qunused-arguments  -fvisibility=hidden -fvisibility-inlines-hidden -Oz -DNDEBUG   -DNDEBUG
--     C++ flags (Debug):           -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security     -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -Qunused-arguments  -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info   -DDEBUG -D_DEBUG
--     C Compiler:                  D:/androidNDK/android-ndk-r22b/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
--     C flags (Release):           -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -Qunused-arguments  -fvisibility=hidden -fvisibility-inlines-hidden -Oz -DNDEBUG   -DNDEBUG
--     C flags (Debug):             -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -Qunused-arguments  -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info   -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments   -Wl,--as-needed
--     Linker flags (Debug):        -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments   -Wl,--as-needed
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          z dl m log
--     3rdparty dependencies:       libcpufeatures libprotobuf ade ittnotify libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf quirc tegra_hal
--
--   OpenCV modules:
--     To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc java ml objdetect photo stitching video videoio
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 python2 python3 ts
--     Applications:                android_examples
--     Documentation:               NO
--     Non-free algorithms:         NO
--
--   Android NDK:                   D:/androidNDK/android-ndk-r22b (ver 22.1.7171670)
--     Android ABI:                 armeabi-v7a
--     NDK toolchain:               arm-linux-androideabi-clang
--     STL type:                    c++_static
--     Native API level:            16
--   Android SDK:                   D:/android-sdk (tools: 25.2.5 build tools: 33.0.0)
--     android tool:                D:/android-sdk/tools/android.bat
--     SDK target:                  android-21
--     Projects build scripts:      Ant/Eclipse compatible
--
--   GUI:                           NONE
--
--   Media I/O:
--     ZLib:                        z (ver 1.2.11)
--     JPEG:                        build-libjpeg-turbo (ver 2.1.2-62)
--     WEBP:                        build (ver encoder: 0x020f)
--     PNG:                         build (ver 1.6.37)
--     TIFF:                        build (ver 42 - 4.2.0)
--     JPEG 2000:                   build (ver 2.4.0)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
--
--   Video I/O:
--
--   Parallel framework:            pthreads
--
--   Trace:                         YES (with Intel ITT)
--
--   Other third-party libraries:
--     Custom HAL:                  YES (carotene (ver 0.0.1))
--     Protobuf:                    build (3.19.1)
--
--   Python (for build):            D:/Python37/python.exe
--
--   Java:                          export all functions
--     ant:                         D:/apache-ant-1.9.16/bin/ant.bat (ver 1.9.16)
--     Java wrappers:               YES
--     Java tests:                  NO
--
--   Install to:                    D:/OpenCV455/buil36/install
-----------------------------------------------------------------**

The errors I get after running 'mingw32-make':

[97%] Built target opencv_java_android_source_copy
[97%]Generating OpenCV Android library project. SDK target:
[97%] Bui1ding OpenCV Android library project
[subant] No sub-builds to iterate on
Target '-code-gen' failed with message 'The following error occurred while executing this line:
D:\android-sdk\tools\ant\build.xml:655: null returned: -1073741819'.
Cannot execute '-compile' - '-code-gen' failed or was not executed.
Cannot execute '-dex' - '-compile' failed or was not executed.
Cannot execute '-package' - '-dex' failed or was not executed.
Cannot execute '-do-debug' - '-package' failed or was not executed.
Cannot execute 'debug' - '-do-debug' failed or was not executed.
BUILD FAILED
D:\android-sdk\tools\ant\build.xml:649: The following error occurred while executing this line:
D:\android-sdk\tools\ant\build.xml:655: null returned: -1073741819

Total time: 2 seconds
mingw32-make[2]: *** [modules\java\android_sdk\CMakeFiles\opencv_java_android.dir\build.make:77: bin/classes.jar] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:2689: modules/java/android_sdk/CMakeFiles/opencv_java_android.dir/all] Error 2

I get similar errors with ninja.

[1568/1595] Building OpenCV Android library project
FAILED: bin/classes.jar CMakeFiles/dephelper/opencv_java_android D:/OpenCV455/build36/bin/classes.jar D:/OpenCV455/build36/CMakeFiles/dephelper/opencv_java_android
cmd.exe /C "cd /D D:\OpenCV455\build36\android_sdk && D:\apache-ant-1.9.16\bin\ant.bat -q -noinput -k debug -Djava.target=1.6 -Djava.source=1.6 && D:\cmake-3.21.3\bin\cmake.exe -E touch D:/OpenCV455/build36/CMakeFiles/dephelper/opencv_java_android"
   [subant] No sub-builds to iterate on
Target '-code-gen' failed with message 'The following error occurred while executing this line:
D:\android-sdk\tools\ant\build.xml:655: null returned: -1073741819'.
Cannot execute '-compile' - '-code-gen' failed or was not executed.
Cannot execute '-dex' - '-compile' failed or was not executed.
Cannot execute '-package' - '-dex' failed or was not executed.
Cannot execute '-do-debug' - '-package' failed or was not executed.
Cannot execute 'debug' - '-do-debug' failed or was not executed.

BUILD FAILED
D:\android-sdk\tools\ant\build.xml:649: The following error occurred while executing this line:
D:\android-sdk\tools\ant\build.xml:655: null returned: -1073741819

Total time: 1 second
[1572/1595] Building CXX object modules/gapi/CMakeFiles/opencv_perf_gapi.dir/perf/gpu/gapi_core_perf_tests_gpu.cpp.o
ninja: build stopped: subcommand failed.

here is the code with error. in build.ninja file:

# Custom command for bin\classes.jar
build bin/classes.jar CMakeFiles/dephelper/opencv_java_android | ${cmake_ninja_workdir}bin/classes.jar ${cmake_ninja_workdir}CMakeFiles/dephelper/opencv_java_android: CUSTOM_COMMAND CMakeFiles/dephelper/opencv_java_android_source_copy android_sdk/build.xml android_sdk/local.properties android_sdk/proguard-project.txt android_sdk/project.properties android_sdk/AndroidManifest.xml || modules/java/android_sdk/opencv_java_android_source_copy modules/java_bindings_generator/gen_opencv_java_source
  COMMAND = cmd.exe /C "cd /D D:\OpenCV455\build34\android_sdk && D:\apache-ant-1.9.16\bin\ant.bat -q -noinput -k debug -Djava.target=1.6 -Djava.source=1.6 && D:\cmake-3.21.3\bin\cmake.exe -E touch D:/OpenCV455/build34/CMakeFiles/dephelper/opencv_java_android"
  DESC = Building OpenCV Android library project
  restat = 1

in build.cmake file:

bin/classes.jar: CMakeFiles/dephelper/opencv_java_android_source_copy
bin/classes.jar: android_sdk/build.xml
bin/classes.jar: android_sdk/local.properties
bin/classes.jar: android_sdk/proguard-project.txt
bin/classes.jar: android_sdk/project.properties
bin/classes.jar: android_sdk/AndroidManifest.xml
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --blue --bold --progress-dir=D:\OpenCV455\build41\CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building OpenCV Android library project"
    cd /d D:\OpenCV455\build41\android_sdk && call D:\apache-ant-1.9.16\bin\ant.bat -q -noinput -k debug -Djava.target=1.6 -Djava.source=1.6
    cd /d D:\OpenCV455\build41\android_sdk && D:\cmake-3.21.3\bin\cmake.exe -E touch D:/OpenCV455/build41/CMakeFiles/dephelper/opencv_java_android

CMakeFiles/dephelper/opencv_java_android: bin/classes.jar

in build.xml file:

<do-only-if-manifest-hasCode
                elseText="hasCode = false. Skipping aidl/renderscript/R.java">
            <echo level="info">Handling aidl files...</echo>
            <aidl executable="${aidl}"
                    framework="${project.target.framework.aidl}"
                    libraryBinAidlFolderPathRefid="project.library.bin.aidl.folder.path"
                    genFolder="${gen.absolute.dir}"
                    aidlOutFolder="${out.aidl.absolute.dir}">
                <source path="${source.absolute.dir}"/>
            </aidl>

How to fix it?

I try to find solutions for similar problem, and here maybe the only similar one. The developer suggetsted that:

Android SDK: not used, projects are not built After that message Java-based projects should be skipped completely. Try to upgrade your Android SDK "BuildTools". CMAKE_CONFIG_GENERATOR="MinGW Makefiles" There is ninja build tool in Android NDK. Use cmake -GNinja ... for better experience. I don't quite grasp how to do, My build tools version is 33.0.0.

Hisak
  • 11
  • 3

1 Answers1

0

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, 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:
#!/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

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

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, armeabi-v7a, x86_64, x86' -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

  • How to integrate OpenCV 4.6.0 in your project -> https://www.youtube.com/watch?v=bR7lL886-uc

Tamada
  • 3
  • 2