Skip to content

Instantly share code, notes, and snippets.

@HannahMitt
Last active May 13, 2023 08:14
Show Gist options
  • Save HannahMitt/99922d4183bdb03356fd339413ba6303 to your computer and use it in GitHub Desktop.
Save HannahMitt/99922d4183bdb03356fd339413ba6303 to your computer and use it in GitHub Desktop.
This is a gradle task to generate the public.xml file in an Android library project. It assumes all public resources are kept in a res-public/ resource source directory. Providing this as a starting point, but there may be more efficient ways.
import groovy.xml.MarkupBuilder
// Task to generate our public.xml file
// See https://developer.android.com/studio/projects/android-library.html#PrivateResources
// We assume resources within res-public are public
task generatepublicxml {
def resDir = project.projectDir.absolutePath + "/src/main/res-public"
// Include the desired res types
// Note: we don't need the qualified resource directories,
// since those resources will already be defined in the unqualified directories
def tree = fileTree(dir: resDir,
includes: ['**/anim/*.xml',
'**/color/*.xml',
'**/drawable/*.xml',
'**/layout/*.xml',
'**/values/*.xml'
],
exclude: '**/public.xml'
);
// Create new public.xml with writer
new File(resDir + "/values/public.xml").withWriter { writer ->
// Create MarkupBuilder with 4 space indent
def destXml = new MarkupBuilder(new IndentPrinter(writer, " ", true));
def destXmlMkp = destXml.getMkp();
// GIST NOTE: our project needed the ResourceName suppression, but its not needed in general
destXml.resources(
'xmlns:tools': 'http://schemas.android.com/tools',
'tools:ignore': 'ResourceName'
) {
// Leave file comment
destXmlMkp.yield "\r\n"
destXmlMkp.comment("AUTO-GENERATED FILE. DO NOT MODIFY. public.xml is generated by the generatepublicxml gradle task")
tree.each { resFile ->
// use the directory name to get the type
def type = resFile.getParentFile().getName()
if (type == "values") {
// Resource files under values. Parse the file, and pull out the resource definitions
def parsePublicResources = new XmlParser().parse(resFile)
parsePublicResources.children().each {
// Type is usually the element, but sometimes a type attribute is present
// example: <item name="line_spacing_multiplier" format="float" type="dimen">1.4</item>
type = it.name()
if(it.@type){
type = it.@type
}
// it.@name is value in name=
"public"("name": it.@name, "type": type)
}
} else {
// Drawable, layout, etc files
"public"("name": resFile.getName().tokenize('.')[0], "type": type)
}
}
}
}
}
@achinverma
Copy link

Hi , i am facing one issue in this page, please can you guide me
https://stackoverflow.com/questions/56034763/gradle-script-how-to-create-and-write-data-in-xml-file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment