# compile dlib and opencv with android ndk

bendaf 07/28/2017. 1 answers, 983 views

I am an android developer, but I am new to Android NDK, please help, or tell me what else would you need/ how would you try to solve this type of problem.

I used this dlib for android example and the official opencv for android library and I would like to integrate the two in one application. When I trying to build the project I get the following error:

FAILURE: Build failed with an exception.

* What went wrong:
> Build command failed.
Error while executing process /home/feli/Android/Sdk/ndk-bundle/ndk-build with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/home/feli/Development/android/test/face-detection/openCVSamplefacedetection/src/main/jni/Android.mk NDK_APPLICATION_MK=/home/feli/Development/android/test/face-detection/openCVSamplefacedetection/src/main/jni/Application.mk APP_ABI=mips64 NDK_ALL_ABIS=mips64 NDK_DEBUG=1 APP_PLATFORM=android-21 NDK_OUT=/home/feli/Development/android/test/face-detection/openCVSamplefacedetection/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=/home/feli/Development/android/test/face-detection/openCVSamplefacedetection/build/intermediates/ndkBuild/debug/lib /home/feli/Development/android/test/face-detection/openCVSamplefacedetection/build/intermediates/ndkBuild/debug/obj/local/mips64/libdetection_based_tracker.so}
[mips64] Compile++      : detection_based_tracker <= DetectionBasedTracker_jni.cpp
[mips64] Prebuilt       : libopencv_java3.so <= /home/feli/Android/openCV/OpenCV-android-sdk/sdk/native/jni/../libs/mips64/
[mips64] SharedLibrary  : libdetection_based_tracker.so
/home/feli/Development/android/test/face-detection/openCVSamplefacedetection/build/intermediates/ndkBuild/debug/obj/local/mips64/objs-debug/detection_based_tracker/DetectionBasedTracker_jni.o: In function dlib_check_consistent_assert_usage':
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/home/feli/Development/android/test/face-detection/openCVSamplefacedetection/build/intermediates/ndkBuild/debug/obj/local/mips64/libdetection_based_tracker.so] Error 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

My first guess were that I haven/t included the source.cpp in my Android.mk but placeing it to the src files didn't solve the porblem. Here is my Android.mk:

LOCAL_PATH := $(call my-dir) ## Build dlib to static library include$(CLEAR_VARS)
LOCAL_MODULE := dlib
LOCAL_C_INCLUDES := $(LOCAL_PATH)/dlib LOCAL_SRC_FILES += \$(LOCAL_PATH)/dlib/dlib/all/source.cpp \
$(LOCAL_PATH)/dlib//dlib/threads/threads_kernel_shared.cpp \$(LOCAL_PATH)/dlib/dlib/entropy_decoder/entropy_decoder_kernel_2.cpp \
$(LOCAL_PATH)/dlib/dlib/base64/base64_kernel_1.cpp \$(LOCAL_PATH)/dlib/dlib/threads/threads_kernel_1.cpp \
$(LOCAL_PATH)/dlib/dlib/threads/threads_kernel_2.cpp LOCAL_EXPORT_C_INCLUDES :=$(LOCAL_C_INCLUDES)
include $(BUILD_STATIC_LIBRARY) # Build opencv include$(CLEAR_VARS)
OPENCVROOT := ~/Android/openCV/OpenCV-android-sdk
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE := SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk LOCAL_MODULE := detection_based_tracker LOCAL_C_INCLUDES +=$(LOCAL_PATH)
LOCAL_SRC_FILES  := DetectionBasedTracker_jni.cpp

LOCAL_LDLIBS     += -lm -llog -ldl -lz -ljnigraphics
LOCAL_CPPFLAGS += -fexceptions -frtti -std=c++11

# LOCAL_STATIC_LIBRARIES += dlib

include \$(BUILD_SHARED_LIBRARY)

Maybe the NDK_PROJECT_PATH=null is the problem? Than why the two libraries are building separately? Or what else can be the problem? I tried a lot modifying the Android.mk, but it's working is a mystery for me. Any help or opinion would be useful.

EDIT:

When I comment out the LOCAL_STATIC_LIBRARIES += dlib row, I got the following exception:

Build command failed.
Error while executing process /home/feli/Android/Sdk/ndk-bundle/ndk-build with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/home/feli/Development/android/test/face-detection/openCVSamplefacedetection/src/main/jni/Android.mk NDK_APPLICATION_MK=/home/feli/Development/android/test/face-detection/openCVSamplefacedetection/src/main/jni/Application.mk APP_ABI=mips64 NDK_ALL_ABIS=mips64 NDK_DEBUG=1 APP_PLATFORM=android-21 NDK_OUT=/home/feli/Development/android/test/face-detection/openCVSamplefacedetection/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=/home/feli/Development/android/test/face-detection/openCVSamplefacedetection/build/intermediates/ndkBuild/debug/lib /home/feli/Development/android/test/face-detection/openCVSamplefacedetection/build/intermediates/ndkBuild/debug/obj/local/mips64/libdetection_based_tracker.so}
[mips64] Compile++      : detection_based_tracker <= DetectionBasedTracker_jni.cpp
[mips64] Compile++      : dlib <= source.cpp
In file included from /home/feli/Development/android/test/face-detection/openCVSamplefacedetection/src/main/jni/dlib/dlib/all/source.cpp:76:
In file included from /home/feli/Development/android/test/face-detection/openCVSamplefacedetection/src/main/jni/dlib/dlib/all/../gui_widgets/fonts.cpp:14:
#include <X11/Xlib.h>
^~~~~~~~~~~~
1 error generated.
make: *** [/home/feli/Development/android/test/face-detection/openCVSamplefacedetection/build/intermediates/ndkBuild/debug/obj/local/mips64/objs-debug/dlib//home/feli/Development/android/test/face-detection/openCVSamplefacedetection/src/main/jni/dlib/dlib/all/source.o] Error 1

Alex Cohn 07/31/2017.

dlib uses cmake, and it is not a trivial setup. It may take a long time for an experience professional to prepare the ndk-build scripts for this library.

I suggest that you build your native library with cmake, instead of ndk-build. You can find an example of a native library that uses OpenCv on GitHub.

Also, I suggest that you start with abiFilters set to only one most relevant ABI, and only when you see that everything works for you, you can build your project for other ABIs. Remember that for some libraries porting to all ABIs may be tricky. And if you don't have a platform to perform at least a brief sanity check for some ABI, there is no sense in building for it.