From fa10505ceaf9d4c41b76bb1b32f257926e96e441 Mon Sep 17 00:00:00 2001 From: Adam Lesinski Date: Sat, 7 Nov 2015 13:34:39 -0800 Subject: AAPT2: Accept aliases defined for external resource types Resource types that are typically stored outside of the resource table (like layout, xml, drawable) can only have aliases (reference to another resource). Change-Id: Idb768801f02bb142e5be5e438904f221499bd756 --- tools/aapt2/ResourceParser.cpp | 17 +++++++++++++++-- tools/aapt2/ResourceParser_test.cpp | 8 ++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/tools/aapt2/ResourceParser.cpp b/tools/aapt2/ResourceParser.cpp index 981ece709778..f2a1878d0dc5 100644 --- a/tools/aapt2/ResourceParser.cpp +++ b/tools/aapt2/ResourceParser.cpp @@ -346,8 +346,21 @@ bool ResourceParser::parseResources(XmlPullParser* parser) { } else if (elementName == u"public-group") { result = parsePublicGroup(parser, &parsedResource); } else { - mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber())) - << "unknown resource type '" << elementName << "'"); + // Try parsing the elementName (or type) as a resource. These shall only be + // resources like 'layout' or 'xml' and they can only be references. + if (const ResourceType* type = parseResourceType(elementName)) { + parsedResource.name.type = *type; + parsedResource.value = parseXml(parser, android::ResTable_map::TYPE_REFERENCE, + false); + if (!parsedResource.value) { + mDiag->error(DiagMessage(parsedResource.source) << "invalid value for type '" + << *type << "'. Expected a reference"); + result = false; + } + } else { + mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber())) + << "unknown resource type '" << elementName << "'"); + } } if (result) { diff --git a/tools/aapt2/ResourceParser_test.cpp b/tools/aapt2/ResourceParser_test.cpp index b3ad03199d0b..b59eb95e7448 100644 --- a/tools/aapt2/ResourceParser_test.cpp +++ b/tools/aapt2/ResourceParser_test.cpp @@ -521,4 +521,12 @@ TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) { EXPECT_EQ(ResourceId(0x01010041), actualId); } +TEST_F(ResourceParserTest, ExternalTypesShouldOnlyBeReferences) { + std::string input = R"EOF(@layout/bar)EOF"; + ASSERT_TRUE(testParse(input)); + + input = R"EOF("this is a string")EOF"; + ASSERT_FALSE(testParse(input)); +} + } // namespace aapt -- cgit v1.2.3-59-g8ed1b