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/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()