flashsturz
flashsturz

Reputation: 13

Cross-compile OpenCV on Linux Host for Android Target: Errors during Cmake configuration

Problem Description:

I am trying to compile OpenCV 4.5.1 on Ubuntu 20.04 LTS for Android (aarch64) with OpenCL support (since the stock OpenCV Android SDK does not have OpenCL support activated). I largely followed this tutorial covering the cross-compilation of OpenCV.

When trying to configure the build, Cmake seems to be unable to correctly create/locate all the necessary folders and thus throws an error (see cmake output below).

I suspect that it might have something to do with Cmake struggling to recognise the ant installation correctly (as indicated by the missing version number following the ant path in the Cmake output).

I have set the environment variables for the ANT executable as well as the Java Home folder through the Cmake build flags (see Cmake command below).

When looking at the error messages at the bottom of the Cmake output, it becomes clear that there is some problem with creating the necessary Android and/or Java folders. Upon inspection of modules/java/android_sdk/CMakeLists.txt (see extract below) I noticed that the variable OPENCV_JAVA_DIR should probably be set at some earlier stage in the cmake configuration. However, when running my command pasted below, this variable remains empty and hence cmake cannot access the folders since the path is not correct.

These are the values of the variables when running my cmake configuration command:

-- OPENCV_JAVA_DIR = /opencv
-- ANDROID_EXECUTABLE = FALSE
-- ANDROID_BUILD_BASE_DIR = 
-- OpenCV_BINARY_DIR= /home/flavio/Downloads/opencv/build_manual

Is there some configuration error in my Cmake buildflags or something entirely different that I am doing wrong?

Thanks for you help!

Related Questions

Cmake cannot find ant

Cmake cannot recognise Java

Ant for Java not showing as installed eventhough it is

I have tried all the proposed changes, so far with no success. Reinstallation of ant did not help, neither creating a symlink to the executable store at /usr/share/ant/bin/ant in /usr/bin.

Cmake Command used (in build folder inside the downloaded opencv-source):

cmake \
-DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk/21.0.6113669/build/cmake/android.toolchain.cmake \
-DANDROID_TOOLCHAIN=clang++ \
-DANDROID_ABI=arm64-v8a \
-DANDROID_TARGET_SDK_VERSION=26 \
-DANDROID_MIN_SDK_VERSION=24 \
-DBUILD_JAVA=ON -DBUILD_FAT_JAVA_LIB=ON DBUILD_ZLIB=ON \
-D CMAKE_BUILD_TYPE=Release \
-D ANDROID_NATIVE_API_LEVEL=24 \
-DANDROID_COMPILE_SDK_VERSION=26 \
-D ANDROID_STL=c++_shared \
-D WITH_CUDA=OFF \
-D WITH_MATLAB=OFF \
-D BUILD_ANDROID_EXAMPLES=OFF -D BUILD_DOCS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=ON -D BUILD_SHARED_LIBS=OFF -DBUILD_ANDROID_PROJECTS=OFF -DBUILD_JNI=ON \
-DWITH_OPENCL=ON -DWITH_OPENGL=ON \
-DANT_HOME=/usr/share/ant -DJAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 -DANT_EXECUTABLE=/bin/ant \
-DSTRIP=/home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip \
..

Extract from modules/java/android_sdk/CMakeList.txt:

project(${the_module}_android)

if(ANDROID_EXECUTABLE)
  set(OPENCV_JAVA_DIR "${OpenCV_BINARY_DIR}/android_sdk" CACHE INTERNAL "")
else()  # gradle
  set(OPENCV_JAVA_DIR "${ANDROID_BUILD_BASE_DIR}/opencv" CACHE INTERNAL "")
endif()
set(OPENCV_ANDROID_LIB_DIR "${OPENCV_JAVA_DIR}" CACHE INTERNAL "")  # for OpenCV samples

message (STATUS "OPENCV_JAVA_DIR = ${OPENCV_JAVA_DIR}")
message (STATUS "ANDROID_BUILD_BASE_DIR = ${ANDROID_BUILD_BASE_DIR}")
message (STATUS "OpenCV_BINARY_DIR= ${OpenCV_BINARY_DIR}")
file(REMOVE_RECURSE "${OPENCV_JAVA_DIR}")
file(MAKE_DIRECTORY "${OPENCV_JAVA_DIR}/bin")
set(java_src_dir "${OPENCV_JAVA_DIR}/src")
file(MAKE_DIRECTORY "${java_src_dir}")

Cmake terminal output

-- Check for working CXX compiler: /home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++
-- Check for working CXX compiler: /home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- Check for working C compiler: /home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Detected processor: aarch64
CMake Warning at cmake/OpenCVUtils.cmake:752 (message):
  Unexpected option: WITH_OPENGL (=ON)

  Condition: IF (NOT;ANDROID;AND;NOT;WINRT)
Call Stack (most recent call first):
  CMakeLists.txt:308 (OCV_OPTION)


-- Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.18", minimum required is "2.7") 
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3.2") 
-- Looking for ccache - not found
<---------- Performed Tests have been cut out from this output to meet character-limit of stackoverflow questions ----------->
-- ANDROID: TRUE
-- Update variable ANDROID_SDK_ROOT from environment: /home/flavio/Android/Sdk
-- Update variable ANDROID_HOME from environment: /home/flavio/Android/Sdk
Android: Projects builds are DISABLED
-- Looking for dlerror in dl
-- Looking for dlerror in dl - found
-- ADE: Download: v0.1.1f.zip
-- Allocator metrics storage type: 'int'
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.sse2.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.sse3.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.ssse3.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.sse4_1.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.sse4_2.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.avx.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.avx2.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.avx512_skx.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin256.avx2.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin256.avx512_skx.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin512.avx512_skx.cpp
-- Excluding from source files list: modules/imgproc/src/corner.avx.cpp
-- Excluding from source files list: modules/imgproc/src/imgwarp.avx2.cpp
-- Excluding from source files list: modules/imgproc/src/imgwarp.sse4_1.cpp
-- Excluding from source files list: modules/imgproc/src/resize.avx2.cpp
-- Excluding from source files list: modules/imgproc/src/resize.sse4_1.cpp
-- Registering hook 'INIT_MODULE_SOURCES_opencv_dnn': /home/flavio/Downloads/opencv/modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake
-- opencv_dnn: filter out cuda4dnn source code
-- Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx.cpp
-- Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx2.cpp
-- Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx512_skx.cpp
-- Excluding from source files list: modules/features2d/src/fast.avx2.cpp
-- OPENCV_JAVA_DIR = /opencv
-- ANDROID_BUILD_BASE_DIR = 
-- OpenCV_BINARY_DIR= /home/flavio/Downloads/opencv/build_manual
CMake Error at modules/java/android_sdk/CMakeLists.txt:15 (file):
  file problem creating directory: /opencv/bin


CMake Error at modules/java/android_sdk/CMakeLists.txt:17 (file):
  file problem creating directory: /opencv/src


CMake Error: Could not open file for write in copy operation /opencv/AndroidManifest.xml.tmp
CMake Error: : System Error: No such file or directory
CMake Error at modules/java/android_sdk/CMakeLists.txt:121 (configure_file):
  configure_file Problem configuring file


CMake Error: Could not open file for write in copy operation /opencv/build.gradle.tmp
CMake Error: : System Error: No such file or directory
CMake Error at modules/java/android_sdk/CMakeLists.txt:121 (configure_file):
  configure_file Problem configuring file


CMake Error: Could not open file for write in copy operation /opencv/res/values/attrs.xml.tmp
CMake Error: : System Error: No such file or directory
CMake Error at modules/java/android_sdk/CMakeLists.txt:121 (configure_file):
  configure_file Problem configuring file


CMake Error: Could not open file for write in copy operation /opencv/libcxx_helper/CMakeLists.txt.tmp
CMake Error: : System Error: No such file or directory
CMake Error at modules/java/android_sdk/CMakeLists.txt:133 (configure_file):
  configure_file Problem configuring file


CMake Error: Could not open file for write in copy operation /opencv/libcxx_helper/dummy.cpp.tmp
CMake Error: : System Error: No such file or directory
CMake Error at modules/java/android_sdk/CMakeLists.txt:133 (configure_file):
  configure_file Problem configuring file


CMake Error: Could not open file for write in copy operation /opencv/build.gradle.tmp
CMake Error: : System Error: No such file or directory
CMake Error at modules/java/android_sdk/CMakeLists.txt:160 (configure_file):
  configure_file Problem configuring file


-- Android OpenCV Manager is ignored
-- 
-- General configuration for OpenCV 4.5.1 =====================================
--   Version control:               unknown
-- 
--   Platform:
--     Timestamp:                   2022-03-20T09:57:50Z
--     Host:                        Linux 5.13.0-35-generic x86_64
--     Target:                      Android 1 aarch64
--     CMake:                       3.16.3
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               Release
-- 
--   CPU/HW features:
--     Baseline:                    NEON FP16
-- 
--   C/C++:
--     Built as dynamic libs?:      NO
--     C++ standard:                11
--     C++ Compiler:                /home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++  (ver 9.0)
--     C++ flags (Release):         -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -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 -fdiagnostics-show-option -Qunused-arguments    -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG   -DNDEBUG
--     C++ flags (Debug):           -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -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 -fdiagnostics-show-option -Qunused-arguments    -fvisibility=hidden -fvisibility-inlines-hidden -O0   -DDEBUG -D_DEBUG
--     C Compiler:                  /home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
--     C flags (Release):           -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -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 -fdiagnostics-show-option -Qunused-arguments    -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG   -DNDEBUG
--     C flags (Debug):             -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -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 -fdiagnostics-show-option -Qunused-arguments    -fvisibility=hidden -fvisibility-inlines-hidden -O0   -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments   -Wl,--as-needed  
--     Linker flags (Debug):        -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments   -Wl,--as-needed  
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          ade dl m log
--     3rdparty dependencies:       libcpufeatures ittnotify libprotobuf zlib 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 ts video videoio
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 python2 python3
--     Applications:                tests
--     Documentation:               NO
--     Non-free algorithms:         NO
-- 
--   Android NDK:                   /home/flavio/Android/Sdk/ndk/21.0.6113669 (ver 21.0.6113669)
--     Android ABI:                 arm64-v8a
--     NDK toolchain:               aarch64-linux-android-4.9
--     STL type:                    c++_shared
--     Native API level:            24
--   Android SDK:                   not used, projects are not built
-- 
--   GUI: 
--     OpenGL support:              NO
-- 
--   Media I/O: 
--     ZLib:                        build (ver 1.2.11)
--     JPEG:                        build-libjpeg-turbo (ver 2.0.6-62)
--     WEBP:                        build (ver encoder: 0x020f)
--     PNG:                         build (ver 1.6.37)
--     TIFF:                        build (ver 42 - 4.0.10)
--     JPEG 2000:                   build (ver 2.3.1)
--     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.5.1)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/flavio/Downloads/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python (for build):            /usr/bin/python2.7
-- 
--   Java:                          export all functions
--     ant:                         /bin/ant (ver )
--     Java wrappers:               YES
--     Java tests:                  NO
-- 
--   Install to:                    /home/flavio/Downloads/opencv/build_manual/install
-- -----------------------------------------------------------------
-- 
-- Configuring incomplete, errors occurred!
See also "/home/flavio/Downloads/opencv/build_manual/CMakeFiles/CMakeOutput.log".
See also "/home/flavio/Downloads/opencv/build_manual/CMakeFiles/CMakeError.log".

Upvotes: 0

Views: 1212

Answers (1)

z b
z b

Reputation: 1

I had similar errors, so I fixed it by turning on -DBUILD_ANDROID_PROJECTS=ON and fix all incompatible "SDK TOOL version" or "gradle versions". Then gradle can set all necessary paths like ANDROID_BUILD_BASE_DIR in modules/java/android_sdk/CMakeLists.txt:15.

For example to update SDK Tools: Open android studio > setting > system settings > Android SDK > SDK Tools > update Android SDK build tools and NDK(side by side)

Upvotes: 0

Related Questions