Android Gradle任务更改生成配置中的布尔值
我想创建一个非常简单的任务,在我的gradle配置中更改布尔值 我在一个Android应用程序上工作,该应用程序可以运行多个配置文件,对于每个构建,都需要指定应用程序是否必须在我的代码中伪造蓝牙 我的学位(相关代码): 在我的java代码中使用的示例:Android Gradle任务更改生成配置中的布尔值,android,android-gradle-plugin,build.gradle,Android,Android Gradle Plugin,Build.gradle,我想创建一个非常简单的任务,在我的gradle配置中更改布尔值 我在一个Android应用程序上工作,该应用程序可以运行多个配置文件,对于每个构建,都需要指定应用程序是否必须在我的代码中伪造蓝牙 我的学位(相关代码): 在我的java代码中使用的示例: if (BuildConfig.fakeBluetooth) { processFictiveBluetoothService(); } else { // other case } 在命令行中使用的示例: ./gradlew
if (BuildConfig.fakeBluetooth) {
processFictiveBluetoothService();
} else {
// other case
}
在命令行中使用的示例:
./gradlew iDebug noFakeBluetooth
及
问题:在这两种情况下,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.defaultConfig.buildConfigField "String", "value", "1"
我认为正确的方法是为BuildType或ProductFlavour定义资源价值:
resValue“字符串”、“键”、“值”
然后从代码中读出,如:
getResources().getString(R.string.key)代码>这也是一种解决方案。依赖于执行。。。感谢在我的情况下,我只是用它来设置任务中的配置参数
./gradlew iDebug -PfakeBluetooth=true
android.defaultConfig.buildConfigField "String", "value", "1"