summaryrefslogtreecommitdiff
path: root/tools/aapt2/optimize
diff options
context:
space:
mode:
Diffstat (limited to 'tools/aapt2/optimize')
-rw-r--r--tools/aapt2/optimize/MultiApkGenerator.cpp343
-rw-r--r--tools/aapt2/optimize/MultiApkGenerator.h72
-rw-r--r--tools/aapt2/optimize/MultiApkGenerator_test.cpp182
-rw-r--r--tools/aapt2/optimize/VersionCollapser.cpp28
4 files changed, 608 insertions, 17 deletions
diff --git a/tools/aapt2/optimize/MultiApkGenerator.cpp b/tools/aapt2/optimize/MultiApkGenerator.cpp
new file mode 100644
index 000000000000..991faadcafc4
--- /dev/null
+++ b/tools/aapt2/optimize/MultiApkGenerator.cpp
@@ -0,0 +1,343 @@
+/*
+ * 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 "MultiApkGenerator.h"
+
+#include <algorithm>
+#include <regex>
+#include <string>
+
+#include "androidfw/StringPiece.h"
+
+#include "LoadedApk.h"
+#include "ResourceUtils.h"
+#include "ValueVisitor.h"
+#include "configuration/ConfigurationParser.h"
+#include "filter/AbiFilter.h"
+#include "filter/Filter.h"
+#include "format/Archive.h"
+#include "format/binary/XmlFlattener.h"
+#include "optimize/VersionCollapser.h"
+#include "process/IResourceTableConsumer.h"
+#include "split/TableSplitter.h"
+#include "util/Files.h"
+#include "xml/XmlDom.h"
+#include "xml/XmlUtil.h"
+
+namespace aapt {
+
+using ::aapt::configuration::AndroidSdk;
+using ::aapt::configuration::OutputArtifact;
+using ::aapt::xml::kSchemaAndroid;
+using ::aapt::xml::XmlResource;
+using ::android::StringPiece;
+
+/**
+ * Context wrapper that allows the min Android SDK value to be overridden.
+ */
+class ContextWrapper : public IAaptContext {
+ public:
+ explicit ContextWrapper(IAaptContext* context)
+ : context_(context), min_sdk_(context_->GetMinSdkVersion()) {
+ }
+
+ PackageType GetPackageType() override {
+ return context_->GetPackageType();
+ }
+
+ SymbolTable* GetExternalSymbols() override {
+ return context_->GetExternalSymbols();
+ }
+
+ IDiagnostics* GetDiagnostics() override {
+ if (source_diag_) {
+ return source_diag_.get();
+ }
+ return context_->GetDiagnostics();
+ }
+
+ const std::string& GetCompilationPackage() override {
+ return context_->GetCompilationPackage();
+ }
+
+ uint8_t GetPackageId() override {
+ return context_->GetPackageId();
+ }
+
+ NameMangler* GetNameMangler() override {
+ return context_->GetNameMangler();
+ }
+
+ bool IsVerbose() override {
+ return context_->IsVerbose();
+ }
+
+ int GetMinSdkVersion() override {
+ return min_sdk_;
+ }
+
+ void SetMinSdkVersion(int min_sdk) {
+ min_sdk_ = min_sdk;
+ }
+
+ void SetSource(const std::string& source) {
+ source_diag_ =
+ util::make_unique<SourcePathDiagnostics>(Source{source}, context_->GetDiagnostics());
+ }
+
+ private:
+ IAaptContext* context_;
+ std::unique_ptr<SourcePathDiagnostics> source_diag_;
+
+ int min_sdk_ = -1;
+};
+
+class SignatureFilter : public IPathFilter {
+ bool Keep(const std::string& path) override {
+ static std::regex signature_regex(R"regex(^META-INF/.*\.(RSA|DSA|EC|SF)$)regex");
+ if (std::regex_search(path, signature_regex)) {
+ return false;
+ }
+ return !(path == "META-INF/MANIFEST.MF");
+ }
+};
+
+MultiApkGenerator::MultiApkGenerator(LoadedApk* apk, IAaptContext* context)
+ : apk_(apk), context_(context) {
+}
+
+bool MultiApkGenerator::FromBaseApk(const MultiApkGeneratorOptions& options) {
+ std::unordered_set<std::string> artifacts_to_keep = options.kept_artifacts;
+ std::unordered_set<std::string> filtered_artifacts;
+ std::unordered_set<std::string> kept_artifacts;
+
+ // For now, just write out the stripped APK since ABI splitting doesn't modify anything else.
+ for (const OutputArtifact& artifact : options.apk_artifacts) {
+ FilterChain filters;
+
+ ContextWrapper wrapped_context{context_};
+ wrapped_context.SetSource(artifact.name);
+
+ if (!options.kept_artifacts.empty()) {
+ const auto& it = artifacts_to_keep.find(artifact.name);
+ if (it == artifacts_to_keep.end()) {
+ filtered_artifacts.insert(artifact.name);
+ if (context_->IsVerbose()) {
+ context_->GetDiagnostics()->Note(DiagMessage(artifact.name) << "skipping artifact");
+ }
+ continue;
+ } else {
+ artifacts_to_keep.erase(it);
+ kept_artifacts.insert(artifact.name);
+ }
+ }
+
+ std::unique_ptr<ResourceTable> table =
+ FilterTable(context_, artifact, *apk_->GetResourceTable(), &filters);
+ if (!table) {
+ return false;
+ }
+
+ IDiagnostics* diag = wrapped_context.GetDiagnostics();
+
+ std::unique_ptr<XmlResource> manifest;
+ if (!UpdateManifest(artifact, &manifest, diag)) {
+ diag->Error(DiagMessage() << "could not update AndroidManifest.xml for output artifact");
+ return false;
+ }
+
+ std::string out = options.out_dir;
+ if (!file::mkdirs(out)) {
+ diag->Warn(DiagMessage() << "could not create out dir: " << out);
+ }
+ file::AppendPath(&out, artifact.name);
+
+ if (context_->IsVerbose()) {
+ diag->Note(DiagMessage() << "Generating split: " << out);
+ }
+
+ std::unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(diag, out);
+
+ if (context_->IsVerbose()) {
+ diag->Note(DiagMessage() << "Writing output: " << out);
+ }
+
+ filters.AddFilter(util::make_unique<SignatureFilter>());
+ if (!apk_->WriteToArchive(&wrapped_context, table.get(), options.table_flattener_options,
+ &filters, writer.get(), manifest.get())) {
+ return false;
+ }
+ }
+
+ // Make sure all of the requested artifacts were valid. If there are any kept artifacts left,
+ // either the config or the command line was wrong.
+ if (!artifacts_to_keep.empty()) {
+ context_->GetDiagnostics()->Error(
+ DiagMessage() << "The configuration and command line to filter artifacts do not match");
+
+ context_->GetDiagnostics()->Error(DiagMessage() << kept_artifacts.size() << " kept:");
+ for (const auto& artifact : kept_artifacts) {
+ context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact);
+ }
+
+ context_->GetDiagnostics()->Error(DiagMessage() << filtered_artifacts.size() << " filtered:");
+ for (const auto& artifact : filtered_artifacts) {
+ context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact);
+ }
+
+ context_->GetDiagnostics()->Error(DiagMessage() << artifacts_to_keep.size() << " missing:");
+ for (const auto& artifact : artifacts_to_keep) {
+ context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact);
+ }
+
+ return false;
+ }
+
+ return true;
+}
+
+std::unique_ptr<ResourceTable> MultiApkGenerator::FilterTable(IAaptContext* context,
+ const OutputArtifact& artifact,
+ const ResourceTable& old_table,
+ FilterChain* filters) {
+ TableSplitterOptions splits;
+ AxisConfigFilter axis_filter;
+ ContextWrapper wrapped_context{context};
+ wrapped_context.SetSource(artifact.name);
+
+ if (!artifact.abis.empty()) {
+ filters->AddFilter(AbiFilter::FromAbiList(artifact.abis));
+ }
+
+ if (!artifact.screen_densities.empty()) {
+ for (const auto& density_config : artifact.screen_densities) {
+ splits.preferred_densities.push_back(density_config.density);
+ }
+ }
+
+ if (!artifact.locales.empty()) {
+ for (const auto& locale : artifact.locales) {
+ axis_filter.AddConfig(locale);
+ }
+ splits.config_filter = &axis_filter;
+ }
+
+ if (artifact.android_sdk) {
+ wrapped_context.SetMinSdkVersion(artifact.android_sdk.value().min_sdk_version);
+ }
+
+ std::unique_ptr<ResourceTable> table = old_table.Clone();
+
+ VersionCollapser collapser;
+ if (!collapser.Consume(&wrapped_context, table.get())) {
+ context->GetDiagnostics()->Error(DiagMessage() << "Failed to strip versioned resources");
+ return {};
+ }
+
+ TableSplitter splitter{{}, splits};
+ splitter.SplitTable(table.get());
+ return table;
+}
+
+bool MultiApkGenerator::UpdateManifest(const OutputArtifact& artifact,
+ std::unique_ptr<XmlResource>* updated_manifest,
+ IDiagnostics* diag) {
+ const xml::XmlResource* apk_manifest = apk_->GetManifest();
+ if (apk_manifest == nullptr) {
+ return false;
+ }
+
+ *updated_manifest = apk_manifest->Clone();
+ XmlResource* manifest = updated_manifest->get();
+
+ // Make sure the first element is <manifest> with package attribute.
+ xml::Element* manifest_el = manifest->root.get();
+ if (manifest_el == nullptr) {
+ return false;
+ }
+
+ if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
+ diag->Error(DiagMessage(manifest->file.source) << "root tag must be <manifest>");
+ return false;
+ }
+
+ // Update the versionCode attribute.
+ xml::Attribute* versionCode = manifest_el->FindAttribute(kSchemaAndroid, "versionCode");
+ if (versionCode == nullptr) {
+ diag->Error(DiagMessage(manifest->file.source) << "manifest must have a versionCode attribute");
+ return false;
+ }
+
+ auto* compiled_version = ValueCast<BinaryPrimitive>(versionCode->compiled_value.get());
+ if (compiled_version == nullptr) {
+ diag->Error(DiagMessage(manifest->file.source) << "versionCode is invalid");
+ return false;
+ }
+
+ int new_version = compiled_version->value.data + artifact.version;
+ versionCode->compiled_value = ResourceUtils::TryParseInt(std::to_string(new_version));
+
+ // Check to see if the minSdkVersion needs to be updated.
+ if (artifact.android_sdk) {
+ // TODO(safarmer): Handle the rest of the Android SDK.
+ const AndroidSdk& android_sdk = artifact.android_sdk.value();
+
+ if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
+ if (xml::Attribute* min_sdk_attr =
+ uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
+ // Populate with a pre-compiles attribute to we don't need to relink etc.
+ const std::string& min_sdk_str = std::to_string(android_sdk.min_sdk_version);
+ min_sdk_attr->compiled_value = ResourceUtils::TryParseInt(min_sdk_str);
+ } else {
+ // There was no minSdkVersion. This is strange since at this point we should have been
+ // through the manifest fixer which sets the default minSdkVersion.
+ diag->Error(DiagMessage(manifest->file.source) << "missing minSdkVersion from <uses-sdk>");
+ return false;
+ }
+ } else {
+ // No uses-sdk present. This is strange since at this point we should have been
+ // through the manifest fixer which should have added it.
+ diag->Error(DiagMessage(manifest->file.source) << "missing <uses-sdk> from <manifest>");
+ return false;
+ }
+ }
+
+ if (!artifact.screen_densities.empty()) {
+ xml::Element* screens_el = manifest_el->FindChild({}, "compatible-screens");
+ if (!screens_el) {
+ // create a new element.
+ std::unique_ptr<xml::Element> new_screens_el = util::make_unique<xml::Element>();
+ new_screens_el->name = "compatible-screens";
+ screens_el = new_screens_el.get();
+ manifest_el->InsertChild(0, std::move(new_screens_el));
+ } else {
+ // clear out the old element.
+ screens_el->GetChildElements().clear();
+ }
+
+ for (const auto& density : artifact.screen_densities) {
+ std::unique_ptr<xml::Element> screen_el = util::make_unique<xml::Element>();
+ screen_el->name = "screen";
+ const char* density_str = density.toString().string();
+ screen_el->attributes.push_back(xml::Attribute{kSchemaAndroid, "screenDensity", density_str});
+ screens_el->AppendChild(std::move(screen_el));
+ }
+ }
+
+ return true;
+}
+
+} // namespace aapt
diff --git a/tools/aapt2/optimize/MultiApkGenerator.h b/tools/aapt2/optimize/MultiApkGenerator.h
new file mode 100644
index 000000000000..19f64cc0e588
--- /dev/null
+++ b/tools/aapt2/optimize/MultiApkGenerator.h
@@ -0,0 +1,72 @@
+/*
+ * 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 AAPT2_APKSPLITTER_H
+#define AAPT2_APKSPLITTER_H
+
+#include <memory>
+#include <string>
+#include <unordered_set>
+#include <vector>
+
+#include "Diagnostics.h"
+#include "LoadedApk.h"
+#include "configuration/ConfigurationParser.h"
+
+namespace aapt {
+
+struct MultiApkGeneratorOptions {
+ std::string out_dir;
+ std::vector<configuration::OutputArtifact> apk_artifacts;
+ TableFlattenerOptions table_flattener_options;
+ std::unordered_set<std::string> kept_artifacts;
+};
+
+/**
+ * Generates a set of APKs that are a subset of the original base APKs. Each of the new APKs contain
+ * only the resources and assets for an artifact in the configuration file.
+ */
+class MultiApkGenerator {
+ public:
+ MultiApkGenerator(LoadedApk* apk, IAaptContext* context);
+
+ /**
+ * Writes a set of APKs to the provided output directory. Each APK is a subset fo the base APK and
+ * represents an artifact in the post processing configuration.
+ */
+ bool FromBaseApk(const MultiApkGeneratorOptions& options);
+
+ protected:
+ virtual std::unique_ptr<ResourceTable> FilterTable(IAaptContext* context,
+ const configuration::OutputArtifact& artifact,
+ const ResourceTable& old_table,
+ FilterChain* chain);
+
+ private:
+ IDiagnostics* GetDiagnostics() {
+ return context_->GetDiagnostics();
+ }
+
+ bool UpdateManifest(const configuration::OutputArtifact& artifact,
+ std::unique_ptr<xml::XmlResource>* updated_manifest, IDiagnostics* diag);
+
+ LoadedApk* apk_;
+ IAaptContext* context_;
+};
+
+} // namespace aapt
+
+#endif // AAPT2_APKSPLITTER_H
diff --git a/tools/aapt2/optimize/MultiApkGenerator_test.cpp b/tools/aapt2/optimize/MultiApkGenerator_test.cpp
new file mode 100644
index 000000000000..e1d951fc9776
--- /dev/null
+++ b/tools/aapt2/optimize/MultiApkGenerator_test.cpp
@@ -0,0 +1,182 @@
+/*
+ * 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 "optimize/MultiApkGenerator.h"
+
+#include <string>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+#include "LoadedApk.h"
+#include "ResourceTable.h"
+#include "configuration/ConfigurationParser.h"
+#include "filter/Filter.h"
+#include "format/Archive.h"
+#include "format/binary/TableFlattener.h"
+#include "process/IResourceTableConsumer.h"
+#include "test/Context.h"
+#include "test/Test.h"
+
+namespace aapt {
+namespace {
+
+using ::aapt::configuration::Abi;
+using ::aapt::configuration::AndroidSdk;
+using ::aapt::configuration::OutputArtifact;
+using ::aapt::test::GetValue;
+using ::aapt::test::GetValueForConfig;
+using ::aapt::test::ParseConfigOrDie;
+using ::testing::Eq;
+using ::testing::IsNull;
+using ::testing::Not;
+using ::testing::NotNull;
+using ::testing::PrintToString;
+using ::testing::Return;
+using ::testing::Test;
+
+/**
+ * Subclass the MultiApkGenerator class so that we can access the protected FilterTable method to
+ * directly test table filter.
+ */
+class MultiApkGeneratorWrapper : public MultiApkGenerator {
+ public:
+ MultiApkGeneratorWrapper(LoadedApk* apk, IAaptContext* context)
+ : MultiApkGenerator(apk, context) {
+ }
+
+ std::unique_ptr<ResourceTable> FilterTable(IAaptContext* context,
+ const configuration::OutputArtifact& artifact,
+ const ResourceTable& old_table,
+ FilterChain* filter_chain) override {
+ return MultiApkGenerator::FilterTable(context, artifact, old_table, filter_chain);
+ }
+};
+
+/** MultiApkGenerator test fixture. */
+class MultiApkGeneratorTest : public ::testing::Test {
+ public:
+ std::unique_ptr<ResourceTable> BuildTable() {
+ return test::ResourceTableBuilder()
+ .AddFileReference(kResourceName, "res/drawable-mdpi/icon.png", mdpi_)
+ .AddFileReference(kResourceName, "res/drawable-hdpi/icon.png", hdpi_)
+ .AddFileReference(kResourceName, "res/drawable-xhdpi/icon.png", xhdpi_)
+ .AddFileReference(kResourceName, "res/drawable-xxhdpi/icon.png", xxhdpi_)
+ .AddFileReference(kResourceName, "res/drawable-v19/icon.xml", v19_)
+ .AddFileReference(kResourceName, "res/drawable-v21/icon.xml", v21_)
+ .AddSimple("android:string/one")
+ .Build();
+ }
+
+ inline FileReference* ValueForConfig(ResourceTable* table, const ConfigDescription& config) {
+ return GetValueForConfig<FileReference>(table, kResourceName, config);
+ };
+
+ void SetUp() override {
+ }
+
+ protected:
+ static constexpr const char* kResourceName = "android:drawable/icon";
+
+ ConfigDescription default_ = ParseConfigOrDie("").CopyWithoutSdkVersion();
+ ConfigDescription mdpi_ = ParseConfigOrDie("mdpi").CopyWithoutSdkVersion();
+ ConfigDescription hdpi_ = ParseConfigOrDie("hdpi").CopyWithoutSdkVersion();
+ ConfigDescription xhdpi_ = ParseConfigOrDie("xhdpi").CopyWithoutSdkVersion();
+ ConfigDescription xxhdpi_ = ParseConfigOrDie("xxhdpi").CopyWithoutSdkVersion();
+ ConfigDescription xxxhdpi_ = ParseConfigOrDie("xxxhdpi").CopyWithoutSdkVersion();
+ ConfigDescription v19_ = ParseConfigOrDie("v19");
+ ConfigDescription v21_ = ParseConfigOrDie("v21");
+};
+
+TEST_F(MultiApkGeneratorTest, VersionFilterNewerVersion) {
+ std::unique_ptr<ResourceTable> table = BuildTable();
+
+ LoadedApk apk = {{"test.apk"}, {}, std::move(table), {}};
+ std::unique_ptr<IAaptContext> ctx = test::ContextBuilder().SetMinSdkVersion(19).Build();
+ FilterChain chain;
+
+ OutputArtifact artifact = test::ArtifactBuilder().AddDensity(xhdpi_).SetAndroidSdk(23).Build();
+
+ MultiApkGeneratorWrapper generator{&apk, ctx.get()};
+ std::unique_ptr<ResourceTable> split =
+ generator.FilterTable(ctx.get(), artifact, *apk.GetResourceTable(), &chain);
+
+ ResourceTable* new_table = split.get();
+ EXPECT_THAT(ValueForConfig(new_table, mdpi_), IsNull());
+ EXPECT_THAT(ValueForConfig(new_table, hdpi_), IsNull());
+ EXPECT_THAT(ValueForConfig(new_table, xxhdpi_), IsNull());
+ EXPECT_THAT(ValueForConfig(new_table, xxxhdpi_), IsNull());
+ EXPECT_THAT(ValueForConfig(new_table, v19_), IsNull());
+
+ // xhdpi directly matches one of the required dimensions.
+ EXPECT_THAT(ValueForConfig(new_table, xhdpi_), NotNull());
+ // drawable-v21 was converted to drawable.
+ EXPECT_THAT(ValueForConfig(new_table, default_), NotNull());
+ EXPECT_THAT(GetValue<Id>(new_table, "android:string/one"), NotNull());
+}
+
+TEST_F(MultiApkGeneratorTest, VersionFilterOlderVersion) {
+ std::unique_ptr<ResourceTable> table = BuildTable();
+
+ LoadedApk apk = {{"test.apk"}, {}, std::move(table), {}};
+ std::unique_ptr<IAaptContext> ctx = test::ContextBuilder().SetMinSdkVersion(1).Build();
+ FilterChain chain;
+
+ OutputArtifact artifact = test::ArtifactBuilder().AddDensity(xhdpi_).SetAndroidSdk(4).Build();
+
+ MultiApkGeneratorWrapper generator{&apk, ctx.get()};;
+ std::unique_ptr<ResourceTable> split =
+ generator.FilterTable(ctx.get(), artifact, *apk.GetResourceTable(), &chain);
+
+ ResourceTable* new_table = split.get();
+ EXPECT_THAT(ValueForConfig(new_table, mdpi_), IsNull());
+ EXPECT_THAT(ValueForConfig(new_table, hdpi_), IsNull());
+ EXPECT_THAT(ValueForConfig(new_table, xxhdpi_), IsNull());
+ EXPECT_THAT(ValueForConfig(new_table, xxxhdpi_), IsNull());
+
+ EXPECT_THAT(ValueForConfig(new_table, xhdpi_), NotNull());
+ EXPECT_THAT(ValueForConfig(new_table, v19_), NotNull());
+ EXPECT_THAT(ValueForConfig(new_table, v21_), NotNull());
+ EXPECT_THAT(GetValue<Id>(new_table, "android:string/one"), NotNull());
+}
+
+TEST_F(MultiApkGeneratorTest, VersionFilterNoVersion) {
+ std::unique_ptr<ResourceTable> table = BuildTable();
+
+ LoadedApk apk = {{"test.apk"}, {}, std::move(table), {}};
+ std::unique_ptr<IAaptContext> ctx = test::ContextBuilder().SetMinSdkVersion(1).Build();
+ FilterChain chain;
+
+ OutputArtifact artifact = test::ArtifactBuilder().AddDensity(xhdpi_).Build();
+
+ MultiApkGeneratorWrapper generator{&apk, ctx.get()};
+ std::unique_ptr<ResourceTable> split =
+ generator.FilterTable(ctx.get(), artifact, *apk.GetResourceTable(), &chain);
+
+ ResourceTable* new_table = split.get();
+ EXPECT_THAT(ValueForConfig(new_table, mdpi_), IsNull());
+ EXPECT_THAT(ValueForConfig(new_table, hdpi_), IsNull());
+ EXPECT_THAT(ValueForConfig(new_table, xxhdpi_), IsNull());
+ EXPECT_THAT(ValueForConfig(new_table, xxxhdpi_), IsNull());
+
+ EXPECT_THAT(ValueForConfig(new_table, xhdpi_), NotNull());
+ EXPECT_THAT(ValueForConfig(new_table, v19_), NotNull());
+ EXPECT_THAT(ValueForConfig(new_table, v21_), NotNull());
+ EXPECT_THAT(GetValue<Id>(new_table, "android:string/one"), NotNull());
+}
+
+} // namespace
+} // namespace aapt
diff --git a/tools/aapt2/optimize/VersionCollapser.cpp b/tools/aapt2/optimize/VersionCollapser.cpp
index d941b487e439..cc1fc1e6910b 100644
--- a/tools/aapt2/optimize/VersionCollapser.cpp
+++ b/tools/aapt2/optimize/VersionCollapser.cpp
@@ -63,11 +63,9 @@ FilterIterator<Iterator, Pred> make_filter_iterator(Iterator begin,
}
/**
- * Every Configuration with an SDK version specified that is less than minSdk
- * will be removed.
- * The exception is when there is no exact matching resource for the minSdk. The
- * next smallest
- * one will be kept.
+ * Every Configuration with an SDK version specified that is less than minSdk will be removed. The
+ * exception is when there is no exact matching resource for the minSdk. The next smallest one will
+ * be kept.
*/
static void CollapseVersions(int min_sdk, ResourceEntry* entry) {
// First look for all sdks less than minSdk.
@@ -80,11 +78,9 @@ static void CollapseVersions(int min_sdk, ResourceEntry* entry) {
const ConfigDescription& config = (*iter)->config;
if (config.sdkVersion <= min_sdk) {
- // This is the first configuration we've found with a smaller or equal SDK
- // level
- // to the minimum. We MUST keep this one, but remove all others we find,
- // which get
- // overridden by this one.
+ // This is the first configuration we've found with a smaller or equal SDK level to the
+ // minimum. We MUST keep this one, but remove all others we find, which get overridden by this
+ // one.
ConfigDescription config_without_sdk = config.CopyWithoutSdkVersion();
auto pred = [&](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
@@ -115,11 +111,9 @@ static void CollapseVersions(int min_sdk, ResourceEntry* entry) {
-> bool { return val == nullptr; }),
entry->values.end());
- // Strip the version qualifiers for every resource with version <= minSdk.
- // This will ensure
- // that the resource entries are all packed together in the same ResTable_type
- // struct
- // and take up less space in the resources.arsc table.
+ // Strip the version qualifiers for every resource with version <= minSdk. This will ensure that
+ // the resource entries are all packed together in the same ResTable_type struct and take up less
+ // space in the resources.arsc table.
bool modified = false;
for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
if (config_value->config.sdkVersion != 0 &&
@@ -137,8 +131,8 @@ static void CollapseVersions(int min_sdk, ResourceEntry* entry) {
}
if (modified) {
- // We've modified the keys (ConfigDescription) by changing the sdkVersion to
- // 0. We MUST re-sort to ensure ordering guarantees hold.
+ // We've modified the keys (ConfigDescription) by changing the sdkVersion to 0. We MUST re-sort
+ // to ensure ordering guarantees hold.
std::sort(entry->values.begin(), entry->values.end(),
[](const std::unique_ptr<ResourceConfigValue>& a,
const std::unique_ptr<ResourceConfigValue>& b) -> bool {