diff --git a/tools/aapt2/Debug.cpp b/tools/aapt2/Debug.cpp index 82da24959521c..351e4202af659 100644 --- a/tools/aapt2/Debug.cpp +++ b/tools/aapt2/Debug.cpp @@ -256,53 +256,35 @@ class ValueBodyPrinter : public ConstValueVisitor { void Debug::PrintTable(const ResourceTable& table, const DebugPrintTableOptions& options, Printer* printer) { - for (const auto& package : table.packages) { - ValueHeadlinePrinter headline_printer(package->name, printer); - ValueBodyPrinter body_printer(package->name, printer); + const auto table_view = table.GetPartitionedView(); + for (const auto& package : table_view.packages) { + ValueHeadlinePrinter headline_printer(package.name, printer); + ValueBodyPrinter body_printer(package.name, printer); printer->Print("Package name="); - printer->Print(package->name); - if (package->id) { - printer->Print(StringPrintf(" id=%02x", package->id.value())); + printer->Print(package.name); + if (package.id) { + printer->Print(StringPrintf(" id=%02x", package.id.value())); } printer->Println(); printer->Indent(); - for (const auto& type : package->types) { + for (const auto& type : package.types) { printer->Print("type "); - printer->Print(to_string(type->type)); - if (type->id) { - printer->Print(StringPrintf(" id=%02x", type->id.value())); - } - printer->Println(StringPrintf(" entryCount=%zd", type->entries.size())); - - std::vector sorted_entries; - for (const auto& entry : type->entries) { - auto iter = std::lower_bound( - sorted_entries.begin(), sorted_entries.end(), entry.get(), - [](const ResourceEntry* a, const ResourceEntry* b) -> bool { - if (a->id && b->id) { - return a->id.value() < b->id.value(); - } else if (a->id) { - return true; - } else { - return false; - } - }); - sorted_entries.insert(iter, entry.get()); + printer->Print(to_string(type.type)); + if (type.id) { + printer->Print(StringPrintf(" id=%02x", type.id.value())); } + printer->Println(StringPrintf(" entryCount=%zd", type.entries.size())); printer->Indent(); - for (const ResourceEntry* entry : sorted_entries) { - const ResourceId id(package->id.value_or_default(0), type->id.value_or_default(0), - entry->id.value_or_default(0)); - + for (const ResourceEntry* entry : type.entries) { printer->Print("resource "); - printer->Print(id.to_string()); + printer->Print(entry->id.value_or_default(0).to_string()); printer->Print(" "); // Write the name without the package (this is obvious and too verbose). - printer->Print(to_string(type->type)); + printer->Print(to_string(type.type)); printer->Print("/"); printer->Print(entry->name); diff --git a/tools/aapt2/LoadedApk.cpp b/tools/aapt2/LoadedApk.cpp index e930b47f2901e..830bc5fa36aa8 100644 --- a/tools/aapt2/LoadedApk.cpp +++ b/tools/aapt2/LoadedApk.cpp @@ -113,7 +113,7 @@ std::unique_ptr LoadedApk::LoadProtoApkFromFileCollection( } std::string error; - table = util::make_unique(/** validate_resources **/ false); + table = util::make_unique(ResourceTable::Validation::kDisabled); if (!DeserializeTableFromPb(pb_table, collection.get(), table.get(), &error)) { diag->Error(DiagMessage(source) << "failed to deserialize " << kProtoResourceTablePath << ": " << error); @@ -157,7 +157,7 @@ std::unique_ptr LoadedApk::LoadBinaryApkFromFileCollection( io::IFile* table_file = collection->FindFile(kApkResourceTablePath); if (table_file != nullptr) { - table = util::make_unique(/** validate_resources **/ false); + table = util::make_unique(ResourceTable::Validation::kDisabled); std::unique_ptr data = table_file->OpenAsData(); if (data == nullptr) { diag->Error(DiagMessage(source) << "failed to open " << kApkResourceTablePath); diff --git a/tools/aapt2/Resource.h b/tools/aapt2/Resource.h index 8fe0eb3b51bfd..cf938703e1e99 100644 --- a/tools/aapt2/Resource.h +++ b/tools/aapt2/Resource.h @@ -138,7 +138,7 @@ struct ResourceId { uint32_t id; ResourceId(); - ResourceId(const ResourceId& rhs); + ResourceId(const ResourceId& rhs) = default; ResourceId(uint32_t res_id); // NOLINT(google-explicit-constructor) ResourceId(uint8_t p, uint8_t t, uint16_t e); @@ -222,8 +222,6 @@ bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b); inline ResourceId::ResourceId() : id(0) {} -inline ResourceId::ResourceId(const ResourceId& rhs) : id(rhs.id) {} - inline ResourceId::ResourceId(uint32_t res_id) : id(res_id) {} inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e) diff --git a/tools/aapt2/ResourceParser.cpp b/tools/aapt2/ResourceParser.cpp index 3d9be59dd9601..a447cef6dbbbb 100644 --- a/tools/aapt2/ResourceParser.cpp +++ b/tools/aapt2/ResourceParser.cpp @@ -118,43 +118,43 @@ static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, Parsed res->comment = trimmed_comment.to_string(); } + NewResourceBuilder res_builder(res->name); if (res->visibility_level != Visibility::Level::kUndefined) { Visibility visibility; visibility.level = res->visibility_level; visibility.source = res->source; visibility.comment = res->comment; - if (!table->SetVisibilityWithId(res->name, visibility, res->id, diag)) { - return false; - } + res_builder.SetVisibility(visibility); + } + + if (res->id.is_valid()) { + res_builder.SetId(res->id); } if (res->allow_new) { AllowNew allow_new; allow_new.source = res->source; allow_new.comment = res->comment; - if (!table->SetAllowNew(res->name, allow_new, diag)) { - return false; - } + res_builder.SetAllowNew(allow_new); } if (res->overlayable_item) { - if (!table->SetOverlayable(res->name, res->overlayable_item.value(), diag)) { - return false; - } + res_builder.SetOverlayable(res->overlayable_item.value()); } if (res->value != nullptr) { // Attach the comment, source and config to the value. res->value->SetComment(std::move(res->comment)); res->value->SetSource(std::move(res->source)); - - if (!table->AddResourceWithId(res->name, res->id, res->config, res->product, - std::move(res->value), diag)) { - return false; - } + res_builder.SetValue(std::move(res->value), res->config, res->product); } bool error = false; + if (!res->name.entry.empty()) { + if (!table->AddResource(res_builder.Build(), diag)) { + return false; + } + } for (ParsedResource& child : res->child_resources) { error |= !AddResourcesToTable(table, diag, &child); } @@ -751,7 +751,7 @@ std::unique_ptr ResourceParser::ParseXml(xml::XmlPullParser* parser, // table. std::unique_ptr id = util::make_unique(); id->SetSource(source_.WithLine(begin_xml_line)); - table_->AddResource(name, {}, {}, std::move(id), diag_); + table_->AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), diag_); }; // Process the raw value. diff --git a/tools/aapt2/ResourceParser_test.cpp b/tools/aapt2/ResourceParser_test.cpp index 9b70079a98c97..53bfa0b374758 100644 --- a/tools/aapt2/ResourceParser_test.cpp +++ b/tools/aapt2/ResourceParser_test.cpp @@ -831,25 +831,13 @@ TEST_F(ResourceParserTest, AutoIncrementIdsInPublicGroup) { Maybe result = table_.FindResource(test::ParseNameOrDie("attr/foo")); ASSERT_TRUE(result); - - ASSERT_TRUE(result.value().package->id); - ASSERT_TRUE(result.value().type->id); ASSERT_TRUE(result.value().entry->id); - ResourceId actual_id(result.value().package->id.value(), - result.value().type->id.value(), - result.value().entry->id.value()); - EXPECT_THAT(actual_id, Eq(ResourceId(0x01010040))); + EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01010040))); result = table_.FindResource(test::ParseNameOrDie("attr/bar")); ASSERT_TRUE(result); - - ASSERT_TRUE(result.value().package->id); - ASSERT_TRUE(result.value().type->id); ASSERT_TRUE(result.value().entry->id); - actual_id = ResourceId(result.value().package->id.value(), - result.value().type->id.value(), - result.value().entry->id.value()); - EXPECT_THAT(actual_id, Eq(ResourceId(0x01010041))); + EXPECT_THAT(result.value().entry->id.value(), Eq(ResourceId(0x01010041))); } TEST_F(ResourceParserTest, StrongestSymbolVisibilityWins) { diff --git a/tools/aapt2/ResourceTable.cpp b/tools/aapt2/ResourceTable.cpp index e0a9a31eee8be..cff98728c325e 100644 --- a/tools/aapt2/ResourceTable.cpp +++ b/tools/aapt2/ResourceTable.cpp @@ -18,20 +18,18 @@ #include #include -#include #include #include "android-base/logging.h" -#include "android-base/stringprintf.h" #include "androidfw/ConfigDescription.h" #include "androidfw/ResourceTypes.h" -#include "Debug.h" #include "NameMangler.h" +#include "ResourceUtils.h" #include "ResourceValues.h" #include "ValueVisitor.h" -#include "trace/TraceBuffer.h" #include "text/Unicode.h" +#include "trace/TraceBuffer.h" #include "util/Util.h" using ::aapt::text::IsValidResourceEntryName; @@ -43,135 +41,108 @@ namespace aapt { const char* Overlayable::kActorScheme = "overlay"; -static bool less_than_type_and_id(const std::unique_ptr& lhs, - const std::pair>& rhs) { - return lhs->type < rhs.first || (lhs->type == rhs.first && rhs.second && lhs->id < rhs.second); +namespace { +bool less_than_type(const std::unique_ptr& lhs, ResourceType rhs) { + return lhs->type < rhs; } template -static bool less_than_struct_with_name(const std::unique_ptr& lhs, const StringPiece& rhs) { +bool less_than_type_and_id(const T& lhs, const std::pair>& rhs) { + return lhs.id != rhs.second ? lhs.id < rhs.second : lhs.type < rhs.first; +} + +template +bool less_than_struct_with_name(const std::unique_ptr& lhs, const StringPiece& rhs) { return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0; } template -static bool less_than_struct_with_name_and_id(const std::unique_ptr& lhs, - const std::pair>& rhs) { - int name_cmp = lhs->name.compare(0, lhs->name.size(), rhs.first.data(), rhs.first.size()); - return name_cmp < 0 || (name_cmp == 0 && rhs.second && lhs->id < rhs.second); +bool greater_than_struct_with_name(const StringPiece& lhs, const std::unique_ptr& rhs) { + return rhs->name.compare(0, rhs->name.size(), lhs.data(), lhs.size()) > 0; } -ResourceTablePackage* ResourceTable::FindPackage(const StringPiece& name) const { - const auto last = packages.end(); - auto iter = std::lower_bound(packages.begin(), last, name, - less_than_struct_with_name); - if (iter != last && name == (*iter)->name) { - return iter->get(); +template +struct NameEqualRange { + bool operator()(const std::unique_ptr& lhs, const StringPiece& rhs) const { + return less_than_struct_with_name(lhs, rhs); } - return nullptr; + bool operator()(const StringPiece& lhs, const std::unique_ptr& rhs) const { + return greater_than_struct_with_name(lhs, rhs); + } +}; + +template +bool less_than_struct_with_name_and_id(const T& lhs, + const std::pair>& rhs) { + if (lhs.id != rhs.second) { + return lhs.id < rhs.second; + } + return lhs.name.compare(0, lhs.name.size(), rhs.first.data(), rhs.first.size()) < 0; } -ResourceTablePackage* ResourceTable::FindPackageById(uint8_t id) const { - for (auto& package : packages) { - if (package->id && package->id.value() == id) { - return package.get(); - } - } - return nullptr; +template +bool less_than_struct_with_name_and_id_pointer(const T* lhs, + const std::pair>& rhs) { + return less_than_struct_with_name_and_id(*lhs, rhs); } -ResourceTablePackage* ResourceTable::CreatePackage(const StringPiece& name, Maybe id) { - TRACE_CALL(); - ResourceTablePackage* package = FindOrCreatePackage(name); - if (id && !package->id) { - package->id = id; - return package; - } - - if (id && package->id && package->id.value() != id.value()) { - return nullptr; - } - return package; +template +T* FindElementsRunAction(const android::StringPiece& name, Elements& entries, Func action) { + const auto iter = + std::lower_bound(entries.begin(), entries.end(), name, less_than_struct_with_name); + const bool found = iter != entries.end() && name == (*iter)->name; + return action(found, iter); } -ResourceTablePackage* ResourceTable::CreatePackageAllowingDuplicateNames(const StringPiece& name, - const Maybe 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); +} // namespace - if (iter != last && name == (*iter)->name && id == (*iter)->id) { - return iter->get(); - } - - std::unique_ptr new_package = util::make_unique(); - new_package->name = name.to_string(); - new_package->id = id; - return packages.emplace(iter, std::move(new_package))->get(); +ResourceTable::ResourceTable(ResourceTable::Validation validation) : validation_(validation) { } -ResourceTablePackage* ResourceTable::FindOrCreatePackage(const StringPiece& name) { - const auto last = packages.end(); - auto iter = std::lower_bound(packages.begin(), last, name, - less_than_struct_with_name); - if (iter != last && name == (*iter)->name) { - return iter->get(); - } - - std::unique_ptr new_package = util::make_unique(); - new_package->name = name.to_string(); - return packages.emplace(iter, std::move(new_package))->get(); +ResourceTablePackage* ResourceTable::FindPackage(const android::StringPiece& name) const { + return FindElementsRunAction( + name, packages, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; }); } -ResourceTableType* ResourceTablePackage::FindType(ResourceType type, const Maybe id) { - const auto last = types.end(); - auto iter = std::lower_bound(types.begin(), last, std::make_pair(type, id), - less_than_type_and_id); - if (iter != last && (*iter)->type == type && (!id || id == (*iter)->id)) { - return iter->get(); - } - return nullptr; +ResourceTablePackage* ResourceTable::FindOrCreatePackage(const android::StringPiece& name) { + return FindElementsRunAction(name, packages, [&](bool found, auto& iter) { + return found ? iter->get() : packages.emplace(iter, new ResourceTablePackage(name))->get(); + }); } -ResourceTableType* ResourceTablePackage::FindOrCreateType(ResourceType type, - const Maybe id) { - const auto last = types.end(); - auto iter = std::lower_bound(types.begin(), last, std::make_pair(type, id), - less_than_type_and_id); - if (iter != last && (*iter)->type == type && (!id || id == (*iter)->id)) { - return iter->get(); - } - - auto new_type = new ResourceTableType(type); - new_type->id = id; - return types.emplace(iter, std::move(new_type))->get(); +template +static ResourceTableType* FindTypeRunAction(ResourceType type, Elements& entries, Func action) { + const auto iter = std::lower_bound(entries.begin(), entries.end(), type, less_than_type); + const bool found = iter != entries.end() && type == (*iter)->type; + return action(found, iter); } -ResourceEntry* ResourceTableType::FindEntry(const StringPiece& name, const Maybe id) { - const auto last = entries.end(); - auto iter = std::lower_bound(entries.begin(), last, std::make_pair(name, id), - less_than_struct_with_name_and_id); - if (iter != last && name == (*iter)->name && (!id || id == (*iter)->id)) { - return iter->get(); - } - return nullptr; +ResourceTableType* ResourceTablePackage::FindType(ResourceType type) const { + return FindTypeRunAction(type, types, + [&](bool found, auto& iter) { return found ? iter->get() : nullptr; }); } -ResourceEntry* ResourceTableType::FindOrCreateEntry(const StringPiece& name, - const Maybe id) { - auto last = entries.end(); - auto iter = std::lower_bound(entries.begin(), last, std::make_pair(name, id), - less_than_struct_with_name_and_id); - if (iter != last && name == (*iter)->name && (!id || id == (*iter)->id)) { - return iter->get(); - } - - auto new_entry = new ResourceEntry(name); - new_entry->id = id; - return entries.emplace(iter, std::move(new_entry))->get(); +ResourceTableType* ResourceTablePackage::FindOrCreateType(ResourceType type) { + return FindTypeRunAction(type, types, [&](bool found, auto& iter) { + return found ? iter->get() : types.emplace(iter, new ResourceTableType(type))->get(); + }); } -ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config) { - return FindValue(config, StringPiece()); +ResourceEntry* ResourceTableType::CreateEntry(const android::StringPiece& name) { + return FindElementsRunAction(name, entries, [&](bool found, auto& iter) { + return entries.emplace(iter, new ResourceEntry(name))->get(); + }); +} + +ResourceEntry* ResourceTableType::FindEntry(const android::StringPiece& name) const { + return FindElementsRunAction( + name, entries, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; }); +} + +ResourceEntry* ResourceTableType::FindOrCreateEntry(const android::StringPiece& name) { + return FindElementsRunAction(name, entries, [&](bool found, auto& iter) { + return found ? iter->get() : entries.emplace(iter, new ResourceEntry(name))->get(); + }); } struct ConfigKey { @@ -188,7 +159,20 @@ bool lt_config_key_ref(const std::unique_ptr& lhs, const Co } ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config, - const StringPiece& product) { + android::StringPiece product) { + auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, + lt_config_key_ref); + if (iter != values.end()) { + ResourceConfigValue* value = iter->get(); + if (value->config == config && StringPiece(value->product) == product) { + return value; + } + } + return nullptr; +} + +const ResourceConfigValue* ResourceEntry::FindValue(const android::ConfigDescription& config, + android::StringPiece product) const { auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, lt_config_key_ref); if (iter != values.end()) { @@ -323,303 +307,174 @@ ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* exist return CollisionResult::kConflict; } -ResourceTable::CollisionResult ResourceTable::IgnoreCollision(Value* /** existing **/, - Value* /** incoming **/) { - return CollisionResult::kKeepBoth; -} +ResourceTableView ResourceTable::GetPartitionedView() const { + ResourceTableView view; + for (const auto& package : packages) { + for (const auto& type : package->types) { + for (const auto& entry : type->entries) { + std::pair> package_key(package->name, {}); + std::pair> entry_key(entry->name, {}); + std::pair> type_key(type->type, {}); + if (entry->id) { + // If the entry has a defined id, use the id to determine insertion position. + package_key.second = entry->id.value().package_id(); + type_key.second = entry->id.value().type_id(); + entry_key.second = entry->id.value(); + } -static StringPiece ResourceNameValidator(const StringPiece& name) { - if (!IsValidResourceEntryName(name)) { - return name; - } - return {}; -} + auto package_it = + std::lower_bound(view.packages.begin(), view.packages.end(), package_key, + less_than_struct_with_name_and_id); + if (package_it == view.packages.end() || package_it->name != package_key.first || + package_it->id != package_key.second) { + ResourceTablePackageView new_package{std::string(package_key.first), package_key.second}; + package_it = view.packages.insert(package_it, new_package); + } -static StringPiece SkipNameValidator(const StringPiece& /*name*/) { - return {}; -} + auto type_it = std::lower_bound(package_it->types.begin(), package_it->types.end(), + type_key, less_than_type_and_id); + if (type_it == package_it->types.end() || type_key.first != type_it->type || + type_it->id != type_key.second) { + ResourceTableTypeView new_type{type_key.first, type_key.second}; + type_it = package_it->types.insert(type_it, new_type); + } -bool ResourceTable::AddResource(const ResourceNameRef& name, - const ConfigDescription& config, - const StringPiece& product, - std::unique_ptr value, - IDiagnostics* diag) { - return AddResourceImpl(name, ResourceId{}, config, product, std::move(value), - (validate_resources_ ? ResourceNameValidator : SkipNameValidator), - (validate_resources_ ? ResolveValueCollision : IgnoreCollision), diag); -} + if (entry->visibility.level == Visibility::Level::kPublic) { + // Only mark the type visibility level as public, it doesn't care about being private. + type_it->visibility_level = Visibility::Level::kPublic; + } -bool ResourceTable::AddResourceWithId(const ResourceNameRef& name, const ResourceId& res_id, - const ConfigDescription& config, const StringPiece& product, - std::unique_ptr value, IDiagnostics* diag) { - return AddResourceImpl(name, res_id, config, product, std::move(value), - (validate_resources_ ? ResourceNameValidator : SkipNameValidator), - (validate_resources_ ? ResolveValueCollision : IgnoreCollision), diag); -} - -bool ResourceTable::AddResourceMangled(const ResourceNameRef& name, const ConfigDescription& config, - const StringPiece& product, std::unique_ptr value, - IDiagnostics* diag) { - return AddResourceImpl(name, ResourceId{}, config, product, std::move(value), SkipNameValidator, - (validate_resources_ ? ResolveValueCollision : IgnoreCollision), diag); -} - -bool ResourceTable::AddResourceWithIdMangled(const ResourceNameRef& name, const ResourceId& id, - const ConfigDescription& config, - const StringPiece& product, - std::unique_ptr value, IDiagnostics* diag) { - return AddResourceImpl(name, id, config, product, std::move(value), SkipNameValidator, - (validate_resources_ ? ResolveValueCollision : IgnoreCollision), diag); -} - -bool ResourceTable::ValidateName(NameValidator name_validator, const ResourceNameRef& name, - const Source& source, IDiagnostics* diag) { - const StringPiece bad_char = name_validator(name.entry); - if (!bad_char.empty()) { - diag->Error(DiagMessage(source) << "resource '" << name << "' has invalid entry name '" - << name.entry << "'. Invalid character '" << bad_char << "'"); - return false; - } - return true; -} - -bool ResourceTable::AddResourceImpl(const ResourceNameRef& name, const ResourceId& res_id, - const ConfigDescription& config, const StringPiece& product, - std::unique_ptr value, NameValidator name_validator, - const CollisionResolverFunc& conflict_resolver, - IDiagnostics* diag) { - CHECK(value != nullptr); - CHECK(diag != nullptr); - - const Source& source = value->GetSource(); - if (!ValidateName(name_validator, name, source, diag)) { - return false; - } - - // Check for package names appearing twice with two different package ids - ResourceTablePackage* package = FindOrCreatePackage(name.package); - if (res_id.is_valid() && package->id && package->id.value() != res_id.package_id()) { - diag->Error(DiagMessage(source) - << "trying to add resource '" << name << "' with ID " << res_id - << " but package '" << package->name << "' already has ID " - << StringPrintf("%02x", package->id.value())); - return false; - } - - // Whether or not to error on duplicate resources - bool check_id = validate_resources_ && res_id.is_valid(); - // Whether or not to create a duplicate resource if the id does not match - bool use_id = !validate_resources_ && res_id.is_valid(); - - ResourceTableType* type = package->FindOrCreateType(name.type, use_id ? res_id.type_id() - : Maybe()); - - // Check for types appearing twice with two different type ids - if (check_id && type->id && type->id.value() != res_id.type_id()) { - diag->Error(DiagMessage(source) - << "trying to add resource '" << name << "' with ID " << res_id - << " but type '" << type->type << "' already has ID " - << StringPrintf("%02x", type->id.value())); - return false; - } - - ResourceEntry* entry = type->FindOrCreateEntry(name.entry, use_id ? res_id.entry_id() - : Maybe()); - - // Check for entries appearing twice with two different entry ids - if (check_id && entry->id && entry->id.value() != res_id.entry_id()) { - diag->Error(DiagMessage(source) - << "trying to add resource '" << name << "' with ID " << res_id - << " but resource already has ID " - << ResourceId(package->id.value(), type->id.value(), entry->id.value())); - return false; - } - - ResourceConfigValue* config_value = entry->FindOrCreateValue(config, product); - if (!config_value->value) { - // Resource does not exist, add it now. - config_value->value = std::move(value); - } else { - switch (conflict_resolver(config_value->value.get(), value.get())) { - case CollisionResult::kKeepBoth: - // Insert the value ignoring for duplicate configurations - entry->values.push_back(util::make_unique(config, product)); - entry->values.back()->value = std::move(value); - break; - - case CollisionResult::kTakeNew: - // Take the incoming value. - config_value->value = std::move(value); - break; - - case CollisionResult::kConflict: - diag->Error(DiagMessage(source) << "duplicate value for resource '" << name << "' " - << "with config '" << config << "'"); - diag->Error(DiagMessage(source) << "resource previously defined here"); - return false; - - case CollisionResult::kKeepOriginal: - break; + auto entry_it = + std::lower_bound(type_it->entries.begin(), type_it->entries.end(), entry_key, + less_than_struct_with_name_and_id_pointer); + type_it->entries.insert(entry_it, entry.get()); + } } } - if (res_id.is_valid()) { - package->id = res_id.package_id(); - type->id = res_id.type_id(); - entry->id = res_id.entry_id(); - } - - return true; + return view; } -bool ResourceTable::GetValidateResources() { - return validate_resources_; -} +bool ResourceTable::AddResource(NewResource&& res, IDiagnostics* diag) { + CHECK(diag != nullptr) << "Diagnostic pointer is null"; -bool ResourceTable::SetVisibility(const ResourceNameRef& name, const Visibility& visibility, - IDiagnostics* diag) { - return SetVisibilityImpl(name, visibility, {}, ResourceNameValidator, diag); -} - -bool ResourceTable::SetVisibilityWithId(const ResourceNameRef& name, const Visibility& visibility, - const ResourceId& res_id, IDiagnostics* diag) { - return SetVisibilityImpl(name, visibility, res_id, ResourceNameValidator, diag); -} - -bool ResourceTable::SetVisibilityWithIdMangled(const ResourceNameRef& name, - const Visibility& visibility, - const ResourceId& res_id, IDiagnostics* diag) { - return SetVisibilityImpl(name, visibility, res_id, SkipNameValidator, diag); -} - -bool ResourceTable::SetVisibilityImpl(const ResourceNameRef& name, const Visibility& visibility, - const ResourceId& res_id, NameValidator name_validator, - IDiagnostics* diag) { - CHECK(diag != nullptr); - - const Source& source = visibility.source; - if (!ValidateName(name_validator, name, source, diag)) { - return false; - } - - // Check for package names appearing twice with two different package ids - ResourceTablePackage* package = FindOrCreatePackage(name.package); - if (res_id.is_valid() && package->id && package->id.value() != res_id.package_id()) { + const bool validate = validation_ == Validation::kEnabled; + const Source source = res.value ? res.value->GetSource() : Source{}; + if (validate && !res.allow_mangled && !IsValidResourceEntryName(res.name.entry)) { diag->Error(DiagMessage(source) - << "trying to add resource '" << name << "' with ID " << res_id - << " but package '" << package->name << "' already has ID " - << StringPrintf("%02x", package->id.value())); + << "resource '" << res.name << "' has invalid entry name '" << res.name.entry); return false; } - // Whether or not to error on duplicate resources - bool check_id = validate_resources_ && res_id.is_valid(); - // Whether or not to create a duplicate resource if the id does not match - bool use_id = !validate_resources_ && res_id.is_valid(); - - ResourceTableType* type = package->FindOrCreateType(name.type, use_id ? res_id.type_id() - : Maybe()); - - // Check for types appearing twice with two different type ids - if (check_id && type->id && type->id.value() != res_id.type_id()) { - diag->Error(DiagMessage(source) - << "trying to add resource '" << name << "' with ID " << res_id - << " but type '" << type->type << "' already has ID " - << StringPrintf("%02x", type->id.value())); + if (res.id.has_value() && !res.id->first.is_valid()) { + diag->Error(DiagMessage(source) << "trying to add resource '" << res.name << "' with ID " + << res.id->first << " but that ID is invalid"); return false; } - ResourceEntry* entry = type->FindOrCreateEntry(name.entry, use_id ? res_id.entry_id() - : Maybe()); + auto package = FindOrCreatePackage(res.name.package); + auto type = package->FindOrCreateType(res.name.type); + auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), res.name.entry, + NameEqualRange{}); + const size_t entry_count = std::distance(entry_it.first, entry_it.second); - // Check for entries appearing twice with two different entry ids - if (check_id && entry->id && entry->id.value() != res_id.entry_id()) { - diag->Error(DiagMessage(source) - << "trying to add resource '" << name << "' with ID " << res_id - << " but resource already has ID " - << ResourceId(package->id.value(), type->id.value(), entry->id.value())); - return false; + ResourceEntry* entry; + if (entry_count == 0) { + // Adding a new resource + entry = type->CreateEntry(res.name.entry); + } else if (entry_count == 1) { + // Assume that the existing resource is being modified + entry = entry_it.first->get(); + } else { + // Multiple resources with the same name exist in the resource table. The only way to + // distinguish between them is using resource id since each resource should have a unique id. + CHECK(res.id.has_value()) << "ambiguous modification of resource entry '" << res.name + << "' without specifying a resource id."; + entry = entry_it.first->get(); + for (auto it = entry_it.first; it != entry_it.second; ++it) { + CHECK((bool)(*it)->id) << "ambiguous modification of resource entry '" << res.name + << "' with multiple entries without resource ids"; + if ((*it)->id == res.id->first) { + entry = it->get(); + break; + } + } } - if (res_id.is_valid()) { - package->id = res_id.package_id(); - type->id = res_id.type_id(); - entry->id = res_id.entry_id(); + if (res.id.has_value()) { + if (entry->id && entry->id.value() != res.id->first) { + if (res.id->second != OnIdConflict::CREATE_ENTRY) { + diag->Error(DiagMessage(source) + << "trying to add resource '" << res.name << "' with ID " << res.id->first + << " but resource already has ID " << entry->id.value()); + return false; + } + entry = type->CreateEntry(res.name.entry); + } + entry->id = res.id->first; } - // Only mark the type visibility level as public, it doesn't care about being private. - if (visibility.level == Visibility::Level::kPublic) { - type->visibility_level = Visibility::Level::kPublic; + if (res.visibility.has_value()) { + // Only mark the type visibility level as public, it doesn't care about being private. + if (res.visibility->level == Visibility::Level::kPublic) { + type->visibility_level = Visibility::Level::kPublic; + } + + if (res.visibility->level > entry->visibility.level) { + // This symbol definition takes precedence, replace. + entry->visibility = res.visibility.value(); + } } - if (visibility.level == Visibility::Level::kUndefined && - entry->visibility.level != Visibility::Level::kUndefined) { - // We can't undefine a symbol (remove its visibility). Ignore. - return true; + if (res.overlayable.has_value()) { + if (entry->overlayable_item) { + diag->Error(DiagMessage(res.overlayable->source) + << "duplicate overlayable declaration for resource '" << res.name << "'"); + diag->Error(DiagMessage(entry->overlayable_item.value().source) + << "previous declaration here"); + return false; + } + entry->overlayable_item = res.overlayable.value(); } - if (visibility.level < entry->visibility.level) { - // We can't downgrade public to private. Ignore. - return true; + if (res.allow_new.has_value()) { + entry->allow_new = res.allow_new.value(); } - // This symbol definition takes precedence, replace. - entry->visibility = visibility; - return true; -} + if (res.value != nullptr) { + auto config_value = entry->FindOrCreateValue(res.config, res.product); + if (!config_value->value) { + // Resource does not exist, add it now. + config_value->value = std::move(res.value); + } else { + // When validation is enabled, ensure that a resource cannot have multiple values defined for + // the same configuration. + auto result = validate ? ResolveValueCollision(config_value->value.get(), res.value.get()) + : CollisionResult::kKeepBoth; + switch (result) { + case CollisionResult::kKeepBoth: + // Insert the value ignoring for duplicate configurations + entry->values.push_back(util::make_unique(res.config, res.product)); + entry->values.back()->value = std::move(res.value); + break; -bool ResourceTable::SetAllowNew(const ResourceNameRef& name, const AllowNew& allow_new, - IDiagnostics* diag) { - return SetAllowNewImpl(name, allow_new, ResourceNameValidator, diag); -} + case CollisionResult::kTakeNew: + // Take the incoming value. + config_value->value = std::move(res.value); + break; -bool ResourceTable::SetAllowNewMangled(const ResourceNameRef& name, const AllowNew& allow_new, - IDiagnostics* diag) { - return SetAllowNewImpl(name, allow_new, SkipNameValidator, diag); -} + case CollisionResult::kConflict: + diag->Error(DiagMessage(source) << "duplicate value for resource '" << res.name << "' " + << "with config '" << res.config << "'"); + diag->Error(DiagMessage(source) << "resource previously defined here"); + return false; -bool ResourceTable::SetAllowNewImpl(const ResourceNameRef& name, const AllowNew& allow_new, - NameValidator name_validator, IDiagnostics* diag) { - CHECK(diag != nullptr); - - if (!ValidateName(name_validator, name, allow_new.source, diag)) { - return false; + case CollisionResult::kKeepOriginal: + break; + } + } } - ResourceTablePackage* package = FindOrCreatePackage(name.package); - ResourceTableType* type = package->FindOrCreateType(name.type); - ResourceEntry* entry = type->FindOrCreateEntry(name.entry); - entry->allow_new = allow_new; - return true; -} - -bool ResourceTable::SetOverlayable(const ResourceNameRef& name, const OverlayableItem& overlayable, - IDiagnostics* diag) { - return SetOverlayableImpl(name, overlayable, ResourceNameValidator, diag); -} - -bool ResourceTable::SetOverlayableImpl(const ResourceNameRef& name, - const OverlayableItem& overlayable, - NameValidator name_validator, IDiagnostics *diag) { - CHECK(diag != nullptr); - - if (!ValidateName(name_validator, name, overlayable.source, diag)) { - return false; - } - - ResourceTablePackage* package = FindOrCreatePackage(name.package); - ResourceTableType* type = package->FindOrCreateType(name.type); - ResourceEntry* entry = type->FindOrCreateEntry(name.entry); - - if (entry->overlayable_item) { - diag->Error(DiagMessage(overlayable.source) - << "duplicate overlayable declaration for resource '" << name << "'"); - diag->Error(DiagMessage(entry->overlayable_item.value().source) - << "previous declaration here"); - return false; - } - - entry->overlayable_item = overlayable; return true; } @@ -641,17 +496,38 @@ Maybe ResourceTable::FindResource(const ResourceNam return SearchResult{package, type, entry}; } +Maybe ResourceTable::FindResource(const ResourceNameRef& name, + ResourceId id) const { + ResourceTablePackage* package = FindPackage(name.package); + if (package == nullptr) { + return {}; + } + + ResourceTableType* type = package->FindType(name.type); + if (type == nullptr) { + return {}; + } + + auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry, + NameEqualRange{}); + for (auto it = entry_it.first; it != entry_it.second; ++it) { + if ((*it)->id == id) { + return SearchResult{package, type, it->get()}; + } + } + return {}; +} + std::unique_ptr ResourceTable::Clone() const { std::unique_ptr new_table = util::make_unique(); for (const auto& pkg : packages) { - ResourceTablePackage* new_pkg = new_table->CreatePackage(pkg->name, pkg->id); + ResourceTablePackage* new_pkg = new_table->FindOrCreatePackage(pkg->name); for (const auto& type : pkg->types) { ResourceTableType* new_type = new_pkg->FindOrCreateType(type->type); - new_type->id = type->id; new_type->visibility_level = type->visibility_level; for (const auto& entry : type->entries) { - ResourceEntry* new_entry = new_type->FindOrCreateEntry(entry->name); + ResourceEntry* new_entry = new_type->CreateEntry(entry->name); new_entry->id = entry->id; new_entry->visibility = entry->visibility; new_entry->allow_new = entry->allow_new; @@ -668,4 +544,51 @@ std::unique_ptr ResourceTable::Clone() const { return new_table; } +NewResourceBuilder::NewResourceBuilder(const ResourceNameRef& name) { + res_.name = name.ToResourceName(); +} + +NewResourceBuilder::NewResourceBuilder(const std::string& name) { + ResourceNameRef ref; + CHECK(ResourceUtils::ParseResourceName(name, &ref)) << "invalid resource name: " << name; + res_.name = ref.ToResourceName(); +} + +NewResourceBuilder& NewResourceBuilder::SetValue(std::unique_ptr value, + android::ConfigDescription config, + std::string product) { + res_.value = std::move(value); + res_.config = std::move(config); + res_.product = std::move(product); + return *this; +} + +NewResourceBuilder& NewResourceBuilder::SetId(ResourceId id, OnIdConflict on_conflict) { + res_.id = std::make_pair(id, on_conflict); + return *this; +} + +NewResourceBuilder& NewResourceBuilder::SetVisibility(Visibility visibility) { + res_.visibility = std::move(visibility); + return *this; +} + +NewResourceBuilder& NewResourceBuilder::SetOverlayable(OverlayableItem overlayable) { + res_.overlayable = std::move(overlayable); + return *this; +} +NewResourceBuilder& NewResourceBuilder::SetAllowNew(AllowNew allow_new) { + res_.allow_new = std::move(allow_new); + return *this; +} + +NewResourceBuilder& NewResourceBuilder::SetAllowMangled(bool allow_mangled) { + res_.allow_mangled = allow_mangled; + return *this; +} + +NewResource NewResourceBuilder::Build() { + return std::move(res_); +} + } // namespace aapt diff --git a/tools/aapt2/ResourceTable.h b/tools/aapt2/ResourceTable.h index 93a7a314d6bab..49392a5db8305 100644 --- a/tools/aapt2/ResourceTable.h +++ b/tools/aapt2/ResourceTable.h @@ -109,7 +109,7 @@ class ResourceEntry { const std::string name; // The entry ID for this resource (the EEEE in 0xPPTTEEEE). - Maybe id; + Maybe id; // Whether this resource is public (and must maintain the same entry ID across builds). Visibility visibility; @@ -124,10 +124,10 @@ class ResourceEntry { explicit ResourceEntry(const android::StringPiece& name) : name(name.to_string()) {} - ResourceConfigValue* FindValue(const android::ConfigDescription& config); - ResourceConfigValue* FindValue(const android::ConfigDescription& config, - const android::StringPiece& product); + android::StringPiece product = {}); + const ResourceConfigValue* FindValue(const android::ConfigDescription& config, + android::StringPiece product = {}) const; ResourceConfigValue* FindOrCreateValue(const android::ConfigDescription& config, const android::StringPiece& product); @@ -156,9 +156,6 @@ class ResourceTableType { // The logical type of resource (string, drawable, layout, etc.). const ResourceType type; - // The type ID for this resource (the TT in 0xPPTTEEEE). - Maybe id; - // Whether this type is public (and must maintain the same type ID across builds). Visibility::Level visibility_level = Visibility::Level::kUndefined; @@ -167,10 +164,9 @@ class ResourceTableType { explicit ResourceTableType(const ResourceType type) : type(type) {} - ResourceEntry* FindEntry(const android::StringPiece& name, - Maybe id = Maybe()); - ResourceEntry* FindOrCreateEntry(const android::StringPiece& name, - Maybe id = Maybe()); + ResourceEntry* CreateEntry(const android::StringPiece& name); + ResourceEntry* FindEntry(const android::StringPiece& name) const; + ResourceEntry* FindOrCreateEntry(const android::StringPiece& name); private: DISALLOW_COPY_AND_ASSIGN(ResourceTableType); @@ -180,70 +176,99 @@ class ResourceTablePackage { public: std::string name; - // The package ID (the PP in 0xPPTTEEEE). - Maybe id; - std::vector> types; + explicit ResourceTablePackage(const android::StringPiece& name) : name(name.to_string()) { + } + ResourceTablePackage() = default; - ResourceTableType* FindType(ResourceType type, Maybe id = Maybe()); - ResourceTableType* FindOrCreateType(const ResourceType type, - Maybe id = Maybe()); + ResourceTableType* FindType(ResourceType type) const; + ResourceTableType* FindOrCreateType(ResourceType type); private: DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage); }; +struct ResourceTableTypeView { + ResourceType type; + Maybe id; + Visibility::Level visibility_level = Visibility::Level::kUndefined; + + // Entries sorted in ascending entry id order. If ids have not been assigned, the entries are + // // sorted lexicographically. + std::vector entries; +}; + +struct ResourceTablePackageView { + std::string name; + Maybe id; + // Types sorted in ascending type id order. If ids have not been assigned, the types are sorted by + // their declaration order in the ResourceType enum. + std::vector types; +}; + +struct ResourceTableView { + // Packages sorted in ascending package id order. If ids have not been assigned, the packages are + // sorted lexicographically. + std::vector packages; +}; + +enum class OnIdConflict { + // If the resource entry already exists but has a different resource id, the resource value will + // not be added to the table. + ERROR, + + // If the resource entry already exists but has a different resource id, create a new resource + // with this resource name and id combination. + CREATE_ENTRY, +}; + +struct NewResource { + ResourceName name; + std::unique_ptr value; + android::ConfigDescription config; + std::string product; + std::optional> id; + std::optional visibility; + std::optional overlayable; + std::optional allow_new; + bool allow_mangled = false; +}; + +struct NewResourceBuilder { + explicit NewResourceBuilder(const ResourceNameRef& name); + explicit NewResourceBuilder(const std::string& name); + NewResourceBuilder& SetValue(std::unique_ptr value, android::ConfigDescription config = {}, + std::string product = {}); + NewResourceBuilder& SetId(ResourceId id, OnIdConflict on_conflict = OnIdConflict::ERROR); + NewResourceBuilder& SetVisibility(Visibility id); + NewResourceBuilder& SetOverlayable(OverlayableItem overlayable); + NewResourceBuilder& SetAllowNew(AllowNew allow_new); + NewResourceBuilder& SetAllowMangled(bool allow_mangled); + NewResource Build(); + + private: + NewResource res_; +}; + // The container and index for all resources defined for an app. class ResourceTable { public: - ResourceTable() = default; - explicit ResourceTable(bool validate_resources) : validate_resources_(validate_resources) {} + enum class Validation { + kEnabled, + kDisabled, + }; enum class CollisionResult { kKeepBoth, kKeepOriginal, kConflict, kTakeNew }; - using CollisionResolverFunc = std::function; + ResourceTable() = default; + explicit ResourceTable(Validation validation); - // When a collision of resources occurs, this method decides which value to keep. - static CollisionResult ResolveValueCollision(Value* existing, Value* incoming); + bool AddResource(NewResource&& res, IDiagnostics* diag); - // When a collision of resources occurs, this method keeps both values - static CollisionResult IgnoreCollision(Value* existing, Value* incoming); - - bool AddResource(const ResourceNameRef& name, const android::ConfigDescription& config, - const android::StringPiece& product, std::unique_ptr value, - IDiagnostics* diag); - - bool AddResourceWithId(const ResourceNameRef& name, const ResourceId& res_id, - const android::ConfigDescription& config, - const android::StringPiece& product, std::unique_ptr value, - IDiagnostics* diag); - - // Same as AddResource, but doesn't verify the validity of the name. This is used - // when loading resources from an existing binary resource table that may have mangled names. - bool AddResourceMangled(const ResourceNameRef& name, const android::ConfigDescription& config, - const android::StringPiece& product, std::unique_ptr value, - IDiagnostics* diag); - - bool AddResourceWithIdMangled(const ResourceNameRef& name, const ResourceId& id, - const android::ConfigDescription& config, - const android::StringPiece& product, std::unique_ptr value, - IDiagnostics* diag); - - bool GetValidateResources(); - - bool SetVisibility(const ResourceNameRef& name, const Visibility& visibility, IDiagnostics* diag); - bool SetVisibilityWithId(const ResourceNameRef& name, const Visibility& visibility, - const ResourceId& res_id, IDiagnostics* diag); - bool SetVisibilityWithIdMangled(const ResourceNameRef& name, const Visibility& visibility, - const ResourceId& res_id, IDiagnostics* diag); - - bool SetOverlayable(const ResourceNameRef& name, const OverlayableItem& overlayable, - IDiagnostics *diag); - - bool SetAllowNew(const ResourceNameRef& name, const AllowNew& allow_new, IDiagnostics* diag); - bool SetAllowNewMangled(const ResourceNameRef& name, const AllowNew& allow_new, - IDiagnostics* diag); + // Retrieves a sorted a view of the packages, types, and entries sorted in ascending resource id + // order. + ResourceTableView GetPartitionedView() const; struct SearchResult { ResourceTablePackage* package; @@ -252,23 +277,19 @@ class ResourceTable { }; Maybe FindResource(const ResourceNameRef& name) const; + Maybe FindResource(const ResourceNameRef& name, ResourceId id) const; // Returns the package struct with the given name, or nullptr if such a package does not // exist. The empty string is a valid package and typically is used to represent the // 'current' package before it is known to the ResourceTable. ResourceTablePackage* FindPackage(const android::StringPiece& name) const; - - ResourceTablePackage* FindPackageById(uint8_t id) const; - - ResourceTablePackage* CreatePackage(const android::StringPiece& name, Maybe 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 id); + ResourceTablePackage* FindOrCreatePackage(const android::StringPiece& name); std::unique_ptr Clone() const; + // When a collision of resources occurs, this method decides which value to keep. + static CollisionResult ResolveValueCollision(Value* existing, Value* incoming); + // The string pool used by this resource table. Values that reference strings must use // this pool to create their strings. // NOTE: `string_pool` must come before `packages` so that it is destroyed after. @@ -286,36 +307,9 @@ class ResourceTable { std::map included_packages_; private: - // The function type that validates a symbol name. Returns a non-empty StringPiece representing - // the offending character (which may be more than one byte in UTF-8). Returns an empty string - // if the name was valid. - using NameValidator = android::StringPiece(const android::StringPiece&); - - ResourceTablePackage* FindOrCreatePackage(const android::StringPiece& name); - - bool ValidateName(NameValidator validator, const ResourceNameRef& name, const Source& source, - IDiagnostics* diag); - - bool AddResourceImpl(const ResourceNameRef& name, const ResourceId& res_id, - const android::ConfigDescription& config, - const android::StringPiece& product, std::unique_ptr value, - NameValidator name_validator, const CollisionResolverFunc& conflict_resolver, - IDiagnostics* diag); - - bool SetVisibilityImpl(const ResourceNameRef& name, const Visibility& visibility, - const ResourceId& res_id, NameValidator name_validator, - IDiagnostics* diag); - - bool SetAllowNewImpl(const ResourceNameRef& name, const AllowNew& allow_new, - NameValidator name_validator, IDiagnostics* diag); - - bool SetOverlayableImpl(const ResourceNameRef &name, const OverlayableItem& overlayable, - NameValidator name_validator, IDiagnostics *diag); - - // Controls whether the table validates resource names and prevents duplicate resource names - bool validate_resources_ = true; - DISALLOW_COPY_AND_ASSIGN(ResourceTable); + + Validation validation_ = Validation::kEnabled; }; } // namespace aapt diff --git a/tools/aapt2/ResourceTable_test.cpp b/tools/aapt2/ResourceTable_test.cpp index 9271a7e6bae15..38391c99f55ae 100644 --- a/tools/aapt2/ResourceTable_test.cpp +++ b/tools/aapt2/ResourceTable_test.cpp @@ -37,31 +37,32 @@ namespace aapt { TEST(ResourceTableTest, FailToAddResourceWithBadName) { ResourceTable table; - EXPECT_FALSE(table.AddResource( - test::ParseNameOrDie("android:id/hey,there"), ConfigDescription{}, "", - test::ValueBuilder().SetSource("test.xml", 21u).Build(), - test::GetDiagnostics())); + EXPECT_FALSE( + table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:id/hey,there")).Build(), + test::GetDiagnostics())); - EXPECT_FALSE(table.AddResource( - test::ParseNameOrDie("android:id/hey:there"), ConfigDescription{}, "", - test::ValueBuilder().SetSource("test.xml", 21u).Build(), - test::GetDiagnostics())); + EXPECT_FALSE( + table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:id/hey:there")).Build(), + test::GetDiagnostics())); } TEST(ResourceTableTest, AddResourceWithWeirdNameWhenAddingMangledResources) { ResourceTable table; - EXPECT_TRUE(table.AddResourceMangled( - test::ParseNameOrDie("android:id/heythere "), ConfigDescription{}, "", - test::ValueBuilder().SetSource("test.xml", 21u).Build(), test::GetDiagnostics())); + EXPECT_TRUE( + table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:id/heythere ")) + .SetAllowMangled(true) + .Build(), + test::GetDiagnostics())); } TEST(ResourceTableTest, AddOneResource) { ResourceTable table; EXPECT_TRUE(table.AddResource( - test::ParseNameOrDie("android:attr/id"), ConfigDescription{}, "", - test::ValueBuilder().SetSource("test/path/file.xml", 23u).Build(), + NewResourceBuilder(test::ParseNameOrDie("android:attr/id")) + .SetValue(test::ValueBuilder().SetSource("test/path/file.xml", 23u).Build()) + .Build(), test::GetDiagnostics())); EXPECT_THAT(test::GetValue(&table, "android:attr/id"), NotNull()); @@ -70,32 +71,36 @@ TEST(ResourceTableTest, AddOneResource) { TEST(ResourceTableTest, AddMultipleResources) { ResourceTable table; - ConfigDescription config; ConfigDescription language_config; memcpy(language_config.language, "pl", sizeof(language_config.language)); EXPECT_TRUE(table.AddResource( - test::ParseNameOrDie("android:attr/layout_width"), config, "", - test::ValueBuilder().SetSource("test/path/file.xml", 10u).Build(), - test::GetDiagnostics())); - - EXPECT_TRUE(table.AddResource( - test::ParseNameOrDie("android:attr/id"), config, "", - test::ValueBuilder().SetSource("test/path/file.xml", 12u).Build(), - test::GetDiagnostics())); - - EXPECT_TRUE(table.AddResource( - test::ParseNameOrDie("android:string/ok"), config, "", - test::ValueBuilder().SetSource("test/path/file.xml", 14u).Build(), - test::GetDiagnostics())); - - EXPECT_TRUE(table.AddResource( - test::ParseNameOrDie("android:string/ok"), language_config, "", - test::ValueBuilder(android::Res_value{}) - .SetSource("test/path/file.xml", 20u) + NewResourceBuilder(test::ParseNameOrDie("android:attr/layout_width")) + .SetValue(test::ValueBuilder().SetSource("test/path/file.xml", 10u).Build()) .Build(), test::GetDiagnostics())); + EXPECT_TRUE(table.AddResource( + NewResourceBuilder(test::ParseNameOrDie("android:attr/id")) + .SetValue(test::ValueBuilder().SetSource("test/path/file.xml", 12u).Build()) + .Build(), + test::GetDiagnostics())); + + EXPECT_TRUE(table.AddResource( + NewResourceBuilder(test::ParseNameOrDie("android:string/ok")) + .SetValue(test::ValueBuilder().SetSource("test/path/file.xml", 14u).Build()) + .Build(), + test::GetDiagnostics())); + + EXPECT_TRUE( + table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:string/ok")) + .SetValue(test::ValueBuilder(android::Res_value{}) + .SetSource("test/path/file.xml", 20u) + .Build(), + language_config) + .Build(), + test::GetDiagnostics())); + EXPECT_THAT(test::GetValue(&table, "android:attr/layout_width"), NotNull()); EXPECT_THAT(test::GetValue(&table, "android:attr/id"), NotNull()); EXPECT_THAT(test::GetValue(&table, "android:string/ok"), NotNull()); @@ -104,17 +109,19 @@ TEST(ResourceTableTest, AddMultipleResources) { TEST(ResourceTableTest, OverrideWeakResourceValue) { ResourceTable table; - - ASSERT_TRUE(table.AddResource(test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "", - test::AttributeBuilder().SetWeak(true).Build(), + ASSERT_TRUE(table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:attr/foo")) + .SetValue(test::AttributeBuilder().SetWeak(true).Build()) + .Build(), test::GetDiagnostics())); Attribute* attr = test::GetValue(&table, "android:attr/foo"); ASSERT_THAT(attr, NotNull()); EXPECT_TRUE(attr->IsWeak()); - ASSERT_TRUE(table.AddResource(test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "", - util::make_unique(), test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(test::ParseNameOrDie("android:attr/foo")) + .SetValue(util::make_unique()) + .Build(), + test::GetDiagnostics())); attr = test::GetValue(&table, "android:attr/foo"); ASSERT_THAT(attr, NotNull()); @@ -130,23 +137,27 @@ TEST(ResourceTableTest, AllowCompatibleDuplicateAttributes) { Attribute attr_two(android::ResTable_map::TYPE_STRING | android::ResTable_map::TYPE_REFERENCE); attr_two.SetWeak(true); - ASSERT_TRUE(table.AddResource(name, ConfigDescription{}, "", - util::make_unique(attr_one), test::GetDiagnostics())); - ASSERT_TRUE(table.AddResource(name, ConfigDescription{}, "", - util::make_unique(attr_two), test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource( + NewResourceBuilder(name).SetValue(util::make_unique(attr_one)).Build(), + test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource( + NewResourceBuilder(name).SetValue(util::make_unique(attr_two)).Build(), + test::GetDiagnostics())); } TEST(ResourceTableTest, ProductVaryingValues) { ResourceTable table; + ASSERT_TRUE(table.AddResource( + NewResourceBuilder(test::ParseNameOrDie("android:string/foo")) + .SetValue(util::make_unique(), test::ParseConfigOrDie("land"), "tablet") + .Build(), + test::GetDiagnostics())); - EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"), - test::ParseConfigOrDie("land"), "tablet", - util::make_unique(), - test::GetDiagnostics())); - EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"), - test::ParseConfigOrDie("land"), "phone", - util::make_unique(), - test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource( + NewResourceBuilder(test::ParseNameOrDie("android:string/foo")) + .SetValue(util::make_unique(), test::ParseConfigOrDie("land"), "phone") + .Build(), + test::GetDiagnostics())); EXPECT_THAT(test::GetValueForConfigAndProduct(&table, "android:string/foo",test::ParseConfigOrDie("land"), "tablet"), NotNull()); EXPECT_THAT(test::GetValueForConfigAndProduct(&table, "android:string/foo",test::ParseConfigOrDie("land"), "phone"), NotNull()); @@ -203,22 +214,26 @@ TEST(ResourceTableTest, SetVisibility) { Visibility visibility; visibility.level = Visibility::Level::kPrivate; visibility.comment = "private"; - ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(), + test::GetDiagnostics())); ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private")); visibility.level = Visibility::Level::kUndefined; visibility.comment = "undefined"; - ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(), + test::GetDiagnostics())); ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private")); visibility.level = Visibility::Level::kPublic; visibility.comment = "public"; - ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(), + test::GetDiagnostics())); ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public")); visibility.level = Visibility::Level::kPrivate; visibility.comment = "private"; - ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetVisibility(visibility).Build(), + test::GetDiagnostics())); ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public")); } @@ -230,14 +245,16 @@ TEST(ResourceTableTest, SetAllowNew) { Maybe result; allow_new.comment = "first"; - ASSERT_TRUE(table.SetAllowNew(name, allow_new, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetAllowNew(allow_new).Build(), + test::GetDiagnostics())); result = table.FindResource(name); ASSERT_TRUE(result); ASSERT_TRUE(result.value().entry->allow_new); ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("first")); allow_new.comment = "second"; - ASSERT_TRUE(table.SetAllowNew(name, allow_new, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetAllowNew(allow_new).Build(), + test::GetDiagnostics())); result = table.FindResource(name); ASSERT_TRUE(result); ASSERT_TRUE(result.value().entry->allow_new); @@ -255,7 +272,8 @@ TEST(ResourceTableTest, SetOverlayable) { overlayable_item.source = Source("res/values/overlayable.xml", 42); const ResourceName name = test::ParseNameOrDie("android:string/foo"); - ASSERT_TRUE(table.SetOverlayable(name, overlayable_item, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item).Build(), + test::GetDiagnostics())); Maybe search_result = table.FindResource(name); ASSERT_TRUE(search_result); @@ -280,17 +298,20 @@ TEST(ResourceTableTest, SetMultipleOverlayableResources) { auto group = std::make_shared("Name", "overlay://theme"); OverlayableItem overlayable(group); overlayable.policies = PolicyFlags::PRODUCT_PARTITION; - ASSERT_TRUE(table.SetOverlayable(foo, overlayable, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(foo).SetOverlayable(overlayable).Build(), + test::GetDiagnostics())); const ResourceName bar = test::ParseNameOrDie("android:string/bar"); OverlayableItem overlayable2(group); overlayable2.policies = PolicyFlags::PRODUCT_PARTITION; - ASSERT_TRUE(table.SetOverlayable(bar, overlayable2, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(bar).SetOverlayable(overlayable2).Build(), + test::GetDiagnostics())); const ResourceName baz = test::ParseNameOrDie("android:string/baz"); OverlayableItem overlayable3(group); overlayable3.policies = PolicyFlags::VENDOR_PARTITION; - ASSERT_TRUE(table.SetOverlayable(baz, overlayable3, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(baz).SetOverlayable(overlayable3).Build(), + test::GetDiagnostics())); } TEST(ResourceTableTest, SetOverlayableDifferentResourcesDifferentName) { @@ -299,12 +320,14 @@ TEST(ResourceTableTest, SetOverlayableDifferentResourcesDifferentName) { const ResourceName foo = test::ParseNameOrDie("android:string/foo"); OverlayableItem overlayable_item(std::make_shared("Name", "overlay://theme")); overlayable_item.policies = PolicyFlags::PRODUCT_PARTITION; - ASSERT_TRUE(table.SetOverlayable(foo, overlayable_item, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(foo).SetOverlayable(overlayable_item).Build(), + test::GetDiagnostics())); const ResourceName bar = test::ParseNameOrDie("android:string/bar"); OverlayableItem overlayable_item2(std::make_shared("Name2", "overlay://theme")); overlayable_item2.policies = PolicyFlags::PRODUCT_PARTITION; - ASSERT_TRUE(table.SetOverlayable(bar, overlayable_item2, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(bar).SetOverlayable(overlayable_item2).Build(), + test::GetDiagnostics())); } TEST(ResourceTableTest, SetOverlayableSameResourcesFail) { @@ -313,10 +336,12 @@ TEST(ResourceTableTest, SetOverlayableSameResourcesFail) { auto overlayable = std::make_shared("Name", "overlay://theme"); OverlayableItem overlayable_item(overlayable); - ASSERT_TRUE(table.SetOverlayable(name, overlayable_item, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item).Build(), + test::GetDiagnostics())); OverlayableItem overlayable_item2(overlayable); - ASSERT_FALSE(table.SetOverlayable(name, overlayable_item2, test::GetDiagnostics())); + ASSERT_FALSE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item2).Build(), + test::GetDiagnostics())); } TEST(ResourceTableTest, SetOverlayableSameResourcesDifferentNameFail) { @@ -325,51 +350,38 @@ TEST(ResourceTableTest, SetOverlayableSameResourcesDifferentNameFail) { auto overlayable = std::make_shared("Name", "overlay://theme"); OverlayableItem overlayable_item(overlayable); - ASSERT_TRUE(table.SetOverlayable(name, overlayable_item, test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item).Build(), + test::GetDiagnostics())); auto overlayable2 = std::make_shared("Other", "overlay://theme"); OverlayableItem overlayable_item2(overlayable2); - ASSERT_FALSE(table.SetOverlayable(name, overlayable_item2, test::GetDiagnostics())); + ASSERT_FALSE(table.AddResource(NewResourceBuilder(name).SetOverlayable(overlayable_item2).Build(), + test::GetDiagnostics())); } -TEST(ResourceTableTest, AllowDuplictaeResourcesNames) { - ResourceTable table(/* validate_resources */ false); +TEST(ResourceTableTest, ConflictingIds) { + ResourceTable table; + const ResourceName name = test::ParseNameOrDie("android:string/foo"); + ASSERT_TRUE(table.AddResource(NewResourceBuilder(name).SetId(0x01010000).Build(), + test::GetDiagnostics())); + ASSERT_FALSE(table.AddResource(NewResourceBuilder(name).SetId(0x01010001).Build(), + test::GetDiagnostics())); +} - const ResourceName foo_name = test::ParseNameOrDie("android:bool/foo"); - ASSERT_TRUE(table.AddResourceWithId(foo_name, ResourceId(0x7f0100ff), ConfigDescription{} , "", - test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 0), - test::GetDiagnostics())); - ASSERT_TRUE(table.AddResourceWithId(foo_name, ResourceId(0x7f010100), ConfigDescription{} , "", - test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 1), - test::GetDiagnostics())); +TEST(ResourceTableTest, ConflictingIdsCreateEntry) { + ResourceTable table; + const ResourceName name = test::ParseNameOrDie("android:string/foo"); + ASSERT_TRUE(table.AddResource( + NewResourceBuilder(name).SetId(0x01010000, OnIdConflict::CREATE_ENTRY).Build(), + test::GetDiagnostics())); + ASSERT_TRUE(table.AddResource( + NewResourceBuilder(name).SetId(0x01010001, OnIdConflict::CREATE_ENTRY).Build(), + test::GetDiagnostics())); - ASSERT_TRUE(table.SetVisibilityWithId(foo_name, Visibility{Visibility::Level::kPublic}, - ResourceId(0x7f0100ff), test::GetDiagnostics())); - ASSERT_TRUE(table.SetVisibilityWithId(foo_name, Visibility{Visibility::Level::kPrivate}, - ResourceId(0x7f010100), test::GetDiagnostics())); - - auto package = table.FindPackageById(0x7f); - ASSERT_THAT(package, NotNull()); - auto type = package->FindType(ResourceType::kBool); - ASSERT_THAT(type, NotNull()); - - auto entry1 = type->FindEntry("foo", 0x00ff); - ASSERT_THAT(entry1, NotNull()); - ASSERT_THAT(entry1->id, Eq(0x00ff)); - ASSERT_THAT(entry1->values[0], NotNull()); - ASSERT_THAT(entry1->values[0]->value, NotNull()); - ASSERT_THAT(ValueCast(entry1->values[0]->value.get()), NotNull()); - ASSERT_THAT(ValueCast(entry1->values[0]->value.get())->value.data, Eq(0u)); - ASSERT_THAT(entry1->visibility.level, Visibility::Level::kPublic); - - auto entry2 = type->FindEntry("foo", 0x0100); - ASSERT_THAT(entry2, NotNull()); - ASSERT_THAT(entry2->id, Eq(0x0100)); - ASSERT_THAT(entry2->values[0], NotNull()); - ASSERT_THAT(entry1->values[0]->value, NotNull()); - ASSERT_THAT(ValueCast(entry2->values[0]->value.get()), NotNull()); - ASSERT_THAT(ValueCast(entry2->values[0]->value.get())->value.data, Eq(1u)); - ASSERT_THAT(entry2->visibility.level, Visibility::Level::kPrivate); + // Non-ambiguous query + ASSERT_TRUE(table.AddResource( + NewResourceBuilder(name).SetId(0x01010001).SetValue(std::make_unique()).Build(), + test::GetDiagnostics())); } } // namespace aapt diff --git a/tools/aapt2/cmd/Compile.cpp b/tools/aapt2/cmd/Compile.cpp index ff54fccda7672..cd5015e812038 100644 --- a/tools/aapt2/cmd/Compile.cpp +++ b/tools/aapt2/cmd/Compile.cpp @@ -190,17 +190,6 @@ static bool CompileTable(IAaptContext* context, const CompileOptions& options, } } - // Ensure we have the compilation package at least. - table.CreatePackage(context->GetCompilationPackage()); - - // Assign an ID to any package that has resources. - for (auto& pkg : table.packages) { - if (!pkg->id) { - // If no package ID was set while parsing (public identifiers), auto assign an ID. - pkg->id = context->GetPackageId(); - } - } - // Create the file/zip entry. if (!writer->StartEntry(output_path, 0)) { context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open"); diff --git a/tools/aapt2/cmd/Compile_test.cpp b/tools/aapt2/cmd/Compile_test.cpp index 8cbd998f2ae27..89757134f3a7a 100644 --- a/tools/aapt2/cmd/Compile_test.cpp +++ b/tools/aapt2/cmd/Compile_test.cpp @@ -217,6 +217,7 @@ static void AssertTranslations(CommandTestFixture *ctf, std::string file_name, }, compiled_files_dir, &diag)); std::unique_ptr apk = LoadedApk::LoadApkFromPath(out_apk, &diag); + ASSERT_NE(apk, nullptr); ResourceTable* table = apk->GetResourceTable(); ASSERT_NE(table, nullptr); diff --git a/tools/aapt2/cmd/Diff.cpp b/tools/aapt2/cmd/Diff.cpp index d56994e3ae24a..0bb044e8c6cb8 100644 --- a/tools/aapt2/cmd/Diff.cpp +++ b/tools/aapt2/cmd/Diff.cpp @@ -95,17 +95,17 @@ static bool IsIdDiff(const Visibility::Level& level_a, const Maybe& id_a, return false; } -static bool EmitResourceConfigValueDiff(IAaptContext* context, LoadedApk* apk_a, - ResourceTablePackage* pkg_a, ResourceTableType* type_a, - ResourceEntry* entry_a, ResourceConfigValue* config_value_a, - LoadedApk* apk_b, ResourceTablePackage* pkg_b, - ResourceTableType* type_b, ResourceEntry* entry_b, - ResourceConfigValue* config_value_b) { +static bool EmitResourceConfigValueDiff( + IAaptContext* context, LoadedApk* apk_a, const ResourceTablePackageView& pkg_a, + const ResourceTableTypeView& type_a, const ResourceEntry* entry_a, + const ResourceConfigValue* config_value_a, LoadedApk* apk_b, + const ResourceTablePackageView& pkg_b, const ResourceTableTypeView& type_b, + const ResourceEntry* entry_b, const ResourceConfigValue* config_value_b) { Value* value_a = config_value_a->value.get(); Value* value_b = config_value_b->value.get(); if (!value_a->Equals(value_b)) { std::stringstream str_stream; - str_stream << "value " << pkg_a->name << ":" << type_a->type << "/" << entry_a->name + str_stream << "value " << pkg_a.name << ":" << type_a.type << "/" << entry_a->name << " config=" << config_value_a->config << " does not match:\n"; value_a->Print(&str_stream); str_stream << "\n vs \n"; @@ -117,16 +117,17 @@ static bool EmitResourceConfigValueDiff(IAaptContext* context, LoadedApk* apk_a, } static bool EmitResourceEntryDiff(IAaptContext* context, LoadedApk* apk_a, - ResourceTablePackage* pkg_a, ResourceTableType* type_a, - ResourceEntry* entry_a, LoadedApk* apk_b, - ResourceTablePackage* pkg_b, ResourceTableType* type_b, - ResourceEntry* entry_b) { + const ResourceTablePackageView& pkg_a, + const ResourceTableTypeView& type_a, const ResourceEntry* entry_a, + LoadedApk* apk_b, const ResourceTablePackageView& pkg_b, + const ResourceTableTypeView& type_b, + const ResourceEntry* entry_b) { bool diff = false; - for (std::unique_ptr& config_value_a : entry_a->values) { - ResourceConfigValue* config_value_b = entry_b->FindValue(config_value_a->config); + for (const std::unique_ptr& config_value_a : entry_a->values) { + auto config_value_b = entry_b->FindValue(config_value_a->config); if (!config_value_b) { std::stringstream str_stream; - str_stream << "missing " << pkg_a->name << ":" << type_a->type << "/" << entry_a->name + str_stream << "missing " << pkg_a.name << ":" << type_a.type << "/" << entry_a->name << " config=" << config_value_a->config; EmitDiffLine(apk_b->GetSource(), str_stream.str()); diff = true; @@ -138,35 +139,47 @@ static bool EmitResourceEntryDiff(IAaptContext* context, LoadedApk* apk_a, } // Check for any newly added config values. - for (std::unique_ptr& config_value_b : entry_b->values) { - ResourceConfigValue* config_value_a = entry_a->FindValue(config_value_b->config); + for (const std::unique_ptr& config_value_b : entry_b->values) { + auto config_value_a = entry_a->FindValue(config_value_b->config); if (!config_value_a) { std::stringstream str_stream; - str_stream << "new config " << pkg_b->name << ":" << type_b->type << "/" << entry_b->name + str_stream << "new config " << pkg_b.name << ":" << type_b.type << "/" << entry_b->name << " config=" << config_value_b->config; EmitDiffLine(apk_b->GetSource(), str_stream.str()); diff = true; } } - return false; + return diff; } static bool EmitResourceTypeDiff(IAaptContext* context, LoadedApk* apk_a, - ResourceTablePackage* pkg_a, ResourceTableType* type_a, - LoadedApk* apk_b, ResourceTablePackage* pkg_b, - ResourceTableType* type_b) { + const ResourceTablePackageView& pkg_a, + const ResourceTableTypeView& type_a, LoadedApk* apk_b, + const ResourceTablePackageView& pkg_b, + const ResourceTableTypeView& type_b) { bool diff = false; - for (std::unique_ptr& entry_a : type_a->entries) { - ResourceEntry* entry_b = type_b->FindEntry(entry_a->name); - if (!entry_b) { + auto entry_a_iter = type_a.entries.begin(); + auto entry_b_iter = type_b.entries.begin(); + while (entry_a_iter != type_a.entries.end() || entry_b_iter != type_b.entries.end()) { + if (entry_b_iter == type_b.entries.end()) { + // Type A contains a type that type B does not have. std::stringstream str_stream; - str_stream << "missing " << pkg_a->name << ":" << type_a->type << "/" << entry_a->name; + str_stream << "missing " << pkg_a.name << ":" << type_a.type << "/" << (*entry_a_iter)->name; + EmitDiffLine(apk_a->GetSource(), str_stream.str()); + diff = true; + } else if (entry_a_iter == type_a.entries.end()) { + // Type B contains a type that type A does not have. + std::stringstream str_stream; + str_stream << "new entry " << pkg_b.name << ":" << type_b.type << "/" + << (*entry_b_iter)->name; EmitDiffLine(apk_b->GetSource(), str_stream.str()); diff = true; } else { + const auto& entry_a = *entry_a_iter; + const auto& entry_b = *entry_a_iter; if (IsSymbolVisibilityDifferent(entry_a->visibility, entry_b->visibility)) { std::stringstream str_stream; - str_stream << pkg_a->name << ":" << type_a->type << "/" << entry_a->name + str_stream << pkg_a.name << ":" << type_a.type << "/" << entry_a->name << " has different visibility ("; if (entry_b->visibility.level == Visibility::Level::kPublic) { str_stream << "PUBLIC"; @@ -185,7 +198,7 @@ static bool EmitResourceTypeDiff(IAaptContext* context, LoadedApk* apk_a, } else if (IsIdDiff(entry_a->visibility.level, entry_a->id, entry_b->visibility.level, entry_b->id)) { std::stringstream str_stream; - str_stream << pkg_a->name << ":" << type_a->type << "/" << entry_a->name + str_stream << pkg_a.name << ":" << type_a.type << "/" << entry_a->name << " has different public ID ("; if (entry_b->id) { str_stream << "0x" << std::hex << entry_b->id.value(); @@ -202,46 +215,43 @@ static bool EmitResourceTypeDiff(IAaptContext* context, LoadedApk* apk_a, EmitDiffLine(apk_b->GetSource(), str_stream.str()); diff = true; } - diff |= EmitResourceEntryDiff(context, apk_a, pkg_a, type_a, entry_a.get(), apk_b, pkg_b, - type_b, entry_b); - } - } - - // Check for any newly added entries. - for (std::unique_ptr& entry_b : type_b->entries) { - ResourceEntry* entry_a = type_a->FindEntry(entry_b->name); - if (!entry_a) { - std::stringstream str_stream; - str_stream << "new entry " << pkg_b->name << ":" << type_b->type << "/" << entry_b->name; - EmitDiffLine(apk_b->GetSource(), str_stream.str()); - diff = true; + diff |= EmitResourceEntryDiff(context, apk_a, pkg_a, type_a, entry_a, apk_b, pkg_b, type_b, + entry_b); } } return diff; } static bool EmitResourcePackageDiff(IAaptContext* context, LoadedApk* apk_a, - ResourceTablePackage* pkg_a, LoadedApk* apk_b, - ResourceTablePackage* pkg_b) { + const ResourceTablePackageView& pkg_a, LoadedApk* apk_b, + const ResourceTablePackageView& pkg_b) { bool diff = false; - for (std::unique_ptr& type_a : pkg_a->types) { - ResourceTableType* type_b = pkg_b->FindType(type_a->type); - if (!type_b) { + auto type_a_iter = pkg_a.types.begin(); + auto type_b_iter = pkg_b.types.begin(); + while (type_a_iter != pkg_a.types.end() || type_b_iter != pkg_b.types.end()) { + if (type_b_iter == pkg_b.types.end()) { + // Type A contains a type that type B does not have. std::stringstream str_stream; - str_stream << "missing " << pkg_a->name << ":" << type_a->type; + str_stream << "missing " << pkg_a.name << ":" << type_a_iter->type; EmitDiffLine(apk_a->GetSource(), str_stream.str()); diff = true; + } else if (type_a_iter == pkg_a.types.end()) { + // Type B contains a type that type A does not have. + std::stringstream str_stream; + str_stream << "new type " << pkg_b.name << ":" << type_b_iter->type; + EmitDiffLine(apk_b->GetSource(), str_stream.str()); + diff = true; } else { - if (type_a->visibility_level != type_b->visibility_level) { + if (type_a_iter->visibility_level != type_b_iter->visibility_level) { std::stringstream str_stream; - str_stream << pkg_a->name << ":" << type_a->type << " has different visibility ("; - if (type_b->visibility_level == Visibility::Level::kPublic) { + str_stream << pkg_a.name << ":" << type_a_iter->type << " has different visibility ("; + if (type_b_iter->visibility_level == Visibility::Level::kPublic) { str_stream << "PUBLIC"; } else { str_stream << "PRIVATE"; } str_stream << " vs "; - if (type_a->visibility_level == Visibility::Level::kPublic) { + if (type_a_iter->visibility_level == Visibility::Level::kPublic) { str_stream << "PUBLIC"; } else { str_stream << "PRIVATE"; @@ -249,18 +259,18 @@ static bool EmitResourcePackageDiff(IAaptContext* context, LoadedApk* apk_a, str_stream << ")"; EmitDiffLine(apk_b->GetSource(), str_stream.str()); diff = true; - } else if (IsIdDiff(type_a->visibility_level, type_a->id, type_b->visibility_level, - type_b->id)) { + } else if (IsIdDiff(type_a_iter->visibility_level, type_a_iter->id, + type_b_iter->visibility_level, type_b_iter->id)) { std::stringstream str_stream; - str_stream << pkg_a->name << ":" << type_a->type << " has different public ID ("; - if (type_b->id) { - str_stream << "0x" << std::hex << type_b->id.value(); + str_stream << pkg_a.name << ":" << type_a_iter->type << " has different public ID ("; + if (type_b_iter->id) { + str_stream << "0x" << std::hex << type_b_iter->id.value(); } else { str_stream << "none"; } str_stream << " vs "; - if (type_a->id) { - str_stream << "0x " << std::hex << type_a->id.value(); + if (type_a_iter->id) { + str_stream << "0x " << std::hex << type_a_iter->id.value(); } else { str_stream << "none"; } @@ -268,47 +278,44 @@ static bool EmitResourcePackageDiff(IAaptContext* context, LoadedApk* apk_a, EmitDiffLine(apk_b->GetSource(), str_stream.str()); diff = true; } - diff |= EmitResourceTypeDiff(context, apk_a, pkg_a, type_a.get(), apk_b, pkg_b, type_b); - } - } - - // Check for any newly added types. - for (std::unique_ptr& type_b : pkg_b->types) { - ResourceTableType* type_a = pkg_a->FindType(type_b->type); - if (!type_a) { - std::stringstream str_stream; - str_stream << "new type " << pkg_b->name << ":" << type_b->type; - EmitDiffLine(apk_b->GetSource(), str_stream.str()); - diff = true; + diff |= EmitResourceTypeDiff(context, apk_a, pkg_a, *type_a_iter, apk_b, pkg_b, *type_b_iter); } } return diff; } static bool EmitResourceTableDiff(IAaptContext* context, LoadedApk* apk_a, LoadedApk* apk_b) { - ResourceTable* table_a = apk_a->GetResourceTable(); - ResourceTable* table_b = apk_b->GetResourceTable(); + const auto table_a = apk_a->GetResourceTable()->GetPartitionedView(); + const auto table_b = apk_b->GetResourceTable()->GetPartitionedView(); bool diff = false; - for (std::unique_ptr& pkg_a : table_a->packages) { - ResourceTablePackage* pkg_b = table_b->FindPackage(pkg_a->name); - if (!pkg_b) { + auto package_a_iter = table_a.packages.begin(); + auto package_b_iter = table_b.packages.begin(); + while (package_a_iter != table_a.packages.end() || package_b_iter != table_b.packages.end()) { + if (package_b_iter == table_b.packages.end()) { + // Table A contains a package that table B does not have. std::stringstream str_stream; - str_stream << "missing package " << pkg_a->name; + str_stream << "missing package " << package_a_iter->name; + EmitDiffLine(apk_a->GetSource(), str_stream.str()); + diff = true; + } else if (package_a_iter == table_a.packages.end()) { + // Table B contains a package that table A does not have. + std::stringstream str_stream; + str_stream << "new package " << package_b_iter->name; EmitDiffLine(apk_b->GetSource(), str_stream.str()); diff = true; } else { - if (pkg_a->id != pkg_b->id) { + if (package_a_iter->id != package_b_iter->id) { std::stringstream str_stream; - str_stream << "package '" << pkg_a->name << "' has different id ("; - if (pkg_b->id) { - str_stream << "0x" << std::hex << pkg_b->id.value(); + str_stream << "package '" << package_a_iter->name << "' has different id ("; + if (package_b_iter->id) { + str_stream << "0x" << std::hex << package_b_iter->id.value(); } else { str_stream << "none"; } str_stream << " vs "; - if (pkg_a->id) { - str_stream << "0x" << std::hex << pkg_a->id.value(); + if (package_a_iter->id) { + str_stream << "0x" << std::hex << package_b_iter->id.value(); } else { str_stream << "none"; } @@ -316,20 +323,10 @@ static bool EmitResourceTableDiff(IAaptContext* context, LoadedApk* apk_a, Loade EmitDiffLine(apk_b->GetSource(), str_stream.str()); diff = true; } - diff |= EmitResourcePackageDiff(context, apk_a, pkg_a.get(), apk_b, pkg_b); + diff |= EmitResourcePackageDiff(context, apk_a, *package_a_iter, apk_b, *package_b_iter); } } - // Check for any newly added packages. - for (std::unique_ptr& pkg_b : table_b->packages) { - ResourceTablePackage* pkg_a = table_a->FindPackage(pkg_b->name); - if (!pkg_a) { - std::stringstream str_stream; - str_stream << "new package " << pkg_b->name; - EmitDiffLine(apk_b->GetSource(), str_stream.str()); - diff = true; - } - } return diff; } diff --git a/tools/aapt2/cmd/Link.cpp b/tools/aapt2/cmd/Link.cpp index 13e090d9d8430..ee6a764966f0f 100644 --- a/tools/aapt2/cmd/Link.cpp +++ b/tools/aapt2/cmd/Link.cpp @@ -25,6 +25,7 @@ #include #include "android-base/errors.h" +#include "android-base/expected.h" #include "android-base/file.h" #include "android-base/stringprintf.h" #include "androidfw/Locale.h" @@ -74,10 +75,25 @@ using ::aapt::io::FileInputStream; using ::android::ConfigDescription; using ::android::StringPiece; +using ::android::base::expected; using ::android::base::StringPrintf; +using ::android::base::unexpected; namespace aapt { +namespace { + +expected GetStaticLibraryPackage(ResourceTable* table) { + // Resource tables built by aapt2 always contain one package. This is a post condition of + // VerifyNoExternalPackages. + if (table->packages.size() != 1u) { + return unexpected("static library contains more than one package"); + } + return table->packages.back().get(); +} + +} // namespace + constexpr uint8_t kAndroidPackageId = 0x01; class LinkContext : public IAaptContext { @@ -633,13 +649,18 @@ bool ResourceFileFlattener::Flatten(ResourceTable* table, IArchiveWriter* archiv const ResourceFile& file = doc->file; dst_path = ResourceUtils::BuildResourceFileName(file, context_->GetNameMangler()); - std::unique_ptr file_ref = + auto file_ref = util::make_unique(table->string_pool.MakeRef(dst_path)); file_ref->SetSource(doc->file.source); + // Update the output format of this XML file. file_ref->type = XmlFileTypeForOutputFormat(options_.output_format); - if (!table->AddResourceMangled(file.name, file.config, {}, std::move(file_ref), - context_->GetDiagnostics())) { + bool result = table->AddResource(NewResourceBuilder(file.name) + .SetValue(std::move(file_ref), file.config) + .SetAllowMangled(true) + .Build(), + context_->GetDiagnostics()); + if (!result) { return false; } } @@ -842,18 +863,15 @@ class Linker { ResourceTable* table = static_apk->GetResourceTable(); // If we are using --no-static-lib-packages, we need to rename the package of this table to - // our compilation package. - if (options_.no_static_lib_packages) { - // Since package names can differ, and multiple packages can exist in a ResourceTable, - // we place the requirement that all static libraries are built with the package - // ID 0x7f. So if one is not found, this is an error. - if (ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId)) { - pkg->name = context_->GetCompilationPackage(); - } else { - context_->GetDiagnostics()->Error(DiagMessage(path) - << "no package with ID 0x7f found in static library"); + // our compilation package so the symbol package name does not get mangled into the entry + // name. + if (options_.no_static_lib_packages && !table->packages.empty()) { + auto lib_package_result = GetStaticLibraryPackage(table); + if (!lib_package_result.has_value()) { + context_->GetDiagnostics()->Error(DiagMessage(path) << lib_package_result.error()); return false; } + lib_package_result.value()->name = context_->GetCompilationPackage(); } context_->GetExternalSymbols()->AppendSource( @@ -982,8 +1000,7 @@ class Linker { // stripped, or there is an error and false is returned. bool VerifyNoExternalPackages() { auto is_ext_package_func = [&](const std::unique_ptr& pkg) -> bool { - return context_->GetCompilationPackage() != pkg->name || !pkg->id || - pkg->id.value() != context_->GetPackageId(); + return context_->GetCompilationPackage() != pkg->name; }; bool error = false; @@ -1027,19 +1044,11 @@ class Linker { bool VerifyNoIdsSet() { for (const auto& package : final_table_.packages) { for (const auto& type : package->types) { - if (type->id) { - context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID " - << StringPrintf("%02x", type->id.value()) - << " assigned"); - return false; - } - for (const auto& entry : type->entries) { if (entry->id) { ResourceNameRef res_name(package->name, type->type, entry->name); - context_->GetDiagnostics()->Error( - DiagMessage() << "entry " << res_name << " has ID " - << StringPrintf("%02x", entry->id.value()) << " assigned"); + context_->GetDiagnostics()->Error(DiagMessage() << "resource " << res_name << " has ID " + << entry->id.value() << " assigned"); return false; } } @@ -1313,12 +1322,17 @@ class Linker { } ResourceTable* table = apk->GetResourceTable(); - ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId); - if (!pkg) { - context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package"); + if (table->packages.empty()) { + return true; + } + + auto lib_package_result = GetStaticLibraryPackage(table); + if (!lib_package_result.has_value()) { + context_->GetDiagnostics()->Error(DiagMessage(input) << lib_package_result.error()); return false; } + ResourceTablePackage* pkg = lib_package_result.value(); bool result; if (options_.no_static_lib_packages) { // Merge all resources as if they were in the compilation package. This is the old behavior @@ -1365,11 +1379,11 @@ class Linker { res_name = mangled_name.value(); } - std::unique_ptr id = util::make_unique(); + auto id = util::make_unique(); id->SetSource(source.WithLine(exported_symbol.line)); - bool result = - final_table_.AddResourceMangled(res_name, ConfigDescription::DefaultConfig(), - std::string(), std::move(id), context_->GetDiagnostics()); + bool result = final_table_.AddResource( + NewResourceBuilder(res_name).SetValue(std::move(id)).SetAllowMangled(true).Build(), + context_->GetDiagnostics()); if (!result) { return false; } @@ -1750,7 +1764,7 @@ class Linker { // anything else being generated, which includes the Java classes. // If required, the package name is modifed before flattening, and then modified back // to its original name. - ResourceTablePackage* package_to_rewrite = nullptr; + std::optional package_to_rewrite; // Pre-O, the platform treats negative resource IDs [those with a package ID of 0x80 // or higher] as invalid. In order to work around this limitation, we allow the use // of traditionally reserved resource IDs [those between 0x02 and 0x7E]. Allow the @@ -1764,10 +1778,11 @@ class Linker { // The base APK is included, and this is a feature split. If the base package is // the same as this package, then we are building an old style Android Instant Apps feature // split and must apply this workaround to avoid requiring namespaces support. - package_to_rewrite = table->FindPackage(context_->GetCompilationPackage()); - if (package_to_rewrite != nullptr) { + auto table_view = table->GetPartitionedView(); + if (!table_view.packages.empty() && + table_view.packages.back().name == context_->GetCompilationPackage()) { + package_to_rewrite = std::move(table_view.packages.back()); CHECK_EQ(1u, table->packages.size()) << "can't change name of package when > 1 package"; - std::string new_package_name = StringPrintf("%s.%s", package_to_rewrite->name.c_str(), app_info_.split_name.value_or_default("feature").c_str()); @@ -1783,7 +1798,7 @@ class Linker { bool success = FlattenTable(table, options_.output_format, writer); - if (package_to_rewrite != nullptr) { + if (package_to_rewrite.has_value()) { // Change the name back. package_to_rewrite->name = context_->GetCompilationPackage(); if (package_to_rewrite->id) { @@ -1925,8 +1940,7 @@ class Linker { for (auto& entry : type->entries) { ResourceName name(package->name, type->type, entry->name); // The IDs are guaranteed to exist. - options_.stable_id_map[std::move(name)] = - ResourceId(package->id.value(), type->id.value(), entry->id.value()); + options_.stable_id_map[std::move(name)] = entry->id.value(); } } } diff --git a/tools/aapt2/cmd/Link_test.cpp b/tools/aapt2/cmd/Link_test.cpp index 73072a963d097..27cbe88fde38d 100644 --- a/tools/aapt2/cmd/Link_test.cpp +++ b/tools/aapt2/cmd/Link_test.cpp @@ -47,6 +47,8 @@ TEST_F(LinkTest, RemoveRawXmlStrings) { // Load the binary xml tree android::ResXMLTree tree; std::unique_ptr apk = LoadedApk::LoadApkFromPath(out_apk, &diag); + ASSERT_THAT(apk, Ne(nullptr)); + std::unique_ptr data = OpenFileAsData(apk.get(), "res/xml/test.xml"); ASSERT_THAT(data, Ne(nullptr)); AssertLoadXml(apk.get(), data.get(), &tree); @@ -73,6 +75,8 @@ TEST_F(LinkTest, KeepRawXmlStrings) { // Load the binary xml tree android::ResXMLTree tree; std::unique_ptr apk = LoadedApk::LoadApkFromPath(out_apk, &diag); + ASSERT_THAT(apk, Ne(nullptr)); + std::unique_ptr data = OpenFileAsData(apk.get(), "res/xml/test.xml"); ASSERT_THAT(data, Ne(nullptr)); AssertLoadXml(apk.get(), data.get(), &tree); @@ -208,6 +212,8 @@ TEST_F(LinkTest, OverlayStyles) { ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag)); std::unique_ptr apk = LoadedApk::LoadApkFromPath(out_apk, &diag); + ASSERT_THAT(apk, Ne(nullptr)); + const Style* actual_style = test::GetValue