gpt4 book ai didi

android - 如何根据gradle flavor 设置变量

转载 作者:IT老高 更新时间:2023-10-28 23:10:55 27 4
gpt4 key购买 nike

我想将一个变量 test 传递给 NDK。但由于某种原因,他总是通过最后一种味道的值(value)。

这是 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/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”部分中进行这些定义。

最佳答案

你可以使用 buildConfigField

productFlavors {
demo {
buildConfigField "int", "FOO", "1"
buildConfigField "String", "FOO_STRING", "\"foo1\""
}
full {
buildConfigField "int", "FOO", "2"
buildConfigField "String", "FOO_STRING", "\"foo2\""
}
}

关于android - 如何根据gradle flavor 设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36037041/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com