Skip to content

Commit e9fa435

Browse files
committed
fix productflavor patching when no android content is present in plugin supplied include.gradle files.
1 parent 4f066c1 commit e9fa435

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

build/project-template-gradle/build.gradle

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -206,24 +206,35 @@ task pluginStructureCheck {
206206
}
207207
}
208208

209-
def createProductFlavorsContent(flavor, dimensionName)
209+
def createProductFlavorsContent(flavor, dimensionName, includeAndroidContent = true)
210210
{
211-
def content = """android {
212-
productFlavors {
213-
${flavor} {
214-
dimension ${dimensionName}
215-
}
216-
}
217-
}"""
218-
return content;
211+
if (includeAndroidContent)
212+
{
213+
def content = """android {
214+
productFlavors {
215+
"${flavor}" {
216+
dimension "${dimensionName}"
217+
}
218+
}
219+
}"""
220+
return content;
221+
}
222+
else
223+
{
224+
def content = """productFlavors {
225+
"${flavor}" {
226+
dimension "${dimensionName}"
227+
}
228+
}"""
229+
return content;
230+
}
219231
}
220232

221233

222234
def createIncludeFile (filePath, flavor, dimensionName) {
223235
println "\t + creating include.gradle file for ${filePath}"
224236

225237
def defaultIncludeFile = new File(filePath, "include.gradle")
226-
227238
defaultIncludeFiletext.text = createProductFlavorsContent(flavor, dimensionName);
228239
}
229240

@@ -265,20 +276,23 @@ def replaceProductFlavorInContent(content, dimension, flavor)
265276
{
266277
def oldProductFlavorsText = content.substring(indexStart, indexEnd - 1);
267278

268-
def newProductFlavorsText = """
269-
productFlavors {
270-
"${flavor}" {
271-
dimension "${dimension}"
272-
}
273-
}
274-
""";
279+
def newProductFlavorsContent = createProductFlavorsContent(flavor, dimension, false);
275280

276-
return content.replace(oldProductFlavorsText, newProductFlavorsText);
281+
return content.replace(oldProductFlavorsText, newProductFlavorsContent);
277282
}
278283
else
279284
{
280-
def newProductFlavorsContent = createProductFlavorsContent(flavor, dimension);
281-
return content.replace("android {", "android {\t ${newProductFlavorsContent}");
285+
def androidContentExists = content.indexOf("android {") != -1;
286+
def newProductFlavorsContent = createProductFlavorsContent(flavor, dimension, !androidContentExists);
287+
288+
if (androidContentExists)
289+
{
290+
return content.replace("android {", "android { ${newProductFlavorsContent}");
291+
}
292+
else
293+
{
294+
return "${newProductFlavorsContent} \t ${content}"
295+
}
282296
}
283297
}
284298

@@ -333,17 +347,16 @@ task createPluginsConfigFile {
333347
println "$configStage createPluginsConfigFile"
334348

335349
def flavorsFile = new File("$configurationsDir/include.gradle")
336-
flavorsFile.write "" //clear config file
337-
350+
338351
if(createPluginConfigFile) {
339-
println "\t+creating product flavors include.gradle file in $configurationsDir folder..."
352+
println "\t Creating product flavors include.gradle file in $configurationsDir folder..."
340353
def flavors = flavorNames.join(", ")
341354

342355
def content = """android {
343356
flavorDimensions ${flavors}
344357
}"""
345358

346-
flavorsFile << content
359+
flavorsFile.text = content
347360
}
348361
}
349362
}

0 commit comments

Comments
 (0)