AAPT2: Add version collapsing
When an app specifies (or imports) resources with various
configurations for different SDK versions, specifying
a minSdk will make many of those resources unreachable.
Version collapsing will prune out the resources specified
for SDK versions less than the minSdk.
If, however, there is no exact matching resource for the
minSdk version, the next smallest SDK version is kept.
Change-Id: Ic7bcab6c59d65c97c67c8767358abb57cdec60a4
diff --git a/tools/aapt2/link/AutoVersioner.cpp b/tools/aapt2/link/AutoVersioner.cpp
index 459c330..8ed27c3 100644
--- a/tools/aapt2/link/AutoVersioner.cpp
+++ b/tools/aapt2/link/AutoVersioner.cpp
@@ -27,7 +27,9 @@
bool shouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config,
const int sdkVersionToGenerate) {
+ // We assume the caller is trying to generate a version greater than the current configuration.
assert(sdkVersionToGenerate > config.sdkVersion);
+
const auto endIter = entry->values.end();
auto iter = entry->values.begin();
for (; iter != endIter; ++iter) {
diff --git a/tools/aapt2/link/AutoVersioner_test.cpp b/tools/aapt2/link/AutoVersioner_test.cpp
index 9b3a87c..99a7c02 100644
--- a/tools/aapt2/link/AutoVersioner_test.cpp
+++ b/tools/aapt2/link/AutoVersioner_test.cpp
@@ -16,10 +16,7 @@
#include "ConfigDescription.h"
#include "link/Linkers.h"
-#include "test/Builders.h"
-#include "test/Context.h"
-
-#include <gtest/gtest.h>
+#include "test/Test.h"
namespace aapt {
@@ -54,7 +51,7 @@
TEST(AutoVersionerTest, VersionStylesForTable) {
std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
.setPackageId(u"app", 0x7f)
- .addValue(u"@app:style/Foo", ResourceId(0x7f020000), test::parseConfigOrDie("v4"),
+ .addValue(u"@app:style/Foo", test::parseConfigOrDie("v4"), ResourceId(0x7f020000),
test::StyleBuilder()
.addItem(u"@android:attr/onClick", ResourceId(0x0101026f),
util::make_unique<Id>())
@@ -65,7 +62,7 @@
.addItem(u"@android:attr/colorAccent", ResourceId(0x01010435),
util::make_unique<Id>())
.build())
- .addValue(u"@app:style/Foo", ResourceId(0x7f020000), test::parseConfigOrDie("v21"),
+ .addValue(u"@app:style/Foo", test::parseConfigOrDie("v21"), ResourceId(0x7f020000),
test::StyleBuilder()
.addItem(u"@android:attr/paddingEnd", ResourceId(0x010103b4),
util::make_unique<Id>())
diff --git a/tools/aapt2/link/Link.cpp b/tools/aapt2/link/Link.cpp
index 4767bc9..9411053 100644
--- a/tools/aapt2/link/Link.cpp
+++ b/tools/aapt2/link/Link.cpp
@@ -123,6 +123,14 @@
mVerbose = val;
}
+ int getMinSdkVersion() override {
+ return mMinSdkVersion;
+ }
+
+ void setMinSdkVersion(int minSdk) {
+ mMinSdkVersion = minSdk;
+ }
+
private:
StdErrDiagnostics mDiagnostics;
NameMangler mNameMangler;
@@ -130,6 +138,7 @@
uint8_t mPackageId = 0x0;
SymbolTable mSymbols;
bool mVerbose = false;
+ int mMinSdkVersion = 0;
};
static bool copyFileToArchive(io::IFile* file, const std::string& outPath,
@@ -578,14 +587,33 @@
return true;
}
- Maybe<AppInfo> extractAppInfoFromManifest(xml::XmlResource* xmlRes) {
+ Maybe<AppInfo> extractAppInfoFromManifest(xml::XmlResource* xmlRes, IDiagnostics* diag) {
// Make sure the first element is <manifest> with package attribute.
if (xml::Element* manifestEl = xml::findRootElement(xmlRes->root.get())) {
- if (manifestEl->namespaceUri.empty() && manifestEl->name == u"manifest") {
- if (xml::Attribute* packageAttr = manifestEl->findAttribute({}, u"package")) {
- return AppInfo{ packageAttr->value };
+ AppInfo appInfo;
+
+ if (!manifestEl->namespaceUri.empty() || manifestEl->name != u"manifest") {
+ diag->error(DiagMessage(xmlRes->file.source) << "root tag must be <manifest>");
+ return {};
+ }
+
+ xml::Attribute* packageAttr = manifestEl->findAttribute({}, u"package");
+ if (!packageAttr) {
+ diag->error(DiagMessage(xmlRes->file.source)
+ << "<manifest> must have a 'package' attribute");
+ return {};
+ }
+
+ appInfo.package = packageAttr->value;
+
+ if (xml::Element* usesSdkEl = manifestEl->findChild({}, u"uses-sdk")) {
+ if (xml::Attribute* minSdk =
+ usesSdkEl->findAttribute(xml::kSchemaAndroid, u"minSdkVersion")) {
+ appInfo.minSdkVersion = minSdk->value;
}
}
+
+ return appInfo;
}
return {};
}
@@ -1089,11 +1117,11 @@
return 1;
}
- if (Maybe<AppInfo> maybeAppInfo = extractAppInfoFromManifest(manifestXml.get())) {
- mContext->setCompilationPackage(maybeAppInfo.value().package);
+ if (Maybe<AppInfo> maybeAppInfo = extractAppInfoFromManifest(manifestXml.get(),
+ mContext->getDiagnostics())) {
+ AppInfo& appInfo = maybeAppInfo.value();
+ mContext->setCompilationPackage(appInfo.package);
} else {
- mContext->getDiagnostics()->error(DiagMessage(mOptions.manifestPath)
- << "no package specified in <manifest> tag");
return 1;
}
@@ -1297,6 +1325,28 @@
}
}
+ Maybe<AppInfo> maybeAppInfo = extractAppInfoFromManifest(manifestXml.get(),
+ mContext->getDiagnostics());
+ if (maybeAppInfo && maybeAppInfo.value().minSdkVersion) {
+ if (Maybe<int> maybeMinSdkVersion =
+ ResourceUtils::tryParseSdkVersion(maybeAppInfo.value().minSdkVersion.value())) {
+ mContext->setMinSdkVersion(maybeMinSdkVersion.value());
+ }
+ }
+
+ if (!mOptions.staticLib && mContext->getMinSdkVersion() > 0) {
+ if (mContext->verbose()) {
+ mContext->getDiagnostics()->note(
+ DiagMessage() << "collapsing resource versions for minimum SDK "
+ << mContext->getMinSdkVersion());
+ }
+
+ VersionCollapser collapser;
+ if (!collapser.consume(mContext, &mFinalTable)) {
+ return 1;
+ }
+ }
+
if (mOptions.staticLib) {
if (!flattenTableToPb(&mFinalTable, archiveWriter.get())) {
mContext->getDiagnostics()->error(DiagMessage()
diff --git a/tools/aapt2/link/Linkers.h b/tools/aapt2/link/Linkers.h
index ec532ab..43b8fb4 100644
--- a/tools/aapt2/link/Linkers.h
+++ b/tools/aapt2/link/Linkers.h
@@ -44,14 +44,21 @@
bool shouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config,
const int sdkVersionToGenerate);
-struct AutoVersioner : public IResourceTableConsumer {
+class AutoVersioner : public IResourceTableConsumer {
+public:
bool consume(IAaptContext* context, ResourceTable* table) override;
};
-struct XmlAutoVersioner : public IXmlResourceConsumer {
+class XmlAutoVersioner : public IXmlResourceConsumer {
+public:
bool consume(IAaptContext* context, xml::XmlResource* resource) override;
};
+class VersionCollapser : public IResourceTableConsumer {
+public:
+ bool consume(IAaptContext* context, ResourceTable* table) override;
+};
+
/**
* If any attribute resource values are defined as public, this consumer will move all private
* attribute resource values to a private ^private-attr type, avoiding backwards compatibility
diff --git a/tools/aapt2/link/VersionCollapser.cpp b/tools/aapt2/link/VersionCollapser.cpp
new file mode 100644
index 0000000..c0e96be
--- /dev/null
+++ b/tools/aapt2/link/VersionCollapser.cpp
@@ -0,0 +1,126 @@
+/*
+ * 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 "ResourceTable.h"
+#include "link/Linkers.h"
+
+#include <algorithm>
+#include <vector>
+
+namespace aapt {
+
+template <typename Iterator, typename Pred>
+class FilterIterator {
+public:
+ FilterIterator(Iterator begin, Iterator end, Pred pred=Pred()) :
+ mCurrent(begin), mEnd(end), mPred(pred) {
+ advance();
+ }
+
+ bool hasNext() {
+ return mCurrent != mEnd;
+ }
+
+ Iterator nextIter() {
+ Iterator iter = mCurrent;
+ ++mCurrent;
+ advance();
+ return iter;
+ }
+
+ typename Iterator::reference next() {
+ return *nextIter();
+ }
+
+private:
+ void advance() {
+ for (; mCurrent != mEnd; ++mCurrent) {
+ if (mPred(*mCurrent)) {
+ return;
+ }
+ }
+ }
+
+ Iterator mCurrent, mEnd;
+ Pred mPred;
+};
+
+template <typename Iterator, typename Pred>
+FilterIterator<Iterator, Pred> makeFilterIterator(Iterator begin, Iterator end=Iterator(),
+ Pred pred=Pred()) {
+ return FilterIterator<Iterator, Pred>(begin, end, pred);
+}
+
+/**
+ * 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 minSdk, ResourceEntry* entry) {
+ // First look for all sdks less than minSdk.
+ for (auto iter = entry->values.rbegin(); iter != entry->values.rend(); ++iter) {
+ // Check if the item was already marked for removal.
+ if (!(*iter)) {
+ continue;
+ }
+
+ const ConfigDescription& config = (*iter)->config;
+ if (config.sdkVersion <= minSdk) {
+ // 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 configWithoutSdk = config;
+ configWithoutSdk.sdkVersion = 0;
+ auto pred = [&](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
+ // Check that the value hasn't already been marked for removal.
+ if (!val) {
+ return false;
+ }
+
+ // Only return Configs that differ in SDK version.
+ configWithoutSdk.sdkVersion = val->config.sdkVersion;
+ return configWithoutSdk == val->config && val->config.sdkVersion <= minSdk;
+ };
+
+ // Remove the rest that match.
+ auto filterIter = makeFilterIterator(iter + 1, entry->values.rend(), pred);
+ while (filterIter.hasNext()) {
+ filterIter.next() = {};
+ }
+ }
+ }
+
+ // Now erase the nullptr values.
+ entry->values.erase(std::remove_if(entry->values.begin(), entry->values.end(),
+ [](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
+ return val == nullptr;
+ }), entry->values.end());
+}
+
+bool VersionCollapser::consume(IAaptContext* context, ResourceTable* table) {
+ const int minSdk = context->getMinSdkVersion();
+ for (auto& package : table->packages) {
+ for (auto& type : package->types) {
+ for (auto& entry : type->entries) {
+ collapseVersions(minSdk, entry.get());
+ }
+ }
+ }
+ return true;
+}
+
+} // namespace aapt
diff --git a/tools/aapt2/link/VersionCollapser_test.cpp b/tools/aapt2/link/VersionCollapser_test.cpp
new file mode 100644
index 0000000..b5387fe
--- /dev/null
+++ b/tools/aapt2/link/VersionCollapser_test.cpp
@@ -0,0 +1,93 @@
+/*
+ * 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 "link/Linkers.h"
+#include "test/Test.h"
+
+namespace aapt {
+
+template <typename T>
+using uptr = std::unique_ptr<T>;
+
+static uptr<ResourceTable> buildTableWithConfigs(const StringPiece16& name,
+ std::initializer_list<std::string> list) {
+ test::ResourceTableBuilder builder;
+ for (const std::string& item : list) {
+ builder.addSimple(name, test::parseConfigOrDie(item));
+ }
+ return builder.build();
+}
+
+TEST(VersionCollapserTest, CollapseVersions) {
+ uptr<IAaptContext> context = test::ContextBuilder().setMinSdkVersion(7).build();
+
+ const StringPiece16 resName = u"@android:string/foo";
+
+ uptr<ResourceTable> table =
+ buildTableWithConfigs(resName,
+ { "land-v4", "land-v5", "sw600dp", "land-v6",
+ "land-v14", "land-v21" });
+
+ VersionCollapser collapser;
+ ASSERT_TRUE(collapser.consume(context.get(), table.get()));
+
+ // These should be removed.
+ EXPECT_EQ(nullptr,
+ test::getValueForConfig<Id>(table.get(), resName, test::parseConfigOrDie("land-v4")));
+ EXPECT_EQ(nullptr,
+ test::getValueForConfig<Id>(table.get(), resName, test::parseConfigOrDie("land-v5")));
+
+ // These should remain.
+ EXPECT_NE(nullptr,
+ test::getValueForConfig<Id>(table.get(), resName, test::parseConfigOrDie("sw600dp")));
+ EXPECT_NE(nullptr,
+ test::getValueForConfig<Id>(table.get(), resName, test::parseConfigOrDie("land-v6")));
+ EXPECT_NE(nullptr,
+ test::getValueForConfig<Id>(table.get(), resName, test::parseConfigOrDie("land-v14")));
+ EXPECT_NE(nullptr,
+ test::getValueForConfig<Id>(table.get(), resName, test::parseConfigOrDie("land-v21")));
+}
+
+TEST(VersionCollapserTest, CollapseVersionsWhenMinSdkIsHighest) {
+ uptr<IAaptContext> context = test::ContextBuilder().setMinSdkVersion(26).build();
+
+ const StringPiece16 resName = u"@android:string/foo";
+
+ uptr<ResourceTable> table =
+ buildTableWithConfigs(resName,
+ { "land-v4", "land-v5", "sw600dp", "land-v6",
+ "land-v14", "land-v21" });
+ VersionCollapser collapser;
+ ASSERT_TRUE(collapser.consume(context.get(), table.get()));
+
+ // These should all be removed.
+ EXPECT_EQ(nullptr,
+ test::getValueForConfig<Id>(table.get(), resName, test::parseConfigOrDie("land-v4")));
+ EXPECT_EQ(nullptr,
+ test::getValueForConfig<Id>(table.get(), resName, test::parseConfigOrDie("land-v5")));
+ EXPECT_EQ(nullptr,
+ test::getValueForConfig<Id>(table.get(), resName, test::parseConfigOrDie("land-v6")));
+ EXPECT_EQ(nullptr,
+ test::getValueForConfig<Id>(table.get(), resName, test::parseConfigOrDie("land-v14")));
+
+ // These should remain.
+ EXPECT_NE(nullptr,
+ test::getValueForConfig<Id>(table.get(), resName, test::parseConfigOrDie("sw600dp")));
+ EXPECT_NE(nullptr,
+ test::getValueForConfig<Id>(table.get(), resName, test::parseConfigOrDie("land-v21")));
+}
+
+} // namespace aapt