diff options
| author | Ryan Mitchell <rtmitchell@google.com> | 2021-04-12 07:50:42 -0700 |
|---|---|---|
| committer | Ryan Mitchell <rtmitchell@google.com> | 2021-04-28 14:58:23 -0700 |
| commit | 326e35ffaf0ee1e3d07c977217f4e600088fd9d5 (patch) | |
| tree | c229a21641960bae0297e0b8bedb03305693024f /tools/aapt2/ResourceValues.cpp | |
| parent | ff68a9adc3454b7cddb2501d8e82bd4b10b2037c (diff) | |
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
Diffstat (limited to 'tools/aapt2/ResourceValues.cpp')
| -rw-r--r-- | tools/aapt2/ResourceValues.cpp | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/tools/aapt2/ResourceValues.cpp b/tools/aapt2/ResourceValues.cpp index 574bd2e44a84..2a90f267f185 100644 --- a/tools/aapt2/ResourceValues.cpp +++ b/tools/aapt2/ResourceValues.cpp @@ -111,12 +111,15 @@ bool Reference::Equals(const Value* value) const { 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 @@ bool Attribute::IsCompatibleWith(const Attribute& attr) const { 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 @@ std::string Attribute::MaskString() const { 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 @@ void Styleable::Print(std::ostream* out) const { << " [" << 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 @@ std::unique_ptr<Styleable> CloningValueTransformer::TransformDerived(const Style 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 |
