AAPT2: Implement density stripping and initial Split support
When a preferred density is supplied, the closest matching densities
will be selected, the rest stripped from the APK.
Split support will be enabled in a later CL. Command line support is still
needed, but the foundation is ready.
Bug:25958912
Change-Id: I56d599806b4ec4ffa24e17aad48d47130ca05c08
diff --git a/tools/aapt2/split/TableSplitter.cpp b/tools/aapt2/split/TableSplitter.cpp
new file mode 100644
index 0000000..0f7649b
--- /dev/null
+++ b/tools/aapt2/split/TableSplitter.cpp
@@ -0,0 +1,264 @@
+/*
+ * Copyright (C) 2016 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 "ConfigDescription.h"
+#include "ResourceTable.h"
+#include "split/TableSplitter.h"
+
+#include <map>
+#include <set>
+#include <unordered_map>
+#include <vector>
+
+namespace aapt {
+
+using ConfigClaimedMap = std::unordered_map<ResourceConfigValue*, bool>;
+using ConfigDensityGroups = std::map<ConfigDescription, std::vector<ResourceConfigValue*>>;
+
+static ConfigDescription copyWithoutDensity(const ConfigDescription& config) {
+ ConfigDescription withoutDensity = config;
+ withoutDensity.density = 0;
+ return withoutDensity;
+}
+
+/**
+ * Selects values that match exactly the constraints given.
+ */
+class SplitValueSelector {
+public:
+ SplitValueSelector(const SplitConstraints& constraints) {
+ for (const ConfigDescription& config : constraints.configs) {
+ if (config.density == 0) {
+ mDensityIndependentConfigs.insert(config);
+ } else {
+ mDensityDependentConfigToDensityMap[copyWithoutDensity(config)] = config.density;
+ }
+ }
+ }
+
+ std::vector<ResourceConfigValue*> selectValues(const ConfigDensityGroups& densityGroups,
+ ConfigClaimedMap* claimedValues) {
+ std::vector<ResourceConfigValue*> selected;
+
+ // Select the regular values.
+ for (auto& entry : *claimedValues) {
+ // Check if the entry has a density.
+ ResourceConfigValue* configValue = entry.first;
+ if (configValue->config.density == 0 && !entry.second) {
+ // This is still available.
+ if (mDensityIndependentConfigs.find(configValue->config) !=
+ mDensityIndependentConfigs.end()) {
+ selected.push_back(configValue);
+
+ // Mark the entry as taken.
+ entry.second = true;
+ }
+ }
+ }
+
+ // Now examine the densities
+ for (auto& entry : densityGroups) {
+ // We do not care if the value is claimed, since density values can be
+ // in multiple splits.
+ const ConfigDescription& config = entry.first;
+ const std::vector<ResourceConfigValue*>& relatedValues = entry.second;
+
+ auto densityValueIter = mDensityDependentConfigToDensityMap.find(config);
+ if (densityValueIter != mDensityDependentConfigToDensityMap.end()) {
+ // Select the best one!
+ ConfigDescription targetDensity = config;
+ targetDensity.density = densityValueIter->second;
+
+ ResourceConfigValue* bestValue = nullptr;
+ for (ResourceConfigValue* thisValue : relatedValues) {
+ if (!bestValue ||
+ thisValue->config.isBetterThan(bestValue->config, &targetDensity)) {
+ bestValue = thisValue;
+ }
+
+ // When we select one of these, they are all claimed such that the base
+ // doesn't include any anymore.
+ (*claimedValues)[thisValue] = true;
+ }
+ assert(bestValue);
+ selected.push_back(bestValue);
+ }
+ }
+ return selected;
+ }
+
+private:
+ std::set<ConfigDescription> mDensityIndependentConfigs;
+ std::map<ConfigDescription, uint16_t> mDensityDependentConfigToDensityMap;
+};
+
+/**
+ * Marking non-preferred densities as claimed will make sure the base doesn't include them,
+ * leaving only the preferred density behind.
+ */
+static void markNonPreferredDensitiesAsClaimed(uint16_t preferredDensity,
+ const ConfigDensityGroups& densityGroups,
+ ConfigClaimedMap* configClaimedMap) {
+ for (auto& entry : densityGroups) {
+ const ConfigDescription& config = entry.first;
+ const std::vector<ResourceConfigValue*>& relatedValues = entry.second;
+
+ ConfigDescription targetDensity = config;
+ targetDensity.density = preferredDensity;
+ ResourceConfigValue* bestValue = nullptr;
+ for (ResourceConfigValue* thisValue : relatedValues) {
+ if (!bestValue) {
+ bestValue = thisValue;
+ } else if (thisValue->config.isBetterThan(bestValue->config, &targetDensity)) {
+ // Claim the previous value so that it is not included in the base.
+ (*configClaimedMap)[bestValue] = true;
+ bestValue = thisValue;
+ } else {
+ // Claim this value so that it is not included in the base.
+ (*configClaimedMap)[thisValue] = true;
+ }
+ }
+ assert(bestValue);
+ }
+}
+
+bool TableSplitter::verifySplitConstraints(IAaptContext* context) {
+ bool error = false;
+ for (size_t i = 0; i < mSplitConstraints.size(); i++) {
+ for (size_t j = i + 1; j < mSplitConstraints.size(); j++) {
+ for (const ConfigDescription& config : mSplitConstraints[i].configs) {
+ if (mSplitConstraints[j].configs.find(config) !=
+ mSplitConstraints[j].configs.end()) {
+ context->getDiagnostics()->error(DiagMessage() << "config '" << config
+ << "' appears in multiple splits, "
+ << "target split ambiguous");
+ error = true;
+ }
+ }
+ }
+ }
+ return !error;
+}
+
+void TableSplitter::splitTable(ResourceTable* originalTable) {
+ const size_t splitCount = mSplitConstraints.size();
+ for (auto& pkg : originalTable->packages) {
+ // Initialize all packages for splits.
+ for (size_t idx = 0; idx < splitCount; idx++) {
+ ResourceTable* splitTable = mSplits[idx].get();
+ splitTable->createPackage(pkg->name, pkg->id);
+ }
+
+ for (auto& type : pkg->types) {
+ if (type->type == ResourceType::kMipmap) {
+ // Always keep mipmaps.
+ continue;
+ }
+
+ for (auto& entry : type->entries) {
+ if (mConfigFilter) {
+ // First eliminate any resource that we definitely don't want.
+ for (std::unique_ptr<ResourceConfigValue>& configValue : entry->values) {
+ if (!mConfigFilter->match(configValue->config)) {
+ // null out the entry. We will clean up and remove nulls at the end
+ // for performance reasons.
+ configValue.reset();
+ }
+ }
+ }
+
+ // Organize the values into two separate buckets. Those that are density-dependent
+ // and those that are density-independent.
+ // One density technically matches all density, it's just that some densities
+ // match better. So we need to be aware of the full set of densities to make this
+ // decision.
+ ConfigDensityGroups densityGroups;
+ ConfigClaimedMap configClaimedMap;
+ for (const std::unique_ptr<ResourceConfigValue>& configValue : entry->values) {
+ if (configValue) {
+ configClaimedMap[configValue.get()] = false;
+
+ if (configValue->config.density != 0) {
+ // Create a bucket for this density-dependent config.
+ densityGroups[copyWithoutDensity(configValue->config)]
+ .push_back(configValue.get());
+ }
+ }
+ }
+
+ // First we check all the splits. If it doesn't match one of the splits, we
+ // leave it in the base.
+ for (size_t idx = 0; idx < splitCount; idx++) {
+ const SplitConstraints& splitConstraint = mSplitConstraints[idx];
+ ResourceTable* splitTable = mSplits[idx].get();
+
+ // Select the values we want from this entry for this split.
+ SplitValueSelector selector(splitConstraint);
+ std::vector<ResourceConfigValue*> selectedValues =
+ selector.selectValues(densityGroups, &configClaimedMap);
+
+ // No need to do any work if we selected nothing.
+ if (!selectedValues.empty()) {
+ // Create the same resource structure in the split. We do this lazily
+ // because we might not have actual values for each type/entry.
+ ResourceTablePackage* splitPkg = splitTable->findPackage(pkg->name);
+ ResourceTableType* splitType = splitPkg->findOrCreateType(type->type);
+ if (!splitType->id) {
+ splitType->id = type->id;
+ splitType->symbolStatus = type->symbolStatus;
+ }
+
+ ResourceEntry* splitEntry = splitType->findOrCreateEntry(entry->name);
+ if (!splitEntry->id) {
+ splitEntry->id = entry->id;
+ splitEntry->symbolStatus = entry->symbolStatus;
+ }
+
+ // Copy the selected values into the new Split Entry.
+ for (ResourceConfigValue* configValue : selectedValues) {
+ ResourceConfigValue* newConfigValue = splitEntry->findOrCreateValue(
+ configValue->config, configValue->product);
+ newConfigValue->value = std::unique_ptr<Value>(
+ configValue->value->clone(&splitTable->stringPool));
+ }
+ }
+ }
+
+ if (mPreferredDensity) {
+ markNonPreferredDensitiesAsClaimed(mPreferredDensity.value(),
+ densityGroups,
+ &configClaimedMap);
+ }
+
+ // All splits are handled, now check to see what wasn't claimed and remove
+ // whatever exists in other splits.
+ for (std::unique_ptr<ResourceConfigValue>& configValue : entry->values) {
+ if (configValue && configClaimedMap[configValue.get()]) {
+ // Claimed, remove from base.
+ configValue.reset();
+ }
+ }
+
+ // Now erase all nullptrs.
+ entry->values.erase(
+ std::remove(entry->values.begin(), entry->values.end(), nullptr),
+ entry->values.end());
+ }
+ }
+ }
+}
+
+} // namespace aapt
diff --git a/tools/aapt2/split/TableSplitter.h b/tools/aapt2/split/TableSplitter.h
new file mode 100644
index 0000000..15e0764
--- /dev/null
+++ b/tools/aapt2/split/TableSplitter.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2016 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 AAPT_SPLIT_TABLESPLITTER_H
+#define AAPT_SPLIT_TABLESPLITTER_H
+
+#include "ConfigDescription.h"
+#include "ResourceTable.h"
+#include "filter/ConfigFilter.h"
+#include "process/IResourceTableConsumer.h"
+
+#include <android-base/macros.h>
+#include <set>
+#include <vector>
+
+namespace aapt {
+
+struct SplitConstraints {
+ std::set<ConfigDescription> configs;
+};
+
+struct TableSplitterOptions {
+ /**
+ * The preferred density to keep in the table, stripping out all others.
+ */
+ Maybe<uint16_t> preferredDensity;
+
+ /**
+ * Configuration filter that determines which resource configuration values end up in
+ * the final table.
+ */
+ IConfigFilter* configFilter = nullptr;
+};
+
+class TableSplitter {
+public:
+ TableSplitter(const std::vector<SplitConstraints>& splits,
+ const TableSplitterOptions& options) :
+ mSplitConstraints(splits), mPreferredDensity(options.preferredDensity),
+ mConfigFilter(options.configFilter) {
+ for (size_t i = 0; i < mSplitConstraints.size(); i++) {
+ mSplits.push_back(util::make_unique<ResourceTable>());
+ }
+ }
+
+ bool verifySplitConstraints(IAaptContext* context);
+
+ void splitTable(ResourceTable* originalTable);
+
+ const std::vector<std::unique_ptr<ResourceTable>>& getSplits() {
+ return mSplits;
+ }
+
+private:
+ std::vector<SplitConstraints> mSplitConstraints;
+ std::vector<std::unique_ptr<ResourceTable>> mSplits;
+ Maybe<uint16_t> mPreferredDensity;
+ IConfigFilter* mConfigFilter;
+
+ DISALLOW_COPY_AND_ASSIGN(TableSplitter);
+};
+
+}
+
+#endif /* AAPT_SPLIT_TABLESPLITTER_H */
diff --git a/tools/aapt2/split/TableSplitter_test.cpp b/tools/aapt2/split/TableSplitter_test.cpp
new file mode 100644
index 0000000..74ca32e
--- /dev/null
+++ b/tools/aapt2/split/TableSplitter_test.cpp
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2016 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 "split/TableSplitter.h"
+#include "test/Builders.h"
+#include "test/Common.h"
+
+#include <gtest/gtest.h>
+
+namespace aapt {
+
+TEST(TableSplitterTest, NoSplitPreferredDensity) {
+ std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
+ .addFileReference(u"@android:drawable/icon", u"res/drawable-mdpi/icon.png",
+ test::parseConfigOrDie("mdpi"))
+ .addFileReference(u"@android:drawable/icon", u"res/drawable-hdpi/icon.png",
+ test::parseConfigOrDie("hdpi"))
+ .addFileReference(u"@android:drawable/icon", u"res/drawable-xhdpi/icon.png",
+ test::parseConfigOrDie("xhdpi"))
+ .addFileReference(u"@android:drawable/icon", u"res/drawable-xxhdpi/icon.png",
+ test::parseConfigOrDie("xxhdpi"))
+ .addSimple(u"@android:string/one", {})
+ .build();
+
+ TableSplitterOptions options;
+ options.preferredDensity = ConfigDescription::DENSITY_XHIGH;
+ TableSplitter splitter({}, options);
+ splitter.splitTable(table.get());
+
+ EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(),
+ u"@android:drawable/icon",
+ test::parseConfigOrDie("mdpi")));
+ EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(),
+ u"@android:drawable/icon",
+ test::parseConfigOrDie("hdpi")));
+ EXPECT_NE(nullptr, test::getValueForConfig<FileReference>(table.get(),
+ u"@android:drawable/icon",
+ test::parseConfigOrDie("xhdpi")));
+ EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(),
+ u"@android:drawable/icon",
+ test::parseConfigOrDie("xxhdpi")));
+ EXPECT_NE(nullptr, test::getValue<Id>(table.get(), u"@android:string/one"));
+}
+
+TEST(TableSplitterTest, SplitTableByConfigAndDensity) {
+ ResourceTable table;
+
+ const ResourceName foo = test::parseNameOrDie(u"@android:string/foo");
+ ASSERT_TRUE(table.addResource(foo, test::parseConfigOrDie("land-hdpi"), {},
+ util::make_unique<Id>(),
+ test::getDiagnostics()));
+ ASSERT_TRUE(table.addResource(foo, test::parseConfigOrDie("land-xhdpi"), {},
+ util::make_unique<Id>(),
+ test::getDiagnostics()));
+ ASSERT_TRUE(table.addResource(foo, test::parseConfigOrDie("land-xxhdpi"), {},
+ util::make_unique<Id>(),
+ test::getDiagnostics()));
+
+ std::vector<SplitConstraints> constraints;
+ constraints.push_back(SplitConstraints{ { test::parseConfigOrDie("land-mdpi") } });
+ constraints.push_back(SplitConstraints{ { test::parseConfigOrDie("land-xhdpi") } });
+
+ TableSplitter splitter(constraints, TableSplitterOptions{});
+ splitter.splitTable(&table);
+
+ ASSERT_EQ(2u, splitter.getSplits().size());
+
+ ResourceTable* splitOne = splitter.getSplits()[0].get();
+ ResourceTable* splitTwo = splitter.getSplits()[1].get();
+
+ // Since a split was defined, all densities should be gone from base.
+ EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, u"@android:string/foo",
+ test::parseConfigOrDie("land-hdpi")));
+ EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, u"@android:string/foo",
+ test::parseConfigOrDie("land-xhdpi")));
+ EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, u"@android:string/foo",
+ test::parseConfigOrDie("land-xxhdpi")));
+
+ EXPECT_NE(nullptr, test::getValueForConfig<Id>(splitOne, u"@android:string/foo",
+ test::parseConfigOrDie("land-hdpi")));
+ EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitOne, u"@android:string/foo",
+ test::parseConfigOrDie("land-xhdpi")));
+ EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitOne, u"@android:string/foo",
+ test::parseConfigOrDie("land-xxhdpi")));
+
+ EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitTwo, u"@android:string/foo",
+ test::parseConfigOrDie("land-hdpi")));
+ EXPECT_NE(nullptr, test::getValueForConfig<Id>(splitTwo, u"@android:string/foo",
+ test::parseConfigOrDie("land-xhdpi")));
+ EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitTwo, u"@android:string/foo",
+ test::parseConfigOrDie("land-xxhdpi")));
+}
+
+} // namespace aapt