Setting-up Dlib and OpenCV for Android

A step-by-step guide on how to integrate natively Dlib and (or) OpenCV for enhanced Android applications.

In this guide I’ll show how to integrate Dlib 19.16 and OpenCV 4.0.1 with Android. If you need only one of them you can skip the steps related to a specific library.

I’m currently using Android Studio 3.2. Kotlin support is not necessary, so you can freely use Java (along C++, anyway). But supporting Kotlin since the beginning of the project could be handy for the future.

0. Prerequisites

In order to build native code and library, three tools are necessary: LLDB, CMake, NDK. All of them can be downloaded from Android Studio at Tools > SDK Manager > SDK Tools.

I also made a little script (available here) that automates the entire process by:

• Compiling Dlib,
• Copying the generated .so files for Dlib and OpenCV to your project.

Anyway, if your are curious the procedure is explained on chapter 2.

1. Project Creation

We’ll need them later.

Now lets open Android Studio and create a new project, with:

• C++ support, and
• Kotlin support (optional)

Click next, select the minimum API level you need (mine is 16) and then select empty activity.

Once you get to this point remember to select C++11, that’s fundamental!. The exception and runtime info support is also optional.

2. Compiling Dlib (optional step)

Unlike OpenCV, Dlib is provided only with its source and header files. So, we have to compile it by ourselves. This can be done (quite easily) with the android-cmake tool (located within the android sdk folder) in conjunction with ninja (Windows) or make (Linux).

At this point, we have to locate (or set environment variables) for:

• android-cmake executable (mine is at ..\Android\sdk\cmake\3.10.2.4988404\bin\cmake.exe),
• ndk-bundle location (mine is at ..\Android\sdk\ndk-bundle),
• android.toolchain.cmake file (mine is at ..\Android\sdk\ndk-bundle\build\cmake\android.toolchain.cmake).

We open a terminal and type:

3. Launching the Script

The setup script I prepared is made of three main functions:

• Compile-Dlib that’s self-explanatory,
• Dlib-Setup that copies every libdlib.so to your Android project,
• Opencv-Setup that copies every libopencv_java4.so to your project.

Inside the script, I defined several variables for each part of the process. Before executing it, is necessary to edit them all. The variables are:

• AndroidCmake: location of the android-cmake executable,
• NDK: location of the Android ndk-bundle,
• TOOLCHAIN: location of the android.toolchain.cmake file,
• ABIs: a list of the ABI you want to support,
• PROJECT_PATH: the path of the Android project you want to configure.

Now it’s time to grab your powerful script and execute it!

On my Windows machine, I had trouble executing the script on my PowerShell. I discover that typing powershell -ExecutionPolicy ByPass -File setup.ps1 solves the problem.

4. The CMakeLists

Now we have all the necessary files and libs in the right place (our project). The next step is to let CMake wire everything together by writing a CMakeLists file. Here it is:

Please note: according to the script I’ve made, in order to let it work correctly, the CMakeLists.txt should be stored inside the app folder of your android project. If you want you can save it to another path, but editing the script is required.

The last file we need is the build.gradle (app):

(Bonus) Add Java Support for OpenCV

From Android Studio go to File > New > Import Module and select the path of your opencv module, like so:

Then is necessary to specify that the opencv module is a dependency for the app module. So, going to File > Project Structure click on app (under modules) and open the Dependencies panel. Press the green plus button (on the right top) and select module dependency:

Next, is necessary to edit the Opencv build.gradle (located inside the opencv module in the root path of the project) by:

• Setting the minSdkVersion equal to the value of the minSdkVersion defined in the app’s build.gradle.
• Changing the value of the variable res.srcDirs to ['res'].

Sync the project.

Finally, in your MainActivity (or main entry point of the application) put a static (Java) or init (Kotlin) block with:

// Kotlin:init {  System.loadLibrary("native-lib")  // or whatever name you choose    // prints initialization stuff, useful to know if opencv works  OpenCVLoader.initDebug()}

Conclusions

I hope the guide will help you to overcome all the difficulties related to native code integration.

If you need an advanced application (as reference for Dlib and OpenCV usage) you can find the mine here.

The compile-copy-stuff script is, again, here.

And, Dlib models can be found here (mine) and here (official).

That’s all folks! 😄

PhD student in Data Science and Computation @Alma Mater Studiorum

More from Luca Anzalone

PhD student in Data Science and Computation @Alma Mater Studiorum