diff --git a/tools/aapt2/link/Link.cpp b/tools/aapt2/link/Link.cpp index 45472fff5088a..acb0f38995cc3 100644 --- a/tools/aapt2/link/Link.cpp +++ b/tools/aapt2/link/Link.cpp @@ -1191,6 +1191,12 @@ public: mContext->getDiagnostics())) { AppInfo& appInfo = maybeAppInfo.value(); mContext->setCompilationPackage(appInfo.package); + if (appInfo.minSdkVersion) { + if (Maybe maybeMinSdkVersion = + ResourceUtils::tryParseSdkVersion(appInfo.minSdkVersion.value())) { + mContext->setMinSdkVersion(maybeMinSdkVersion.value()); + } + } } else { return 1; } @@ -1393,32 +1399,6 @@ public: return 1; } - // Must come before ResourceFileFlattener as ResourceFileFlattener - // relies on the minSdkVersion to properly flatten files. - Maybe maybeAppInfo = extractAppInfoFromManifest(manifestXml.get(), - mContext->getDiagnostics()); - if (maybeAppInfo && maybeAppInfo.value().minSdkVersion) { - if (Maybe 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(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 @@ public: } } + // 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(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()