Skip to content

Commit f8aea99

Browse files
author
Xavier Ducrohet
committed
Make aapt ignore tools-related data.
This patchset introduces a new standard namespace http://schemas.android.com/tools which will be used for tools specific XML attributes. Any attributes using this namespace will not be compiled into the binary XML file. The namespace node is also not written at all, and its string is not collected to ensure that there is no impact on the devices. (cherry picked from commit a5d5e9d) Change-Id: I62937b8bc34c07ac544930aa8eadd7797e0179d5
1 parent 633de7b commit f8aea99

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

tools/aapt/XMLNode.cpp

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ bool isWhitespace(const char16_t* str)
4545

4646
static const String16 RESOURCES_PREFIX(RESOURCES_ROOT_NAMESPACE);
4747
static const String16 RESOURCES_PRV_PREFIX(RESOURCES_ROOT_PRV_NAMESPACE);
48+
static const String16 RESOURCES_TOOLS_NAMESPACE("http://schemas.android.com/tools");
4849

4950
String16 getNamespaceResourcePackage(String16 namespaceUri, bool* outIsPublic)
5051
{
@@ -761,13 +762,16 @@ status_t XMLNode::addAttribute(const String16& ns, const String16& name,
761762
SourcePos(mFilename, getStartLineNumber()).error("Child to CDATA node.");
762763
return UNKNOWN_ERROR;
763764
}
764-
attribute_entry e;
765-
e.index = mNextAttributeIndex++;
766-
e.ns = ns;
767-
e.name = name;
768-
e.string = value;
769-
mAttributes.add(e);
770-
mAttributeOrder.add(e.index, mAttributes.size()-1);
765+
766+
if (ns != RESOURCES_TOOLS_NAMESPACE) {
767+
attribute_entry e;
768+
e.index = mNextAttributeIndex++;
769+
e.ns = ns;
770+
e.name = name;
771+
e.string = value;
772+
mAttributes.add(e);
773+
mAttributeOrder.add(e.index, mAttributes.size()-1);
774+
}
771775
return NO_ERROR;
772776
}
773777

@@ -1215,11 +1219,13 @@ status_t XMLNode::collect_strings(StringPool* dest, Vector<uint32_t>* outResIds,
12151219
collect_attr_strings(dest, outResIds, true);
12161220

12171221
int i;
1218-
if (mNamespacePrefix.size() > 0) {
1219-
dest->add(mNamespacePrefix, true);
1220-
}
1221-
if (mNamespaceUri.size() > 0) {
1222-
dest->add(mNamespaceUri, true);
1222+
if (RESOURCES_TOOLS_NAMESPACE != mNamespaceUri) {
1223+
if (mNamespacePrefix.size() > 0) {
1224+
dest->add(mNamespacePrefix, true);
1225+
}
1226+
if (mNamespaceUri.size() > 0) {
1227+
dest->add(mNamespaceUri, true);
1228+
}
12231229
}
12241230
if (mElementName.size() > 0) {
12251231
dest->add(mElementName, true);
@@ -1338,6 +1344,7 @@ status_t XMLNode::flatten_node(const StringPool& strings, const sp<AaptFile>& de
13381344
const void* extData = NULL;
13391345
size_t extSize = 0;
13401346
ResXMLTree_attribute attr;
1347+
bool writeCurrentNode = true;
13411348

13421349
const size_t NA = mAttributes.size();
13431350
const size_t NC = mChildren.size();
@@ -1350,7 +1357,7 @@ status_t XMLNode::flatten_node(const StringPool& strings, const sp<AaptFile>& de
13501357
const String16 style16("style");
13511358

13521359
const type type = getType();
1353-
1360+
13541361
memset(&node, 0, sizeof(node));
13551362
memset(&attr, 0, sizeof(attr));
13561363
node.header.headerSize = htods(sizeof(node));
@@ -1395,17 +1402,21 @@ status_t XMLNode::flatten_node(const StringPool& strings, const sp<AaptFile>& de
13951402
}
13961403
}
13971404
} else if (type == TYPE_NAMESPACE) {
1398-
node.header.type = htods(RES_XML_START_NAMESPACE_TYPE);
1399-
extData = &namespaceExt;
1400-
extSize = sizeof(namespaceExt);
1401-
memset(&namespaceExt, 0, sizeof(namespaceExt));
1402-
if (mNamespacePrefix.size() > 0) {
1403-
namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix));
1405+
if (mNamespaceUri == RESOURCES_TOOLS_NAMESPACE) {
1406+
writeCurrentNode = false;
14041407
} else {
1405-
namespaceExt.prefix.index = htodl((uint32_t)-1);
1408+
node.header.type = htods(RES_XML_START_NAMESPACE_TYPE);
1409+
extData = &namespaceExt;
1410+
extSize = sizeof(namespaceExt);
1411+
memset(&namespaceExt, 0, sizeof(namespaceExt));
1412+
if (mNamespacePrefix.size() > 0) {
1413+
namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix));
1414+
} else {
1415+
namespaceExt.prefix.index = htodl((uint32_t)-1);
1416+
}
1417+
namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix));
1418+
namespaceExt.uri.index = htodl(strings.offsetForString(mNamespaceUri));
14061419
}
1407-
namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix));
1408-
namespaceExt.uri.index = htodl(strings.offsetForString(mNamespaceUri));
14091420
LOG_ALWAYS_FATAL_IF(NA != 0, "Namespace nodes can't have attributes!");
14101421
} else if (type == TYPE_CDATA) {
14111422
node.header.type = htods(RES_XML_CDATA_TYPE);
@@ -1422,9 +1433,11 @@ status_t XMLNode::flatten_node(const StringPool& strings, const sp<AaptFile>& de
14221433

14231434
node.header.size = htodl(sizeof(node) + extSize + (sizeof(attr)*NA));
14241435

1425-
dest->writeData(&node, sizeof(node));
1426-
if (extSize > 0) {
1427-
dest->writeData(extData, extSize);
1436+
if (writeCurrentNode) {
1437+
dest->writeData(&node, sizeof(node));
1438+
if (extSize > 0) {
1439+
dest->writeData(extData, extSize);
1440+
}
14281441
}
14291442

14301443
for (i=0; i<NA; i++) {
@@ -1476,12 +1489,14 @@ status_t XMLNode::flatten_node(const StringPool& strings, const sp<AaptFile>& de
14761489
dest->writeData(&node, sizeof(node));
14771490
dest->writeData(&endElementExt, sizeof(endElementExt));
14781491
} else if (type == TYPE_NAMESPACE) {
1479-
node.header.type = htods(RES_XML_END_NAMESPACE_TYPE);
1480-
node.lineNumber = htodl(getEndLineNumber());
1481-
node.comment.index = htodl((uint32_t)-1);
1482-
node.header.size = htodl(sizeof(node)+extSize);
1483-
dest->writeData(&node, sizeof(node));
1484-
dest->writeData(extData, extSize);
1492+
if (writeCurrentNode) {
1493+
node.header.type = htods(RES_XML_END_NAMESPACE_TYPE);
1494+
node.lineNumber = htodl(getEndLineNumber());
1495+
node.comment.index = htodl((uint32_t)-1);
1496+
node.header.size = htodl(sizeof(node)+extSize);
1497+
dest->writeData(&node, sizeof(node));
1498+
dest->writeData(extData, extSize);
1499+
}
14851500
}
14861501

14871502
return NO_ERROR;

0 commit comments

Comments
 (0)