Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android gradle build: how to set global variables

How can I set a global variable that can be accessed from build.gradle and tasks?

like image 286
Guy Avatar asked Sep 01 '14 07:09

Guy


People also ask

How do I set environment variables in gradle Linux?

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 .

What is global variable in Android?

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.


1 Answers

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 
like image 140
Guy Avatar answered Sep 20 '22 17:09

Guy