This repository was archived by the owner on Apr 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathAapt.groovy
More file actions
179 lines (151 loc) · 5.92 KB
/
Copy pathAapt.groovy
File metadata and controls
179 lines (151 loc) · 5.92 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.didi.virtualapk.aapt
import com.didi.virtualapk.collector.res.ResourceEntry
import com.didi.virtualapk.collector.res.StyleableEntry
import com.google.common.collect.ListMultimap
import com.google.common.io.Files
import groovy.io.FileType
import org.gradle.api.Project
/**
* Class to expand aapt function
*/
public class Aapt {
public static final int ID_DELETED = -1
public static final int ID_NO_ATTR = -2
public static final String RESOURCES_ARSC = 'resources.arsc'
private static final String ENTRY_SEPARATOR = '/'
private final File assetDir
private final File symbolFile
private final def toolsRevision
Aapt(final File assetDir, File symbolFile, toolsRevision) {
this.assetDir = assetDir
this.symbolFile = symbolFile
this.toolsRevision = toolsRevision
}
/**
* Filter package assets by specific types
*
* @param retainedTypes
* the resource types to retain
* @param pp
* new package id
* @param idMaps
*/
void filterPackage(final List<?> retainedTypes, final List<?> retainedStyleables, final int pp, final Map<?, ?> idMaps, final Map<?, ?> libRefTable, final Set<String> outUpdatedResources) {
final File arscFile = new File(this.assetDir, RESOURCES_ARSC)
final def arscEditor = new ArscEditor(arscFile, toolsRevision)
// Filter R.txt
if (this.symbolFile != null) {
this.filterRTxt(this.symbolFile, retainedTypes, retainedStyleables)
}
arscEditor.slice(pp, idMaps, libRefTable, retainedTypes)
outUpdatedResources.add(RESOURCES_ARSC)
this.resetAllXmlPackageId(this.assetDir, pp, idMaps, outUpdatedResources)
}
/**
* Filter resources with specific types
*
* @param retainedTypes
*/
void filterResources(final List<?> retainedTypes, final Set<String> outFilteredResources) {
def resDir = new File(assetDir, 'res')
resDir.listFiles().each { typeDir ->
def type = retainedTypes.find {
if (typeDir.name.startsWith("animator")) {
it.name == "animator"
} else {
typeDir.name.startsWith(it.name)
}
}
if (type == null) {
typeDir.listFiles().each {
outFilteredResources.add("res/$typeDir.name/$it.name")
}
typeDir.deleteDir()
return
}
def entryFiles = typeDir.listFiles()
def retainedEntryCount = entryFiles.size()
entryFiles.each { entryFile ->
def entry = type.entries.find { entryFile.name.startsWith("${it.name}.") }
if (entry == null) {
outFilteredResources.add("res/$typeDir.name/$entryFile.name")
entryFile.delete()
retainedEntryCount--
}
}
if (retainedEntryCount == 0) {
typeDir.deleteDir()
}
}
}
/**
* Reset package id for *.xml
*/
private static void resetAllXmlPackageId(final File dir, final int pp, final Map<?, ?> idMaps, final Set<String> outUpdatedResources) {
int len = dir.canonicalPath.length() + 1 // bypass '/'
def isWindows = (File.separator != ENTRY_SEPARATOR)
dir.eachFileRecurse(FileType.FILES) { file ->
if ('xml'.equalsIgnoreCase(Files.getFileExtension(file.name))) {
new AXmlEditor(file).setPackageId(pp, idMaps)
if (outUpdatedResources != null) {
def path = file.canonicalPath.substring(len)
if (isWindows) { // compat for windows
path = path.replaceAll('\\\\', ENTRY_SEPARATOR)
}
outUpdatedResources.add(path)
}
}
}
}
public static void generateRJava(File dest, String pkg, ListMultimap<String, ResourceEntry> resources, List<StyleableEntry> styleables) {
if (!dest.parentFile.exists()) {
dest.parentFile.mkdirs()
}
if (!dest.exists()) {
dest.createNewFile()
}
dest.withPrintWriter { pw ->
pw.println "/* AUTO-GENERATED FILE. DO NOT MODIFY."
pw.println " * "
pw.println " * This class was automatically generated by the"
pw.println " * aapt tool from the resource data it found. It"
pw.println " * should not be modified by hand."
pw.println " */"
pw.println "package ${pkg};"
pw.println "public final class R {"
resources.keySet().each { type ->
pw.println " public static final class ${type} {"
resources.get(type).each { entry ->
pw.println " public static final int ${entry.resourceName} = ${entry.hexNewResourceId};"
}
pw.println " }"
}
pw.println " public static final class styleable {"
styleables.each { styleable ->
pw.println " public static final ${styleable.valueType} ${styleable.name} = ${styleable.value};"
}
pw.println " }"
pw.println "}"
}
}
/**
* Filter specify types for R.txt
*
* @param rTxt
* The R.txt
* @param retainedTypes
*/
private static def filterRTxt(final File rTxt, final List<?> retainedTypes, final List<?> retainedStyleables) {
rTxt.write('')
rTxt.withPrintWriter { pw ->
retainedTypes.each { t ->
t.entries.each { e ->
pw.println("${t.type} ${t.name} ${e.name} ${e._vs}")
}
}
retainedStyleables.each {
pw.println("${it.vtype} ${it.type} ${it.key} ${it.idStr}")
}
}
}
}