Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a9ff1409 authored by Alexandria Cornwall's avatar Alexandria Cornwall
Browse files

AAPT2: Strip dedicated tools namespace from XML

Android has a dedicated XML namespace dedicated for tools that should not be
included in the final APK. AAPT strips this out, but the feature was missing
from AAPT2. See: http://tools.android.com/tech-docs/tools-attributes

Bug: 29115919
Change-Id: I8f4fc79e6c8592a313a691134e44d16fd91f36ed
parent a4a100d2
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -88,10 +88,15 @@ struct XmlFlattenerVisitor : public xml::Visitor {
    }

    void visit(xml::Namespace* node) override {
        if (node->namespaceUri == xml::kSchemaTools) {
            // Skip dedicated tools namespace.
            xml::Visitor::visit(node);
        } else {
            writeNamespace(node, android::RES_XML_START_NAMESPACE_TYPE);
            xml::Visitor::visit(node);
            writeNamespace(node, android::RES_XML_END_NAMESPACE_TYPE);
        }
    }

    void visit(xml::Text* node) override {
        if (util::trimWhitespace(node->text).empty()) {
@@ -183,6 +188,9 @@ struct XmlFlattenerVisitor : public xml::Visitor {
                    continue;
                }
            }
            if (attr.namespaceUri == xml::kSchemaTools) {
                continue;
            }
            mFilteredAttrs.push_back(&attr);
        }

+27 −0
Original line number Diff line number Diff line
@@ -167,6 +167,33 @@ TEST_F(XmlFlattenerTest, FlattenCompiledXmlAndStripSdk21) {
    EXPECT_EQ(uint32_t(0x010103b3), tree.getAttributeNameResID(0));
}

TEST_F(XmlFlattenerTest, FlattenCompiledXmlAndStripOnlyTools) {
    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF(
            <View xmlns:tools="http://schemas.android.com/tools"
                xmlns:foo="http://schemas.android.com/foo"
                foo:bar="Foo"
                tools:ignore="MissingTranslation"/>)EOF");

    android::ResXMLTree tree;
    ASSERT_TRUE(flatten(doc.get(), &tree));

    ASSERT_EQ(tree.next(), android::ResXMLTree::START_NAMESPACE);

    size_t len;
    const char16_t* namespacePrefix = tree.getNamespacePrefix(&len);
    EXPECT_EQ(StringPiece16(namespacePrefix, len), u"foo");

    const char16_t* namespaceUri = tree.getNamespaceUri(&len);
    ASSERT_EQ(StringPiece16(namespaceUri, len), u"http://schemas.android.com/foo");

    ASSERT_EQ(tree.next(), android::ResXMLTree::START_TAG);

    EXPECT_EQ(
            tree.indexOfAttribute("http://schemas.android.com/tools", "ignore"),
            android::NAME_NOT_FOUND);
    EXPECT_GE(tree.indexOfAttribute("http://schemas.android.com/foo", "bar"), 0);
}

TEST_F(XmlFlattenerTest, AssignSpecialAttributeIndices) {
    std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF(
            <View xmlns:android="http://schemas.android.com/apk/res/android"
+1 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ constexpr const char* kSchemaAuto = "http://schemas.android.com/apk/res-auto";
constexpr const char* kSchemaPublicPrefix = "http://schemas.android.com/apk/res/";
constexpr const char* kSchemaPrivatePrefix = "http://schemas.android.com/apk/prv/res/";
constexpr const char* kSchemaAndroid = "http://schemas.android.com/apk/res/android";
constexpr const char* kSchemaTools = "http://schemas.android.com/tools";

/**
 * Result of extracting a package name from a namespace URI declaration.