Merge "aapt2 sparse encoding bugfix"

This commit is contained in:
Brandon Liu
2022-08-30 20:04:23 +00:00
committed by Android (Google) Code Review
2 changed files with 49 additions and 5 deletions

View File

@@ -369,9 +369,13 @@ class PackageFlattener {
bool sparse_encode = use_sparse_entries_;
// Only sparse encode if the entries will be read on platforms O+.
sparse_encode =
sparse_encode && (context_->GetMinSdkVersion() >= SDK_O || config.sdkVersion >= SDK_O);
if (context_->GetMinSdkVersion() == 0 && config.sdkVersion == 0) {
// Sparse encode if sdk version is not set in context and config.
} else {
// Otherwise, only sparse encode if the entries will be read on platforms S_V2+.
sparse_encode = sparse_encode &&
(context_->GetMinSdkVersion() >= SDK_S_V2 || config.sdkVersion >= SDK_S_V2);
}
// Only sparse encode if the offsets are representable in 2 bytes.
sparse_encode =

View File

@@ -330,7 +330,7 @@ TEST_F(TableFlattenerTest, FlattenSparseEntryWithMinSdkO) {
std::unique_ptr<IAaptContext> context = test::ContextBuilder()
.SetCompilationPackage("android")
.SetPackageId(0x01)
.SetMinSdkVersion(SDK_O)
.SetMinSdkVersion(SDK_S_V2)
.Build();
const ConfigDescription sparse_config = test::ParseConfigOrDie("en-rGB");
@@ -376,7 +376,7 @@ TEST_F(TableFlattenerTest, FlattenSparseEntryWithConfigSdkVersionO) {
.SetMinSdkVersion(SDK_LOLLIPOP)
.Build();
const ConfigDescription sparse_config = test::ParseConfigOrDie("en-rGB-v26");
const ConfigDescription sparse_config = test::ParseConfigOrDie("en-rGB-v32");
auto table_in = BuildTableWithSparseEntries(context.get(), sparse_config, 0.25f);
TableFlattenerOptions options;
@@ -391,6 +391,46 @@ TEST_F(TableFlattenerTest, FlattenSparseEntryWithConfigSdkVersionO) {
EXPECT_GT(no_sparse_contents.size(), sparse_contents.size());
}
TEST_F(TableFlattenerTest, FlattenSparseEntryWithSdkVersionNotSet) {
std::unique_ptr<IAaptContext> context =
test::ContextBuilder().SetCompilationPackage("android").SetPackageId(0x01).Build();
const ConfigDescription sparse_config = test::ParseConfigOrDie("en-rGB");
auto table_in = BuildTableWithSparseEntries(context.get(), sparse_config, 0.25f);
TableFlattenerOptions options;
options.use_sparse_entries = true;
std::string no_sparse_contents;
ASSERT_TRUE(Flatten(context.get(), {}, table_in.get(), &no_sparse_contents));
std::string sparse_contents;
ASSERT_TRUE(Flatten(context.get(), options, table_in.get(), &sparse_contents));
EXPECT_GT(no_sparse_contents.size(), sparse_contents.size());
// Attempt to parse the sparse contents.
ResourceTable sparse_table;
BinaryResourceParser parser(context->GetDiagnostics(), &sparse_table, Source("test.arsc"),
sparse_contents.data(), sparse_contents.size());
ASSERT_TRUE(parser.Parse());
auto value = test::GetValueForConfig<BinaryPrimitive>(&sparse_table, "android:string/foo_0",
sparse_config);
ASSERT_THAT(value, NotNull());
EXPECT_EQ(0u, value->value.data);
ASSERT_THAT(test::GetValueForConfig<BinaryPrimitive>(&sparse_table, "android:string/foo_1",
sparse_config),
IsNull());
value = test::GetValueForConfig<BinaryPrimitive>(&sparse_table, "android:string/foo_4",
sparse_config);
ASSERT_THAT(value, NotNull());
EXPECT_EQ(4u, value->value.data);
}
TEST_F(TableFlattenerTest, DoNotUseSparseEntryForDenseConfig) {
std::unique_ptr<IAaptContext> context = test::ContextBuilder()
.SetCompilationPackage("android")