Allow using reserved package IDs
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].
Bug: 78041707
Test: ./out/host/linux-x86/nativetest64/aapt2_tests/aapt2_tests
Test: ./out/host/linux-x86/nativetest/libandroidfw_tests/libandroidfw_tests
Test: Manual. Create a feature split with a package ID of 0x7E and see it runs and can reference base resources.
Change-Id: I3d9782cc05d3a55e1a2467bf39566788847e1160
diff --git a/tools/aapt2/cmd/Link.cpp b/tools/aapt2/cmd/Link.cpp
index a3b7664..ea59722 100644
--- a/tools/aapt2/cmd/Link.cpp
+++ b/tools/aapt2/cmd/Link.cpp
@@ -129,6 +129,12 @@
// Stable ID options.
std::unordered_map<ResourceName, ResourceId> stable_id_map;
Maybe<std::string> resource_id_map_path;
+
+ // When 'true', allow reserved package IDs to be used for applications. 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].
+ bool allow_reserved_package_id = false;
};
class LinkContext : public IAaptContext {
@@ -895,9 +901,7 @@
// Capture the shared libraries so that the final resource table can be properly flattened
// with support for shared libraries.
for (auto& entry : asset_source->GetAssignedPackageIds()) {
- if (entry.first > kFrameworkPackageId && entry.first < kAppPackageId) {
- final_table_.included_packages_[entry.first] = entry.second;
- } else if (entry.first == kAppPackageId) {
+ if (entry.first == kAppPackageId) {
// Capture the included base feature package.
included_feature_base_ = entry.second;
} else if (entry.first == kFrameworkPackageId) {
@@ -911,6 +915,8 @@
// android:versionCode from the framework AndroidManifest.xml.
ExtractCompileSdkVersions(asset_source->GetAssetManager());
}
+ } else if (asset_source->IsPackageDynamic(entry.first)) {
+ final_table_.included_packages_[entry.first] = entry.second;
}
}
@@ -1595,7 +1601,15 @@
// If required, the package name is modifed before flattening, and then modified back
// to its original name.
ResourceTablePackage* package_to_rewrite = nullptr;
- if (context_->GetPackageId() > kAppPackageId &&
+ // 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
+ // definition of what a valid "split" package ID is to account for this.
+ const bool isSplitPackage = (options_.allow_reserved_package_id &&
+ context_->GetPackageId() != kAppPackageId &&
+ context_->GetPackageId() != kFrameworkPackageId)
+ || (!options_.allow_reserved_package_id && context_->GetPackageId() > kAppPackageId);
+ if (isSplitPackage &&
included_feature_base_ == make_value(context_->GetCompilationPackage())) {
// 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
@@ -2139,6 +2153,10 @@
"Generates a text file containing the resource symbols of the R class in\n"
"the specified folder.",
&options.generate_text_symbols_path)
+ .OptionalSwitch("--allow-reserved-package-id",
+ "Allows the use of a reserved package ID. This should on be used for\n"
+ "packages with a pre-O min-sdk\n",
+ &options.allow_reserved_package_id)
.OptionalSwitch("--auto-add-overlay",
"Allows the addition of new resources in overlays without\n"
"<add-resource> tags.",
@@ -2238,7 +2256,9 @@
}
const uint32_t package_id_int = maybe_package_id_int.value();
- if (package_id_int < kAppPackageId || package_id_int > std::numeric_limits<uint8_t>::max()) {
+ if (package_id_int > std::numeric_limits<uint8_t>::max()
+ || package_id_int == kFrameworkPackageId
+ || (!options.allow_reserved_package_id && package_id_int < kAppPackageId)) {
context.GetDiagnostics()->Error(
DiagMessage() << StringPrintf(
"invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int));