Skip to content

Instantly share code, notes, and snippets.

@IhorKlimov
Created May 5, 2017 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IhorKlimov/90b3d70b1dd03238feab9cf1e4b49061 to your computer and use it in GitHub Desktop.
Save IhorKlimov/90b3d70b1dd03238feab9cf1e4b49061 to your computer and use it in GitHub Desktop.
Android Gradle Plugin for defining default wrap_content width/height attributes for views in layout. No need to declare those. Works with Data Binding now only. Add this code to your module's build.gradle and you'll get it automatically
apply plugin: 'com.google.gms.google-services'
class DefaultAttributesPlugin implements Plugin<Project> {
void apply(Project project) {
project.afterEvaluate {
def task = project.task('checkAttributes', type: DefaultAttributes)
project.android.applicationVariants.all { variant ->
project.tasks."dataBindingProcessLayouts${capitalizeFirstLetter(variant.flavorName)}${capitalizeFirstLetter(variant.buildType.name)}".doLast {
task.execute()
}
}
}
}
def capitalizeFirstLetter(String input) {
return input.substring(0, 1).toUpperCase() + input.substring(1)
}
}
class DefaultAttributes extends DefaultTask {
DefaultAttributes() {
group = 'pluginBasics'
}
@TaskAction
def check() {
description = 'Updates ReadMe file with the latest version'
project.android.applicationVariants.all { variant ->
File folder = new File("${project.projectDir}/src/main/res/layout")
println folder.isDirectory()
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
println "found layout " + listOfFiles[i].getName()
def filePath = "${project.buildDir}/intermediates/data-binding-layout-out/${variant.flavorName}/${variant.buildType.name}/layout/${listOfFiles[i].getName()}"
File file = new File(filePath)
if (file.exists()) {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance()
DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
Document doc = docBuilder.parse(filePath)
checkAttributes(doc.childNodes)
saveChanges(doc, filePath)
}
}
}
}
def saveChanges(Document doc, String filePath) {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
def transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filePath));
transformer.transform(source, result);
}
def checkAttributes(NodeList nodes) {
for (def i = 0; i < nodes.length; i++) {
def node = nodes.item(i)
println node.nodeName
if (node.nodeName != "#text") {
if (!node.hasAttribute("android:layout_height")) {
node.setAttribute("android:layout_height", "wrap_content")
}
if (!node.hasAttribute("android:layout_width")) {
node.setAttribute("android:layout_width", "wrap_content")
}
}
if (node.childNodes != null && node.childNodes.length > 0) {
checkAttributes(node.childNodes)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment