Android: Parse JSON as gradle argument

34 Views Asked by At

I would need to parse the JSON as a gradle parameter to supply UI tests by that data. What I am currently doing in build.gradle:

def getJson() {
project.getProperties().get('getJson')
}

and then I am calling this in gradle as:

buildConfigField "String", 'MYDATA', '"' + getJson + '"'

In the command line I am using the next call:

-Pjson = "{\"key1\": \"value1\", \"key2\": \"value2\"}"

But I am getting an error with BuildConfig: the escaped backslash \" just removing the slashes at all and if I will add 1 more backslash it will remove the quote and will add 2 backslashes insted. Do you know the proper way of passing JSONString here?

1

There are 1 best solutions below

0
usr153.K On
./gradlew -Pjson="{\\\"key1\\\": \\\"value1\\\", \\\"key2\\\": \\\"value2\\\"}"

\\\" is converted to \\" from CLI to gradle. And \\" is converted from code to String ".