com.mobgen.halo.android.gradle.tasks.HaloSMSManifestTask.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of halo-plugin Show documentation
Show all versions of halo-plugin Show documentation
This plugin helps on the configuration of the SDK.
package com.mobgen.halo.android.gradle.tasks
import com.android.build.gradle.api.BaseVariant
import groovy.xml.Namespace
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
/**
* Task to write the halo configuration file to the system.
*/
public class HaloSMSManifestTask extends DefaultTask {
BaseVariant androidVariant
@TaskAction
public onProcessManifests() {
String manifestLocation = getManifestLocation()
def xml = new XmlParser().parse(manifestLocation)
appendPermissions(xml)
//Add the permissions
XmlNodePrinter printer = new XmlNodePrinter(new PrintWriter(new FileWriter(manifestLocation)))
printer.setPreserveWhitespace(true)
printer.print(xml)
}
private String getManifestLocation() {
def manifestLocation = "${getProject().getBuildDir()}/intermediates/manifests/full"
if (androidVariant.getFlavorName() != null) {
manifestLocation += "/${androidVariant.getFlavorName()}"
}
manifestLocation += "/${androidVariant.getBuildType().getName()}/AndroidManifest.xml"
return manifestLocation
}
static def appendPermissions(Node xml) {
String internetPermission = " "
xml.append(new XmlParser().parseText(internetPermission))
//Remove repeated node items
def androidNS = new Namespace("http://schemas.android.com/apk/res/android", "android")
List nodePermissions = new ArrayList<>()
((NodeList) xml.get("uses-permission")).each { node ->
String permission = node.attribute(androidNS.name)
if (nodePermissions.contains(permission)) {
xml.remove(node)
} else {
nodePermissions.add(permission)
}
}
}
}