我的Android项目中有几个构建变体。
如何检查Kotlin代码中正在编译什么变体?我想做一个这样的条件:如果它是本地变体,那么如果它是远程变体,请这样做...
例如这样的东西:
class App extends Component {
state = {
page: 0,
}
setPageChange = () => {
setState({ page: props.key })
}
render() {
return (
<div>
<NavBar />
{if (this.state.page === 1) {
return (<Products />)
} else if(this.state.page === 2) {
return (<Contact />)
} else {
<Landing />
}}
<Footer />
</div>
);
}
}
答案 0 :(得分: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