移植dlib+opencv到android
2018年11月18日 2018年11月26日

如果我们只是把dlib移植到android只需添加如下内容到 cmakelist.txt中
1 2 3 4 5 6 7 |
include(${CMAKE_SOURCE_DIR}/src/main/cpp/dlib-19.16/dlib/cmake) target_link_libraries( # Specifies the target library. native-lib dlib # Links the target library to the log library # included in the NDK. ${log-lib}) |
如果要这样玩的话没啥意思,因为dlib里面大部分功能需要依赖opencv,所以你可以看一下我 前面写的一篇opencv移植到android免OpenCV Manager
我这里改动了一下,把dlib和opencv sdk放到了cpp目录下面,libopencv_java3.so还是放到jniLibs对应平台下面

cmakelist.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html # Sets the minimum version of CMake required to build the native library. cmake_minimum_required(VERSION 3.4.1) # set(CMAKE_VERBOSE_MAKEFILE on) set(libs "${CMAKE_SOURCE_DIR}/src/main/jniLibs") include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/OpenCV-android-sdk/sdk\native/jni/include) add_library(libopencv_java3 SHARED IMPORTED ) set_target_properties(libopencv_java3 PROPERTIES IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_java3.so") include(${CMAKE_SOURCE_DIR}/src/main/cpp/dlib-19.16/dlib/cmake) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -fexceptions -frtti") #set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}) # # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. add_library( # Sets the name of the library. native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/cpp/native-lib.cpp src/main/cpp/mydlib.cpp) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build. find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want CMake to locate. log) # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. native-lib libopencv_java3 dlib # Links the target library to the log library # included in the NDK. ${log-lib}) |
build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
apply plugin: 'com.android.application' android { compileSdkVersion 28 defaultConfig { applicationId "com.godgou.dlibtoandroid" minSdkVersion 23 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { arguments "-DANDROID_STL=c++_shared","-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang","-DCMAKE_BUILD_TYPE=Release" //abiFilters "armeabi-v7a","arm64-v8a","x86","x86_64"//,"armeabi","mips","mips64" cppFlags "-std=c++11 -frtti -fexceptions" } } ndk{ abiFilters "armeabi-v7a","arm64-v8a","x86","x86_64"//,"armeabi","mips","mips64" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } externalNativeBuild { cmake { path "CMakeLists.txt" } } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation project(':openCVLibrary343') } |
MainActivity.JAVA
1 2 3 4 5 6 7 8 |
public class MainActivity extends AppCompatActivity { // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("opencv_java3"); System.loadLibrary("native-lib"); } } |
如果报错:More than one file was found with OS independent path ‘lib/${ANDROID_ABI}/*.so’
解决方法:
注释掉cmakelist.txt中的
1 |
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}) |
再把jniLibs目录下对应${ANDROID_ABI}报错的.so文件删除掉
如果报错:
DLIB_NO_GUI_SUPPORT is defined so you can’t use the GUI….
Also make sure you have libx11-dev installed….
- 解决方法:
- 注释DLIB_NO_GUI_SUPPORT的error指令。打开dlib/gui_core/gui_core_kernel_2.h,修改为下面的代码。
1 2 3 4 |
#ifdef DLIB_NO_GUI_SUPPORT //#error "DLIB_NO_GUI_SUPPORT is defined so you can't use the GUI code. Turn DLIB_NO_GUI_SUPPORT off if you want to use it." //#error "Also make sure you have libx11-dev installed on your system" #endif |