diff --git a/libs/androidfw/ResourceTypes.cpp b/libs/androidfw/ResourceTypes.cpp index 763a178ed43b0..0f5b65c446863 100644 --- a/libs/androidfw/ResourceTypes.cpp +++ b/libs/androidfw/ResourceTypes.cpp @@ -6074,6 +6074,10 @@ bool ResTable::getResourceFlags(uint32_t resID, uint32_t* outFlags) const { return true; } +static bool keyCompare(const ResTable_sparseTypeEntry& entry , uint16_t entryIdx) { + return dtohs(entry.idx) < entryIdx; +} + status_t ResTable::getEntry( const PackageGroup* packageGroup, int typeIndex, int entryIndex, const ResTable_config* config, @@ -6115,6 +6119,9 @@ status_t ResTable::getEntry( currentTypeIsOverlay = true; } + // Check that the entry idx is within range of the declared entry count (ResTable_typeSpec). + // Particular types (ResTable_type) may be encoded with sparse entries, and so their + // entryCount do not need to match. if (static_cast(realEntryIndex) >= typeSpec->entryCount) { ALOGW("For resource 0x%08x, entry index(%d) is beyond type entryCount(%d)", Res_MAKEID(packageGroup->id - 1, typeIndex, entryIndex), @@ -6169,11 +6176,37 @@ status_t ResTable::getEntry( continue; } - // Check if there is the desired entry in this type. const uint32_t* const eindex = reinterpret_cast( reinterpret_cast(thisType) + dtohs(thisType->header.headerSize)); - uint32_t thisOffset = dtohl(eindex[realEntryIndex]); + uint32_t thisOffset; + + // Check if there is the desired entry in this type. + if (thisType->flags & ResTable_type::FLAG_SPARSE) { + // This is encoded as a sparse map, so perform a binary search. + const ResTable_sparseTypeEntry* sparseIndices = + reinterpret_cast(eindex); + const ResTable_sparseTypeEntry* result = std::lower_bound( + sparseIndices, sparseIndices + dtohl(thisType->entryCount), realEntryIndex, + keyCompare); + if (result == sparseIndices + dtohl(thisType->entryCount) + || dtohs(result->idx) != realEntryIndex) { + // No entry found. + continue; + } + + // Extract the offset from the entry. Each offset must be a multiple of 4 + // so we store it as the real offset divided by 4. + thisOffset = dtohs(result->offset) * 4u; + } else { + if (static_cast(realEntryIndex) >= dtohl(thisType->entryCount)) { + // Entry does not exist. + continue; + } + + thisOffset = dtohl(eindex[realEntryIndex]); + } + if (thisOffset == ResTable_type::NO_ENTRY) { // There is no entry for this index and configuration. continue; @@ -6480,12 +6513,6 @@ status_t ResTable::parsePackage(const ResTable_package* const pkg, } Type* t = typeList.editItemAt(typeList.size() - 1); - if (newEntryCount != t->entryCount) { - ALOGE("ResTable_type entry count inconsistent: given %d, previously %d", - (int)newEntryCount, (int)t->entryCount); - return (mError=BAD_TYPE); - } - if (t->package != package) { ALOGE("No TypeSpec for type %d", type->id); return (mError=BAD_TYPE); @@ -7096,8 +7123,17 @@ void ResTable::print(bool inclValues) const thisConfig.copyFromDtoH(type->config); String8 configStr = thisConfig.toString(); - printf(" config %s:\n", configStr.size() > 0 + printf(" config %s", configStr.size() > 0 ? configStr.string() : "(default)"); + if (type->flags != 0u) { + printf(" flags=0x%02x", type->flags); + if (type->flags & ResTable_type::FLAG_SPARSE) { + printf(" [sparse]"); + } + } + + printf(":\n"); + size_t entryCount = dtohl(type->entryCount); uint32_t entriesStart = dtohl(type->entriesStart); if ((entriesStart&0x3) != 0) { @@ -7109,18 +7145,30 @@ void ResTable::print(bool inclValues) const printf(" NON-INTEGER ResTable_type header.size: 0x%x\n", typeSize); continue; } - for (size_t entryIndex=0; entryIndexheader.headerSize)); - uint32_t thisOffset = dtohl(eindex[entryIndex]); - if (thisOffset == ResTable_type::NO_ENTRY) { - continue; + const uint32_t* const eindex = (const uint32_t*) + (((const uint8_t*)type) + dtohs(type->header.headerSize)); + for (size_t entryIndex=0; entryIndexflags & ResTable_type::FLAG_SPARSE) { + const ResTable_sparseTypeEntry* entry = + reinterpret_cast( + eindex + entryIndex); + entryId = dtohs(entry->idx); + // Offsets are encoded as divided by 4. + thisOffset = static_cast(dtohs(entry->offset)) * 4u; + } else { + entryId = entryIndex; + thisOffset = dtohl(eindex[entryIndex]); + if (thisOffset == ResTable_type::NO_ENTRY) { + continue; + } } uint32_t resID = (0xff000000 & ((packageId)<<24)) | (0x00ff0000 & ((typeIndex+1)<<16)) - | (0x0000ffff & (entryIndex)); + | (0x0000ffff & (entryId)); if (packageId == 0) { pg->dynamicRefTable.lookupResourceId(&resID); } diff --git a/libs/androidfw/TypeWrappers.cpp b/libs/androidfw/TypeWrappers.cpp index 06b40405f4040..647aa197a94db 100644 --- a/libs/androidfw/TypeWrappers.cpp +++ b/libs/androidfw/TypeWrappers.cpp @@ -16,23 +16,45 @@ #include +#include + namespace android { +TypeVariant::TypeVariant(const ResTable_type* data) : data(data), mLength(dtohl(data->entryCount)) { + if (data->flags & ResTable_type::FLAG_SPARSE) { + const uint32_t entryCount = dtohl(data->entryCount); + const uintptr_t containerEnd = reinterpret_cast(data) + dtohl(data->header.size); + const uint32_t* const entryIndices = reinterpret_cast( + reinterpret_cast(data) + dtohs(data->header.headerSize)); + if (reinterpret_cast(entryIndices) + (sizeof(uint32_t) * entryCount) + > containerEnd) { + ALOGE("Type's entry indices extend beyond its boundaries"); + mLength = 0; + } else { + mLength = ResTable_sparseTypeEntry{entryIndices[entryCount - 1]}.idx + 1; + } + } +} + TypeVariant::iterator& TypeVariant::iterator::operator++() { mIndex++; - if (mIndex > dtohl(mTypeVariant->data->entryCount)) { - mIndex = dtohl(mTypeVariant->data->entryCount); + if (mIndex > mTypeVariant->mLength) { + mIndex = mTypeVariant->mLength; } return *this; } +static bool keyCompare(uint32_t entry, uint16_t index) { + return dtohs(ResTable_sparseTypeEntry{entry}.idx) < index; +} + const ResTable_entry* TypeVariant::iterator::operator*() const { const ResTable_type* type = mTypeVariant->data; - const uint32_t entryCount = dtohl(type->entryCount); - if (mIndex >= entryCount) { + if (mIndex >= mTypeVariant->mLength) { return NULL; } + const uint32_t entryCount = dtohl(mTypeVariant->data->entryCount); const uintptr_t containerEnd = reinterpret_cast(type) + dtohl(type->header.size); const uint32_t* const entryIndices = reinterpret_cast( @@ -42,7 +64,19 @@ const ResTable_entry* TypeVariant::iterator::operator*() const { return NULL; } - const uint32_t entryOffset = dtohl(entryIndices[mIndex]); + uint32_t entryOffset; + if (type->flags & ResTable_type::FLAG_SPARSE) { + auto iter = std::lower_bound(entryIndices, entryIndices + entryCount, mIndex, keyCompare); + if (iter == entryIndices + entryCount + || dtohs(ResTable_sparseTypeEntry{*iter}.idx) != mIndex) { + return NULL; + } + + entryOffset = static_cast(dtohs(ResTable_sparseTypeEntry{*iter}.offset)) * 4u; + } else { + entryOffset = dtohl(entryIndices[mIndex]); + } + if (entryOffset == ResTable_type::NO_ENTRY) { return NULL; } diff --git a/libs/androidfw/include/androidfw/ResourceTypes.h b/libs/androidfw/include/androidfw/ResourceTypes.h index 04a5d958c6140..5c5c9e6b4f551 100644 --- a/libs/androidfw/include/androidfw/ResourceTypes.h +++ b/libs/androidfw/include/androidfw/ResourceTypes.h @@ -1339,12 +1339,21 @@ struct ResTable_typeSpec /** * A collection of resource entries for a particular resource data - * type. Followed by an array of uint32_t defining the resource + * type. + * + * If the flag FLAG_SPARSE is not set in `flags`, then this struct is + * followed by an array of uint32_t defining the resource * values, corresponding to the array of type strings in the * ResTable_package::typeStrings string block. Each of these hold an * index from entriesStart; a value of NO_ENTRY means that entry is * not defined. * + * If the flag FLAG_SPARSE is set in `flags`, then this struct is followed + * by an array of ResTable_sparseTypeEntry defining only the entries that + * have values for this type. Each entry is sorted by their entry ID such + * that a binary search can be performed over the entries. The ID and offset + * are encoded in a uint32_t. See ResTabe_sparseTypeEntry. + * * There may be multiple of these chunks for a particular resource type, * supply different configuration variations for the resource values of * that type. @@ -1365,10 +1374,17 @@ struct ResTable_type // resource identifier). 0 is invalid. uint8_t id; + enum { + // If set, the entry is sparse, and encodes both the entry ID and offset into each entry, + // and a binary search is used to find the key. Only available on platforms >= O. + // Mark any types that use this with a v26 qualifier to prevent runtime issues on older + // platforms. + FLAG_SPARSE = 0x01, + }; + uint8_t flags; + // Must be 0. - uint8_t res0; - // Must be 0. - uint16_t res1; + uint16_t reserved; // Number of uint32_t entry indices that follow. uint32_t entryCount; @@ -1380,6 +1396,24 @@ struct ResTable_type ResTable_config config; }; +/** + * An entry in a ResTable_type with the flag `FLAG_SPARSE` set. + */ +union ResTable_sparseTypeEntry { + // Holds the raw uint32_t encoded value. Do not read this. + uint32_t entry; + struct { + // The index of the entry. + uint16_t idx; + + // The offset from ResTable_type::entriesStart, divided by 4. + uint16_t offset; + }; +}; + +static_assert(sizeof(ResTable_sparseTypeEntry) == sizeof(uint32_t), + "ResTable_sparseTypeEntry must be 4 bytes in size"); + /** * This is the beginning of information about an entry in the resource * table. It holds the reference to the name of this entry, and is diff --git a/libs/androidfw/include/androidfw/TypeWrappers.h b/libs/androidfw/include/androidfw/TypeWrappers.h index f1daf3365c28b..5cfe54e5759dd 100644 --- a/libs/androidfw/include/androidfw/TypeWrappers.h +++ b/libs/androidfw/include/androidfw/TypeWrappers.h @@ -23,8 +23,7 @@ namespace android { struct TypeVariant { - TypeVariant(const ResTable_type* data) - : data(data) {} + TypeVariant(const ResTable_type* data); class iterator { public: @@ -72,10 +71,13 @@ struct TypeVariant { } iterator endEntries() const { - return iterator(this, dtohl(data->entryCount)); + return iterator(this, mLength); } const ResTable_type* data; + +private: + size_t mLength; }; } // namespace android diff --git a/libs/androidfw/tests/Android.mk b/libs/androidfw/tests/Android.mk index 19527c51e816e..921fd147aa800 100644 --- a/libs/androidfw/tests/Android.mk +++ b/libs/androidfw/tests/Android.mk @@ -45,6 +45,8 @@ testFiles := \ benchmarkFiles := \ AssetManager2_bench.cpp \ BenchMain.cpp \ + BenchmarkHelpers.cpp \ + SparseEntry_bench.cpp \ TestHelpers.cpp \ Theme_bench.cpp diff --git a/libs/androidfw/tests/AssetManager2_bench.cpp b/libs/androidfw/tests/AssetManager2_bench.cpp index 273290a26050a..67de741b1b66f 100644 --- a/libs/androidfw/tests/AssetManager2_bench.cpp +++ b/libs/androidfw/tests/AssetManager2_bench.cpp @@ -22,6 +22,7 @@ #include "androidfw/AssetManager2.h" #include "androidfw/ResourceTypes.h" +#include "BenchmarkHelpers.h" #include "TestHelpers.h" #include "data/basic/R.h" #include "data/libclient/R.h" @@ -112,34 +113,6 @@ static void GetResourceBenchmark(const std::vector& paths, } } -static void GetResourceBenchmarkOld(const std::vector& paths, - const ResTable_config* config, uint32_t resid, - benchmark::State& state) { - AssetManager assetmanager; - for (const std::string& path : paths) { - if (!assetmanager.addAssetPath(String8(path.c_str()), nullptr /* cookie */, - false /* appAsLib */, false /* isSystemAssets */)) { - state.SkipWithError(base::StringPrintf("Failed to load assets %s", path.c_str()).c_str()); - return; - } - } - - if (config != nullptr) { - assetmanager.setConfiguration(*config); - } - - const ResTable& table = assetmanager.getResources(true); - - Res_value value; - ResTable_config selected_config; - uint32_t flags; - - while (state.KeepRunning()) { - table.getResource(resid, &value, false /*may_be_bag*/, 0u /*density*/, &flags, - &selected_config); - } -} - static void BM_AssetManagerGetResource(benchmark::State& state) { GetResourceBenchmark({GetTestDataPath() + "/basic/basic.apk"}, nullptr /*config*/, basic::R::integer::number1, state); diff --git a/libs/androidfw/tests/BenchmarkHelpers.cpp b/libs/androidfw/tests/BenchmarkHelpers.cpp new file mode 100644 index 0000000000000..3619b7ee83abc --- /dev/null +++ b/libs/androidfw/tests/BenchmarkHelpers.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "BenchmarkHelpers.h" + +#include "android-base/stringprintf.h" +#include "androidfw/AssetManager.h" + +namespace android { + +void GetResourceBenchmarkOld(const std::vector& paths, const ResTable_config* config, + uint32_t resid, benchmark::State& state) { + AssetManager assetmanager; + for (const std::string& path : paths) { + if (!assetmanager.addAssetPath(String8(path.c_str()), nullptr /* cookie */, + false /* appAsLib */, false /* isSystemAssets */)) { + state.SkipWithError(base::StringPrintf("Failed to load assets %s", path.c_str()).c_str()); + return; + } + } + + if (config != nullptr) { + assetmanager.setConfiguration(*config); + } + + const ResTable& table = assetmanager.getResources(true); + + Res_value value; + ResTable_config selected_config; + uint32_t flags; + + while (state.KeepRunning()) { + table.getResource(resid, &value, false /*may_be_bag*/, 0u /*density*/, &flags, + &selected_config); + } +} + +} // namespace android diff --git a/libs/androidfw/tests/BenchmarkHelpers.h b/libs/androidfw/tests/BenchmarkHelpers.h new file mode 100644 index 0000000000000..fc366642ca36b --- /dev/null +++ b/libs/androidfw/tests/BenchmarkHelpers.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TESTS_BENCHMARKHELPERS_H_ +#define TESTS_BENCHMARKHELPERS_H_ + +#include +#include + +#include "benchmark/benchmark.h" + +#include "androidfw/ResourceTypes.h" + +namespace android { + +void GetResourceBenchmarkOld(const std::vector& paths, const ResTable_config* config, + uint32_t resid, benchmark::State& state); + +} // namespace android + +#endif /* TESTS_BENCHMARKHELPERS_H_ */ diff --git a/libs/androidfw/tests/ResTable_test.cpp b/libs/androidfw/tests/ResTable_test.cpp index ad1cd2b289d67..2df41305237e1 100644 --- a/libs/androidfw/tests/ResTable_test.cpp +++ b/libs/androidfw/tests/ResTable_test.cpp @@ -41,6 +41,34 @@ TEST(ResTableTest, ShouldLoadSuccessfully) { ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); } +TEST(ResTableTest, ShouldLoadSparseEntriesSuccessfully) { + std::string contents; + ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/sparse/sparse.apk", "resources.arsc", + &contents)); + + ResTable table; + ASSERT_EQ(NO_ERROR, table.add(contents.data(), contents.size())); + + ResTable_config config; + memset(&config, 0, sizeof(config)); + config.sdkVersion = 26; + table.setParameters(&config); + + String16 name(u"com.android.sparse:integer/foo_9"); + uint32_t flags; + uint32_t resid = + table.identifierForName(name.string(), name.size(), nullptr, 0, nullptr, 0, &flags); + ASSERT_NE(0u, resid); + + Res_value val; + ResTable_config selected_config; + ASSERT_GE( + table.getResource(resid, &val, false /*mayBeBag*/, 0u /*density*/, &flags, &selected_config), + 0); + EXPECT_EQ(Res_value::TYPE_INT_DEC, val.dataType); + EXPECT_EQ(900u, val.data); +} + TEST(ResTableTest, SimpleTypeIsRetrievedCorrectly) { std::string contents; ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", diff --git a/libs/androidfw/tests/SparseEntry_bench.cpp b/libs/androidfw/tests/SparseEntry_bench.cpp new file mode 100644 index 0000000000000..1ebf7ce623bd4 --- /dev/null +++ b/libs/androidfw/tests/SparseEntry_bench.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "androidfw/AssetManager.h" +#include "androidfw/ResourceTypes.h" + +#include "BenchmarkHelpers.h" +#include "TestHelpers.h" +#include "data/sparse/R.h" + +namespace sparse = com::android::sparse; + +namespace android { + +static void BM_SparseEntryGetResourceSparseSmall(benchmark::State& state) { + ResTable_config config; + memset(&config, 0, sizeof(config)); + config.sdkVersion = 26; + GetResourceBenchmarkOld({GetTestDataPath() + "/sparse/sparse.apk"}, &config, + sparse::R::integer::foo_9, state); +} +BENCHMARK(BM_SparseEntryGetResourceSparseSmall); + +static void BM_SparseEntryGetResourceNotSparseSmall(benchmark::State& state) { + ResTable_config config; + memset(&config, 0, sizeof(config)); + config.sdkVersion = 26; + GetResourceBenchmarkOld({GetTestDataPath() + "/sparse/not_sparse.apk"}, &config, + sparse::R::integer::foo_9, state); +} +BENCHMARK(BM_SparseEntryGetResourceNotSparseSmall); + +static void BM_SparseEntryGetResourceSparseLarge(benchmark::State& state) { + ResTable_config config; + memset(&config, 0, sizeof(config)); + config.sdkVersion = 26; + GetResourceBenchmarkOld({GetTestDataPath() + "/sparse/sparse.apk"}, &config, + sparse::R::string::foo_999, state); +} +BENCHMARK(BM_SparseEntryGetResourceSparseLarge); + +static void BM_SparseEntryGetResourceNotSparseLarge(benchmark::State& state) { + ResTable_config config; + memset(&config, 0, sizeof(config)); + config.sdkVersion = 26; + GetResourceBenchmarkOld({GetTestDataPath() + "/sparse/not_sparse.apk"}, &config, + sparse::R::string::foo_999, state); +} +BENCHMARK(BM_SparseEntryGetResourceNotSparseLarge); + +} // namespace android diff --git a/libs/androidfw/tests/TestHelpers.h b/libs/androidfw/tests/TestHelpers.h index a11ea8416c7d2..ec78b2ae5efc8 100644 --- a/libs/androidfw/tests/TestHelpers.h +++ b/libs/androidfw/tests/TestHelpers.h @@ -19,6 +19,7 @@ #include #include +#include #include "androidfw/ResourceTypes.h" #include "gtest/gtest.h" diff --git a/libs/androidfw/tests/data/sparse/.gitignore b/libs/androidfw/tests/data/sparse/.gitignore new file mode 100644 index 0000000000000..52e32d4fcbf21 --- /dev/null +++ b/libs/androidfw/tests/data/sparse/.gitignore @@ -0,0 +1 @@ +*.flata diff --git a/libs/androidfw/tests/data/sparse/AndroidManifest.xml b/libs/androidfw/tests/data/sparse/AndroidManifest.xml new file mode 100644 index 0000000000000..27911b62447a9 --- /dev/null +++ b/libs/androidfw/tests/data/sparse/AndroidManifest.xml @@ -0,0 +1,20 @@ + + + + + + diff --git a/libs/androidfw/tests/data/sparse/R.h b/libs/androidfw/tests/data/sparse/R.h new file mode 100644 index 0000000000000..243e74fac65a8 --- /dev/null +++ b/libs/androidfw/tests/data/sparse/R.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TESTS_DATA_SPARSE_R_H_ +#define TESTS_DATA_SPARSE_R_H_ + +#include + +namespace com { +namespace android { +namespace sparse { + +struct R { + struct integer { + enum : uint32_t { + foo_0 = 0x7f010000, + foo_1 = 0x7f010000, + foo_2 = 0x7f010000, + foo_3 = 0x7f010000, + foo_4 = 0x7f010000, + foo_5 = 0x7f010000, + foo_6 = 0x7f010000, + foo_7 = 0x7f010000, + foo_8 = 0x7f010000, + foo_9 = 0x7f010000, + }; + }; + + struct string { + enum : uint32_t { + foo_999 = 0x7f0203e7, + }; + }; +}; + +} // namespace sparse +} // namespace android +} // namespace com + +#endif /* TESTS_DATA_SPARSE_R_H_ */ diff --git a/libs/androidfw/tests/data/sparse/build b/libs/androidfw/tests/data/sparse/build new file mode 100755 index 0000000000000..305593f018754 --- /dev/null +++ b/libs/androidfw/tests/data/sparse/build @@ -0,0 +1,24 @@ +#!/bin/bash +# +# Copyright (C) 2017 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +set -e + +PATH_TO_FRAMEWORK_RES=${ANDROID_BUILD_TOP}/prebuilts/sdk/current/android.jar + +aapt2 compile --dir res -o compiled.flata +aapt2 link --manifest AndroidManifest.xml -I $PATH_TO_FRAMEWORK_RES -o sparse.apk --enable-sparse-encoding compiled.flata +aapt2 link --manifest AndroidManifest.xml -I $PATH_TO_FRAMEWORK_RES -o not_sparse.apk compiled.flata diff --git a/libs/androidfw/tests/data/sparse/gen_strings.sh b/libs/androidfw/tests/data/sparse/gen_strings.sh new file mode 100755 index 0000000000000..e7e1d603ea4ed --- /dev/null +++ b/libs/androidfw/tests/data/sparse/gen_strings.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +OUTPUT_default=res/values/strings.xml +OUTPUT_v26=res/values-v26/strings.xml + +echo "" > $OUTPUT_default +echo "" > $OUTPUT_v26 +for i in {0..999} +do + echo " $i" >> $OUTPUT_default + if [ "$(($i % 3))" -eq "0" ] + then + echo " $(($i * 10))" >> $OUTPUT_v26 + fi +done +echo "" >> $OUTPUT_default +echo "" >> $OUTPUT_v26 + diff --git a/libs/androidfw/tests/data/sparse/not_sparse.apk b/libs/androidfw/tests/data/sparse/not_sparse.apk new file mode 100644 index 0000000000000..599a370dbfb13 Binary files /dev/null and b/libs/androidfw/tests/data/sparse/not_sparse.apk differ diff --git a/libs/androidfw/tests/data/sparse/res/values-v26/strings.xml b/libs/androidfw/tests/data/sparse/res/values-v26/strings.xml new file mode 100644 index 0000000000000..b6f82997d18b8 --- /dev/null +++ b/libs/androidfw/tests/data/sparse/res/values-v26/strings.xml @@ -0,0 +1,336 @@ + + 0 + 30 + 60 + 90 + 120 + 150 + 180 + 210 + 240 + 270 + 300 + 330 + 360 + 390 + 420 + 450 + 480 + 510 + 540 + 570 + 600 + 630 + 660 + 690 + 720 + 750 + 780 + 810 + 840 + 870 + 900 + 930 + 960 + 990 + 1020 + 1050 + 1080 + 1110 + 1140 + 1170 + 1200 + 1230 + 1260 + 1290 + 1320 + 1350 + 1380 + 1410 + 1440 + 1470 + 1500 + 1530 + 1560 + 1590 + 1620 + 1650 + 1680 + 1710 + 1740 + 1770 + 1800 + 1830 + 1860 + 1890 + 1920 + 1950 + 1980 + 2010 + 2040 + 2070 + 2100 + 2130 + 2160 + 2190 + 2220 + 2250 + 2280 + 2310 + 2340 + 2370 + 2400 + 2430 + 2460 + 2490 + 2520 + 2550 + 2580 + 2610 + 2640 + 2670 + 2700 + 2730 + 2760 + 2790 + 2820 + 2850 + 2880 + 2910 + 2940 + 2970 + 3000 + 3030 + 3060 + 3090 + 3120 + 3150 + 3180 + 3210 + 3240 + 3270 + 3300 + 3330 + 3360 + 3390 + 3420 + 3450 + 3480 + 3510 + 3540 + 3570 + 3600 + 3630 + 3660 + 3690 + 3720 + 3750 + 3780 + 3810 + 3840 + 3870 + 3900 + 3930 + 3960 + 3990 + 4020 + 4050 + 4080 + 4110 + 4140 + 4170 + 4200 + 4230 + 4260 + 4290 + 4320 + 4350 + 4380 + 4410 + 4440 + 4470 + 4500 + 4530 + 4560 + 4590 + 4620 + 4650 + 4680 + 4710 + 4740 + 4770 + 4800 + 4830 + 4860 + 4890 + 4920 + 4950 + 4980 + 5010 + 5040 + 5070 + 5100 + 5130 + 5160 + 5190 + 5220 + 5250 + 5280 + 5310 + 5340 + 5370 + 5400 + 5430 + 5460 + 5490 + 5520 + 5550 + 5580 + 5610 + 5640 + 5670 + 5700 + 5730 + 5760 + 5790 + 5820 + 5850 + 5880 + 5910 + 5940 + 5970 + 6000 + 6030 + 6060 + 6090 + 6120 + 6150 + 6180 + 6210 + 6240 + 6270 + 6300 + 6330 + 6360 + 6390 + 6420 + 6450 + 6480 + 6510 + 6540 + 6570 + 6600 + 6630 + 6660 + 6690 + 6720 + 6750 + 6780 + 6810 + 6840 + 6870 + 6900 + 6930 + 6960 + 6990 + 7020 + 7050 + 7080 + 7110 + 7140 + 7170 + 7200 + 7230 + 7260 + 7290 + 7320 + 7350 + 7380 + 7410 + 7440 + 7470 + 7500 + 7530 + 7560 + 7590 + 7620 + 7650 + 7680 + 7710 + 7740 + 7770 + 7800 + 7830 + 7860 + 7890 + 7920 + 7950 + 7980 + 8010 + 8040 + 8070 + 8100 + 8130 + 8160 + 8190 + 8220 + 8250 + 8280 + 8310 + 8340 + 8370 + 8400 + 8430 + 8460 + 8490 + 8520 + 8550 + 8580 + 8610 + 8640 + 8670 + 8700 + 8730 + 8760 + 8790 + 8820 + 8850 + 8880 + 8910 + 8940 + 8970 + 9000 + 9030 + 9060 + 9090 + 9120 + 9150 + 9180 + 9210 + 9240 + 9270 + 9300 + 9330 + 9360 + 9390 + 9420 + 9450 + 9480 + 9510 + 9540 + 9570 + 9600 + 9630 + 9660 + 9690 + 9720 + 9750 + 9780 + 9810 + 9840 + 9870 + 9900 + 9930 + 9960 + 9990 + diff --git a/libs/androidfw/tests/data/sparse/res/values-v26/values.xml b/libs/androidfw/tests/data/sparse/res/values-v26/values.xml new file mode 100644 index 0000000000000..b396ad24aa8c5 --- /dev/null +++ b/libs/androidfw/tests/data/sparse/res/values-v26/values.xml @@ -0,0 +1,22 @@ + + + + + 0 + 400 + 500 + 900 + diff --git a/libs/androidfw/tests/data/sparse/res/values/strings.xml b/libs/androidfw/tests/data/sparse/res/values/strings.xml new file mode 100644 index 0000000000000..6ff839a167a20 --- /dev/null +++ b/libs/androidfw/tests/data/sparse/res/values/strings.xml @@ -0,0 +1,1002 @@ + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 + 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 + 60 + 61 + 62 + 63 + 64 + 65 + 66 + 67 + 68 + 69 + 70 + 71 + 72 + 73 + 74 + 75 + 76 + 77 + 78 + 79 + 80 + 81 + 82 + 83 + 84 + 85 + 86 + 87 + 88 + 89 + 90 + 91 + 92 + 93 + 94 + 95 + 96 + 97 + 98 + 99 + 100 + 101 + 102 + 103 + 104 + 105 + 106 + 107 + 108 + 109 + 110 + 111 + 112 + 113 + 114 + 115 + 116 + 117 + 118 + 119 + 120 + 121 + 122 + 123 + 124 + 125 + 126 + 127 + 128 + 129 + 130 + 131 + 132 + 133 + 134 + 135 + 136 + 137 + 138 + 139 + 140 + 141 + 142 + 143 + 144 + 145 + 146 + 147 + 148 + 149 + 150 + 151 + 152 + 153 + 154 + 155 + 156 + 157 + 158 + 159 + 160 + 161 + 162 + 163 + 164 + 165 + 166 + 167 + 168 + 169 + 170 + 171 + 172 + 173 + 174 + 175 + 176 + 177 + 178 + 179 + 180 + 181 + 182 + 183 + 184 + 185 + 186 + 187 + 188 + 189 + 190 + 191 + 192 + 193 + 194 + 195 + 196 + 197 + 198 + 199 + 200 + 201 + 202 + 203 + 204 + 205 + 206 + 207 + 208 + 209 + 210 + 211 + 212 + 213 + 214 + 215 + 216 + 217 + 218 + 219 + 220 + 221 + 222 + 223 + 224 + 225 + 226 + 227 + 228 + 229 + 230 + 231 + 232 + 233 + 234 + 235 + 236 + 237 + 238 + 239 + 240 + 241 + 242 + 243 + 244 + 245 + 246 + 247 + 248 + 249 + 250 + 251 + 252 + 253 + 254 + 255 + 256 + 257 + 258 + 259 + 260 + 261 + 262 + 263 + 264 + 265 + 266 + 267 + 268 + 269 + 270 + 271 + 272 + 273 + 274 + 275 + 276 + 277 + 278 + 279 + 280 + 281 + 282 + 283 + 284 + 285 + 286 + 287 + 288 + 289 + 290 + 291 + 292 + 293 + 294 + 295 + 296 + 297 + 298 + 299 + 300 + 301 + 302 + 303 + 304 + 305 + 306 + 307 + 308 + 309 + 310 + 311 + 312 + 313 + 314 + 315 + 316 + 317 + 318 + 319 + 320 + 321 + 322 + 323 + 324 + 325 + 326 + 327 + 328 + 329 + 330 + 331 + 332 + 333 + 334 + 335 + 336 + 337 + 338 + 339 + 340 + 341 + 342 + 343 + 344 + 345 + 346 + 347 + 348 + 349 + 350 + 351 + 352 + 353 + 354 + 355 + 356 + 357 + 358 + 359 + 360 + 361 + 362 + 363 + 364 + 365 + 366 + 367 + 368 + 369 + 370 + 371 + 372 + 373 + 374 + 375 + 376 + 377 + 378 + 379 + 380 + 381 + 382 + 383 + 384 + 385 + 386 + 387 + 388 + 389 + 390 + 391 + 392 + 393 + 394 + 395 + 396 + 397 + 398 + 399 + 400 + 401 + 402 + 403 + 404 + 405 + 406 + 407 + 408 + 409 + 410 + 411 + 412 + 413 + 414 + 415 + 416 + 417 + 418 + 419 + 420 + 421 + 422 + 423 + 424 + 425 + 426 + 427 + 428 + 429 + 430 + 431 + 432 + 433 + 434 + 435 + 436 + 437 + 438 + 439 + 440 + 441 + 442 + 443 + 444 + 445 + 446 + 447 + 448 + 449 + 450 + 451 + 452 + 453 + 454 + 455 + 456 + 457 + 458 + 459 + 460 + 461 + 462 + 463 + 464 + 465 + 466 + 467 + 468 + 469 + 470 + 471 + 472 + 473 + 474 + 475 + 476 + 477 + 478 + 479 + 480 + 481 + 482 + 483 + 484 + 485 + 486 + 487 + 488 + 489 + 490 + 491 + 492 + 493 + 494 + 495 + 496 + 497 + 498 + 499 + 500 + 501 + 502 + 503 + 504 + 505 + 506 + 507 + 508 + 509 + 510 + 511 + 512 + 513 + 514 + 515 + 516 + 517 + 518 + 519 + 520 + 521 + 522 + 523 + 524 + 525 + 526 + 527 + 528 + 529 + 530 + 531 + 532 + 533 + 534 + 535 + 536 + 537 + 538 + 539 + 540 + 541 + 542 + 543 + 544 + 545 + 546 + 547 + 548 + 549 + 550 + 551 + 552 + 553 + 554 + 555 + 556 + 557 + 558 + 559 + 560 + 561 + 562 + 563 + 564 + 565 + 566 + 567 + 568 + 569 + 570 + 571 + 572 + 573 + 574 + 575 + 576 + 577 + 578 + 579 + 580 + 581 + 582 + 583 + 584 + 585 + 586 + 587 + 588 + 589 + 590 + 591 + 592 + 593 + 594 + 595 + 596 + 597 + 598 + 599 + 600 + 601 + 602 + 603 + 604 + 605 + 606 + 607 + 608 + 609 + 610 + 611 + 612 + 613 + 614 + 615 + 616 + 617 + 618 + 619 + 620 + 621 + 622 + 623 + 624 + 625 + 626 + 627 + 628 + 629 + 630 + 631 + 632 + 633 + 634 + 635 + 636 + 637 + 638 + 639 + 640 + 641 + 642 + 643 + 644 + 645 + 646 + 647 + 648 + 649 + 650 + 651 + 652 + 653 + 654 + 655 + 656 + 657 + 658 + 659 + 660 + 661 + 662 + 663 + 664 + 665 + 666 + 667 + 668 + 669 + 670 + 671 + 672 + 673 + 674 + 675 + 676 + 677 + 678 + 679 + 680 + 681 + 682 + 683 + 684 + 685 + 686 + 687 + 688 + 689 + 690 + 691 + 692 + 693 + 694 + 695 + 696 + 697 + 698 + 699 + 700 + 701 + 702 + 703 + 704 + 705 + 706 + 707 + 708 + 709 + 710 + 711 + 712 + 713 + 714 + 715 + 716 + 717 + 718 + 719 + 720 + 721 + 722 + 723 + 724 + 725 + 726 + 727 + 728 + 729 + 730 + 731 + 732 + 733 + 734 + 735 + 736 + 737 + 738 + 739 + 740 + 741 + 742 + 743 + 744 + 745 + 746 + 747 + 748 + 749 + 750 + 751 + 752 + 753 + 754 + 755 + 756 + 757 + 758 + 759 + 760 + 761 + 762 + 763 + 764 + 765 + 766 + 767 + 768 + 769 + 770 + 771 + 772 + 773 + 774 + 775 + 776 + 777 + 778 + 779 + 780 + 781 + 782 + 783 + 784 + 785 + 786 + 787 + 788 + 789 + 790 + 791 + 792 + 793 + 794 + 795 + 796 + 797 + 798 + 799 + 800 + 801 + 802 + 803 + 804 + 805 + 806 + 807 + 808 + 809 + 810 + 811 + 812 + 813 + 814 + 815 + 816 + 817 + 818 + 819 + 820 + 821 + 822 + 823 + 824 + 825 + 826 + 827 + 828 + 829 + 830 + 831 + 832 + 833 + 834 + 835 + 836 + 837 + 838 + 839 + 840 + 841 + 842 + 843 + 844 + 845 + 846 + 847 + 848 + 849 + 850 + 851 + 852 + 853 + 854 + 855 + 856 + 857 + 858 + 859 + 860 + 861 + 862 + 863 + 864 + 865 + 866 + 867 + 868 + 869 + 870 + 871 + 872 + 873 + 874 + 875 + 876 + 877 + 878 + 879 + 880 + 881 + 882 + 883 + 884 + 885 + 886 + 887 + 888 + 889 + 890 + 891 + 892 + 893 + 894 + 895 + 896 + 897 + 898 + 899 + 900 + 901 + 902 + 903 + 904 + 905 + 906 + 907 + 908 + 909 + 910 + 911 + 912 + 913 + 914 + 915 + 916 + 917 + 918 + 919 + 920 + 921 + 922 + 923 + 924 + 925 + 926 + 927 + 928 + 929 + 930 + 931 + 932 + 933 + 934 + 935 + 936 + 937 + 938 + 939 + 940 + 941 + 942 + 943 + 944 + 945 + 946 + 947 + 948 + 949 + 950 + 951 + 952 + 953 + 954 + 955 + 956 + 957 + 958 + 959 + 960 + 961 + 962 + 963 + 964 + 965 + 966 + 967 + 968 + 969 + 970 + 971 + 972 + 973 + 974 + 975 + 976 + 977 + 978 + 979 + 980 + 981 + 982 + 983 + 984 + 985 + 986 + 987 + 988 + 989 + 990 + 991 + 992 + 993 + 994 + 995 + 996 + 997 + 998 + 999 + diff --git a/libs/androidfw/tests/data/sparse/res/values/values.xml b/libs/androidfw/tests/data/sparse/res/values/values.xml new file mode 100644 index 0000000000000..5cc42d8f83455 --- /dev/null +++ b/libs/androidfw/tests/data/sparse/res/values/values.xml @@ -0,0 +1,28 @@ + + + + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + diff --git a/libs/androidfw/tests/data/sparse/sparse.apk b/libs/androidfw/tests/data/sparse/sparse.apk new file mode 100644 index 0000000000000..1f9bba31b0a13 Binary files /dev/null and b/libs/androidfw/tests/data/sparse/sparse.apk differ diff --git a/tools/aapt2/LoadedApk.cpp b/tools/aapt2/LoadedApk.cpp index 3d7bd9479ad8c..407550b112dc2 100644 --- a/tools/aapt2/LoadedApk.cpp +++ b/tools/aapt2/LoadedApk.cpp @@ -94,7 +94,9 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, IArchiveWriter* writer) { // The resource table needs to be reserialized since it might have changed. if (path == "resources.arsc") { BigBuffer buffer = BigBuffer(1024); - TableFlattener flattener(&buffer); + // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode + // with sparse entries) b/35389232. + TableFlattener flattener({}, &buffer); if (!flattener.Consume(context, table_.get())) { return false; } diff --git a/tools/aapt2/SdkConstants.h b/tools/aapt2/SdkConstants.h index 98ba94badfedf..c2ee2524c5e8b 100644 --- a/tools/aapt2/SdkConstants.h +++ b/tools/aapt2/SdkConstants.h @@ -25,7 +25,7 @@ namespace aapt { -enum { +enum : int { SDK_CUPCAKE = 3, SDK_DONUT = 4, SDK_ECLAIR = 5, @@ -49,7 +49,7 @@ enum { SDK_MARSHMALLOW = 23, SDK_NOUGAT = 24, SDK_NOUGAT_MR1 = 25, - SDK_O = 26, // STOPSHIP Replace with real version + SDK_O = 26, // STOPSHIP Replace with real version }; size_t FindAttributeSdkLevel(const ResourceId& id); diff --git a/tools/aapt2/flatten/TableFlattener.cpp b/tools/aapt2/flatten/TableFlattener.cpp index 19d030ec4a25f..697b07fa414db 100644 --- a/tools/aapt2/flatten/TableFlattener.cpp +++ b/tools/aapt2/flatten/TableFlattener.cpp @@ -26,6 +26,7 @@ #include "ResourceTable.h" #include "ResourceValues.h" +#include "SdkConstants.h" #include "ValueVisitor.h" #include "flatten/ChunkWriter.h" #include "flatten/ResourceTypeExtensions.h" @@ -216,8 +217,11 @@ class MapFlattenVisitor : public RawValueVisitor { class PackageFlattener { public: - PackageFlattener(IDiagnostics* diag, ResourceTablePackage* package) - : diag_(diag), package_(package) {} + PackageFlattener(IAaptContext* context, ResourceTablePackage* package, bool use_sparse_entries) + : context_(context), + diag_(context->GetDiagnostics()), + package_(package), + use_sparse_entries_(use_sparse_entries) {} bool FlattenPackage(BigBuffer* buffer) { ChunkWriter pkg_writer(buffer); @@ -298,9 +302,12 @@ class PackageFlattener { return true; } - bool FlattenConfig(const ResourceTableType* type, - const ConfigDescription& config, - std::vector* entries, BigBuffer* buffer) { + bool FlattenConfig(const ResourceTableType* type, const ConfigDescription& config, + const size_t num_total_entries, std::vector* entries, + BigBuffer* buffer) { + CHECK(num_total_entries != 0); + CHECK(num_total_entries <= std::numeric_limits::max()); + ChunkWriter type_writer(buffer); ResTable_type* type_header = type_writer.StartChunk(RES_TABLE_TYPE_TYPE); @@ -308,39 +315,60 @@ class PackageFlattener { type_header->config = config; type_header->config.swapHtoD(); - auto max_accum = [](uint32_t max, - const std::unique_ptr& a) -> uint32_t { - return std::max(max, (uint32_t)a->id.value()); - }; + std::vector offsets; + offsets.resize(num_total_entries, 0xffffffffu); - // Find the largest entry ID. That is how many entries we will have. - const uint32_t entry_count = - std::accumulate(type->entries.begin(), type->entries.end(), 0, - max_accum) + - 1; - - type_header->entryCount = util::HostToDevice32(entry_count); - uint32_t* indices = type_writer.NextBlock(entry_count); - - CHECK((size_t)entry_count <= std::numeric_limits::max()); - memset(indices, 0xff, entry_count * sizeof(uint32_t)); - - type_header->entriesStart = util::HostToDevice32(type_writer.size()); - - const size_t entry_start = type_writer.buffer()->size(); + BigBuffer values_buffer(512); for (FlatEntry& flat_entry : *entries) { - CHECK(flat_entry.entry->id.value() < entry_count); - indices[flat_entry.entry->id.value()] = - util::HostToDevice32(type_writer.buffer()->size() - entry_start); - if (!FlattenValue(&flat_entry, type_writer.buffer())) { + CHECK(static_cast(flat_entry.entry->id.value()) < num_total_entries); + offsets[flat_entry.entry->id.value()] = values_buffer.size(); + if (!FlattenValue(&flat_entry, &values_buffer)) { diag_->Error(DiagMessage() << "failed to flatten resource '" - << ResourceNameRef(package_->name, type->type, - flat_entry.entry->name) + << ResourceNameRef(package_->name, type->type, flat_entry.entry->name) << "' for configuration '" << config << "'"); return false; } } + + 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); + + // Only sparse encode if the offsets are representable in 2 bytes. + sparse_encode = + sparse_encode && (values_buffer.size() / 4u) <= std::numeric_limits::max(); + + // Only sparse encode if the ratio of populated entries to total entries is below some + // threshold. + sparse_encode = + sparse_encode && ((100 * entries->size()) / num_total_entries) < kSparseEncodingThreshold; + + if (sparse_encode) { + type_header->entryCount = util::HostToDevice32(entries->size()); + type_header->flags |= ResTable_type::FLAG_SPARSE; + ResTable_sparseTypeEntry* indices = + type_writer.NextBlock(entries->size()); + for (size_t i = 0; i < num_total_entries; i++) { + if (offsets[i] != ResTable_type::NO_ENTRY) { + CHECK((offsets[i] & 0x03) == 0); + indices->idx = util::HostToDevice16(i); + indices->offset = util::HostToDevice16(offsets[i] / 4u); + indices++; + } + } + } else { + type_header->entryCount = util::HostToDevice32(num_total_entries); + uint32_t* indices = type_writer.NextBlock(num_total_entries); + for (size_t i = 0; i < num_total_entries; i++) { + indices[i] = util::HostToDevice32(offsets[i]); + } + } + + type_header->entriesStart = util::HostToDevice32(type_writer.size()); + type_writer.buffer()->AppendBuffer(std::move(values_buffer)); type_writer.Finish(); return true; } @@ -370,8 +398,7 @@ class PackageFlattener { CHECK(bool(entry->id)) << "entry must have an ID set"; sorted_entries.push_back(entry.get()); } - std::sort(sorted_entries.begin(), sorted_entries.end(), - cmp_ids); + std::sort(sorted_entries.begin(), sorted_entries.end(), cmp_ids); return sorted_entries; } @@ -443,22 +470,22 @@ class PackageFlattener { type_pool_.MakeRef(ToString(type->type)); std::vector sorted_entries = CollectAndSortEntries(type); - if (!FlattenTypeSpec(type, &sorted_entries, buffer)) { return false; } + // Since the entries are sorted by ID, the last ID will be the largest. + const size_t num_entries = sorted_entries.back()->id.value() + 1; + // The binary resource table lists resource entries for each // configuration. // We store them inverted, where a resource entry lists the values for // each // configuration available. Here we reverse this to match the binary // table. - std::map> - config_to_entry_list_map; + std::map> config_to_entry_list_map; for (ResourceEntry* entry : sorted_entries) { - const uint32_t key_index = - (uint32_t)key_pool_.MakeRef(entry->name).index(); + const uint32_t key_index = (uint32_t)key_pool_.MakeRef(entry->name).index(); // Group values by configuration. for (auto& config_value : entry->values) { @@ -469,7 +496,7 @@ class PackageFlattener { // Flatten a configuration value. for (auto& entry : config_to_entry_list_map) { - if (!FlattenConfig(type, entry.first, &entry.second, buffer)) { + if (!FlattenConfig(type, entry.first, num_entries, &entry.second, buffer)) { return false; } } @@ -477,8 +504,10 @@ class PackageFlattener { return true; } + IAaptContext* context_; IDiagnostics* diag_; ResourceTablePackage* package_; + bool use_sparse_entries_; StringPool type_pool_; StringPool key_pool_; }; @@ -513,7 +542,7 @@ bool TableFlattener::Consume(IAaptContext* context, ResourceTable* table) { // Flatten each package. for (auto& package : table->packages) { - PackageFlattener flattener(context->GetDiagnostics(), package.get()); + PackageFlattener flattener(context, package.get(), options_.use_sparse_entries); if (!flattener.FlattenPackage(&package_buffer)) { return false; } diff --git a/tools/aapt2/flatten/TableFlattener.h b/tools/aapt2/flatten/TableFlattener.h index 53f52c29a6a35..223aef8119c09 100644 --- a/tools/aapt2/flatten/TableFlattener.h +++ b/tools/aapt2/flatten/TableFlattener.h @@ -25,15 +25,29 @@ namespace aapt { +// The percentage of used entries for a type for which using a sparse encoding is +// preferred. +constexpr const size_t kSparseEncodingThreshold = 60; + +struct TableFlattenerOptions { + // When true, types for configurations with a sparse set of entries are encoded + // as a sparse map of entry ID and offset to actual data. + // This is only available on platforms O+ and will only be respected when + // minSdk is O+. + bool use_sparse_entries = false; +}; + class TableFlattener : public IResourceTableConsumer { public: - explicit TableFlattener(BigBuffer* buffer) : buffer_(buffer) {} + explicit TableFlattener(const TableFlattenerOptions& options, BigBuffer* buffer) + : options_(options), buffer_(buffer) {} bool Consume(IAaptContext* context, ResourceTable* table) override; private: DISALLOW_COPY_AND_ASSIGN(TableFlattener); + TableFlattenerOptions options_; BigBuffer* buffer_; }; diff --git a/tools/aapt2/flatten/TableFlattener_test.cpp b/tools/aapt2/flatten/TableFlattener_test.cpp index c72624066fb85..ff717423fc92d 100644 --- a/tools/aapt2/flatten/TableFlattener_test.cpp +++ b/tools/aapt2/flatten/TableFlattener_test.cpp @@ -16,7 +16,10 @@ #include "flatten/TableFlattener.h" +#include "android-base/stringprintf.h" + #include "ResourceUtils.h" +#include "SdkConstants.h" #include "test/Test.h" #include "unflatten/BinaryResourceParser.h" #include "util/Util.h" @@ -34,32 +37,40 @@ class TableFlattenerTest : public ::testing::Test { .Build(); } - ::testing::AssertionResult Flatten(ResourceTable* table, - ResTable* out_table) { + ::testing::AssertionResult Flatten(IAaptContext* context, const TableFlattenerOptions& options, + ResourceTable* table, std::string* out_content) { BigBuffer buffer(1024); - TableFlattener flattener(&buffer); - if (!flattener.Consume(context_.get(), table)) { + TableFlattener flattener(options, &buffer); + if (!flattener.Consume(context, table)) { return ::testing::AssertionFailure() << "failed to flatten ResourceTable"; } + *out_content = buffer.to_string(); + return ::testing::AssertionSuccess(); + } - std::unique_ptr data = util::Copy(buffer); - if (out_table->add(data.get(), buffer.size(), -1, true) != NO_ERROR) { + ::testing::AssertionResult Flatten(IAaptContext* context, const TableFlattenerOptions& options, + ResourceTable* table, ResTable* out_table) { + std::string content; + auto result = Flatten(context, options, table, &content); + if (!result) { + return result; + } + + if (out_table->add(content.data(), content.size(), -1, true) != NO_ERROR) { return ::testing::AssertionFailure() << "flattened ResTable is corrupt"; } return ::testing::AssertionSuccess(); } - ::testing::AssertionResult Flatten(ResourceTable* table, - ResourceTable* out_table) { - BigBuffer buffer(1024); - TableFlattener flattener(&buffer); - if (!flattener.Consume(context_.get(), table)) { - return ::testing::AssertionFailure() << "failed to flatten ResourceTable"; + ::testing::AssertionResult Flatten(IAaptContext* context, const TableFlattenerOptions options, + ResourceTable* table, ResourceTable* out_table) { + std::string content; + auto result = Flatten(context, options, table, &content); + if (!result) { + return result; } - std::unique_ptr data = util::Copy(buffer); - BinaryResourceParser parser(context_.get(), out_table, {}, data.get(), - buffer.size()); + BinaryResourceParser parser(context, out_table, {}, content.data(), content.size()); if (!parser.Parse()) { return ::testing::AssertionFailure() << "flattened ResTable is corrupt"; } @@ -127,7 +138,7 @@ class TableFlattenerTest : public ::testing::Test { return ::testing::AssertionSuccess(); } - private: + protected: std::unique_ptr context_; }; @@ -153,7 +164,7 @@ TEST_F(TableFlattenerTest, FlattenFullyLinkedTable) { .Build(); ResTable res_table; - ASSERT_TRUE(Flatten(table.get(), &res_table)); + ASSERT_TRUE(Flatten(context_.get(), {}, table.get(), &res_table)); EXPECT_TRUE(Exists(&res_table, "com.app.test:id/one", ResourceId(0x7f020000), {}, Res_value::TYPE_INT_BOOLEAN, 0u, 0u)); @@ -200,7 +211,7 @@ TEST_F(TableFlattenerTest, FlattenEntriesWithGapsInIds) { .Build(); ResTable res_table; - ASSERT_TRUE(Flatten(table.get(), &res_table)); + ASSERT_TRUE(Flatten(context_.get(), {}, table.get(), &res_table)); EXPECT_TRUE(Exists(&res_table, "com.app.test:id/one", ResourceId(0x7f020001), {}, Res_value::TYPE_INT_BOOLEAN, 0u, 0u)); @@ -222,7 +233,7 @@ TEST_F(TableFlattenerTest, FlattenMinMaxAttributes) { .Build(); ResourceTable result; - ASSERT_TRUE(Flatten(table.get(), &result)); + ASSERT_TRUE(Flatten(context_.get(), {}, table.get(), &result)); Attribute* actualAttr = test::GetValue(&result, "android:attr/foo"); @@ -233,4 +244,119 @@ TEST_F(TableFlattenerTest, FlattenMinMaxAttributes) { EXPECT_EQ(attr.max_int, actualAttr->max_int); } +static std::unique_ptr BuildTableWithSparseEntries( + IAaptContext* context, const ConfigDescription& sparse_config, float load) { + std::unique_ptr table = + test::ResourceTableBuilder() + .SetPackageId(context->GetCompilationPackage(), context->GetPackageId()) + .Build(); + + // Add regular entries. + int stride = static_cast(1.0f / load); + for (int i = 0; i < 100; i++) { + const ResourceName name = test::ParseNameOrDie( + base::StringPrintf("%s:string/foo_%d", context->GetCompilationPackage().data(), i)); + const ResourceId resid(context->GetPackageId(), 0x02, static_cast(i)); + const auto value = + util::make_unique(Res_value::TYPE_INT_DEC, static_cast(i)); + CHECK(table->AddResource(name, resid, ConfigDescription::DefaultConfig(), "", + std::unique_ptr(value->Clone(nullptr)), + context->GetDiagnostics())); + + // Every few entries, write out a sparse_config value. This will give us the desired load. + if (i % stride == 0) { + CHECK(table->AddResource(name, resid, sparse_config, "", + std::unique_ptr(value->Clone(nullptr)), + context->GetDiagnostics())); + } + } + return table; +} + +TEST_F(TableFlattenerTest, FlattenSparseEntryWithMinSdkO) { + std::unique_ptr context = test::ContextBuilder() + .SetCompilationPackage("android") + .SetPackageId(0x01) + .SetMinSdkVersion(SDK_O) + .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.get(), &sparse_table, Source("test.arsc"), + sparse_contents.data(), sparse_contents.size()); + ASSERT_TRUE(parser.Parse()); + + auto value = test::GetValueForConfig(&sparse_table, "android:string/foo_0", + sparse_config); + ASSERT_NE(nullptr, value); + EXPECT_EQ(0u, value->value.data); + + ASSERT_EQ(nullptr, test::GetValueForConfig(&sparse_table, "android:string/foo_1", + sparse_config)); + + value = test::GetValueForConfig(&sparse_table, "android:string/foo_4", + sparse_config); + ASSERT_NE(nullptr, value); + EXPECT_EQ(4u, value->value.data); +} + +TEST_F(TableFlattenerTest, FlattenSparseEntryWithConfigSdkVersionO) { + std::unique_ptr context = test::ContextBuilder() + .SetCompilationPackage("android") + .SetPackageId(0x01) + .SetMinSdkVersion(SDK_LOLLIPOP) + .Build(); + + const ConfigDescription sparse_config = test::ParseConfigOrDie("en-rGB-v26"); + 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()); +} + +TEST_F(TableFlattenerTest, DoNotUseSparseEntryForDenseConfig) { + std::unique_ptr context = test::ContextBuilder() + .SetCompilationPackage("android") + .SetPackageId(0x01) + .SetMinSdkVersion(SDK_O) + .Build(); + + const ConfigDescription sparse_config = test::ParseConfigOrDie("en-rGB"); + auto table_in = BuildTableWithSparseEntries(context.get(), sparse_config, 0.80f); + + 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_EQ(no_sparse_contents.size(), sparse_contents.size()); +} + } // namespace aapt diff --git a/tools/aapt2/link/Link.cpp b/tools/aapt2/link/Link.cpp index 0501a3bb5fcad..f07e20bbc78a0 100644 --- a/tools/aapt2/link/Link.cpp +++ b/tools/aapt2/link/Link.cpp @@ -96,6 +96,9 @@ struct LinkOptions { // Products to use/filter on. std::unordered_set products; + // Flattening options. + TableFlattenerOptions table_flattener_options; + // Split APK options. TableSplitterOptions table_splitter_options; std::vector split_constraints; @@ -874,7 +877,7 @@ class LinkCommand { bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) { BigBuffer buffer(1024); - TableFlattener flattener(&buffer); + TableFlattener flattener(options_.table_flattener_options, &buffer); if (!flattener.Consume(context_, table)) { return false; } @@ -1870,24 +1873,19 @@ int Link(const std::vector& args) { .RequiredFlag("-o", "Output path", &options.output_path) .RequiredFlag("--manifest", "Path to the Android manifest to build", &options.manifest_path) - .OptionalFlagList("-I", "Adds an Android APK to link against", - &options.include_paths) - .OptionalFlagList( - "-R", - "Compilation unit to link, using `overlay` semantics.\n" - "The last conflicting resource given takes precedence.", - &overlay_arg_list) + .OptionalFlagList("-I", "Adds an Android APK to link against", &options.include_paths) + .OptionalFlagList("-R", + "Compilation unit to link, using `overlay` semantics.\n" + "The last conflicting resource given takes precedence.", + &overlay_arg_list) .OptionalFlag("--java", "Directory in which to generate R.java", &options.generate_java_class_path) - .OptionalFlag("--proguard", - "Output file for generated Proguard rules", + .OptionalFlag("--proguard", "Output file for generated Proguard rules", &options.generate_proguard_rules_path) - .OptionalFlag( - "--proguard-main-dex", - "Output file for generated Proguard rules for the main dex", - &options.generate_main_dex_proguard_rules_path) - .OptionalSwitch("--no-auto-version", - "Disables automatic style and layout SDK versioning", + .OptionalFlag("--proguard-main-dex", + "Output file for generated Proguard rules for the main dex", + &options.generate_main_dex_proguard_rules_path) + .OptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning", &options.no_auto_version) .OptionalSwitch("--no-version-vectors", "Disables automatic versioning of vector drawables. " @@ -1903,25 +1901,22 @@ int Link(const std::vector& args) { "Disables automatic deduping of resources with\n" "identical values across compatible configurations.", &options.no_resource_deduping) - .OptionalSwitch( - "-x", - "Legacy flag that specifies to use the package identifier 0x01", - &legacy_x_flag) - .OptionalSwitch("-z", - "Require localization of strings marked 'suggested'", + .OptionalSwitch("--enable-sparse-encoding", + "Enables encoding sparse entries using a binary search tree.\n" + "This decreases APK size at the cost of resource retrieval performance.", + &options.table_flattener_options.use_sparse_entries) + .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01", + &legacy_x_flag) + .OptionalSwitch("-z", "Require localization of strings marked 'suggested'", &require_localization) - .OptionalFlag( - "-c", - "Comma separated list of configurations to include. The default\n" - "is all configurations", - &configs) - .OptionalFlag( - "--preferred-density", - "Selects the closest matching density and strips out all others.", - &preferred_density) - .OptionalFlag("--product", - "Comma separated list of product names to keep", - &product_list) + .OptionalFlag("-c", + "Comma separated list of configurations to include. The default\n" + "is all configurations", + &configs) + .OptionalFlag("--preferred-density", + "Selects the closest matching density and strips out all others.", + &preferred_density) + .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list) .OptionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified " "by -o", @@ -1935,11 +1930,10 @@ int Link(const std::vector& args) { "Default minimum SDK version to use for " "AndroidManifest.xml", &options.manifest_fixer_options.min_sdk_version_default) - .OptionalFlag( - "--target-sdk-version", - "Default target SDK version to use for " - "AndroidManifest.xml", - &options.manifest_fixer_options.target_sdk_version_default) + .OptionalFlag("--target-sdk-version", + "Default target SDK version to use for " + "AndroidManifest.xml", + &options.manifest_fixer_options.target_sdk_version_default) .OptionalFlag("--version-code", "Version code (integer) to inject into the " "AndroidManifest.xml if none is present", @@ -1948,8 +1942,7 @@ int Link(const std::vector& args) { "Version name to inject into the AndroidManifest.xml " "if none is present", &options.manifest_fixer_options.version_name_default) - .OptionalSwitch("--static-lib", "Generate a static Android library", - &options.static_lib) + .OptionalSwitch("--static-lib", "Generate a static Android library", &options.static_lib) .OptionalSwitch("--no-static-lib-packages", "Merge all library resources under the app's package", &options.no_static_lib_packages) @@ -1957,14 +1950,12 @@ int Link(const std::vector& args) { "Generates R.java without the final modifier.\n" "This is implied when --static-lib is specified.", &options.generate_non_final_ids) - .OptionalFlag("--stable-ids", - "File containing a list of name to ID mapping.", + .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.", &stable_id_file_path) - .OptionalFlag( - "--emit-ids", - "Emit a file at the given path with a list of name to ID\n" - "mappings, suitable for use with --stable-ids.", - &options.resource_id_map_path) + .OptionalFlag("--emit-ids", + "Emit a file at the given path with a list of name to ID\n" + "mappings, suitable for use with --stable-ids.", + &options.resource_id_map_path) .OptionalFlag("--private-symbols", "Package name to use when generating R.java for " "private symbols.\n" @@ -1972,8 +1963,7 @@ int Link(const std::vector& args) { "the application's " "package name", &options.private_symbols) - .OptionalFlag("--custom-package", - "Custom Java package under which to generate R.java", + .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java", &options.custom_java_package) .OptionalFlagList("--extra-packages", "Generate the same R.java but with different " @@ -1987,23 +1977,19 @@ int Link(const std::vector& args) { "Allows the addition of new resources in " "overlays without tags", &options.auto_add_overlay) - .OptionalFlag("--rename-manifest-package", - "Renames the package in AndroidManifest.xml", + .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml", &options.manifest_fixer_options.rename_manifest_package) - .OptionalFlag( - "--rename-instrumentation-target-package", - "Changes the name of the target package for instrumentation. " - "Most useful " - "when used\nin conjunction with --rename-manifest-package", - &options.manifest_fixer_options - .rename_instrumentation_target_package) + .OptionalFlag("--rename-instrumentation-target-package", + "Changes the name of the target package for instrumentation. " + "Most useful " + "when used\nin conjunction with --rename-manifest-package", + &options.manifest_fixer_options.rename_instrumentation_target_package) .OptionalFlagList("-0", "File extensions not to compress", &options.extensions_to_not_compress) - .OptionalFlagList( - "--split", - "Split resources matching a set of configs out to a " - "Split APK.\nSyntax: path/to/output.apk:[,[...]]", - &split_args) + .OptionalFlagList("--split", + "Split resources matching a set of configs out to a " + "Split APK.\nSyntax: path/to/output.apk:[,[...]]", + &split_args) .OptionalSwitch("-v", "Enables verbose logging", &verbose); if (!flags.Parse("aapt2 link", args, &std::cerr)) { diff --git a/tools/aapt2/util/BigBuffer.cpp b/tools/aapt2/util/BigBuffer.cpp index ef99dca286a49..75fa78915b655 100644 --- a/tools/aapt2/util/BigBuffer.cpp +++ b/tools/aapt2/util/BigBuffer.cpp @@ -76,4 +76,12 @@ void* BigBuffer::NextBlock(size_t* out_size) { return blocks_.back().buffer.get(); } +std::string BigBuffer::to_string() const { + std::string result; + for (const Block& block : blocks_) { + result.append(block.buffer.get(), block.buffer.get() + block.size); + } + return result; +} + } // namespace aapt diff --git a/tools/aapt2/util/BigBuffer.h b/tools/aapt2/util/BigBuffer.h index d23c41d4d6f0f..30452552888ec 100644 --- a/tools/aapt2/util/BigBuffer.h +++ b/tools/aapt2/util/BigBuffer.h @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -116,6 +117,8 @@ class BigBuffer { const_iterator begin() const; const_iterator end() const; + std::string to_string() const; + private: DISALLOW_COPY_AND_ASSIGN(BigBuffer);