您可以使用这些插件之一。例如de.fuerstenau.buildconfig
:
build.gradle
:
plugins {
id 'de.fuerstenau.buildconfig' version '1.1.8'
}
buildConfig {
buildConfigField 'String', 'QUESTION', '"Life, The Universe, and Everything"'
buildConfigField 'int', 'ANSWER', '42'
}
然后得到一个像这样的BuildConfig
类:
public final class BuildConfig
{
private BuildConfig () { /*. no instance */ }
public static final String VERSION = "unspecified";
public static final String NAME = "DemoProject";
public static final String QUESTION = "Life, The Universe, and Everything";
public static final int ANSWER = 42;
}
如果您正在使用 Kotlin 并想要生成 Kotlin 版本BuildConfig
,请查看io.pixeloutlaw.gradle.buildconfigkt
。
如果不喜欢这个想法,你可以做的是资源过滤:
build.gradle
:
processResources {
expand project.properties
}
gradle.properties
project.question
(这些值与和相同project.answer
):
question=Life, The Universe, and Everything
answer=42
src/main/resources/buildconfig.properties
:
question=${question}
answer=${answer}
然后只需读buildconfig.properties
入Properties
您的应用程序并使用这些值。