如何根据 gradle 口味设置变量
How to set variable according to gradle flavors
我想将一个变量 test
传递给 NDK,我根据风格设置了不同的变量作为定义。但是不知为什么他总是传递最后一个味道的值。
这里是build.gradle:
apply plugin: 'com.android.library'
def test
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultPublishConfig "flavorARelease"
publishNonDefault true
defaultConfig {
minSdkVersion 15
targetSdkVersion 17
ndk {
moduleName "test"
ldLibs "log"
}
}
productFlavors {
flavorA {
test = 1
}
flavorB {
test = 2
}
}
buildTypes {
debug {
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DLOGGING=1 -DTEST="+test+" "
}
minifyEnabled false
}
release {
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DLOGGING=0 -DTEST="+test+" "
}
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
}
这里是生成的 Android.mk
中的 CFLAG 行
build/intermediates/ndk/flavorA/debug/Android.mk:
LOCAL_CFLAGS := -DLOGGING=1 -DTEST=2
我预计这里 -DTEST=1
build/intermediates/ndk/flavorB/debug/Android.mk:
LOCAL_CFLAGS := -DLOGGING=1 -DTEST=2
那么我的错误在哪里呢?或者我怎样才能实现我的目标?还请考虑实际问题更复杂,如果可能的话,我想在 "buildTypes" 段中进行这些定义。
我找到了解决方案:
首先代替 def test
为所有 productFlavors
指定一个新字段
productFlavors.all {
ext.dTest = null
}
然后每个flavor都设置这个字段(代码不变)
productFlavors {
flavorA {
dTest = 1
}
flavorB {
dTest = 2
}
}
最后你可以在 buildTypes 中做到这一点
buildTypes {
all {
productFlavors {
all {
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DTEST="+dTest+" "
}
}
}
}
debug {
minifyEnabled false
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DLOGGING=1 "
}
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DLOGGING=0 "
}
}
}
这里是完整的文件:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultPublishConfig "flavorARelease"
publishNonDefault true
defaultConfig {
minSdkVersion 15
targetSdkVersion 17
ndk {
moduleName "dTest"
ldLibs "log"
}
}
productFlavors.all {
ext.dTest = null
}
productFlavors {
flavorA {
dTest = 1
}
flavorB {
dTest = 2
}
}
buildTypes {
all {
productFlavors {
all {
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DTEST="+dTest+" "
}
}
}
}
debug {
minifyEnabled false
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DLOGGING=1 "
}
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DLOGGING=0 "
}
}
}
}
您可以使用buildConfigField
productFlavors {
demo {
buildConfigField "int", "FOO", "1"
buildConfigField "String", "FOO_STRING", "\"foo1\""
}
full {
buildConfigField "int", "FOO", "2"
buildConfigField "String", "FOO_STRING", "\"foo2\""
}
}
我想将一个变量 test
传递给 NDK,我根据风格设置了不同的变量作为定义。但是不知为什么他总是传递最后一个味道的值。
这里是build.gradle:
apply plugin: 'com.android.library'
def test
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultPublishConfig "flavorARelease"
publishNonDefault true
defaultConfig {
minSdkVersion 15
targetSdkVersion 17
ndk {
moduleName "test"
ldLibs "log"
}
}
productFlavors {
flavorA {
test = 1
}
flavorB {
test = 2
}
}
buildTypes {
debug {
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DLOGGING=1 -DTEST="+test+" "
}
minifyEnabled false
}
release {
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DLOGGING=0 -DTEST="+test+" "
}
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
}
这里是生成的 Android.mk
中的 CFLAG 行build/intermediates/ndk/flavorA/debug/Android.mk:
LOCAL_CFLAGS := -DLOGGING=1 -DTEST=2
我预计这里 -DTEST=1
build/intermediates/ndk/flavorB/debug/Android.mk:
LOCAL_CFLAGS := -DLOGGING=1 -DTEST=2
那么我的错误在哪里呢?或者我怎样才能实现我的目标?还请考虑实际问题更复杂,如果可能的话,我想在 "buildTypes" 段中进行这些定义。
我找到了解决方案:
首先代替 def test
为所有 productFlavors
productFlavors.all {
ext.dTest = null
}
然后每个flavor都设置这个字段(代码不变)
productFlavors {
flavorA {
dTest = 1
}
flavorB {
dTest = 2
}
}
最后你可以在 buildTypes 中做到这一点
buildTypes {
all {
productFlavors {
all {
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DTEST="+dTest+" "
}
}
}
}
debug {
minifyEnabled false
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DLOGGING=1 "
}
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DLOGGING=0 "
}
}
}
这里是完整的文件:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultPublishConfig "flavorARelease"
publishNonDefault true
defaultConfig {
minSdkVersion 15
targetSdkVersion 17
ndk {
moduleName "dTest"
ldLibs "log"
}
}
productFlavors.all {
ext.dTest = null
}
productFlavors {
flavorA {
dTest = 1
}
flavorB {
dTest = 2
}
}
buildTypes {
all {
productFlavors {
all {
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DTEST="+dTest+" "
}
}
}
}
debug {
minifyEnabled false
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DLOGGING=1 "
}
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
ndk {
if (cFlags == null) { cFlags = "" }
cFlags = cFlags + " -DLOGGING=0 "
}
}
}
}
您可以使用buildConfigField
productFlavors {
demo {
buildConfigField "int", "FOO", "1"
buildConfigField "String", "FOO_STRING", "\"foo1\""
}
full {
buildConfigField "int", "FOO", "2"
buildConfigField "String", "FOO_STRING", "\"foo2\""
}
}