如何在运行时在 Android Studio 中获取构建变体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23431354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the build variant at runtime in Android Studio?
提问by styler1972
I'd like to get the build variant during runtime, is this possible without any extra config or code?
我想在运行时获取构建变体,这可能不需要任何额外的配置或代码吗?
回答by ashishduh
Look at the generated BuildConfig
class.
查看生成的BuildConfig
类。
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.example.app";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "";
}
回答by checkmate711
Another option would be to create a separate build config variable for each build variant and use it in your code like this:
另一种选择是为每个构建变体创建一个单独的构建配置变量,并在您的代码中使用它,如下所示:
In your build.gradle file:
在您的 build.gradle 文件中:
productFlavors {
production {
buildConfigField "String", "BUILD_VARIANT", "\"prod\""
}
dev {
buildConfigField "String", "BUILD_VARIANT", "\"dev\""
}
}
To use it in your code:
要在您的代码中使用它:
if (BuildConfig.BUILD_VARIANT.equals("prod")){ // do something cool }
回答by yeberiah
You can try with
你可以试试
getPackageName();
it will return what you've defined in build.gradle
它将返回您在 build.gradle 中定义的内容
productFlavours{
flavour1{
applicationId 'com.example.package.flavour1'
}
flavour2{
applicationId 'com.example.package.flavour2'
}
}
回答by Phan Van Linh
Here is an example to define and get BuildConfig
for different flavor
这是定义和获取BuildConfig
不同风味的示例
android {
defaultConfig {
...
buildTypes {
...
}
flavorDimensions "default"
productFlavors {
develop {
applicationIdSuffix ".dev"
versionNameSuffix "-dev"
}
staging {
applicationIdSuffix ".stg"
versionNameSuffix "-stg"
}
production {
applicationIdSuffix ""
versionNameSuffix ""
}
}
applicationVariants.all { variant ->
def BASE_URL = ""
if (variant.getName().contains("develop")) {
BASE_URL = "https://localhost:8080.com/"
} else if (variant.getName().contains("staging")) {
BASE_URL = "https://stagingdomain.com/"
} else if (variant.getName().contains("production")) {
BASE_URL = "https://productdomain.com/"
}
variant.buildConfigField "String", "BASE_URL", "\"${BASE_URL}\""
}
}
Using
使用
BuildConfig.BASE_URL
BuildConfig.BASE_URL
回答by Anurag Srivastava
If you are already flavoring then no need to provide extra string field in your gradle. Just follow simple steps to get the build details:
如果您已经在调味,那么无需在您的 gradle 中提供额外的字符串字段。只需按照简单的步骤即可获取构建详细信息:
For build variant : BuildConfig.FLAVOR
For build version code : BuildConfig.VERSION_CODE
For build version name : BuildConfig.VERSION_NAME
对于构建变体:BuildConfig.FLAVOR
对于构建版本代码:BuildConfig.VERSION_CODE
对于构建版本名称:BuildConfig.VERSION_NAME