AAPT2: Reorder link steps

AAPT2's link steps were writing the res/* files before optimizations to the
resource table were performed (such as version collapsing). This was causing
unreferenced res/* files to remain in the archive, even though they're no
longer necessary.

Bug: 30705740
Change-Id: I50d7943ef7429b14e46587093855c15d54038299
diff --git a/tools/aapt2/link/Link.cpp b/tools/aapt2/link/Link.cpp
index 45472ff..acb0f38 100644
--- a/tools/aapt2/link/Link.cpp
+++ b/tools/aapt2/link/Link.cpp
@@ -1191,6 +1191,12 @@
                                                                      mContext->getDiagnostics())) {
             AppInfo& appInfo = maybeAppInfo.value();
             mContext->setCompilationPackage(appInfo.package);
+            if (appInfo.minSdkVersion) {
+                if (Maybe<int> maybeMinSdkVersion =
+                        ResourceUtils::tryParseSdkVersion(appInfo.minSdkVersion.value())) {
+                    mContext->setMinSdkVersion(maybeMinSdkVersion.value());
+                }
+            }
         } else {
             return 1;
         }
@@ -1393,32 +1399,6 @@
             return 1;
         }
 
-        // Must come before ResourceFileFlattener as ResourceFileFlattener
-        // relies on the minSdkVersion to properly flatten files.
-        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());
-            }
-        }
-
-        ResourceFileFlattenerOptions fileFlattenerOptions;
-        fileFlattenerOptions.keepRawValues = mOptions.staticLib;
-        fileFlattenerOptions.doNotCompressAnything = mOptions.doNotCompressAnything;
-        fileFlattenerOptions.extensionsToNotCompress = mOptions.extensionsToNotCompress;
-        fileFlattenerOptions.noAutoVersion = mOptions.noAutoVersion;
-        fileFlattenerOptions.noVersionVectors = mOptions.noVersionVectors;
-        fileFlattenerOptions.updateProguardSpec =
-                static_cast<bool>(mOptions.generateProguardRulesPath);
-        ResourceFileFlattener fileFlattener(fileFlattenerOptions, mContext, &proguardKeepSet);
-
-        if (!fileFlattener.flatten(&mFinalTable, archiveWriter.get())) {
-            mContext->getDiagnostics()->error(DiagMessage() << "failed linking file resources");
-            return 1;
-        }
-
         if (!mOptions.noAutoVersion) {
             AutoVersioner versioner;
             if (!versioner.consume(mContext, &mFinalTable)) {
@@ -1440,6 +1420,23 @@
             }
         }
 
+        // Write out the table to an archive. Optimizations to the table should come before this
+        // step.
+        ResourceFileFlattenerOptions fileFlattenerOptions;
+        fileFlattenerOptions.keepRawValues = mOptions.staticLib;
+        fileFlattenerOptions.doNotCompressAnything = mOptions.doNotCompressAnything;
+        fileFlattenerOptions.extensionsToNotCompress = mOptions.extensionsToNotCompress;
+        fileFlattenerOptions.noAutoVersion = mOptions.noAutoVersion;
+        fileFlattenerOptions.noVersionVectors = mOptions.noVersionVectors;
+        fileFlattenerOptions.updateProguardSpec =
+                static_cast<bool>(mOptions.generateProguardRulesPath);
+        ResourceFileFlattener fileFlattener(fileFlattenerOptions, mContext, &proguardKeepSet);
+
+        if (!fileFlattener.flatten(&mFinalTable, archiveWriter.get())) {
+            mContext->getDiagnostics()->error(DiagMessage() << "failed linking file resources");
+            return 1;
+        }
+
         if (mOptions.staticLib) {
             if (!flattenTableToPb(&mFinalTable, archiveWriter.get())) {
                 mContext->getDiagnostics()->error(DiagMessage()