Custom Fields For A Build Type In Gradle
I am trying to embed a few server addresses in my build.gradle file but I am unsure about how to do this. I know that in maven, you can write http://tes
Solution 1:
Gradle for Android offers buildConfigField
, allowing you to add arbitrary data members to the code-generated BuildConfig
class:
buildTypes {
debug {
buildConfigField "String", "SERVER_URL", '"http://test.this-is-so-fake.com"'
}
release {
buildConfigField "String", "SERVER_URL", '"http://prod.this-is-so-fake.com"'
}
mezzanine.initWith(buildTypes.release)
mezzanine {
buildConfigField "String", "SERVER_URL", '"http://stage.this-is-so-fake.com"'
}
}
In your Java code, you can refer to BuildConfig.SERVER_URL
, and it will be populated with the string based on the build type you choose at compile time.
Solution 2:
To just use a field independently from build types and product flavors, you could add it to your defaultConfig
by:
defaultConfig {
...
buildConfigField "String", "OS", '"android"'
}
Then the BuildConfig
looks like this:
publicfinalclassBuildConfig{
publicstaticfinalboolean DEBUG = Boolean.parseBoolean("true");
publicstaticfinalString APPLICATION_ID = "com.example.app";
publicstaticfinalString BUILD_TYPE = "debug";
publicstaticfinalString FLAVOR = "";
publicstaticfinalint VERSION_CODE = 1;
publicstaticfinalString VERSION_NAME = "1.0";
// Fields from default config.publicstaticfinalString OS = "android";
}
Post a Comment for "Custom Fields For A Build Type In Gradle"