gpt4 book ai didi

android - Gradle buildType/productFlavor 使用意外的 buildConfigField

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:45:44 26 4
gpt4 key购买 nike

给定以下配置:

productFlavors {
normal {
applicationId "com.app"
}

mock {
applicationId "com.app.mock"
}
}

buildTypes {
debug {
productFlavors.normal.buildConfigField "boolean", "mockMode", "false"
productFlavors.mock.buildConfigField "boolean", "mockMode", "true"
}

release {
productFlavors.normal.buildConfigField "boolean", "mockMode", "false"
// Release should never point to mocks. Ever.
productFlavors.mock.buildConfigField "boolean", "mockMode", "false"
}
}

enter image description here

我期望 BuildConfig.mockMode = true;,但是,这是生成的构建配置:

public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "*****";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "mock";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Fields from product flavor: mock
public static final boolean mockMode = false;
}

通过一些调查/调试,我意识到如果我在发布 buildType 中更改产品风格的值,它实际上会更新 BuildConfig.mockMode 值,尽管有 mockDebug 选择作为我的构建变体。

我已经有了更好的解决方案来实现我想做的事情,所以我只是在寻找一个答案来帮助我理解为什么 Gradle 基于配置以这种方式运行,以帮助我更多地了解它在做什么。

最佳答案

您可以将决定 BuildConfig 字段实际值的逻辑提取到它自己的方法中。这样,DSL 配置只有一行。它看起来像这样(未测试 - 预计语法错误):

buildTypes {
applicationVariants.all { variant ->
variant.buildConfigField "boolean", "mockMode", mockMode(variant)
}
}

def mockMode(variant) {
//Return true or false depending on variant.buildType and variant.productFlavors
}

关于android - Gradle buildType/productFlavor 使用意外的 buildConfigField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30233457/

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