80

I'm new to gradle and am currently just trying to follow the tutorials and quite a few times I've seen single and double quotes intermixed. I just wanted to know if there was a difference of when one set should be used over the other. One example of this is section 6.12 of the tutorial - Default tasks:

defaultTasks 'clean', 'run'

task clean << {
    println 'Default Cleaning!'
}

task run << {
    println 'Default Running!'
}

task other << {
    println "I'm not a default task!"
}

So, I would just like to know if I should be paying attention to these differences or if they are inter-changable and I can use either single or double quotes when printing strings in gradle.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
Dan W
  • 5,718
  • 4
  • 33
  • 44

5 Answers5

86

Gradle build scripts are written in Groovy. Groovy has both double-quoted and single-quoted String literals. The main difference is that double-quoted String literals support String interpolation:

def x = 10
println "result is $x" // prints: result is 10

You can learn more about Groovy String interpolation in this or other Groovy articles on the web.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 3
    Thanks for the clear explanation. But now I have a follow up question, is there any reason to not always use double quotes then? – Dan W Mar 02 '13 at 15:31
  • 7
    Mostly subjective/situational ones like "looks better", "used to this style from another language", "want to use literal `$` or `"` without having to escape them", "easier to type". Many Groovy users seem to prefer single quotes by default. I prefer double quotes because they allow me to add/remove String interpolation without changing the quotes. Also I'm used to double quotes from languages like Java. – Peter Niederwieser Mar 03 '13 at 09:52
14

Yes, you can use one or the other. The only difference is that double-quoted strings can be GStrings, which can contain evaluated expressions like in the following example taken from the Groovy documentation:

foxtype = 'quick'
foxcolor = ['b', 'r', 'o', 'w', 'n']
println "The $foxtype ${foxcolor.join()} fox"
// => The quick brown fox
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
7

According to the gradle docs:

Favor single quotes for plain strings in build script listings

This is mostly to ensure consistency across guides, but single quotes are also a little less noisy than double quotes. Only use double quotes if you want to include an embedded expression in the string.

Community
  • 1
  • 1
Julian A.
  • 10,928
  • 16
  • 67
  • 107
  • 2
    Looks like that guide and guidance has disappeared. – Dave L. May 18 '20 at 00:18
  • @DaveL. Yeah, and I can't find it anywhere else. – Julian A. May 18 '20 at 19:00
  • 1
    Found some similar guidance here - https://groovy-lang.org/style-guide.html "Last but not least, prefer using single quoted strings when you need string constants, and use double quoted strings when you are explicitly relying on string interpolation." – AlgoRyan Jul 06 '20 at 02:35
  • imo, mixing is much more noisy than only using double quotes. – Android Feb 15 '21 at 07:40
2

Single-quoted strings are a series of characters surrounded by single quotes. like :

def str='a single quoted string'
println str

Ouput :

a single quoted string

Whereas Double-quoted strings allow us the String interpolation Here, we have a string with a placeholder referencing a local variable:

def name = 'Guillaume' // a plain string
def greeting = "Hello ${name}"

Output : Hello Guillaume

In your code,If you want to print the task name. So in that case, you need to use Double-quotes:

defaultTasks 'clean', 'run'

task clean << {
    println 'Default Cleaning!'
}

task run << {
    println "Default Running $run.name!"
    // here Double Quotes are required to interpolate task-name
}

task other << {
    println "I'm not a default task!"
}
deepakbhavale67
  • 347
  • 5
  • 13
0

Kotlin DSL files (like build.gradle.kts or settings.gradle.kts) are regular Kotlin code.
So, string literals are always wrapped in double quotes:

val myFirstString = "4 / 2 equals ${4 / 2}"
val mySecondString = """The property "hide" was not found"""
Mahozad
  • 18,032
  • 13
  • 118
  • 133