Ashiqul Islam Shaon
5 min readFeb 11, 2021

--

Setting up Dlib in your Android Projects using Android Studio for Windows and Mac/Linux

Step by step guide to integrate Dlib in your Android Projects with OpenCV library.

Dlib is a general-purpose cross-platform software library written in the programming language C++. Its design is heavily influenced by ideas from a design by contract and component-based software engineering. Thus it is, first and foremost, a set of independent software components.

See the image,

From your Android Studio, Go to Settings -> Android SDK -> SDK Tools

Select NDK and CMake and click ok.

Download them.

(Same for Mac/Linux/Windows)

Now we make some simple changes to a script, written by Luca

setup.ps1 is for Windows

setup.sh is for Linux/Mac

Download the file which is necessary for your OS and place it into your project directory.

Now Download Dlib Library and OpenCV. If necessary extract them in a place where they will be kept for a long time.

In my demo, I have placed it in my project directory. Don’t do this for your project. Mine is a demo.

Let’s create a project and add C++ support to the project.

Select Native C++

Select Native C++ and click next.

Click next,

Select C++11 and click finish.

Now, Let’s do the main part. We need to compile Dlib and OpenCV in order to access them in our project. We will execute our pre-defined script.

Open script.sh or script.ps1 (required by your OS).

We will edit. Let’s find out our NDK, CMAKE, and project directory.

We will make changes on setup.ps1 for our windows operating system

For windows

Place your CMake and NDK directory.

For Mac or Linux.

Change this path to your project path. Please provide the full project path.

Change Dlib and OpenCV path where you downloaded and extracted.

Now save it and run it.

For Mac/Linux

open your terminal and type sh setup.sh

For your windows

open your PowerShell and type

powershell -ExecutionPolicy ByPass -File setup.ps1

It will compile Dlib and OpenCV library into your project. It will take some time to compile.

After a successful build, you will see this directory and files in your project.

Now go to your cpp folder and edit CMakeLists.txt file. See the below image for clarification. See this

Change it to your project path

your open cv path

Don’t need to change anything else.

The last file we need is the build.gradle (app): Change your build.gradle file like this.

plugins {
id 'com.android.application'
id 'kotlin-android'
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.shaon2016.dlibrealtimefacedetectionandeyeblinkingwith5pointfaciallandmark"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

// specify the platform you want to build for
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}

externalNativeBuild {
// set c++ flags, specify the STL: c++_shared, gnustl...
cmake {
cFlags "-O3"
// cppFlags "-std=c++11 -frtti -fexceptions"
cppFlags "-std=c++11"
arguments "-DANDROID_PLATFORM=android-21", // remember: same min sdk as before
"-DANDROID_TOOLCHAIN=clang",
"-DANDROID_STL=c++_shared",
"-DANDROID_CPP_FEATURES=rtti exceptions"
}
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
sourceSets {
// tell to CMake where to find the native pre-build libraries:
main {
jniLibs.srcDirs = [
"src/main/cppLibs/dlib/lib", // remove if you only need opencv
"src/main/cppLibs/opencv" // remove if you only need dlib
]
}
}
packagingOptions {
pickFirst "**/libc++_shared.so"

pickFirst 'lib/armeabi-v7a/libopencv_java4.so'
pickFirst 'lib/arm64-v8a/libopencv_java4.so'
pickFirst 'lib/x86/libopencv_java4.so'
pickFirst 'lib/x86_64/libopencv_java4.so'

pickFirst 'lib/armeabi-v7a/libdlib.so'
pickFirst 'lib/arm64-v8a/libdlib.so'
pickFirst 'lib/x86/libdlib.so'
pickFirst 'lib/x86_64/libdlib.so'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

Now build the project and enjoy.

Special thanks goes to Luca

Related resources

  1. https://github.com/tzutalin/dlib-android-app
  2. https://medium.com/@luca_anzalone/setting-up-dlib-and-opencv-for-android-3efdbfcf9e7f
  3. https://github.com/Luca96/android-face-landmarks
  4. https://github.com/Luca96/dlib-for-android

Demo for Dlib and OpenCV

Tzutalin’s Project updated dependency and solved some errors

--

--