I'm trying to include a custom field in BuildConfig
containing the build date using this function:
def getDate() {
def date = new Date()
def formattedDate = date.format('yyyyMMddHHmmss')
return formattedDate
}
Then, in defaultConfig I put:
buildConfigField "String", "BUILD_NUMBER", getDate()
Problem is, the field generated by Gradle is:
public static final String BUILD_NUMBER = 20141108114911;
which throws "Integer too large", but I don't want an Integer, I want a String!
I tried to explicitly replace the def
with String
, tried getDate().toString
, getDate() as String
and "${getDate()}"
, and still no quote to surround my String. I also tried to put a character like "-" in the middle of the date, it stills doesn't generate quotes, making:
public static final String BUILD_NUMBER = 20141108-114911;
clearly not making any sense...
I am out of idea here, not being familiar enough with Groovy and so not sure if there's another (working) way to "enforce" a String.
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.
Single quotes for String are allowed but they can't do interpolation: def b = true def date = java.time.LocalDate.now() def s = '$ {b}, here\'s the date: $ {date} ' println s + 'done!' $ {b}, here's the date: $ {date} done! Inside the single quoted string, a single quote can be escaped as \' . As seen above single quotes can be used for string.
So when it comes to initializing a variable of type 'char' we cannot use single quotes along with with def or with no type (check out this tutorial on types). In that case we must: Triple quotes are used as triplets of single quotes. large groups.
The first method to print the double quotes with the string uses an escape sequence, which is a backslash ( \ ) with a character. It is sometimes also called an escape character. Our goal is to insert double quotes at the starting and the ending point of our String. ‘\’ is the escape sequence that is used to insert a double quote.
You need to add escaped quotes:
buildConfigField "String", "BUILD_NUMBER", "\"${new Date().format('yyyyMMddHHmmss')}\""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With