Hello :wave: We have an Android app and need to s...
# community-support
d
Hello 👋 We have an Android app and need to setup a bunch of variables needed for different parts of our build system/tasks. We don't want the file to be checked in. We need to be able to override the properties through e.g environmental variables, or arguments provided when running gradle. From my understand
gradle.properties
is not the way to go since we want it to be checked it,
local.properties
is used by AGP and not recommended to be used by others even though a lot of apps do utilize it, also it does not solve the problem of loading variables, from e.g Environment. As I understand it we basically have to write our own function that are able to fetch the properties from all the different places (e.g enviroment, gradle arguements or properties file). Feels like there should be some standardized way to do this that I'm missing?
By a custom function I mean writing something like this:
Copy code
fun getCustomProperty(name: String, defaultValue: String): String =
    System.getenv(name)
        ?: rootProject.properties.getOrDefault(name, null) as? String
        // Some function get the a custom property from our x.properties file
        ?: customProperties(rootProject.projectDir, providers).getProperty(name)
        ?: defaultValue
v
You can define the default values in
gradle.properties
. User-specific values can be in
<GRADLE_USER_HOME>/gradle.properties
which overwrite the project ones. You can also use gradle arguments on the commandline, system properties, or environment variables to overwrite those.
d
Thanks for the answer Björn! It feels to disconnected from all the other properties that the user might set, but seems like it is the way to go. It would also would be applied for all other gradle projects, right? So if you have two projects using the same variable name it would basically collide/not be possible with out passing enviroment/property directly to gradle.
A key insight I glanced over is the defaults value part of
gradle.properties
, then the defaults can neatly be kept in one place.
v
Unfortunately, yes. Gradle folks refuse to add such capability as official feature: https://github.com/gradle/gradle/issues/12283#issuecomment-1439226142 😞 There is the https://github.com/stevesaliman/gradle-properties-plugin that provides this capability, but only in a limited way and with some quirks. For example currently it completely breaks task-configuration avoidance. And plugins cannot set Gradle properties or Project properties, so the plugin sets extra properties. That means that for example consumers that use
providers.gradleProperty
will not see the values from those additional files, but only consumers that use the "traditional"
project.getProperty
and friends as those consider the extra properties for overwriting Project properties.
👍 1
d
Thanks, I'll look into the plugin and check what best fits our needs. 🙏👍
👌 1