AAPT2: Implement attribute compat versioning
This change defines some hardcoded rules to degrade
attributes in newer SDKs to specific older attributes.
An attribute with a degrade rule will generate a new XML for the API
in which the attribute resulting from the degradation was introduced.
Since API 22 (Lollipop MR1), attributes are correctly ignored and do
not need to be versioned. In XML files defined for APIs 22+, the
original and degraded attributes coexist in the same XML file.
One such example is paddingHorizontal, introduced in API 26.
paddingHorizontal degrades to paddingLeft and paddingRight, which
were both introduced in API 1.
Bug: 35763493
Test: make aapt2_tests
Change-Id: I4aa8755a9ee2c0cc5afdc55c3d30093fd3a47f3d
diff --git a/tools/aapt2/xml/XmlDom.cpp b/tools/aapt2/xml/XmlDom.cpp
index 98f5f1d..885ab3e 100644
--- a/tools/aapt2/xml/XmlDom.cpp
+++ b/tools/aapt2/xml/XmlDom.cpp
@@ -356,17 +356,16 @@
return util::make_unique<XmlResource>(ResourceFile{}, std::move(string_pool), std::move(root));
}
-std::unique_ptr<Node> Namespace::Clone() {
+std::unique_ptr<Node> Namespace::Clone(const ElementCloneFunc& el_cloner) {
auto ns = util::make_unique<Namespace>();
ns->comment = comment;
ns->line_number = line_number;
ns->column_number = column_number;
ns->namespace_prefix = namespace_prefix;
ns->namespace_uri = namespace_uri;
-
ns->children.reserve(children.size());
for (const std::unique_ptr<xml::Node>& child : children) {
- ns->AppendChild(child->Clone());
+ ns->AppendChild(child->Clone(el_cloner));
}
return std::move(ns);
}
@@ -412,6 +411,15 @@
return nullptr;
}
+const Attribute* Element::FindAttribute(const StringPiece& ns, const StringPiece& name) const {
+ for (const auto& attr : attributes) {
+ if (ns == attr.namespace_uri && name == attr.name) {
+ return &attr;
+ }
+ }
+ return nullptr;
+}
+
Element* Element::FindChild(const StringPiece& ns, const StringPiece& name) {
return FindChildWithAttribute(ns, name, {}, {}, {});
}
@@ -464,29 +472,23 @@
return elements;
}
-std::unique_ptr<Node> Element::Clone() {
+std::unique_ptr<Node> Element::Clone(const ElementCloneFunc& el_cloner) {
auto el = util::make_unique<Element>();
el->comment = comment;
el->line_number = line_number;
el->column_number = column_number;
el->name = name;
el->namespace_uri = namespace_uri;
-
el->attributes.reserve(attributes.size());
- for (xml::Attribute& attr : attributes) {
- // Don't copy compiled values or attributes.
- el->attributes.push_back(
- xml::Attribute{attr.namespace_uri, attr.name, attr.value});
- }
-
+ el_cloner(*this, el.get());
el->children.reserve(children.size());
for (const std::unique_ptr<xml::Node>& child : children) {
- el->AppendChild(child->Clone());
+ el->AppendChild(child->Clone(el_cloner));
}
return std::move(el);
}
-std::unique_ptr<Node> Text::Clone() {
+std::unique_ptr<Node> Text::Clone(const ElementCloneFunc&) {
auto t = util::make_unique<Text>();
t->comment = comment;
t->line_number = line_number;