How can I set a global variable that can be accessed from build.gradle and tasks?
In File Explorer right-click on the This PC (or Computer ) icon, then click Properties → Advanced System Settings → Environmental Variables . Under System Variables select Path , then click Edit . Add an entry for C:\Gradle\gradle-7.5. 1\bin .
Android global variables Sometimes we need to define global variables accesible from inside our android application to hold some shared values. Simplest way to implement this is to subclass Android. app. Application class and define static variables that will hold our data.
To set a global variable
project.ext.set("variableName", value)
To access it from anywhere in the project:
project.variableName
For instance:
project.ext.set("newVersionName", versionString)
and then...
println project.newVersionName
For more information see: http://www.gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html
EDIT: As commented by Dmitry, In new versions you can use the following shorthand:
project.ext.variableName = value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With