andorid开发build.gradle 增加几种产品的方法

时间:2021-03-05 12:01:09

因为需要有些ndk的开发,cmakelists里需要定义不同的变量,这个在网上搜索解决方案,不是很容易就直接找到答案。

尝试了不少,最后找到解决方案。升级Gradle3.0,这个文章价值高。

在raspi平台下,需要使用到androidthings的库,但全志的R16平台下,支持的android版本低,没有androidthings的库。我就这样做的。

1. 修改 几个app和依赖cpp库的build.gradle和androidmanifest.xml文件。

app的build.gradle加入了:

flavorDimensions "platform"
productFlavors {
raspi {
dimension "platform"
minSdkVersion 23
buildConfigField 'String', "THINGSLIB", "com.google.android.things"
applicationId 'com.estone.uvc.raspirf4463test'
targetSdkVersion 27
versionCode 1
versionName '1.0'
versionNameSuffix '-ras'
}
r16 {
dimension "platform"
minSdkVersion 19
buildConfigField 'String', "THINGSLIB", ""
applicationId 'com.estone.uvc.raspirf4463test'
targetSdkVersion 27
versionCode 1
versionName '1.0'
versionNameSuffix '-r16'
}
} 依赖库的buid.gradle这样修改:
    flavorDimensions "platform"

    productFlavors {
r16 {
dimension "platform"
minSdkVersion 19
targetSdkVersion 23
buildConfigField 'String', "THINGSLIB", ""
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
versionCode 1
versionName '1.0'
externalNativeBuild {
cmake {
arguments "-DPLATFORM=R16"
}
}
// applicationIdSuffix ".r16"
}
raspi {
dimension "platform"
minSdkVersion 23
targetSdkVersion 27
buildConfigField 'String', "THINGSLIB", "com.google.android.things"
versionCode 1
versionName '1.0'
externalNativeBuild {
cmake {
arguments "-DPLATFORM=RASPI"
}
}
}
}
rapsi的minisdkversion不能是19,定义THINGSLIB,是要传导到androidmanifest.xml中。
每个app和依赖库的androidmanifest.xml都这样修改:
<!--<uses-library android:name="com.google.android.things" />-->
<uses-library android:name="${THINGSLIB}" /> 2. 修改依赖库的CMakeLists.txt文件,

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/../../../libandroidthings)
find_package(AndroidThings REQUIRED)

add_library(bsp4463 SHARED
RF4463.cpp RF4463.h radio_config_Si4463.h RF4463_JNI.cpp)

target_include_directories(bsp4463 PRIVATE
${PROJECT_DIR}/android-system-properties
${ANDROID_NDK}/sources/android/native_app_glue
${ANDROIDTHINGS_INCLUDE_DIRS})

if(PLATFORM STREQUAL "RASPI")
add_library(androidthings SHARED
IMPORTED)
set_target_properties(androidthings
PROPERTIES IMPORTED_LOCATION
${ANDROIDTHINGS_LIBRARIES})
endif()

if(PLATFORM STREQUAL "RASPI")
ADD_DEFINITIONS(-D__RASPI)
target_link_libraries(bsp4463
android
log
androidthings)
else()
target_link_libraries(bsp4463
android
log)
endif()

依赖库(Cpp库)的build.gradle,通过 arguments "-DPLATFORM=RASPI",把参数传入到cmake中,cmake中检查变量PLATFORM,……。