android studio 3.6.3 引入ncnn
先安装好ndk和cmake
一、下载ncnn-android-lib.zip
https://github.com/Tencent/ncnn/releases
一、设置项目的ndk版本对应ncnn
二、cpp下新建jniLibs文件夹,将对应文件复制进jniLibs
三、include的头文件放至cpp目录下(没有ncnn文件夹)
四、把要用到的c++的接口文件放在cpp目录下
五、CmakeList文件的编写
# 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)
# 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.
#include头文件目录
include_directories(include)
#source directory源文件目录
file(GLOB MTCNN_SRC *.h
*.cpp)
set(MTCNN_COMPILE_CODE ${MTCNN_SRC})
#添加ncnn库
add_library(libncnn STATIC IMPORTED )
set_target_properties(libncnn
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libncnn.a)
#编译为动态库
add_library(mtcnn SHARED ${MTCNN_COMPILE_CODE})
#添加工程所依赖的库
find_library( log-lib log )
target_link_libraries( mtcnn
libncnn
android
jnigraphics
z
${log-lib} )
六、修改app/build.gradle下, defaultConfig里面加入下面的代码
externalNativeBuild {
cmake {
arguments "-DANDROID_TOOLCHAIN=clang"
cFlags "-fopenmp -O2 -fvisibility=hidden -fomit-frame-pointer -fstrict-aliasing -ffunction-sections -fdata-sections -ffast-math "
cppFlags "-fopenmp -O2 -fvisibility=hidden -fvisibility-inlines-hidden -fomit-frame-pointer -fstrict-aliasing -ffunction-sections -fdata-sections -ffast-math "
arguments "-DANDROID_STL=c++_shared", "-DANDROID_CPP_FEATURES=rtti exceptions"
cppFlags ""
cppFlags "-std=c++14"
cppFlags "-frtti"
cppFlags "-fexceptions"
}
}
ndk {
abiFilters 'armeabi-v7a'// , 'arm64-v8a' //,'x86', 'x86_64', 'armeabi'
stl "gnustl_static"
}