gpt4 book ai didi

android - 如何在 kotlin 中获取当前构建变体

转载 作者:行者123 更新时间:2023-11-29 00:53:16 24 4
gpt4 key购买 nike

我的 Android 项目中有几个构建变体。

如何检查 Kotlin 代码中正在编译的变体?我想提出这样的条件:如果它是本地变体,那么如果它是远程变体,则执行此操作...

例如这样的事情:

// kotlin code:
if (build.variant=="local") {
...
}
else
{
...
}

最佳答案

At build time, Gradle generates the BuildConfig class so your app code can inspect information about the current build.

查看该类提供的选项: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

关于android - 如何在 kotlin 中获取当前构建变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57488138/

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