I have been using this plugin for several years for providing various Android Gradle settings, but now I want to extend to also using it to hold settings that are used in the app itself.
Here's the code I came up with to do that:
androidComponents {
onVariants(selector().all(), { variant ->
env.allVariables.each {
if (it.key.startsWith("ANDROID_")) {
def key = it.key.substring(8)
.split("_")
.collect {it.substring(0,1).toUpperCase(Locale.ROOT)+it.substring(1).toLowerCase(Locale.ROOT)}
.join("")
key = key.substring(0,1).toLowerCase(Locale.ROOT)+key.substring(1)
def type = "String"
def wrapper = '"%s"'
def value = it.value
if (value.isEmpty()) {
wrapper = '%s'
value = null
} else if (value.matches("-?\\d+")) {
type = "int"
wrapper = '%s'
} else if (value.matches("-?\\d+(.\\d+)")) {
type = "double"
wrapper = '%s'
} else if (value.matches("httpS?://.*")) {
type = "android.net.Uri"
wrapper = 'android.net.Uri.parse("%s")'
}
variant.buildConfigFields.put(key, new BuildConfigField(type, String.format(wrapper, value), "Field from .env"))
}
}
})
}
This adds any environment variable (including from .env) that starts with ANDROID_ to the BuildConfig file to allow the app to use it.
The problem is that I want to have a sane null value for any variable that is defined in .env.template but is not set with an actual value in .env. For example, in production I can set ANDROID_SENTRY_DSN to the actual Sentry reporting URL for this app, but in development I need to make sure to leave the key in the .env file, set to an empty string.
Another problem is that I want a way to warn other developers that they are missing values from .env (such as when a developer adds a new value). While I could manually keep a list of variables that are required, that introduces an extra maintenance burden of having to add new variables to three places instead of just two: the actual .env, the .env.template, and the list in Gradle.
I see that env.allVariables filters out null values. It would be really nice if there was a way to keep the null values for anything that is present in .env.template as a way to positively show that those are values that we want to see but they are missing? Perhaps a getter for allVariablesWithUndefined (or similar) that is the same as allVariables but skips the null check?
I have been using this plugin for several years for providing various Android Gradle settings, but now I want to extend to also using it to hold settings that are used in the app itself.
Here's the code I came up with to do that:
androidComponents { onVariants(selector().all(), { variant -> env.allVariables.each { if (it.key.startsWith("ANDROID_")) { def key = it.key.substring(8) .split("_") .collect {it.substring(0,1).toUpperCase(Locale.ROOT)+it.substring(1).toLowerCase(Locale.ROOT)} .join("") key = key.substring(0,1).toLowerCase(Locale.ROOT)+key.substring(1) def type = "String" def wrapper = '"%s"' def value = it.value if (value.isEmpty()) { wrapper = '%s' value = null } else if (value.matches("-?\\d+")) { type = "int" wrapper = '%s' } else if (value.matches("-?\\d+(.\\d+)")) { type = "double" wrapper = '%s' } else if (value.matches("httpS?://.*")) { type = "android.net.Uri" wrapper = 'android.net.Uri.parse("%s")' } variant.buildConfigFields.put(key, new BuildConfigField(type, String.format(wrapper, value), "Field from .env")) } } }) }This adds any environment variable (including from
.env) that starts withANDROID_to theBuildConfigfile to allow the app to use it.The problem is that I want to have a sane
nullvalue for any variable that is defined in.env.templatebut is not set with an actual value in.env. For example, in production I can setANDROID_SENTRY_DSNto the actual Sentry reporting URL for this app, but in development I need to make sure to leave the key in the.envfile, set to an empty string.Another problem is that I want a way to warn other developers that they are missing values from
.env(such as when a developer adds a new value). While I could manually keep a list of variables that are required, that introduces an extra maintenance burden of having to add new variables to three places instead of just two: the actual.env, the.env.template, and the list in Gradle.I see that
env.allVariablesfilters outnullvalues. It would be really nice if there was a way to keep the null values for anything that is present in.env.templateas a way to positively show that those are values that we want to see but they are missing? Perhaps a getter forallVariablesWithUndefined(or similar) that is the same asallVariablesbut skips the null check?