Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle not generating a String with quotes

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.

like image 347
Matthieu Harlé Avatar asked Nov 08 '14 11:11

Matthieu Harlé


People also ask

Should I use single or double quotes in my Gradle script?

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.

How to use single quotes in a string in Java?

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.

Why can't we use single quotes in a char variable?

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.

How to insert double quotes in a string in Python?

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.


1 Answers

You need to add escaped quotes:

buildConfigField "String", "BUILD_NUMBER", "\"${new Date().format('yyyyMMddHHmmss')}\""
like image 68
tim_yates Avatar answered Oct 11 '22 07:10

tim_yates