人脸识别c++ ndk

Android-Studio Gradle Build Flav

2019-07-08  本文已影响0人  chengjian666

Build Flavor 作用

使用方法

比如现在有如下需求:

步骤

在app的build.gradle的android节点中添加dimension:

android {
    //......
    flavorDimensions "pipeline", "platform"
}

其中:

pipeline 可取 : face++ / sensetime ;

platform 可取 : 835 / 845

在productFlavors节点中添加新的flavor并指定dimension:

 productFlavors {
        flavor845 {
            dimension "platform"
            applicationId 'com.test.845'
            externalNativeBuild {
                cmake {
                    arguments "-DFLAVOR=845"
                    cppFlags "-frtti -fexceptions -std=c++14 -llog -fpermissive -Wno-deprecated"
                }

                ndk {
                    abiFilters "armeabi-v7a"
                }
            }
        }

        flavor835 {
            dimension "platform"
            applicationId 'com.test.835'
            externalNativeBuild {
                cmake {
                    arguments "-DFLAVOR=835"
                    cppFlags "-frtti -fexceptions -std=c++14 -llog -fpermissive -Wno-deprecated"
                }

                ndk {
                    abiFilters "armeabi-v7a"
                }
            }
        }

        flavor_SenseTime {
            dimension "pipeline"
            externalNativeBuild {
                cmake {
                    arguments "-DFLAVOR=senseTime"
                    cppFlags "-frtti -fexceptions -std=c++14 -llog -fpermissive -Wno-deprecated"
                }

                ndk {
                    abiFilters "armeabi-v7a"
                }
            }
        }

        flavor_Face++ {
            dimension "pipeline"
            externalNativeBuild {
                cmake {
                    arguments "-DFLAVOR=face++"
                    cppFlags "-frtti -fexceptions -std=c++14 -llog -fpermissive -Wno-deprecated"
                }

                ndk {
                    abiFilters "armeabi-v7a"
                }
            }
        }

如上所示,在各个flavor中添加了dimension,按照需要添加cmake的一些编译条件,其中注意cmake中的 arguments属性,是用于CMakeLists中可以用于条件分支判断使用的值域(注意不是前面flavor_name)

 sourceSets {
        main {
            jniLibs.srcDirs = ['src/main/cpp/testJNI',
                               'src/main/cpp/libs_for_android_n',]
  
        }
        flavor835 {
            //other source sets will overlay main
            jniLibs.srcDirs = ['src/main/cpp/dsp_libs_835']
        }
        flavor845 {
            jniLibs.srcDirs = ['src/main/cpp/dsp_libs_845']
        }

        flavor_Face++ {

            if (getBuildTypes().getNames().contains("debug")){
                jniLibs.srcDirs = ['src/main/cpp/face++_libs_debug']
            }else{
                jniLibs.srcDirs = ['src/main/cpp/face++_libs_release']
            }
        }

        flavor_SenseTime {

            if (getBuildTypes().getNames().contains("debug")){
                jniLibs.srcDirs = ['src/main/cpp/senseTime_libs_debug']
            }else{
                jniLibs.srcDirs = ['src/main/cpp/senseTime_libs_release']
            }
        }
    }

如上所示,可以根据所选的Build Variant选择对应的代码/so库文件进行编译。需要注意的是,在main节点中所指定的srcDirs只会作为基准被其他不同的flavor所append,并不会被覆盖,而在同一节点内会被覆盖。

如果有JNI代码需要选择不同代码/so库文件编译新的so,那么需要在CMakeLists.txt中访问以上Gradle中定义的部分变量进行条件分支选择:

if (${FLAVOR} STREQUAL "face++")
    # load face++ related libs
    add_library(lib_face++ SHARED IMPORTED)
    set_target_properties( lib_face++
            PROPERTIES IMPORTED_LOCATION
            ${CMAKE_CURRENT_SOURCE_DIR}/armeabi-v7a/libface++.so )

    if (CMAKE_BUILD_TYPE MATCHES Debug)
        message("building - debug version...")
        # add the debug flag if necessary
        add_definitions("-DMSFR_ENABLE_DEBUG")
        add_library(lib_msfr SHARED IMPORTED)
        set_target_properties( lib_face++_so
                PROPERTIES IMPORTED_LOCATION
                ${CMAKE_CURRENT_SOURCE_DIR}/../face_libs_debug/armeabi-v7a/lib_face++_so.so )

    else(CMAKE_BUILD_TYPE MATCHES Debug)
        message("building - release version...")
        add_library(lib_msfr SHARED IMPORTED)
        set_target_properties( lib_your_so
                PROPERTIES IMPORTED_LOCATION
                ${CMAKE_CURRENT_SOURCE_DIR}/../face_libs_release/armeabi-v7a/lib_face++_so.so )

    endif(CMAKE_BUILD_TYPE MATCHES Debug)
endif(${FLAVOR} STREQUAL "face++")

if (${FLAVOR} STREQUAL "senseTime")
    # load face++ related libs
    add_library(lib_face++ SHARED IMPORTED)
    set_target_properties( lib_face++
            PROPERTIES IMPORTED_LOCATION
            ${CMAKE_CURRENT_SOURCE_DIR}/armeabi-v7a/libface++.so )

    if (CMAKE_BUILD_TYPE MATCHES Debug)
        message("building - debug version...")
        # add the debug flag if necessary
        add_definitions("-DMSFR_ENABLE_DEBUG")
        add_library(lib_msfr SHARED IMPORTED)
        set_target_properties( lib_sensetime_so
                PROPERTIES IMPORTED_LOCATION
                ${CMAKE_CURRENT_SOURCE_DIR}/../face_libs_debug/armeabi-v7a/lib_sensetime_so.so )

    else(CMAKE_BUILD_TYPE MATCHES Debug)
        message("building - release version...")
        add_library(lib_msfr SHARED IMPORTED)
        set_target_properties( lib_sensetime_so
                PROPERTIES IMPORTED_LOCATION
                ${CMAKE_CURRENT_SOURCE_DIR}/../face_libs_release/armeabi-v7a/lib_sensetime_so.so )

    endif(CMAKE_BUILD_TYPE MATCHES Debug)
endif(${FLAVOR} STREQUAL "senseTime")

if (${FLAVOR} STREQUAL "835")
    add_library(lib_cnnARM SHARED IMPORTED)
    set_target_properties( lib_cdsp
            PROPERTIES IMPORTED_LOCATION
            ${CMAKE_CURRENT_SOURCE_DIR}/../dsp_libs_835/armeabi-v7a/libcdsp.so )
else(${FLAVOR} STREQUAL "835")
    add_library(lib_cnnARM SHARED IMPORTED)
    set_target_properties( lib_cdsp
            PROPERTIES IMPORTED_LOCATION
            ${CMAKE_CURRENT_SOURCE_DIR}/../dsp_libs_845/armeabi-v7a/libcdsp.so )
endif(${FLAVOR} STREQUAL "835")


message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")


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)

        #---------------------------------------------------------------------------------------------------
if(${FLAVOR} STREQUAL "senseTime")
    message("adding the source code for SenseTime...")
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc_senseTime)
    add_library( # Sets the name of the library.
             your_built_lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             # Associated headers in the same location as their source
             # file are automatically included.
             ${CMAKE_CURRENT_SOURCE_DIR}/SourceCodeJNI_SenseTime.cpp)
    target_link_libraries(your_built_lib lib_sensetime_so  ${log-lib})
else(${FLAVOR} STREQUAL "senseTime")
    message("adding the source code for Face++...")
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc_Face++)
    add_library( # Sets the name of the library.
            your_built_lib

            # Sets the library as a shared library.
            SHARED

            # Provides a relative path to your source file(s).
            # Associated headers in the same location as their source
            # file are automatically included.
            ${CMAKE_CURRENT_SOURCE_DIR}/SourceCodeJNI_Face++.cpp)
    target_link_libraries(your_built_lib lib_face++_so  ${log-lib})
endif(${FLAVOR} STREQUAL "senseTime")

如上代码则是在CMakeLists.txt中根据不同的Build Flavor选择不同代码/头文件/so库文件进行编译生成 'your_built_lib'.

以上说明共有8种build variant组合供选择,但是如果告知SenseTime目前不支持835及以下平台,那则需要在build Variant中有选择性的过滤掉某些Flavor的组合:

android {

// ......

variantFilter { variant ->
        //since we only have the MTCNN libs for 845+
        if (variant.getName().toLowerCase().contains("senseTime") && variant.getName().toLowerCase().contains("835")){
            setIgnore(true)
        }
    }
}

通过variantFilter获取当前build flavor的字段进行条件判断,针对特定组合进行过滤即可。完成Gradle Sync之后,在Android Studio的Build Variant中仅会存在6种供选择编译。

上一篇 下一篇

猜你喜欢

热点阅读