Add <macro> tag to aapt2
AAPT2 Macros are compile-time resources definitions that are expanded
when referenced during the link phase.
A macro must be defined in the res/values.xml directory. A macro
definition for a macro named "foo" looks like the following:
<macro name="foo">contents</macro>
When "@macro/foo" is used in the res/values directory or in a compiled
XML file, the contents of the macro replace the macro reference and
then the substituted contents are compiled and linked. If the macro
contents reference xml namespaces from its original definition, the
namespaces of the original macro definition will be used to determine
which package is being referenced.
Macros can be used anywhere resources can be referenced using the
@package:type/entry syntax.
Macros are not included in the final resource table or the R.java since
they are not actual resources.
Bug: 175616308
Test: aapt2_tests
Change-Id: I48b29ab6564357b32b4b4e32bff7ef06036382bc
diff --git a/tools/aapt2/ResourceValues.cpp b/tools/aapt2/ResourceValues.cpp
index 574bd2e..2a90f26 100644
--- a/tools/aapt2/ResourceValues.cpp
+++ b/tools/aapt2/ResourceValues.cpp
@@ -111,12 +111,15 @@
if (!other) {
return false;
}
- return reference_type == other->reference_type &&
- private_reference == other->private_reference && id == other->id &&
- name == other->name;
+ return reference_type == other->reference_type && private_reference == other->private_reference &&
+ id == other->id && name == other->name && type_flags == other->type_flags;
}
bool Reference::Flatten(android::Res_value* out_value) const {
+ if (name && name.value().type == ResourceType::kMacro) {
+ return false;
+ }
+
const ResourceId resid = id.value_or_default(ResourceId(0));
const bool dynamic = resid.is_valid() && is_dynamic;
@@ -551,7 +554,7 @@
return this_type_mask == that_type_mask;
}
-std::string Attribute::MaskString() const {
+std::string Attribute::MaskString(uint32_t type_mask) {
if (type_mask == android::ResTable_map::TYPE_ANY) {
return "any";
}
@@ -650,6 +653,10 @@
return out.str();
}
+std::string Attribute::MaskString() const {
+ return MaskString(type_mask);
+}
+
void Attribute::Print(std::ostream* out) const {
*out << "(attr) " << MaskString();
@@ -1017,6 +1024,21 @@
<< " [" << util::Joiner(entries, ", ") << "]";
}
+bool Macro::Equals(const Value* value) const {
+ const Macro* other = ValueCast<Macro>(value);
+ if (!other) {
+ return false;
+ }
+ return other->raw_value == raw_value && other->style_string.spans == style_string.spans &&
+ other->style_string.str == style_string.str &&
+ other->untranslatable_sections == untranslatable_sections &&
+ other->alias_namespaces == alias_namespaces;
+}
+
+void Macro::Print(std::ostream* out) const {
+ *out << "(macro) ";
+}
+
bool operator<(const Reference& a, const Reference& b) {
int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
if (cmp != 0) return cmp < 0;
@@ -1149,4 +1171,9 @@
return CopyValueFields(std::move(new_value), value);
}
+std::unique_ptr<Macro> CloningValueTransformer::TransformDerived(const Macro* value) {
+ auto new_value = std::make_unique<Macro>(*value);
+ return CopyValueFields(std::move(new_value), value);
+}
+
} // namespace aapt