本文主要是介绍Android-Studio Gradle Build Flavor 使用小结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Build Flavor 作用
-
- 在一个分支上,编译不同的版本(包名/应用图标/debug/release)
-
- 多个分支代码片段/so库,合并到同一分支进行维护,并且可以根据不同的Build Variant 进行选择性编译;
使用方法
比如现在有如下需求:
- 第一维度是高通835/845平台(不同平台可能使用不同的DSP相关的so)
- 第二维度是算法提供商,以人脸检测为例提供商有SenseTime和Face++;
- 第三维度是Debug/Release版本
那么结果是在Android Studio中生成8种Build Variant供编译选择。
步骤
- Step - 1
在app的build.gradle的android节点中添加dimension:
android {//......flavorDimensions "pipeline", "platform"
}
其中:
pipeline 可取 : face++ / sensetime ;
platform 可取 : 835 / 845
- Step - 2
在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)
- Step - 3 (optional)
如果需要根据不同的build variant 选择不同的代码段/so库进行编译,可以继续往下看:
sourceSets {main {jniLibs.srcDirs = ['src/main/cpp/testJNI','src/main/cpp/libs_for_android_n',]}flavor835 {//other source sets will overlay mainjniLibs.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,并不会被覆盖,而在同一节点内会被覆盖。
- Step - 4 (optional)
如果有JNI代码需要选择不同代码/so库文件编译新的so,那么需要在CMakeLists.txt中访问以上Gradle中定义的部分变量进行条件分支选择:
if (${FLAVOR} STREQUAL "face++")# load face++ related libsadd_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 necessaryadd_definitions("-DMSFR_ENABLE_DEBUG")add_library(lib_msfr SHARED IMPORTED)set_target_properties( lib_face++_soPROPERTIES 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_soPROPERTIES 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 libsadd_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 necessaryadd_definitions("-DMSFR_ENABLE_DEBUG")add_library(lib_msfr SHARED IMPORTED)set_target_properties( lib_sensetime_soPROPERTIES 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_soPROPERTIES 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_cdspPROPERTIES 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_cdspPROPERTIES 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’.
- Step - 5 (optional)
以上说明共有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种供选择编译。
这篇关于Android-Studio Gradle Build Flavor 使用小结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!