Setting-up Dlib and OpenCV for Android

Android with OpenCV and Dlib

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.

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

1. Project Creation

Downloading the needed libraries:

  • C++ support, and
  • Kotlin support (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).

  • 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).
# I assume that android-cmake is set on $PATH:
cd \path\to\dlib\
mkdir build
cd build
# compile:
cmake -DBUILD_SHARED_LIBS=1
-DCMAKE_TOOLCHAIN_FILE=android\toolchain\path
-DANDROID_NDK=ndk-bundle\path
-GNinja # only for windows users!
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_CXX_FLAGS=-std=c++11 -frtti -fexceptions
-DANDROID_ABI=abi-you-want-to-support
-DANDROID_PLATFORM=android-16 # >= 16 supported
-DANDROID_TOOLCHAIN=clang
-DANDROID_STL=c++_shared
-DANDROID_CPP_FEATURES=rtti exceptions
"..\path\to\dlib\CMakeLists.txt"
cmake --build . # done
  • armeabi-v7a
  • arm64-v8a
  • x86
  • x86_64.

Shrinking the library size

After building the library we can notice that the generated libdlib.so are around 20MB in size, that’s too much!

  • armeabi-v7a: requires the arm-linux-androideabi-strip,
  • arm64-v8a: requires the aarch64-linux-android-strip and,
  • x86, x86_64: either needs the x86_64-linux-android-strip.
# stripping libdlib.so for arm64-v8a:
$> cd path-to-stripper\
$> aarch64-linux-android-strip.exe --strip-unneeded library-path\libdlib.so

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.
  • 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.

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:

5. The build.gradle

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:

  • 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'].
Change the path of the opencv resource directory
// 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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luca Anzalone

Luca Anzalone

PhD student in Data Science and Computation @Alma Mater Studiorum