Jim Factor
Jim Factor

Reputation: 1535

How do you define variables in a Gradle file?

Specifically an Android project?

Upvotes: 0

Views: 5792

Answers (2)

Mayur Patel
Mayur Patel

Reputation: 2326

If you want to define variable in project level dependancy build.gradle(Project:NameOfYourProject) then you can define variables in ext.

like :

ext {
    vCode = 340
    vName = "v3.4.0"
}

and if you want to define variable in app level dependancy build.gradle(Module:app) then you can use def for variable declaration.

Like :

def CHAT_BUCKET_NAME = '"' + CHAT_BUCKET_NAME + '"' ?: '"'

Example :

Example

Use it like : Use variable

Upvotes: 3

Jim Factor
Jim Factor

Reputation: 1535

In the top most build.gradle define any variables you want inside of ext{}

ext{
    my_version = "16.0.0"
}

And then use it in sub folder .gradle files automatically (Android Studio) with

implementation "com.google.android.gms:play-services-analytics:$my_version"

Upvotes: 3

Related Questions