summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author David Chaloupka <chaloupka@google.com> 2018-01-19 10:57:53 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2018-01-19 10:57:53 +0000
commitcfd1b4b263da8fac7bda724fae0e42e618256871 (patch)
tree3abe0d80a7f5ba306e7580d13357f3d2d0c923c1
parent24de6a45aae21d576e84b1398ce346baf7639710 (diff)
parente3c1a4a9d21e7698e9e5196086198569ac5cc6cd (diff)
Merge "Handle multiple packages of same name in 'aapt2 convert'"
-rw-r--r--tools/aapt2/ResourceTable.cpp23
-rw-r--r--tools/aapt2/ResourceTable.h8
-rw-r--r--tools/aapt2/format/proto/ProtoDeserialize.cpp3
3 files changed, 32 insertions, 2 deletions
diff --git a/tools/aapt2/ResourceTable.cpp b/tools/aapt2/ResourceTable.cpp
index 9905f827d663..95bf9210ba97 100644
--- a/tools/aapt2/ResourceTable.cpp
+++ b/tools/aapt2/ResourceTable.cpp
@@ -47,6 +47,13 @@ static bool less_than_struct_with_name(const std::unique_ptr<T>& lhs, const Stri
return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0;
}
+template <typename T>
+static bool less_than_struct_with_name_and_id(const std::unique_ptr<T>& lhs,
+ const std::pair<StringPiece, Maybe<uint8_t>>& rhs) {
+ int name_cmp = lhs->name.compare(0, lhs->name.size(), rhs.first.data(), rhs.first.size());
+ return name_cmp < 0 || (name_cmp == 0 && lhs->id < rhs.second);
+}
+
ResourceTablePackage* ResourceTable::FindPackage(const StringPiece& name) const {
const auto last = packages.end();
auto iter = std::lower_bound(packages.begin(), last, name,
@@ -79,6 +86,22 @@ ResourceTablePackage* ResourceTable::CreatePackage(const StringPiece& name, Mayb
return package;
}
+ResourceTablePackage* ResourceTable::CreatePackageAllowingDuplicateNames(const StringPiece& name,
+ const Maybe<uint8_t> id) {
+ const auto last = packages.end();
+ auto iter = std::lower_bound(packages.begin(), last, std::make_pair(name, id),
+ less_than_struct_with_name_and_id<ResourceTablePackage>);
+
+ if (iter != last && name == (*iter)->name && id == (*iter)->id) {
+ return iter->get();
+ }
+
+ std::unique_ptr<ResourceTablePackage> new_package = util::make_unique<ResourceTablePackage>();
+ new_package->name = name.to_string();
+ new_package->id = id;
+ return packages.emplace(iter, std::move(new_package))->get();
+}
+
ResourceTablePackage* ResourceTable::FindOrCreatePackage(const StringPiece& name) {
const auto last = packages.end();
auto iter = std::lower_bound(packages.begin(), last, name,
diff --git a/tools/aapt2/ResourceTable.h b/tools/aapt2/ResourceTable.h
index 95e30c442042..374fe1ea66a1 100644
--- a/tools/aapt2/ResourceTable.h
+++ b/tools/aapt2/ResourceTable.h
@@ -229,6 +229,11 @@ class ResourceTable {
ResourceTablePackage* CreatePackage(const android::StringPiece& name, Maybe<uint8_t> id = {});
+ // Attempts to find a package having the specified name and ID. If not found, a new package
+ // of the specified parameters is created and returned.
+ ResourceTablePackage* CreatePackageAllowingDuplicateNames(const android::StringPiece& name,
+ const Maybe<uint8_t> id);
+
std::unique_ptr<ResourceTable> Clone() const;
// The string pool used by this resource table. Values that reference strings must use
@@ -239,7 +244,8 @@ class ResourceTable {
// destroyed.
StringPool string_pool;
- // The list of packages in this table, sorted alphabetically by package name.
+ // The list of packages in this table, sorted alphabetically by package name and increasing
+ // package ID (missing ID being the lowest).
std::vector<std::unique_ptr<ResourceTablePackage>> packages;
// Set of dynamic packages that this table may reference. Their package names get encoded
diff --git a/tools/aapt2/format/proto/ProtoDeserialize.cpp b/tools/aapt2/format/proto/ProtoDeserialize.cpp
index 064878b5f83f..81bc2c88939e 100644
--- a/tools/aapt2/format/proto/ProtoDeserialize.cpp
+++ b/tools/aapt2/format/proto/ProtoDeserialize.cpp
@@ -381,7 +381,8 @@ static bool DeserializePackageFromPb(const pb::Package& pb_package, const ResStr
std::map<ResourceId, ResourceNameRef> id_index;
- ResourceTablePackage* pkg = out_table->CreatePackage(pb_package.package_name(), id);
+ ResourceTablePackage* pkg =
+ out_table->CreatePackageAllowingDuplicateNames(pb_package.package_name(), id);
for (const pb::Type& pb_type : pb_package.type()) {
const ResourceType* res_type = ParseResourceType(pb_type.name());
if (res_type == nullptr) {