Skip to content

Instantly share code, notes, and snippets.

@paour
Last active October 5, 2016 12:04

Revisions

  1. paour revised this gist Feb 24, 2014. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions build_extras.gradle
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,9 @@
    def overrideProviderAuthority(packageName, inFile, outFile) {

    def xml = new XmlParser().parse(inFile)

    xml.findAll{it.name() == 'string'}.each{item ->
    if (!item.value().isEmpty() && item.value()[0] instanceof String && item.value()[0].startsWith(".res-auto")) {
    println "Overriding ${item}"
    item.value()[0] = item.value()[0].replace(".res-auto", packageName)
    println "New value: ${item} ${item.value()[0].getClass()}"
    }
    }

    @@ -27,15 +24,21 @@ android.applicationVariants.all { variant ->
    def packageName = variant.mergedFlavor.packageName + (variant.buildType.packageNameSuffix == null ? "" : variant.buildType.packageNameSuffix)

    def outFile = "${buildDir}/res-auto-values/${variant.dirName}/values/strings.xml"

    // define the strings that need to be auto-replaced in this file, and do NOT define them elsewhere
    // make sure your AndroidManifest.xml and searchable.xml reference these resources
    // this file should contain string resources; their values will be updated to replace .res-auto with the package name
    def inFile = "variants/res-auto-values.xml"

    def taskName = "override${flavor.capitalize()}${buildType.capitalize()}Authority"
    task(taskName) << {
    overrideProviderAuthority(packageName, inFile, outFile)
    }

    // instead of chnaging resource files from under Gradle, just add a source folder to the resource set
    android.sourceSets[variant.name].res.srcDir file(outFile).parentFile.parent

    // add in and out files to allow for incremental builds (not hugely important)
    tasks[taskName].inputs.file file(inFile)
    tasks[taskName].outputs.file file(outFile)

  2. paour renamed this gist Feb 24, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. paour created this gist Feb 24, 2014.
    43 changes: 43 additions & 0 deletions build_extras
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    def overrideProviderAuthority(packageName, inFile, outFile) {

    def xml = new XmlParser().parse(inFile)

    xml.findAll{it.name() == 'string'}.each{item ->
    if (!item.value().isEmpty() && item.value()[0] instanceof String && item.value()[0].startsWith(".res-auto")) {
    println "Overriding ${item}"
    item.value()[0] = item.value()[0].replace(".res-auto", packageName)
    println "New value: ${item} ${item.value()[0].getClass()}"
    }
    }

    saveXML(outFile, xml)
    }

    def saveXML(pathToFile, xml) {
    file(pathToFile).parentFile.mkdirs()
    def writer = new FileWriter(pathToFile)
    def printer = new XmlNodePrinter(new PrintWriter(writer))
    printer.preserveWhitespace = true
    printer.print(xml)
    }

    android.applicationVariants.all { variant ->
    def flavor = variant.productFlavors.get(0).name
    def buildType = variant.buildType.name
    def packageName = variant.mergedFlavor.packageName + (variant.buildType.packageNameSuffix == null ? "" : variant.buildType.packageNameSuffix)

    def outFile = "${buildDir}/res-auto-values/${variant.dirName}/values/strings.xml"
    def inFile = "variants/res-auto-values.xml"

    def taskName = "override${flavor.capitalize()}${buildType.capitalize()}Authority"
    task(taskName) << {
    overrideProviderAuthority(packageName, inFile, outFile)
    }

    android.sourceSets[variant.name].res.srcDir file(outFile).parentFile.parent

    tasks[taskName].inputs.file file(inFile)
    tasks[taskName].outputs.file file(outFile)

    variant.mergeResources.dependsOn tasks[taskName]
    }