1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
import groovy.xml.MarkupBuilder
apply plugin: 'com.github.dcendents.android-maven'
// build a jar with source files
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
failOnError false
source = android.sourceSets.main.java.sourceFiles
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
classpath += configurations.compile
}
// build a jar with javadoc
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
// 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"
def publicFolder = file(resDir + "/values")
if (!publicFolder.exists()) publicFolder.mkdirs()
// 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
// however, there are special cases like transition-v21 that is only available on lollipop and up
def tree = fileTree(dir: resDir,
includes: ['**/anim/*.xml',
'**/color/*.xml',
'**/drawable/*.xml',
'**/layout/*.xml',
'**/transition-v21/*.xml',
'**/values/*.xml'
],
exclude: '**/public.xml'
)
println "Generating public XML: ${project.name}"
// Create new public.xml with writer
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")
if (tree.isEmpty())
"public"("name": "dummy", "type": "id")
else
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.tokenize('-')[0])
}
}
}
}
}
build.dependsOn generatepublicxml
|