如何在 kotlin 中获取当前的构建变体
Posted
技术标签:
【中文标题】如何在 kotlin 中获取当前的构建变体【英文标题】:How to get current build variant in kotlin 【发布时间】:2019-08-14 04:24:45 【问题描述】:我的 android 项目中有几个构建变体。
如何检查 Kotlin 代码中正在编译的变体?我想做出这样的条件:如果是本地变体,则执行此操作,否则如果是远程变体,则执行此操作...
例如这样的:
// kotlin code:
if (build.variant=="local")
...
else
...
【问题讨论】:
【参考方案1】:在构建时,Gradle 会生成 BuildConfig 类,以便您的应用代码 可以检查有关当前构建的信息。
查看该类提供的选项:taken from
public final class BuildConfig
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLCATION_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 = "";
您也可以根据自己的需求定义自定义变量:
android
...
buildTypes
release
// These values are defined only for the release build, which
// is typically used for full builds and continuous builds.
buildConfigField("String", "BUILD_TIME", "\"$minutesSinceEpoch\"")
resValue("string", "build_time", "$minutesSinceEpoch")
...
debug
// Use static values for incremental builds to ensure that
// resource files and BuildConfig aren't rebuilt with each run.
// If they were dynamic, they would prevent certain benefits of
// Instant Run as well as Gradle UP-TO-DATE checks.
buildConfigField("String", "BUILD_TIME", "\"0\"")
resValue("string", "build_time", "0")
并使用它:
Log.i(TAG, BuildConfig.BUILD_TIME)
Log.i(TAG, getString(R.string.build_time))
了解更多info
【讨论】:
以上是关于如何在 kotlin 中获取当前的构建变体的主要内容,如果未能解决你的问题,请参考以下文章
如何获取 Sharepoint (MOSS) 2007 中的当前语言或变体?