Well, it is actually not a porting. The Dlib library is so well written that porting it to other platforms does not require changes in the library itself. The only thing it takes is to figure out a way to build it and link to it from your project. And again, building it is easy because it written in C++ and it has few non standard system dependencies.

Android is such a case. Assume that you are making an Android application and need to use the machine learning or image processing algorithms of Dlib. Due to the JNI interface, calling the C++ code of Dlib is entirely possible. As with all external libraries, all you need to do is build it for the architectures your project supports (armeabi etc) and make its api calls available to your JNI code.

Having gone through that process myself, I have created a simple github project, that you can download, in order to build the dlib library for Android. You can find the Dlib4Android project here

You can clone it to your environment and follow the instructions. They are simple and you will be up and running in no time. Keep in mind that Dlib is not included in the Dlib4Android project but it is only a linked submodule. In order to clone it with the linked Dlib submodule you need to do:

git clone --recursive https://github.com/detsikas/Dlib4Android

in your command line interface.

Let us go through each step of the process and explain it. First, since Dlib has no JNI interface, you cannot use it directly from your Java code. You need a C++ module in your project, with a JNI interface that you will call from your Java code and through which you will be calling Dlib functions at will.

We will be using the Dlib library as a prebuilt ndk library. This means that we have to build it with ndk first, which in turn means that we need Android.mk and Application.mk files.

You will find the details of these files in the github link.

Having setup the build files (Android.mk and Application.mk), we are ready to build our library. This can be done with

ndk-build -j2

which should be given inside the dlib_v19.0 folder of the cloned repository.

This will create the libs folder, inside which you will find, the shared libraries for each architecture you specified. Here is how it will look like, depending on the architectures you specified

That was it. We are now done with Dlib. We now need to turn to our project and set some things up there.

First, we need to copy the libraries we just build somewhere inside our project tree structure. I have made a folder called dlib, at the root of my project tree and copied the above paths there.  You are free to choose your own location.

Having done that we need to tell our jni module that it will use these prebuilt libraries. We can do that inside its Android.mk file, by adding a prebuilt session, which will tell well the prebuilt Dlib libraries are and where the header files are. These are all explained in more detail in the github link.

By now we have pretty much finished. You can build your project and start using the Dlib api.

Thank you for reading!

Related Articles