diff options
| author | 2021-04-12 07:50:42 -0700 | |
|---|---|---|
| committer | 2021-04-28 14:58:23 -0700 | |
| commit | 326e35ffaf0ee1e3d07c977217f4e600088fd9d5 (patch) | |
| tree | c229a21641960bae0297e0b8bedb03305693024f /tools/aapt2/java | |
| 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/java')
| -rw-r--r-- | tools/aapt2/java/JavaClassGenerator.cpp | 5 | ||||
| -rw-r--r-- | tools/aapt2/java/JavaClassGenerator_test.cpp | 21 | ||||
| -rw-r--r-- | tools/aapt2/java/ProguardRules_test.cpp | 2 |
3 files changed, 25 insertions, 3 deletions
diff --git a/tools/aapt2/java/JavaClassGenerator.cpp b/tools/aapt2/java/JavaClassGenerator.cpp index e1e2e0135cf7..de6524dc7027 100644 --- a/tools/aapt2/java/JavaClassGenerator.cpp +++ b/tools/aapt2/java/JavaClassGenerator.cpp @@ -616,8 +616,9 @@ bool JavaClassGenerator::Generate(const StringPiece& package_name_to_generate, for (const auto& package : table_->packages) { for (const auto& type : package->types) { - if (type->type == ResourceType::kAttrPrivate) { - // We generate these as part of the kAttr type, so skip them here. + if (type->type == ResourceType::kAttrPrivate || type->type == ResourceType::kMacro) { + // We generate kAttrPrivate as part of the kAttr type, so skip them here. + // Macros are not actual resources, so skip them as well. continue; } diff --git a/tools/aapt2/java/JavaClassGenerator_test.cpp b/tools/aapt2/java/JavaClassGenerator_test.cpp index d08b61e5ff66..40395ed64fe3 100644 --- a/tools/aapt2/java/JavaClassGenerator_test.cpp +++ b/tools/aapt2/java/JavaClassGenerator_test.cpp @@ -570,4 +570,25 @@ TEST(JavaClassGeneratorTest, SortsDynamicAttributesAfterFrameworkAttributes) { EXPECT_THAT(output, HasSubstr("public static final int MyStyleable_dynamic_attr=1;")); } +TEST(JavaClassGeneratorTest, SkipMacros) { + std::unique_ptr<ResourceTable> table = + test::ResourceTableBuilder() + .AddValue("android:macro/bar", ResourceId(0x01010000), test::AttributeBuilder().Build()) + .Build(); + + std::unique_ptr<IAaptContext> context = + test::ContextBuilder() + .AddSymbolSource(util::make_unique<ResourceTableSymbolSource>(table.get())) + .SetNameManglerPolicy(NameManglerPolicy{"android"}) + .Build(); + JavaClassGenerator generator(context.get(), table.get(), {}); + + std::string output; + StringOutputStream out(&output); + EXPECT_TRUE(generator.Generate("android", &out)); + out.Flush(); + + EXPECT_THAT(output, Not(HasSubstr("bar"))); +} + } // namespace aapt diff --git a/tools/aapt2/java/ProguardRules_test.cpp b/tools/aapt2/java/ProguardRules_test.cpp index b7dfec3a6b28..e1040666e410 100644 --- a/tools/aapt2/java/ProguardRules_test.cpp +++ b/tools/aapt2/java/ProguardRules_test.cpp @@ -264,7 +264,7 @@ TEST(ProguardRulesTest, IncludedLayoutRulesAreConditional) { </View>)"); foo_layout->file.name = test::ParseNameOrDie("com.foo:layout/foo"); - XmlReferenceLinker xml_linker; + XmlReferenceLinker xml_linker(nullptr); ASSERT_TRUE(xml_linker.Consume(context.get(), bar_layout.get())); ASSERT_TRUE(xml_linker.Consume(context.get(), foo_layout.get())); |