diff --git a/tools/aapt2/Android.mk b/tools/aapt2/Android.mk index 0622dc6288c13..05034c395e3ae 100644 --- a/tools/aapt2/Android.mk +++ b/tools/aapt2/Android.mk @@ -27,6 +27,7 @@ main := Main.cpp sources := \ BigBuffer.cpp \ BinaryResourceParser.cpp \ + BinaryXmlPullParser.cpp \ BindingXmlPullParser.cpp \ ConfigDescription.cpp \ Files.cpp \ @@ -51,7 +52,9 @@ sources := \ ScopedXmlPullParser.cpp \ SourceXmlPullParser.cpp \ XliffXmlPullParser.cpp \ - XmlFlattener.cpp + XmlFlattener.cpp \ + ZipEntry.cpp \ + ZipFile.cpp testSources := \ BigBuffer_test.cpp \ @@ -63,6 +66,7 @@ testSources := \ Locale_test.cpp \ ManifestParser_test.cpp \ Maybe_test.cpp \ + NameMangler_test.cpp \ ResourceParser_test.cpp \ Resource_test.cpp \ ResourceTable_test.cpp \ diff --git a/tools/aapt2/BinaryResourceParser.cpp b/tools/aapt2/BinaryResourceParser.cpp index 3eb96bcaaa57a..71016c18e9746 100644 --- a/tools/aapt2/BinaryResourceParser.cpp +++ b/tools/aapt2/BinaryResourceParser.cpp @@ -17,6 +17,7 @@ #include "BinaryResourceParser.h" #include "Logger.h" #include "ResChunkPullParser.h" +#include "Resolver.h" #include "ResourceParser.h" #include "ResourceTable.h" #include "ResourceTypeExtensions.h" @@ -33,28 +34,14 @@ namespace aapt { using namespace android; -template -inline static const T* convertTo(const ResChunk_header* chunk) { - if (chunk->headerSize < sizeof(T)) { - return nullptr; - } - return reinterpret_cast(chunk); -} - -inline static const uint8_t* getChunkData(const ResChunk_header& chunk) { - return reinterpret_cast(&chunk) + chunk.headerSize; -} - -inline static size_t getChunkDataLen(const ResChunk_header& chunk) { - return chunk.size - chunk.headerSize; -} - /* * Visitor that converts a reference's resource ID to a resource name, * given a mapping from resource ID to resource name. */ struct ReferenceIdToNameVisitor : ValueVisitor { - ReferenceIdToNameVisitor(const std::map& cache) : mCache(cache) { + ReferenceIdToNameVisitor(const std::shared_ptr& resolver, + std::map* cache) : + mResolver(resolver), mCache(cache) { } void visit(Reference& reference, ValueVisitorArgs&) override { @@ -104,24 +91,39 @@ private: return; } - auto cacheIter = mCache.find(reference.id); - if (cacheIter == std::end(mCache)) { - Logger::note() << "failed to find " << reference.id << std::endl; - } else { + auto cacheIter = mCache->find(reference.id); + if (cacheIter != mCache->end()) { reference.name = cacheIter->second; reference.id = 0; + } else { + const android::ResTable& table = mResolver->getResTable(); + android::ResTable::resource_name resourceName; + if (table.getResourceName(reference.id.id, false, &resourceName)) { + const ResourceType* type = parseResourceType(StringPiece16(resourceName.type, + resourceName.typeLen)); + assert(type); + reference.name.package.assign(resourceName.package, resourceName.packageLen); + reference.name.type = *type; + reference.name.entry.assign(resourceName.name, resourceName.nameLen); + reference.id = 0; + + // Add to cache. + mCache->insert({reference.id, reference.name}); + } } } - const std::map& mCache; + std::shared_ptr mResolver; + std::map* mCache; }; -BinaryResourceParser::BinaryResourceParser(std::shared_ptr table, +BinaryResourceParser::BinaryResourceParser(const std::shared_ptr& table, + const std::shared_ptr& resolver, const Source& source, const void* data, size_t len) : - mTable(table), mSource(source), mData(data), mDataLen(len) { + mTable(table), mResolver(resolver), mSource(source), mData(data), mDataLen(len) { } bool BinaryResourceParser::parse() { @@ -421,7 +423,7 @@ bool BinaryResourceParser::parsePackage(const ResChunk_header* chunk) { // Now go through the table and change resource ID references to // symbolic references. - ReferenceIdToNameVisitor visitor(mIdIndex); + ReferenceIdToNameVisitor visitor(mResolver, &mIdIndex); for (auto& type : *mTable) { for (auto& entry : type->entries) { for (auto& configValue : entry->values) { @@ -676,7 +678,8 @@ std::unique_ptr BinaryResourceParser::parseMapEntry(const ResourceNameRef std::unique_ptr + diff --git a/tools/aapt2/data/res/values/styles.xml b/tools/aapt2/data/res/values/styles.xml index 71ce388fdbbf3..c5dd27650af7e 100644 --- a/tools/aapt2/data/res/values/styles.xml +++ b/tools/aapt2/data/res/values/styles.xml @@ -1,6 +1,6 @@ -