I want to use values from gradle.properties
that should go into a template string.
A naive first:
println("${project.properties[somekey]}")
doesn't work: Unresolved reference: somekey
So, quotes required?
println("${project.properties[\"somekey\"]}")
is completely broken syntax: Expecting an expression
for the first .
I couldn't find any example how to do this, yet the official documentation says expressions.
Question: is it possible to access a map in string template, and if so, how?
Yes and as follows:
"${project.properties["someKey"]}"
assuming the Map has the following signature: Map<String, Any?>
(or Map<Any...
)
Alternatives:
"${project.properties.getValue("someKey")}"
"${project.properties.getOrElse("someKey") { "lazy-evaluation-default-value" }}"
"${project.properties.getOrDefault("someKey", "someFixedDefaultValue")}"
Basically all the code you put in the ${}
is just plain Kotlin code... no further quoting/escaping required, except for the dollar sign $
itself, e.g. use "\$test"
if you do not want it to be substituted with a variable named test
or """${"$"}test"""
if you use a raw string
Note that in this println
case the following would have sufficed as well (which also goes for all the shown alternatives above. You may omit the outer surrounding quotes and ${}
altogether):
println(project.properties["someKey"])
See also Basic types - String templates