我想创建一个非常简单的任务来更改我的 gradle 配置中的 bool 值。
我在开发一个可以使用多个配置文件运行的 Android 应用程序,并且对于每个构建都需要在我的代码中指定该应用程序是否必须伪造蓝牙。
我的gradle(相关代码):
def fakeBluetooth = "true"
buildTypes {
debug {
minifyEnabled false
signingConfig android.signingConfigs.debug
buildConfigField "boolean", "fakeBluetooth", fakeBluetooth
}
release {
minifyEnabled true
signingConfig android.signingConfigs.release
buildConfigField "boolean", "fakeBluetooth", fakeBluetooth
}
}
task noFakeBluetooth {
fakeBluetooth = "false"
}
我的 java 代码中的使用示例:
if (BuildConfig.fakeBluetooth) {
processFictiveBluetoothService();
} else {
// other case
}
命令行使用示例:
./gradlew iDebug noFakeBluetooth
和
./gradlew iDebug
问题:在这两种情况下,fakeBluetooth 的值始终为“true”(cmd 行中有或没有“noFakeBluetooth”)。
最佳答案
您可以使用项目属性来传递值:
buildTypes {
debug {
minifyEnabled false
signingConfig android.signingConfigs.debug
buildConfigField "boolean", "fakeBluetooth", fakeBluetooth()
}
release {
minifyEnabled true
signingConfig android.signingConfigs.release
buildConfigField "boolean", "fakeBluetooth", fakeBluetooth()
}
}
def fakeBluetooth() {
def value = project.getProperties().get("fakeBluetooth")
return value != null ? value : "true"
}
然后您可以通过以下方式传递属性:
./gradlew iDebug -PfakeBluetooth=true
关于android - Gradle 任务更改构建配置中的 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28474958/