OpenCV 4.X for Android built with cmake on Linux

Hi,

I need help building .so files and header files for:

  • arm64-v8a
  • armeabi-v7a
  • x86
  • x86_64

I am interested in any 4.X version which works.

I tried with 4.0.1, 4.5.3, 4.5.2 releases from Git.

    cmake \
        -D CMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_HOME_21}/build/cmake/android.toolchain.cmake \
        -D OPENCV_EXTRA_MODULES_PATH=$SOURCE/opencv/opencv_contrib/modules/ \
        -D BUILD_SHARED_LIBS=ON \
        -D BUILD_TESTS=OFF \
        -D BUILD_PERF_TESTS=OFF \
        -D ANDROID_STL=c++_static \
        -D BUILD_ANDROID_EXAMPLES=OFF \
        -D BUILD_ANDROID_PROJECTS=OFF \
        -D BUILD_JAVA=OFF \
        -D WITH_QT=OFF \
        -D ANDROID_ABI=$ARCH \
        -D ANDROID_NATIVE_API_LEVEL=$ANDROID_VERSION \
        -D CMAKE_BUILD_TYPE=Release \
        -D 3P_LIBRARY_OUTPUT_PATH=$ROOT/output/$OUTARCH/lib/ \
        -D CMAKE_INSTALL_PREFIX:PATH=$ROOT/output/$OUTARCH/lib/ \
        ..

I always get an error that -- Configuring incomplete, errors occurred!

Some of the errors from CMakeError.log:

cpp:21:25: error: use of undeclared identifier 'vcvt_f16_f32'
    float16x4_t v_dst = vcvt_f16_f32(v_src);

CheckIncludeFile.c:1:10: fatal error: 'io.h' file not found

Can somebody please give me some directions?

UPDATE: I am able to build without EXTRA MODULES.

1 Like

the extra modules (contrib) can be a hassle. if you have trouble with specific modules (in the master branch), and need that resolved, feel free to open an issue on github.

related topic (maybe disabling the vfp3 cpu extension helps)

I managed to build it with this:

 cmake \
        -D CMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_HOME_21}/build/cmake/android.toolchain.cmake \
        -D ANDROID_NATIVE_API_LEVEL=$ANDROID_VERSION \
        -D BUILD_SHARED_LIBS=ON \
        -D BUILD_TESTS=OFF \
        -D BUILD_PERF_TESTS=OFF \
        -D BUILD_ANDROID_EXAMPLES=OFF \
        -D BUILD_ANDROID_PROJECTS=OFF \
        -D BUILD_JAVA=OFF \
        -D WITH_QT=OFF \
        -D WITH_CAROTENE=ON \
        -D WITH_OPENCL=ON \
        -D WITH_OPENGL=ON \
        -D ANDROID_ABI=$ARCH \
        -D CMAKE_BUILD_TYPE=Release \
        -D 3P_LIBRARY_OUTPUT_PATH=$ROOT/output/$OUTARCH/lib/ \
        -D CMAKE_INSTALL_PREFIX:PATH=$ROOT/output/$OUTARCH/lib/ \
        ..

But there is another error, perhaps not related to OpenCV.
When I try building gomobile with gocv I get this:

# gocv.io/x/gocv
ld: error: undefined symbol: cv::dnn::dnn4_v20210608::readNet(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&)
>>> referenced by dnn.cpp:4
>>>               $WORK/b180/_x021.o:(Net_ReadNet)

ld: error: undefined symbol: cv::dnn::dnn4_v20210608::readNet(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&, std::__ndk1::vector<unsigned char, std::__ndk1::allocator<unsigned char> > const&, std::__ndk1::vector<unsigned char, std::__ndk1::allocator<unsigned char> > const&)
>>> referenced by dnn.cpp:11
>>>               $WORK/b180/_x021.o:(Net_ReadNetBytes)

waay off chartered territory ;(
(noone kows, noone can help …)

what is that chimera ? you seem to mix STL’s here.
your app has to link against the same stl, your libs were build with

Solution is to build OpenCV 4.5.3 with NDK23.

This simply works…

 cmake \
        -D CMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake \
        -D ANDROID_NATIVE_API_LEVEL=$ANDROID_VERSION \
        -D BUILD_SHARED_LIBS=ON \
        -D BUILD_TESTS=OFF \
        -D ANDROID_STL=c++_shared \
        -D ENABLE_CXX11=ON \
        -D BUILD_PERF_TESTS=OFF \
        -D BUILD_ANDROID_EXAMPLES=OFF \
        -D BUILD_ANDROID_PROJECTS=OFF \
        -D BUILD_JAVA=OFF \
        -D BUILD_OPENCV_PYTHON=NO \
        -D BUILD_OPENCV_PYTHON2=NO \
        -D BUILD_OPENCV_PYTHON3=NO \
        -D WITH_QT=OFF \
        -D WITH_CAROTENE=ON \
        -D WITH_OPENCL=ON \
        -D WITH_OPENGL=ON \
        -D ANDROID_ABI=$ARCH \
        -D CMAKE_BUILD_TYPE=Release \
        -D 3P_LIBRARY_OUTPUT_PATH=$ROOT/output/$OUTARCH/lib/ \
        -D CMAKE_INSTALL_PREFIX:PATH=$ROOT/output/$OUTARCH/lib/ \
        ..