It's been some time since I tried to use some third party libraries in a proejct I am currently working on and I'd like to spend some time reviewing what problems I had been faced with and how they were solved.
To start with, our project requries three third party libraries namely Opencv, ceres and Eigen . They are used for image , least square as well as matrix manipulations. In order to use the libraries in Android I need to use NDK, which is a powerfull tool to build libraries for different platforms such as x86, arm and arm64 from sources mainly written in C and C++. Luckily enough , these three libraries can be easily used in an Android device with the help fo NDK.
Opencv4android is an opencv library that has supports for Android which you can download from its official website and it is quite easy to use opencv in one project. With a brief scan at its sample project and its Android.mk file:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) ifdef OPENCV_ANDROID_SDK ifneq ("","$(wildcard $(OPENCV_ANDROID_SDK)/OpenCV.mk)") // to use opencv , simply include OpenCV.mk where you can find in its root path. include ${OPENCV_ANDROID_SDK}/OpenCV.mk else include ${OPENCV_ANDROID_SDK}/sdk/native/jni/OpenCV.mk endif else include ../../../sdk/native/jni/OpenCV.mk endif LOCAL_MODULE := mixed_sample LOCAL_SRC_FILES := jni_part.cpp LOCAL_LDLIBS += -llog -ldl include $(BUILD_EXECUTABLE)
Some times I find it useful to include opencv header file path in the mk file . And according to its official websit, try to add LOCAL_PLATFORM := android -15 to resolve errors like "undefined referecens." related to opencv modules.
Now lets move on to Eigen.
Eigen is an interesting library that it is made entirely of header files, that's right, no cpp files need to be included in Android.mk file.
For Ceres, although defautly one can compile it to a static library more easily (simply go into its jni folder and execute ndk-build), we can change its Android.mk and Application.mk file to compile it to a shared_library with some minor changes shown below:
LOCAL_CFLAGS += -DCERES_BUILDING_SHARED_LIBRARY -DCERES_RESTRICT_SCHUR_SPECIALIZATION
include $(BUILD_SHARED_LIBRARY)
//add&change above lines in ceres/jni/Andoird.mk and
Add LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
in Android.mk
Now we are done with introducing how to use them with NDK, lets move on to more specific problems I met.
1. Try to include header paths for each module unless you are sure it is not used;
2. To include a prebuilt libary, you need to specify a module (start with INCLUDE(CLEAR_VARS) , ends with INCLUDE(BUILD_STATIC_LIBARARY))for each one of prebuilt libraries.
3. some times I simply couldnt make it work while building static libraries, changing to shared library will help
4. while using adb shell, we can set current path as default path , but it expires every time adb shell restarts:
export LD_LIBRARY_PATH=./
... ...