In this post I’m sharing the required steps for building OpenCV 3.3.X (to be precise, OpenCV 3.3.0 which was released about a week ago) for Android. You can also download the pre-built libraries from here but it’s always a good idea to build the libraries yourself in case you want any special configurations such as Nvidia Carotene, OpenCV World and so on. So here it is:
First let’s go over the prerequisites:
- OpenCV Source Files (You can also clone the latest source codes from OpenCV Git Repository but the source codes in release are usually “Stable”er)
- CMake (just download and use the latest version and be done with it, but just in case I’m using 3.8.1)
- MinGW (this is what I use as the Make system, but you might be able to achieve the same with other Make systems too. You don’t need the MinGW compiler but the installation usually includes the whole kit. I use MinGW 530 included in the Qt 5.9.1 Installer for Windows)
- Android NDK (I used the latest version which is r15c but I assume some of the earlier versions will also work, you need to try this for yourself if you’re curious)
- JDK (Install the latest, and it’ll keep itself updated regularly)
After getting all of the requirements we need to make sure all environment values are set. Make sure the following environment values are set:
- Add MinGW installation folder to PATH
- Create an environment variable called ANDROID_NDK and set it to Android NDK folder (this is where you extracted Android NDK)
Now start by extracting OpenCV source codes somewhere (in my case it’s “d:\cv\opencv_3.3.0\sources” but you can use whatever you like)
Run CMake, set the source codes folder (1), a binary folder of your choice (2), hit Configure button (3), select MinGW Makefiles as the generator (4), select “Specify toolchain …” option (5) and hit “Next” button (6), as seen in the image below:
In the dialog that appears select “android.toolchain.cmake” file which is inside the folder where you extracted OpenCV source codes, under /platforms/android (similar to what is seen below):
After some moments (or minutes depending on what your idea of a PC is) you will see something similar to the following:
Make sure you see the “Configuring Done”, otherwise you might need to start again with this guide or post a question in the comments section below.
Select any options you like and press Generate button. I usually build for armeabi-v7a, with BUILD_opencv_world checked and WITH_CAROTENE unchecked but you can try different configurations for yourself.
After a few moments OpenCV is ready to be built. Just make sure you see “Configuring done” and “Generating done” messages at the end of CMake log.
Now run a Command Prompt and switch to the binaries folder you provided to CMake. Just to compare with mine, it’s “D:/cv/opencv_3.3.0/build_android”
Run “mingw32-make” command.
OpenCV build process will start, and you’ll see some colorful stuff for quite a while:
Wait until you see 100% built. When building is finished you need to execute another command. “mingw32-make install”.
Wait until it finished and you’ll have all your beloved libraries inside binaries folder you chose on CMake. Again, just to compare with mine, here’s where I got all produced files after building with the above instructions: D:/cv/opencv_3.3.0/sources/build_android/install