82

I am using Gradle in my project. I have a task for doing some extra configuration with my war. I need to build a string to use in my task like, lets say I have:

task extraStuff{
    doStuff 'org.springframework:spring-web:3.0.6.RELEASE@war'
}

This works fine. What I need to do is define version (actually already defined in properties file) and use this in the task like:

springVersion=3.0.6.RELEASE

task extraStuff{
    doStuff 'org.springframework:spring-web:${springVersion}@war'
}

My problem is spring version is not recognised as variable. So how can I pass it inside the string?

huzeyfe
  • 3,554
  • 6
  • 39
  • 49
  • 1
    Don't forget, you can break it out as well: `doStuff group: 'org.springframework', name: 'spring-web', version: ${springVersion}, ext: 'war'` – Jesse Chisholm Feb 07 '17 at 21:41
  • 1
    Don't forget, you can remove the parenthesis `{}` from `${spingVersion}` as well: `doStuff group: 'org.springframework', name: 'spring-web', version: $springVersion, ext: 'war'` – HendraWD Jun 08 '18 at 03:37

5 Answers5

102

If you're developing an Android application using Gradle, you can declare a variable (i.e holding a dependency version) thanks to the keyword def like below:

def version = '1.2'
    
dependencies {
  compile "groupId:artifactId:${version}"
}
hzitoun
  • 5,492
  • 1
  • 36
  • 43
53

I think the problem may lay on string literal delimiters:

  1. The string literals are defined exactly as in groovy so enclose it in single or double quotes (e.g. "3.0.6.RELEASE");
  2. Gstrings are not parsed in single quotes strings (both single '...' or triple '''...''' ones) if i recall correctly;

So the code will be:

springVersion = '3.0.6.RELEASE' //or with double quotes "..."

task extraStuff{
    doStuff "org.springframework:spring-web:${springVersion}@war"
}
Giuseppe Ricupero
  • 6,134
  • 3
  • 23
  • 32
18

On android there are actually 2 possibilities how to achieve this. It really depends which suits your needs. Those two possibilities have their pros and cons. You can use def variable or ext{} block. Variable def is awesome because it lets you click on the variable and points exactly where it is defined in the file compared to ext{} block which does NOT points to that exact variable. On the other hand ext{} has one good advantage and that is you can refer variables from project_name/build.gradle to project_name/app/build.gradle which in some cases is very useful BUT as I said if you click on that variable lets say only inside only one file it wont points out to the definition of that variable which is very bad because it takes you more search time if your dependency list grows.

1) def option which is propably best and saves you search time.

def lifecycle = '2.0.0'

dependencies {
    implementation 'androidx.lifecycle:lifecycle-extensions:$lifecycle'
}

2) second ext{} block. Its kinda ok if dependency list is not huge.

ext {
    lifecycle = '1.1.1'
}

dependencies {
    implementation 'androidx.lifecycle:lifecycle-extensions:$lifecycle'
}

3) In some cases if you want to share variables between project_name/build.gradle and project_name/app/build.gradle use ext{}

in project_name/build.gradle you define kotlin_shared_variable:

buildscript {
    ext.kotlin_shared_variable = '1.3.41'

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_shared_variable"
    }
}

which you can use in project_name/app/build.gradle

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_shared_variable"
}

and of course you can combine them.

Kebab Krabby
  • 1,574
  • 13
  • 21
  • So, the `ext{}` option works, but I cannot override that property using the `-Plifecycle=2.0.0` command-line flag. The only way I've seen to do this is by providing the property in `gradle.properties`. Is that the recommended way of doing that? – FilBot3 Jan 27 '20 at 18:37
0

see here.

Double-quoted strings are plain java.lang.String if there’s no interpolated expression, but are groovy.lang.GString instances if interpolation is present.

Gradle uses Groovy as a DSL. Here "${springVersion}" is a placeholder, what you want is to interpolate, so you should use the double quote, only the double quote in GString has the capability to interpolate.

Yu Chai
  • 77
  • 4
0

You can also define variables in the gradle.properties file at the root of your project. You don't have to use double quotes in that file. You would need to add the following line:

lifecycle=2.0.0

drferpa
  • 26
  • 2