我想传递一个变量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行
建立/中间体/ NDK / flavorA /调试/ Android.mk:
LOCAL_CFLAGS := -DLOGGING=1 -DTEST=2
我希望-DTEST=1
在这里
建立/中间体/ NDK / flavorB /调试/ Android.mk:
LOCAL_CFLAGS := -DLOGGING=1 -DTEST=2
那我的错误在哪里?或者我如何实现目标?还请考虑真正的问题更复杂,我想在可能的情况下在“buildTypes”段中进行这些定义。
答案 0 :(得分:10)
我找到了解决方案:
首先代替def test
为所有productFlavors指定一个新字段
productFlavors.all {
ext.dTest = null
}
然后在每个风格中设置此字段(代码未更改)
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 "
}
}
}
}
答案 1 :(得分:9)
您可以使用buildConfigField
productFlavors {
demo {
buildConfigField "int", "FOO", "1"
buildConfigField "String", "FOO_STRING", "\"foo1\""
}
full {
buildConfigField "int", "FOO", "2"
buildConfigField "String", "FOO_STRING", "\"foo2\""
}
}