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:
- Android SDK
- Android NDK 17.X
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.