One in a while I start working on an Android app with computer vision capabilities and obviously I need OpenCV for that. However, I’m not surprised that every time (or most of the time to be fair) there’s some change in the way OpenCV is built which renders my build scripts useless, or buggy at least. This is understandable because of the nature of OpenCV library and the fact that it is trying to keep up with many new features and fixes and so on. In any case, this post is a reminder of all of the things I just mentioned and a guide to help you build OpenCV for Native Android development, and especially in Qt Framework which I’ll describe in a separate post soon.
First things first, I’m going to assume the following minimum requirements are present on your computer:
- Git
- CMake
- MinGW
- Android SDK
- Android NDK 17.X
- PowerShell
Get the latest version of all of the requirements to be on the safe side. If there are any version specific requirements that I have mentioned, make sure to stick to them.
Run PowerShell, cd to a folder of your choice that you’re going to use for the rest of the tutorial, then start by cloning OpenCV using the following command:
git clone "https://github.com/opencv/opencv" --branch 3.4.11 --single-branch
Now using the following commands, cd into the cloned folder, then create a build folder and cd into it:
cd "opencv"
mkdir "build"
cd "build"
It’s time to run the CMake command and prepare for building. In the following command, replace the Android Toolchain, Android NDK and Android SDK paths with the actual ones on your computer and then run it:
cmake ".." -G "MinGW Makefiles" `
-DCMAKE_TOOLCHAIN_FILE="C:/opencv/platforms/android/android.toolchain.cmake" `
-DANDROID_NDK="C:/Android/ndk/17.2.4988734" `
-DANDROID_SDK_ROOT="C:/Android/Sdk" `
-DANDROID_NATIVE_API_LEVEL=28 `
-DBUILD_ANDROID_PROJECTS=OFF `
-DBUILD_ANDROID_EXAMPLES=OFF `
-DBUILD_PERF_TESTS=OFF `
-DBUILD_TESTS=ON `
-DBUILD_SHARED_LIBS=ON `
-DBUILD_opencv_world=ON
Now you can start build OpenCV by using the following commands:
mingw32-make
mingw32-make install
It goes without saying that MinGW and CMake must be in the PATH, otherwise you won’t be able to run these commands.
Good luck building and post your questions below.
[ 2%] Linking C static library ..\lib\libzlib.a
[ 2%] Built target zlib
[ 2%] Processing OpenCL kernels (core)
Scanning dependencies of target opencv_core
[ 2%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/algorithm.cpp.obj
[ 2%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/alloc.cpp.obj
[ 2%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/arithm.cpp.obj
[ 3%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/arithm.dispatch.cpp.obj
[ 3%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/array.cpp.obj
[ 3%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/async.cpp.obj
D:\gc1\opencv-3.4\modules\core\src\async.cpp:40:18: error: ‘mutex’ in namespace ‘std’ does not name a type
mutable std::mutex mtx;
^~~~~
D:\gc1\opencv-3.4\modules\core\src\async.cpp:41:18: error: ‘condition_variable’ in namespace ‘std’ does not name a type
mutable std::condition_variable cond_var;
^~~~~~~~~~~~~~~~~~
D:\gc1\opencv-3.4\modules\core\src\async.cpp: In member function ‘bool cv::AsyncArray::Impl::get(cv::OutputArray, int64) const’:
D:\gc1\opencv-3.4\modules\core\src\async.cpp:91:26: error: ‘mutex’ is not a member of ‘std’
std::unique_lock lock(mtx);
^~~
D:\gc1\opencv-3.4\modules\core\src\async.cpp:91:26: error: ‘mutex’ is not a member of ‘std’
D:\gc1\opencv-3.4\modules\core\src\async.cpp:91:36: error: template argument 1 is invalid
std::unique_lock lock(mtx);
^
D:\gc1\opencv-3.4\modules\core\src\async.cpp:91:43: error: ‘mtx’ was not declared in this scop
can you help me
Are you even building for Android?