Setting up Dlib for Android on Windows

I am trying to set up dlib in android studio in windows following this tutorial, https://github.com/Luca96/dlib-for-android. I have changed path of various variables but get the same error. I do not need opencv just dlib. Opencv is already setup and working correctly.

I replaced -GNinja with -DCMAKE_MAKE_PROGRAM=C:UserscomputerAppDataLocalAndroidSdkcmake3.10.2.4988404binninja.exe and still get error.

Any alternate procedure for setting up dlib with android studio will also work.

My custom Power Shell script based on the sample provided:

# Luca Anzalone

# -----------------------------------------------------------------------------
# -- DLIB FOR ANDROID: setup script
# -----------------------------------------------------------------------------

# Android-cmake path: REPLACE WITH YOUR CMAKE PATH!
$AndroidCmake = 'C:UserscomputerAppDataLocalAndroidSdkcmake3.10.2.4988404bincmake.exe'

# Android-ndk path: REPLACE WITH YOUR NDK PATH!
if (Get-Variable 'ANDROID_NDK' -Scope Global -ErrorAction 'Ignore') {
    $NDK = $ANDROID_NDK
} else {
    $NDK = 'C:UserscomputerAppDataLocalAndroidSdkndk21.0.6113669'
}

$TOOLCHAIN = "C:UserscomputerAppDataLocalAndroidSdkndk21.0.6113669buildcmakeandroid.toolchain.cmake"

# Supported Android ABI: TAKE ONLY WHAT YOU NEED!
# $ABIs = 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
$ABIs = 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'

# path to stripper tool: REPLACE WITH YOURS, ACCORDING TO OS!!
$STRIPPER_PATH = "$NDKtoolchainsllvmprebuiltwindows-x86_64bin"
$STRIPPERS = @{
    'armeabi-v7a' = "$STRIPPER_PATHarm-linux-androideabi-strip.exe";
    'arm64-v8a'   = "$STRIPPER_PATHaarch64-linux-android-strip.exe";
    'x86'         = "$STRIPPER_PATHx86_64-linux-android-strip.exe" ;
    'x86_64'      = "$STRIPPER_PATHx86_64-linux-android-strip.exe" ;    
} 

# Minimum supported sdk: SHOULD BE GREATER THAN 16
$MIN_SDK = 24

# Android project path: REPLACE WITH YOUR PROJECT PATH!
$PROJECT_PATH = 'C:UserscomputerAndroidStudioProjectsOpencvPose'

# Directory for storing native libraries
$NATIVE_DIR = "$PROJECT_PATHappsrcmaincppLibs"

# -----------------------------------------------------------------------------
# -- Utils
# -----------------------------------------------------------------------------
function Make-Dir {
    if (-Not (Test-Path $args[0])) {
        New-Item -ItemType directory -Path $args[0] > $null
    }
}

function Copy-Directory ($from, $to, $tab) {
    $Items = Get-ChildItem $from

    ForEach ($item in $Items) {
        if ((Get-Item $item.FullName) -is [System.IO.DirectoryInfo]) {
            Write-Host "$tab > $item"
            Make-Dir "$to$item"
            Copy-Directory "$from$item" "$to$item" "$tab  "
        } else {
            Copy-Item -Path $item.FullName -Destination $to
            Write-Host "$tab | $item copied." 
        }
    }
}

# -----------------------------------------------------------------------------
# -- Dlib
# ----------------------------------------------------------------------------- 

# Dlib library path:
$DLIB_PATH = 'C:UserscomputerDesktopdlib-19.19'

function Compile-Dlib {
    Set-Location $DLIB_PATH
    Make-Dir 'build'

    Write-Host '=> Compiling Dlib...'
    sleep 0.5

    ForEach ($abi in $ABIs) {
        Write-Host
        Write-Host "=> Compiling Dlib for ABI: '$abi'..."
        sleep 0.5

        Make-Dir "build$abi"
        Set-Location "build$abi"

        $cmakeArguments = @(
                "-DBUILD_SHARED_LIBS=1",
                "-DANDROID_NDK=$NDK",
                "-DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN",
                "-DCMAKE_MAKE_PROGRAM=C:UserscomputerAppDataLocalAndroidSdkcmake3.10.2.4988404binninja.exe",
                "-DCMAKE_BUILD_TYPE=Release",
                "-DCMAKE_CXX_FLAGS=-std=c++11 -frtti -fexceptions",
                "-DCMAKE_C_FLAGS=-O3",
                "-DANDROID_ABI=$abi",
                "-DANDROID_PLATFORM=android-$MIN_SDK",
                "-DANDROID_TOOLCHAIN=clang",
                "-DANDROID_STL=c++_shared",
                "-DANDROID_CPP_FEATURES=rtti exceptions",
                "-DCMAKE_PREFIX_PATH=....",
                "...."
        )

        $stripArguments = @(
                "--strip-unneeded",
                "dlib/libdlib.so"
        )

        & $AndroidCmake $cmakeArguments
        sleep 0.5

        Write-Host "=> Generating the 'dlib/libdlib.so' for ABI: '$abi'..."
        & $AndroidCmake --build .
        sleep 0.5

        Write-Host "=> Stripping libdlib.so for ABI: '$abi'to reduce space..."
        & $STRIPPERS[$abi] $stripArguments
        sleep 0.5

        Write-Host '=> done.'
        Set-Location ....
        sleep 0.5
    }    
}

function Dlib-Setup {
    Write-Host '=> Making directories for Dlib...'
    Make-Dir "$NATIVE_DIRdlib"
    Write-Host "=> '$NATIVE_DIRdlib' created."
    Make-Dir "$NATIVE_DIRdliblib"
    Write-Host "=> '$NATIVE_DIRdliblib' created."
    Make-Dir "$NATIVE_DIRdlibinclude"
    Write-Host "=> '$NATIVE_DIRdlibinclude' created."
    Make-Dir "$NATIVE_DIRdlibincludedlib"
    Write-Host "=> '$NATIVE_DIRdlibincludedlib' created."

    Write-Host "=> Copying Dlib headers..."
    Copy-Directory "$DLIB_PATHdlib" "$NATIVE_DIRdlibincludedlib" ''

    Write-Host "=> Copying 'libdlib.so' for each ABI..."
    ForEach ($abi in $ABIs) {
        Make-Dir "$NATIVE_DIRdliblib$abi"
        Copy-Item -Path "$DLIB_PATHbuild$abidliblibdlib.so" -Destination "$NATIVE_DIRdliblib$abi"
        Write-Host " > Copied libdlib.so for $abi"
    }
}

# COMMENT TO DISABLE COMPILATION
Compile-Dlib

# -----------------------------------------------------------------------------
# -- OpenCV
# -----------------------------------------------------------------------------

# OpenCV library path: REPLACE WITH YOUR OPENCV PATH!
$OPENCV_PATH='path-to-youropencv-4.0.1-android-sdksdknative'

function Opencv-Setup {
    Make-Dir "$NATIVE_DIRopencv"

    Write-Host "=> Copying 'libopencv_java4.so' for each ABI..."
    ForEach ($abi in $ABIs) {
        Make-Dir "$NATIVE_DIRopencv$abi"
        Copy-Item -Path "$OPENCV_PATHlibs$abilibopencv_java4.so" -Destination "$NATIVE_DIRopencv$abi"
        Write-Host " > Copied libopencv_java4.so for $abi"
    }
}

# -----------------------------------------------------------------------------
# -- Project setup
# -----------------------------------------------------------------------------

Make-Dir $NATIVE_DIR

# COMMENT TO NOT COPY DLIB '.so' FILES
Dlib-Setup

# COMMENT TO NOT COPY OPENCV '.so' FILES
Opencv-Setup

Write-Host "=> Project configuration completed."

# -----------------------------------------------------------------------------

Partial Error log:

PS C:UserscomputerDesktopdlib-19.19build> & "C:UserscomputerDesktopsetup1.ps1"
=> Compiling Dlib...

=> Compiling Dlib for ABI: 'armeabi-v7a'...
-- Building for: Visual Studio 15 2017
CMake Error at CMakeLists.txt:3 (project):
  CMAKE_SYSTEM_NAME is 'Android' but 'NVIDIA Nsight Tegra Visual Studio
  Edition' is not installed.


-- Configuring incomplete, errors occurred!
See also "C:/Users/computer/Desktop/dlib-19.19/build/armeabi-v7a/CMakeFiles/CMakeOutput.log".
=> Generating the 'dlib/libdlib.so' for ABI: 'armeabi-v7a'...
ninja: error: loading 'build.ninja': The system cannot find the file specified.

=> Stripping libdlib.so for ABI: 'armeabi-v7a'to reduce space...
C:UserscomputerAppDataLocalAndroidSdkndk21.0.6113669toolchainsllvmprebuiltwindows-x86_64binarm-linux-androideabi-strip.exe: 'dlib/libdlib.so': No such file
=> done.

=> Compiling Dlib for ABI: 'arm64-v8a'...
-- Building for: Visual Studio 15 2017
CMake Error at CMakeLists.txt:3 (project):
  CMAKE_SYSTEM_NAME is 'Android' but 'NVIDIA Nsight Tegra Visual Studio
  Edition' is not installed.


-- Configuring incomplete, errors occurred!
See also "C:/Users/computer/Desktop/dlib-19.19/build/arm64-v8a/CMakeFiles/CMakeOutput.log".
=> Generating the 'dlib/libdlib.so' for ABI: 'arm64-v8a'...
ninja: error: loading 'build.ninja': The system cannot find the file specified.

=> Stripping libdlib.so for ABI: 'arm64-v8a'to reduce space...
C:UserscomputerAppDataLocalAndroidSdkndk21.0.6113669toolchainsllvmprebuiltwindows-x86_64binaarch64-linux-android-strip.exe: 'dlib/libdlib.so': No such file
=> done.

=> Compiling Dlib for ABI: 'x86'...
-- Building for: Visual Studio 15 2017
CMake Error at CMakeLists.txt:3 (project):
  CMAKE_SYSTEM_NAME is 'Android' but 'NVIDIA Nsight Tegra Visual Studio
  Edition' is not installed.


-- Configuring incomplete, errors occurred!
See also "C:/Users/computer/Desktop/dlib-19.19/build/x86/CMakeFiles/CMakeOutput.log".
=> Generating the 'dlib/libdlib.so' for ABI: 'x86'...
ninja: error: loading 'build.ninja': The system cannot find the file specified.

=> Stripping libdlib.so for ABI: 'x86'to reduce space...
C:UserscomputerAppDataLocalAndroidSdkndk21.0.6113669toolchainsllvmprebuiltwindows-x86_64binx86_64-linux-android-strip.exe: 'dlib/libdlib.so': No such file
=> done.

=> Compiling Dlib for ABI: 'x86_64'...
-- Building for: Visual Studio 15 2017
CMake Error at CMakeLists.txt:3 (project):
  CMAKE_SYSTEM_NAME is 'Android' but 'NVIDIA Nsight Tegra Visual Studio
  Edition' is not installed.


-- Configuring incomplete, errors occurred!
See also "C:/Users/computer/Desktop/dlib-19.19/build/x86_64/CMakeFiles/CMakeOutput.log".
=> Generating the 'dlib/libdlib.so' for ABI: 'x86_64'...
ninja: error: loading 'build.ninja': The system cannot find the file specified.

=> Stripping libdlib.so for ABI: 'x86_64'to reduce space...
C:UserscomputerAppDataLocalAndroidSdkndk21.0.6113669toolchainsllvmprebuiltwindows-x86_64binx86_64-linux-android-strip.exe: 'dlib/libdlib.so': No such file
=> done.
=> Making directories for Dlib...
=> 'C:UserscomputerAndroidStudioProjectsOpencvPoseappsrcmaincppLibsdlib' created.
=> 'C:UserscomputerAndroidStudioProjectsOpencvPoseappsrcmaincppLibsdliblib' created.
=> 'C:UserscomputerAndroidStudioProjectsOpencvPoseappsrcmaincppLibsdlibinclude' created.
=> 'C:UserscomputerAndroidStudioProjectsOpencvPoseappsrcmaincppLibsdlibincludedlib' created.

Answer

EDIT: Use DLIB master from Github instead of 19.19 from their website to avoid errors with OpenCV. Change the paths accordingly, set dlib-19.19 to dlib-master where applicable.

I was able to compile dlib by making some changes to path and flags. A big change was to include both flags,

-GNinja -DCMAKE_MAKE_PROGRAM="C:UserscomputerAppDataLocalAndroidSdkcmake3.10.2.4988404binninja.exe"

I extracted latest dlib(19.19) to desktop and also setup cmake, NDK, lldb. Compiling for x86_64 only,

C:UserscomputerAppDataLocalAndroidSdkcmake3.10.2.4988404bincmake.exe -DBUILD_SHARED_LIBS=1 -DCMAKE_TOOLCHAIN_FILE="C:UserscomputerAppDataLocalAndroidSdkndk21.0.6113669buildcmakeandroid.toolchain.cmake" -DANDROID_NDK="C:UserscomputerAppDataLocalAndroidSdkndk21.0.6113669" -GNinja -DCMAKE_MAKE_PROGRAM="C:UserscomputerAppDataLocalAndroidSdkcmake3.10.2.4988404binninja.exe" -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=-std=c++11 -frtti -fexceptions -DANDROID_ABI=x86_64 -DANDROID_PLATFORM=android-24 -DANDROID_TOOLCHAIN=clang -DANDROID_STL=c++_shared -DANDROID_CPP_FEATURES=rtti exceptions "C:UserscomputerDesktopdlib-19.19CMakeLists.txt"

Run above code by pasting it into power shell. It will generate libdlib.so in dlib folder.

Compilation Script:

# Luca Anzalone

# -----------------------------------------------------------------------------
# -- DLIB FOR ANDROID: setup script
# -----------------------------------------------------------------------------

# Android-cmake path: REPLACE WITH YOUR CMAKE PATH!
$AndroidCmake = 'C:UserscomputerAppDataLocalAndroidSdkcmake3.10.2.4988404bincmake.exe'

# Android-ndk path: REPLACE WITH YOUR NDK PATH!
if (Get-Variable 'ANDROID_NDK' -Scope Global -ErrorAction 'Ignore') {
    $NDK = $ANDROID_NDK
} else {
    $NDK = 'C:UserscomputerAppDataLocalAndroidSdkndk21.0.6113669'
}

$TOOLCHAIN = "C:UserscomputerAppDataLocalAndroidSdkndk21.0.6113669buildcmakeandroid.toolchain.cmake"

# Supported Android ABI: TAKE ONLY WHAT YOU NEED!
# $ABIs = 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
$ABIs = 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'

# path to stripper tool: REPLACE WITH YOURS, ACCORDING TO OS!!
$STRIPPER_PATH = "$NDKtoolchainsllvmprebuiltwindows-x86_64bin"
$STRIPPERS = @{
    'armeabi-v7a' = "$STRIPPER_PATHarm-linux-androideabi-strip.exe";
    'arm64-v8a'   = "$STRIPPER_PATHaarch64-linux-android-strip.exe";
    'x86'         = "$STRIPPER_PATHx86_64-linux-android-strip.exe" ;
    'x86_64'      = "$STRIPPER_PATHx86_64-linux-android-strip.exe" ;    
} 

# Minimum supported sdk: SHOULD BE GREATER THAN 16
$MIN_SDK = 24

# Android project path: REPLACE WITH YOUR PROJECT PATH!
$PROJECT_PATH = 'C:UserscomputerAndroidStudioProjectsOpencvPose'

# Directory for storing native libraries
$NATIVE_DIR = "$PROJECT_PATHappsrcmaincppLibs"

# -----------------------------------------------------------------------------
# -- Utils
# -----------------------------------------------------------------------------
function Make-Dir {
    if (-Not (Test-Path $args[0])) {
        New-Item -ItemType directory -Path $args[0] > $null
    }
}

function Copy-Directory ($from, $to, $tab) {
    $Items = Get-ChildItem $from

    ForEach ($item in $Items) {
        if ((Get-Item $item.FullName) -is [System.IO.DirectoryInfo]) {
            Write-Host "$tab > $item"
            Make-Dir "$to$item"
            Copy-Directory "$from$item" "$to$item" "$tab  "
        } else {
            Copy-Item -Path $item.FullName -Destination $to
            Write-Host "$tab | $item copied." 
        }
    }
}

# -----------------------------------------------------------------------------
# -- Dlib
# ----------------------------------------------------------------------------- 

# Dlib library path:
$DLIB_PATH = 'C:UserscomputerDesktopdlib-19.19dlib'

function Compile-Dlib {
    Set-Location $DLIB_PATH
    Make-Dir 'build'

    Write-Host '=> Compiling Dlib...'
    sleep 0.5

    ForEach ($abi in $ABIs) {
        Write-Host
        Write-Host "=> Compiling Dlib for ABI: '$abi'..."
        sleep 0.5

        Make-Dir "build$abi"
        Set-Location "build$abi"

        $cmakeArguments = @(
                "-DBUILD_SHARED_LIBS=1",
                "-DANDROID_NDK=$NDK",
                "-DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN",
                "-GNinja"
                "-DCMAKE_MAKE_PROGRAM=C:UserscomputerAppDataLocalAndroidSdkcmake3.10.2.4988404binninja.exe",
                "-DCMAKE_BUILD_TYPE=Release",
                "-DCMAKE_CXX_FLAGS=-std=c++11 -frtti -fexceptions",
                "-DCMAKE_C_FLAGS=-O3",
                "-DANDROID_ABI=$abi",
                "-DANDROID_PLATFORM=android-$MIN_SDK",
                "-DANDROID_TOOLCHAIN=clang",
                "-DANDROID_STL=c++_shared",
                "-DANDROID_CPP_FEATURES=rtti exceptions",
                "-DCMAKE_PREFIX_PATH=....",
                "...."
        )

        $stripArguments = @(
                "--strip-unneeded",
                "dlib/libdlib.so"
        )

        & $AndroidCmake $cmakeArguments
        sleep 0.5

        Write-Host "=> Generating the 'dlib/libdlib.so' for ABI: '$abi'..."
        & $AndroidCmake --build .
        sleep 0.5

        Write-Host "=> Stripping libdlib.so for ABI: '$abi'to reduce space..."
        & $STRIPPERS[$abi] $stripArguments
        sleep 0.5

        Write-Host '=> done.'
        Set-Location ....
        sleep 0.5
    }    
}

function Dlib-Setup {
    Write-Host '=> Making directories for Dlib...'
    Make-Dir "$NATIVE_DIRdlib"
    Write-Host "=> '$NATIVE_DIRdlib' created."
    Make-Dir "$NATIVE_DIRdliblib"
    Write-Host "=> '$NATIVE_DIRdliblib' created."
    Make-Dir "$NATIVE_DIRdlibinclude"
    Write-Host "=> '$NATIVE_DIRdlibinclude' created."
    Make-Dir "$NATIVE_DIRdlibincludedlib"
    Write-Host "=> '$NATIVE_DIRdlibincludedlib' created."

    Write-Host "=> Copying Dlib headers..."
    Copy-Directory "$DLIB_PATH" "$NATIVE_DIRdlibincludedlib" ''

    Write-Host "=> Copying 'libdlib.so' for each ABI..."
    ForEach ($abi in $ABIs) {
        Make-Dir "$NATIVE_DIRdliblib$abi"
        Copy-Item -Path "$DLIB_PATHbuild$abilibdlib.so" -Destination "$NATIVE_DIRdliblib$abi"
        Write-Host " > Copied libdlib.so for $abi"
    }
}

# COMMENT TO DISABLE COMPILATION
Compile-Dlib

# -----------------------------------------------------------------------------
# -- OpenCV: Not used in this
# -----------------------------------------------------------------------------

# OpenCV library path: REPLACE WITH YOUR OPENCV PATH!
#$OPENCV_PATH='path-to-youropencv-4.0.1-android-sdksdknative'

#function Opencv-Setup {
#    Make-Dir "$NATIVE_DIRopencv"
#
#    Write-Host "=> Copying 'libopencv_java4.so' for each ABI..."
#    ForEach ($abi in $ABIs) {
#        Make-Dir "$NATIVE_DIRopencv$abi"
#        Copy-Item -Path "$OPENCV_PATHlibs$abilibopencv_java4.so" -Destination "$NATIVE_DIRopencv$abi"
#        Write-Host " > Copied libopencv_java4.so for $abi"
#    }
#}


# -----------------------------------------------------------------------------
# -- Project setup
# -----------------------------------------------------------------------------

Make-Dir $NATIVE_DIR

# COMMENT TO NOT COPY DLIB '.so' FILES
Dlib-Setup

# COMMENT TO NOT COPY OPENCV '.so' FILES
#Opencv-Setup

Write-Host "=> Project configuration completed."

# -----------------------------------------------------------------------------

Save the above code into a power shell file with extension setup1.ps1. The above code should generate libdlib.so file inside dlib-19.19/dlib/build folder for each platform folder.

In order to run the code first open power shell as administrator. Next, use set-location C:UserscomputerDesktopdlib-19.19. Here use Set-ExecutionPolicy RemoteSigned and press A to be able to execute powershell code. After all done(libdlib.so generated and copied) use Set-ExecutionPolicy Restricted.

Run the power shell code by pasting & "C:UserscomputerDesktopsetup1.ps1" into shell. It will take some time to compile and after compilation it will copy headers and libdlib.so for different platform to android studio project folder as in variable $PROJECT_PATH in script above.