构建要在代码中使用的gradle变量,具体取决于flavor和build类型 [英] build gradle variables to be used in code depending on flavor AND build type
问题描述
有没有办法在我的代码中使用build.gradle中的变量,这取决于flavor和buildType?
Is there a way to use variables from build.gradle in my code, which is dependent on flavor AND buildType?
在这个示例中:可以在Gradle中声明一个可用于Java的变量?新资源值仅取决于它是否为调试或发布版本类型。
In this example here: Is it possible to declare a variable in Gradle usable in Java? the new resource value only depends on if its a debug or release build type.
我想要的是每个可能的buildVariant的一个变量。
What I would like to have is one variable for each possible buildVariant.
所以类似于:
flavor1Debug => resValue "string", "app_name", "App 1 Debug"
flavor1Release => resValue "string", "app_name", "App 1"
flavor2Debug => resValue "string", "app_name", "App 2 Debug"
flavor2Release => resValue "string", "app_name", "App 2"
有没有一个很好的方法来做到这一点通过build.gradle或其他不包含switch或if else语句的方式?
Is there a nice way to do this either through build.gradle or another way that doesn't included switches or if else statements?
推荐答案
在build.gradle中为你的应用程序,你可以实现这一点的方法是:
In build.gradle for your app, one way you could achieve this would be:
buildTypes {
debug {
productFlavors.flavor1.buildConfigField "String", "app_name", "\"App 1 Debug\""
productFlavors.flavor2.buildConfigField "String", "app_name", "\"App 2 Debug\""
}
release {
productFlavors.flavor1.buildConfigField "String", "app_name", "\"App 1\""
productFlavors.flavor2.buildConfigField "String", "app_name", "\"App 2\""
}
}
请注意,您需要在buildTypes之前定义productFlavors {}块。
Note that you'll want to define your productFlavors{} block before your buildTypes.
这篇关于构建要在代码中使用的gradle变量,具体取决于flavor和build类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!