gpt4 book ai didi

android - 在运行时访问 build.gradle ext

转载 作者:行者123 更新时间:2023-11-29 23:47:04 25 4
gpt4 key购买 nike

有谁知道如何在运行时访问我的 build.gradle 文件中的内容?

buildscript {
ext {
exoplayerVersion = "2.8.2"
}
}

我想在运行时获取 2.8.2 值,但我不知道该怎么做。感谢您的帮助。

最佳答案

您可以使用 buildConfigField .

示例声明(取自官方文档):

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);

更新:

您不能在 Java 代码中访问 ext 属性。只是为了答案的完整性,您可以在项目的其他 gradle 文件中使用这些属性。

关于android - 在运行时访问 build.gradle ext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51238094/

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