For a number of different reasons, you might want/need to build OpenCV from scratch instead of using the pre-built and official libraries provided by OpenCV. Since you’ve ended up reading this post, there’s a good chance that you already know why you might need to do this, but if you don’t, you can check out my similar post from a couple of years ago for some answers on this. That post was based on OpenCV 3.3 which is considered out-of-date these days, especially with OpenCV 4 out in the market (OpenCV 4.0.1 at the time of writing this article), so I decided to write a new tutorial to address some of the differences. So without without further ado, here we go.
Please note that this tutorial is written with Windows users in mind but you should be able to build with “almost” the same steps under Linux and macOS as well. You just need to convert the steps with MinGW to default make command in Linux/Unix.
First things first, the prerequisites for building OpenCV 4.x for Android are:
- OpenCV 4.x source codes of course (You can get them here in a zip file if you choose sources instead of pre-built binaries or you can clone then from here, I’ll use OpenCV 4.0.1 just for reference)
- CMake (Get the latest version from CMake website, I’ll use CMake 3.13.3)
- Get MinGW (If you’re a Qt fan and you want to use it for building Android app, Qt installer includes MinGW by default so you can use that one, otherwise refer to the official MinGW website to get it)
- Android Studio (It’s available here, download the latest version available.)
- Use SDK manager in Android Studio to get the latest Android SDK and NDK
Important note for OpenCV version 4.0.1 prerequisites: You’ll need Android NDK Revision 16b for properly building this particular version of OpenCV and you might face issues if you use a version of NDK higher that the one we mentioned. You can get individual Android NDK revisions from here.
Here is a question that is usually skipped in tutorials, so for those of you who are interested in understanding the whole process, we’re going to ask it and try to provide some clear but brief answers for it.
What is the purpose of each one of the prerequisites?
Let’s start with CMake, it allows us to create projects out of source codes and build them using our favorite IDEs or make/build systems. We’ll be using CMake to create MinGW Makefiles that can be built with MinGW make system and Android armv7 cross-compilers. So, we’ll be using MinGW as the make system. Android Studio on the other hand is not directly used for building OpenCV but it includes an easy way of installing Android SDK and NDK and managing different installations and versions of the SDK, using its built-in SDK Manager. Finally, SDK and NDK contain all the required source codes, headers, compilers and literally everything needed for building apps for Android.
That’s a very brief explanation but it contains enough keywords in case you want to dig deeper and learn more about each one of them. So, assuming you’ve got all the prerequisites on your computer, we can start by running CMake and choosing the OpenCV source codes folder for “Where is the source code” and then choose a build folder, preferably under the same path, and preferably call it “build_android” for “Where to build the binaries”.
Then press the “Configure” button. This will take you to the configuration page. Select “MinGW Makefiles” for the generator and press “Next”.
In the next page, we need to choose the right Toolchain for cross-compiling OpenCV for Android. To do this you have to choose the “android.toolchain.cmake” file which can be found under OpenCV source codes folder and inside “platforms/android” as seen in the following screenshot:
This will start a CMake configuration which might end up with some errors, but don’t worry about it, what you have to do is adding a few entries manually and making sure that MinGW is in the PATH environment variable. You can manually add entries to CMake by using the “Add Entry” button and entering the “Name”, “Type” and “Value” of the entry. Here is an example:
So start by adding the following entries:
- Add “ANDROID_NDK” with a type of “PATH” and value of Android NDK path (Note that for OpenCV 4.0.1 this must be Android NDK r16b)
- Add “ANDROID_SDK_ROOT” with a type of “PATH” and value to Android Sdk root folder
- Add “ANDROID_NATIVE_API_LEVEL” with a type of “PATH” and value of “27”
Press “Configure” again and you’ll notice a longer configuration taking place and if everything goes well you should not receive any errors. You’re ready to press “Generate” now, but if you want you can use the following options as well to further customize your OpenCV build:
- Disable BUILD_ANDROID_PROJECTS
- Disable BUILD_ANDROID_EXAMPLES
- Disable BUILD_PERF_TESTS
- Disable BUILD_TESTS
Android libraries are built statically by default but if you want the *.so file and a dynamic build (which is the recommended route for developers that want to integrate OpenCV and Qt framework), then you can enable BUILD_SHARED_LIBS to build the shared/dynamic libraries instead of the static ones. Note that enabling shared libs when building OpenCV for Android, will force you to disable BUILD_FAT_JAVA_LIB as well, so just make sure the checkbox next to it is unchecked and press generate to get the project files.
The last step is to actually build the OpenCV libraries so you can start that by opening up a command prompt instance and switching to the OpenCV build folder that you had set in CMake and type the following command:
You need to be patient for the OpenCV libraries to be built. After the build is done, you can use need to execute the following command to get the nicely packed OpenCV 4.x library inside the install folder:
And that should be it. Make sure to post your comments and questions below in case you face issues following the steps above.