AAPT2: Propagate SPEC_OVERLAYABLE flag to final APK
Resources can be marked as overlayable, which means they can
be overlaid by runtime resource overlays.
This change propagates this state to the final resource table that
is installed on device.
Future work:
- Have the idmap tool respect the overlayable state and ignore
entries that overlay anything else.
Bug: 64980941
Test: make aapt2_tests
Change-Id: Id45b1e141a281be2ee32a4ac3096fcf1114d523b
diff --git a/tools/aapt2/link/PrivateAttributeMover.cpp b/tools/aapt2/link/PrivateAttributeMover.cpp
index eee4b60..675b02a 100644
--- a/tools/aapt2/link/PrivateAttributeMover.cpp
+++ b/tools/aapt2/link/PrivateAttributeMover.cpp
@@ -62,7 +62,7 @@
continue;
}
- if (type->symbol_status.state != SymbolState::kPublic) {
+ if (type->visibility_level != Visibility::Level::kPublic) {
// No public attributes, so we can safely leave these private attributes
// where they are.
continue;
@@ -72,7 +72,7 @@
move_if(type->entries, std::back_inserter(private_attr_entries),
[](const std::unique_ptr<ResourceEntry>& entry) -> bool {
- return entry->symbol_status.state != SymbolState::kPublic;
+ return entry->visibility.level != Visibility::Level::kPublic;
});
if (private_attr_entries.empty()) {
diff --git a/tools/aapt2/link/PrivateAttributeMover_test.cpp b/tools/aapt2/link/PrivateAttributeMover_test.cpp
index 7fcf6e7..168234b 100644
--- a/tools/aapt2/link/PrivateAttributeMover_test.cpp
+++ b/tools/aapt2/link/PrivateAttributeMover_test.cpp
@@ -30,9 +30,9 @@
.AddSimple("android:attr/publicB")
.AddSimple("android:attr/privateB")
.SetSymbolState("android:attr/publicA", ResourceId(0x01010000),
- SymbolState::kPublic)
+ Visibility::Level::kPublic)
.SetSymbolState("android:attr/publicB", ResourceId(0x01010000),
- SymbolState::kPublic)
+ Visibility::Level::kPublic)
.Build();
PrivateAttributeMover mover;
@@ -81,7 +81,7 @@
std::unique_ptr<ResourceTable> table =
test::ResourceTableBuilder()
.AddSimple("android:attr/pub")
- .SetSymbolState("android:attr/pub", ResourceId(0x01010000), SymbolState::kPublic)
+ .SetSymbolState("android:attr/pub", ResourceId(0x01010000), Visibility::Level::kPublic)
.Build();
ResourceTablePackage* package = table->FindPackage("android");
diff --git a/tools/aapt2/link/ReferenceLinker.cpp b/tools/aapt2/link/ReferenceLinker.cpp
index ad7d8b6..b8f8804 100644
--- a/tools/aapt2/link/ReferenceLinker.cpp
+++ b/tools/aapt2/link/ReferenceLinker.cpp
@@ -363,8 +363,8 @@
NameMangler::Unmangle(&name.entry, &name.package);
// Symbol state information may be lost if there is no value for the resource.
- if (entry->symbol_status.state != SymbolState::kUndefined && entry->values.empty()) {
- context->GetDiagnostics()->Error(DiagMessage(entry->symbol_status.source)
+ if (entry->visibility.level != Visibility::Level::kUndefined && entry->values.empty()) {
+ context->GetDiagnostics()->Error(DiagMessage(entry->visibility.source)
<< "no definition for declared symbol '" << name << "'");
error = true;
}
diff --git a/tools/aapt2/link/TableMerger.cpp b/tools/aapt2/link/TableMerger.cpp
index 58d0607..e819f51 100644
--- a/tools/aapt2/link/TableMerger.cpp
+++ b/tools/aapt2/link/TableMerger.cpp
@@ -83,44 +83,58 @@
static bool MergeType(IAaptContext* context, const Source& src, ResourceTableType* dst_type,
ResourceTableType* src_type) {
- if (dst_type->symbol_status.state < src_type->symbol_status.state) {
+ if (src_type->visibility_level > dst_type->visibility_level) {
// The incoming type's visibility is stronger, so we should override the visibility.
- if (src_type->symbol_status.state == SymbolState::kPublic) {
+ if (src_type->visibility_level == Visibility::Level::kPublic) {
// Only copy the ID if the source is public, or else the ID is meaningless.
dst_type->id = src_type->id;
}
- dst_type->symbol_status = std::move(src_type->symbol_status);
- } else if (dst_type->symbol_status.state == SymbolState::kPublic &&
- src_type->symbol_status.state == SymbolState::kPublic &&
- dst_type->id && src_type->id &&
- dst_type->id.value() != src_type->id.value()) {
+ dst_type->visibility_level = src_type->visibility_level;
+ } else if (dst_type->visibility_level == Visibility::Level::kPublic &&
+ src_type->visibility_level == Visibility::Level::kPublic && dst_type->id &&
+ src_type->id && dst_type->id.value() != src_type->id.value()) {
// Both types are public and have different IDs.
- context->GetDiagnostics()->Error(DiagMessage(src)
- << "cannot merge type '" << src_type->type
- << "': conflicting public IDs");
+ context->GetDiagnostics()->Error(DiagMessage(src) << "cannot merge type '" << src_type->type
+ << "': conflicting public IDs");
return false;
}
return true;
}
-static bool MergeEntry(IAaptContext* context, const Source& src, ResourceEntry* dst_entry,
- ResourceEntry* src_entry) {
- if (dst_entry->symbol_status.state < src_entry->symbol_status.state) {
- // The incoming type's visibility is stronger, so we should override the visibility.
- if (src_entry->symbol_status.state == SymbolState::kPublic) {
- // Only copy the ID if the source is public, or else the ID is meaningless.
+static bool MergeEntry(IAaptContext* context, const Source& src, bool overlay,
+ ResourceEntry* dst_entry, ResourceEntry* src_entry) {
+ // Copy over the strongest visibility.
+ if (src_entry->visibility.level > dst_entry->visibility.level) {
+ // Only copy the ID if the source is public, or else the ID is meaningless.
+ if (src_entry->visibility.level == Visibility::Level::kPublic) {
dst_entry->id = src_entry->id;
}
- dst_entry->symbol_status = std::move(src_entry->symbol_status);
- } else if (src_entry->symbol_status.state == SymbolState::kPublic &&
- dst_entry->symbol_status.state == SymbolState::kPublic &&
- dst_entry->id && src_entry->id &&
- dst_entry->id.value() != src_entry->id.value()) {
+ dst_entry->visibility = std::move(src_entry->visibility);
+ } else if (src_entry->visibility.level == Visibility::Level::kPublic &&
+ dst_entry->visibility.level == Visibility::Level::kPublic && dst_entry->id &&
+ src_entry->id && src_entry->id != dst_entry->id) {
// Both entries are public and have different IDs.
context->GetDiagnostics()->Error(DiagMessage(src) << "cannot merge entry '" << src_entry->name
<< "': conflicting public IDs");
return false;
}
+
+ // Copy over the rest of the properties, if needed.
+ if (src_entry->allow_new) {
+ dst_entry->allow_new = std::move(src_entry->allow_new);
+ }
+
+ if (src_entry->overlayable) {
+ if (dst_entry->overlayable && !overlay) {
+ context->GetDiagnostics()->Error(DiagMessage(src_entry->overlayable.value().source)
+ << "duplicate overlayable declaration for resource '"
+ << src_entry->name << "'");
+ context->GetDiagnostics()->Error(DiagMessage(dst_entry->overlayable.value().source)
+ << "previous declaration here");
+ return false;
+ }
+ dst_entry->overlayable = std::move(src_entry->overlayable);
+ }
return true;
}
@@ -202,7 +216,7 @@
}
ResourceEntry* dst_entry;
- if (allow_new_resources || src_entry->symbol_status.allow_new) {
+ if (allow_new_resources || src_entry->allow_new) {
dst_entry = dst_type->FindOrCreateEntry(entry_name);
} else {
dst_entry = dst_type->FindEntry(entry_name);
@@ -220,7 +234,7 @@
continue;
}
- if (!MergeEntry(context_, src, dst_entry, src_entry.get())) {
+ if (!MergeEntry(context_, src, overlay, dst_entry, src_entry.get())) {
error = true;
continue;
}
diff --git a/tools/aapt2/link/TableMerger_test.cpp b/tools/aapt2/link/TableMerger_test.cpp
index 6aab8de..34461c6 100644
--- a/tools/aapt2/link/TableMerger_test.cpp
+++ b/tools/aapt2/link/TableMerger_test.cpp
@@ -182,14 +182,12 @@
std::unique_ptr<ResourceTable> base =
test::ResourceTableBuilder()
.SetPackageId("", 0x7f)
- .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001),
- SymbolState::kPublic)
+ .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
.Build();
std::unique_ptr<ResourceTable> overlay =
test::ResourceTableBuilder()
.SetPackageId("", 0x7f)
- .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001),
- SymbolState::kPublic)
+ .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
.Build();
ResourceTable final_table;
@@ -205,14 +203,12 @@
std::unique_ptr<ResourceTable> base =
test::ResourceTableBuilder()
.SetPackageId("", 0x7f)
- .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001),
- SymbolState::kPublic)
+ .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
.Build();
std::unique_ptr<ResourceTable> overlay =
test::ResourceTableBuilder()
.SetPackageId("", 0x7f)
- .SetSymbolState("bool/foo", ResourceId(0x7f, 0x02, 0x0001),
- SymbolState::kPublic)
+ .SetSymbolState("bool/foo", ResourceId(0x7f, 0x02, 0x0001), Visibility::Level::kPublic)
.Build();
ResourceTable final_table;
@@ -228,14 +224,12 @@
std::unique_ptr<ResourceTable> base =
test::ResourceTableBuilder()
.SetPackageId("", 0x7f)
- .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001),
- SymbolState::kPublic)
+ .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
.Build();
std::unique_ptr<ResourceTable> overlay =
test::ResourceTableBuilder()
.SetPackageId("", 0x7f)
- .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0002),
- SymbolState::kPublic)
+ .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0002), Visibility::Level::kPublic)
.Build();
ResourceTable final_table;
@@ -253,7 +247,7 @@
std::unique_ptr<ResourceTable> table_b =
test::ResourceTableBuilder()
.SetPackageId("", 0x7f)
- .SetSymbolState("bool/foo", {}, SymbolState::kUndefined, true /*allow new overlay*/)
+ .SetSymbolState("bool/foo", {}, Visibility::Level::kUndefined, true /*allow new overlay*/)
.AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
.Build();