我知道可以在buildTypes中声明,但是我想在productFlavors中声明。

像这样:

productFlavors {
    f1 {
        some_variable: "v_f1"
    }
    f2 {
        some_variable: "v_f2"
    }
}

类似问题:

Is it possible to declare a variable in Gradle usable in Java?

最佳答案

是的,像这样:

productFlavors {
    f1 {
        buildConfigField "boolean", "aBoolean", "true"
        buildConfigField "String", "aString", "foo"
    }
    f2 {
        buildConfigField "boolean", "aBoolean", "false"
        buildConfigField "String", "aString", "bar"
    }
}

然后在运行时,您可以像这样访问它们:
if (BuildConfig.aBoolean) {
    // do something
}

09-28 09:42