Gradle任务在构建配置中更改布尔值 [英] Gradle task to change a boolean in build config

查看:528
本文介绍了Gradle任务在构建配置中更改布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个非常简单的任务,该任务可以更改gradle配置中的布尔值.

I would like to create a very simple task which change a boolean in my gradle config.

我在一个可以使用多个配置文件运行的Android应用程序上工作,对于每次构建,都需要指定我的代码中该应用程序是否必须伪造蓝牙.

I work on an Android application which can be run with several profiles, and for each build a need to specify if in my code the app must fake the bluetooth or not.

我的gradle(相关代码):

My gradle (relevant code) :

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代码中的使用示例:

Example of use in my java code :

if (BuildConfig.fakeBluetooth) {
    processFictiveBluetoothService();
} else {
    // other case
}

在命令行中的使用示例:

Examples of use in command line :

./gradlew iDebug noFakeBluetooth

./gradlew iDebug

问题:在两种情况下,fakeBluetooth的值始终为"true"(cmd行中带有或不带有"noFakeBluetooth").

Problem : in both cases the value of fakeBluetooth is always "true" (with or without "noFakeBluetooth" in cmd line).

推荐答案

您可以使用项目属性来传递值:

You can use project properties to pass the value:

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"
}

然后您可以通过以下方式传递属性:

And then you can pass the property with:

./gradlew iDebug -PfakeBluetooth=true

这篇关于Gradle任务在构建配置中更改布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆