In Gradle for Android, I\'m trying to generate the equivalent of this string-array resource...
Here's the closest I got, with guidance from @schwiz. I still can't find a way to do this with resValue, but it's possible to define a buildConfigField that can accomplish the same goal:
buildConfigField "String[]", "URL_ARRAY",
"{" +
"\"http://www.url1.com\"," +
"\"http://www.url2.com\"," +
"\"http://www.url3.com\"" +
"}"
That gives you access to the array, via BuildConfig:
public static final String[] URL_ARRAY = {
"http://www.url1.com",
"http://www.url2.com",
"http://www.url3.com"}; // whitespace added for clarity
You can then override the buildConfigField value per buildType. So, unless you specifically need this to be in R.array.*, this will meet your needs. Leaving this open for now in case anyone else knows how to do this with resValue.
The other solution will work. but i found that it wont work with other build config files like BUCK
you could also just have a long static URL string that contains all the URLs but are comma-separated (or separated by some other delimiter). then during runtime, u read out the value, split it up by the comma.
To be able to create array via resValue
you shoul store all array elements inside item
tag. So you should write in gradle something like this:
resValue('array', 'ver_2_skus', "<item>sku1</item><item>sku2</item>")
But there is an awful bug - all <
symbols writes to res as <
. I spend some time to find how can I write <
with gradle, but failed. So I made one hack - I replace <
with some string and then, after build, replace it with <
. I hate this hack, maybe I miss something, but it works. Here is code, that you must put to the end of gradle script (yes, code must be duplicated to work).
<
with some string, i.e. IWANTTOREPLACEITWITHLEFTARROW
:resValue('array', 'ver_2_skus', "IWANTTOREPLACEITWITHLEFTARROWitem>sku1IWANTTOREPLACEITWITHLEFTARROW/item>IWANTTOREPLACEITWITHLEFTARROWitem>sku2IWANTTOREPLACEITWITHLEFTARROW/item>")
android.applicationVariants.all { variant ->
println("variant: "+variant.dirName)
variant.mergeResources.doLast {
try {
println("3")
ext.env = System.getenv()
File valuesFile = file("${buildDir}/intermediates/res/merged/${variant.dirName}/values/values.xml")
String content = valuesFile.getText('UTF-8')
content = content.replaceAll(/IWANTTOREPLACEITWITHLEFTARROW/, '<')
valuesFile.write(content, 'UTF-8')
} catch (Exception e) {
println("Exception = " + e)
}
}
try {
println("try")
ext.env = System.getenv()
File valuesFile = file("${buildDir}/intermediates/res/merged/${variant.dirName}/values/values.xml")
String content = valuesFile.getText('UTF-8')
content = content.replaceAll(/IWANTTOREPLACEITWITHLEFTARROW/, '<')
valuesFile.write(content, 'UTF-8')
} catch (Exception e) {
println("Exception = " + e)
}
}
Defining String[] of Urls in BuildConfig:
android {
...
def tests1 = ["url-a1", "url-b1", "url-c1"]
ext {
tests2 = ["url-a2", "url-b2", "url-c2"]
}
// Specifies one flavor dimension.
flavorDimensions "default"
productFlavors {
devel {
dimension 'default'
}
}
//Default values for all productFlavors
productFlavors.all {
ext.tests3 = ["url-a3", "url-b3", "url-c3"]
}
android.applicationVariants.all { variant ->
buildConfigField("String[]", "TESTS1", '{' + tests1.collect {
"\"${it}\""
}.join(",") + '}')
buildConfigField("String[]", "TESTS2", '{' + android.ext.tests2.collect {
"\"${it}\""
}.join(",") + '}')
//TEST3 works only when is some Flavor defined
buildConfigField("String[]", "TESTS3", '{' + variant.productFlavors.get(0).ext.tests3.collect {
"\"${it}\""
}.join(",") + '}')
}
}