OpenCV Android与NDK的冲突问题

使用Android studio在Ubuntu中编译某个安卓项目时,发现当NDK版本为16时会出现以下错误:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':libuvccamera:ndkClean'.
> Process 'command '/home/luohanjie/Android/Sdk/ndk/android-ndk-r16b/ndk-build'' finished with non-zero exit value 2

而NDK版本改为20或者21时,则没有问题。

而由于所使用的部分库是使用NDK16版本进行编译的,所以需要对库使用NDK21进行重新编译,但是编译时发生以下问题:

CMakeFiles/allencv.dir/detect/HRB.cpp.o:(.data.rel.ro+0x90): undefined reference to `cv::Feature2D::detect(cv::_InputArray const&, std::__ndk1::vector<cv::KeyPoint, std::__ndk1::allocator<cv::KeyPoint> >&, cv::_InputArray const&)'
CMakeFiles/allencv.dir/detect/HRB.cpp.o:(.data.rel.ro+0x98): undefined reference to `cv::Feature2D::detect(cv::_InputArray const&, std::__ndk1::vector<std::__ndk1::vector<cv::KeyPoint, std::__ndk1::allocator<cv::KeyPoint> >, std::__ndk1::allocator<std::__ndk1::vector<cv::KeyPoint, std::__ndk1::allocator<cv::KeyPoint> > > >&, cv::_InputArray const&)'
CMakeFiles/allencv.dir/detect/HRB.cpp.o:(.data.rel.ro+0xa0): undefined reference to `cv::Feature2D::compute(cv::_InputArray const&, std::__ndk1::vector<cv::KeyPoint, std::__ndk1::allocator<cv::KeyPoint> >&, cv::_OutputArray const&)'
...

最终发现原因是NDK与OpenCV Android发生冲突[1]。新版本的NDK改用libc++作为默认的STL,而OpenCV Android 3.4版本使用的是gnustl。可以升级到OpenCV 4.0版本来兼容新版本的NDK,或者可以使用libc++重新编译OpenCV:

cd $OPENCV_SRC
mkdir build
cd build
export ANDROID_NDK=/home/luohanjie/Android/Sdk/ndk/android-ndk-r21
export ANDROID_SDK_ROOT=/home/luohanjie/Android/Sdk
cmake -DCMAKE_TOOLCHAIN_FILE=/home/luohanjie/Android/Sdk/ndk/android-ndk-r21/build/cmake/android.toolchain.cmake -DANDROID_ABI="arm64-v8a" -DANDROID_STL=c++_static -DCMAKE_BUILD_TYPE=Release -DANDROID_NATIVE_API_LEVEL=android-27 -DANDROID_PLATFORM=27 -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_ANDROID_EXAMPLES=OFF -DBUILD_opencv_java=ON -DBUILD_FAT_JAVA_LIB=ON -DBUILD_ANDROID_PROJECTS=ON -DJAVA_AWT_INCLUDE_PATH=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include -DJAVA_AWT_LIBRARY=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/jawt.h -DJAVA_INCLUDE_PATH=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include -DJAVA_INCLUDE_PATH2=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/linux -DJAVA_JVM_LIBRARY=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/jni.h -DANT_EXECUTABLE=/usr/bin/ant -DOPENCV_EXTRA_MODULES_PATH=/home/luohanjie/Documents/software/opencv-3.4.2/opencv_contrib/modules ..
-- General configuration for OpenCV 3.4.2 =====================================
-- Version control: unknown
--
-- Platform:
-- Timestamp: 2020-06-18T02:57:12Z
-- Host: Linux 4.15.0-99-generic x86_64
-- Target: Android 1 aarch64
-- CMake: 3.12.3
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: Release
--
-- CPU/HW features:
-- Baseline: NEON FP16
-- required: NEON
-- disabled: VFPV3
--
-- C/C++:
-- Built as dynamic libs?: NO
-- C++11: YES
-- C++ Compiler: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21/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 -Winit-self -Winconsistent-missing-override -Wno-narrowing -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 -Winit-self -Winconsistent-missing-override -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info -DDEBUG -D_DEBUG
-- C Compiler: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21/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 -Winit-self -Winconsistent-missing-override -Wno-narrowing -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 -Winit-self -Winconsistent-missing-override -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -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_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments
-- Linker flags (Debug): -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments
-- ccache: YES
-- Precompiled headers: NO
-- Extra dependencies: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/libz.a dl m log
-- 3rdparty dependencies: libcpufeatures libprotobuf libjpeg-turbo libwebp libpng libtiff libjasper IlmImf tegra_hal
--
-- OpenCV modules:
-- To be built: calib3d core dnn features2d flann highgui imgcodecs imgproc java java_bindings_generator ml objdetect photo python_bindings_generator shape stitching superres video videoio videostab
-- Disabled: js world
-- Disabled by dependency: -
-- Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev python2 python3 ts viz
-- Applications: -
-- Documentation: NO
-- Non-free algorithms: NO
--
-- Android NDK: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21 (ver 21.0.6113669)
-- Android ABI: arm64-v8a
-- NDK toolchain: aarch64-linux-android-clang
-- STL type: c++_static
-- Native API level: 27
-- Android SDK: /home/luohanjie/Android/Sdk (tools: 24.4.1 build tools: 30.0.0)
-- android tool: /home/luohanjie/Android/Sdk/tools/android
-- SDK target: android-27
-- Projects build scripts: Ant/Eclipse compatible
--
-- GUI:
--
-- Media I/O:
-- ZLib: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/libz.a (ver 1.2.7)
-- JPEG: build-libjpeg-turbo (ver 1.5.3-62)
-- WEBP: build (ver encoder: 0x020e)
-- PNG: build (ver 1.6.34)
-- TIFF: build (ver 42 - 4.0.9)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 1.7.1)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
--
-- Video I/O:
--
-- Parallel framework: pthreads
--
-- Trace: YES (built-in)
--
-- Other third-party libraries:
-- Custom HAL: YES (carotene (ver 0.0.1))
-- Protobuf: build (3.5.1)
--
-- Python (for build): /usr/bin/python2.7
--
-- Java: export all functions
-- ant: /usr/bin/ant (ver 1.9.6)
-- Java wrappers: YES
-- Java tests: NO
--
-- Install to: /home/luohanjie/Documents/software/opencv-3.4.2/build/install
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/luohanjie/Documents/software/opencv-3.4.2/build

make -j4
make install

  1. https://stackoverflow.com/questions/50677231/linking-errors-on-android-with-opencv-3-4-0-and-ndk