Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 34314e83 authored by Rohit Agrawal's avatar Rohit Agrawal Committed by Android (Google) Code Review
Browse files

Merge "AAPT2: ProGuard config for components in main dex."

parents 70382849 e49bb30d
Loading
Loading
Loading
Loading
+26 −4
Original line number Diff line number Diff line
@@ -140,7 +140,8 @@ struct TransitionVisitor : public BaseVisitor {
};

struct ManifestVisitor : public BaseVisitor {
    ManifestVisitor(const Source& source, KeepSet* keepSet) : BaseVisitor(source, keepSet) {
    ManifestVisitor(const Source& source, KeepSet* keepSet, bool mainDexOnly)
            : BaseVisitor(source, keepSet), mMainDexOnly(mainDexOnly) {
    }

    virtual void visit(xml::Element* node) override {
@@ -161,6 +162,13 @@ struct ManifestVisitor : public BaseVisitor {
                        addClass(node->lineNumber, result.value());
                    }
                }
                if (mMainDexOnly) {
                    xml::Attribute* defaultProcess = node->findAttribute(xml::kSchemaAndroid,
                                                                         u"process");
                    if (defaultProcess) {
                        mDefaultProcess = defaultProcess->value;
                    }
                }
            } else if (node->name == u"activity" || node->name == u"service" ||
                    node->name == u"receiver" || node->name == u"provider" ||
                    node->name == u"instrumentation") {
@@ -169,7 +177,18 @@ struct ManifestVisitor : public BaseVisitor {

            if (getName) {
                xml::Attribute* attr = node->findAttribute(xml::kSchemaAndroid, u"name");
                if (attr) {
                getName = attr != nullptr;

                if (getName && mMainDexOnly) {
                    xml::Attribute* componentProcess = node->findAttribute(xml::kSchemaAndroid,
                                                                           u"process");

                    const std::u16string& process = componentProcess ? componentProcess->value
                                                                     : mDefaultProcess;
                    getName = !process.empty() && process[0] != u':';
                }

                if (getName) {
                    Maybe<std::u16string> result = util::getFullyQualifiedClassName(mPackage,
                                                                                    attr->value);
                    if (result) {
@@ -181,12 +200,15 @@ struct ManifestVisitor : public BaseVisitor {
        BaseVisitor::visit(node);
    }

private:
    std::u16string mPackage;
    const bool mMainDexOnly;
    std::u16string mDefaultProcess;
};

bool collectProguardRulesForManifest(const Source& source, xml::XmlResource* res,
                                     KeepSet* keepSet) {
    ManifestVisitor visitor(source, keepSet);
                                     KeepSet* keepSet, bool mainDexOnly) {
    ManifestVisitor visitor(source, keepSet, mainDexOnly);
    if (res->root) {
        res->root->accept(&visitor);
        return true;
+2 −1
Original line number Diff line number Diff line
@@ -46,7 +46,8 @@ private:
    std::map<std::u16string, std::set<Source>> mKeepMethodSet;
};

bool collectProguardRulesForManifest(const Source& source, xml::XmlResource* res, KeepSet* keepSet);
bool collectProguardRulesForManifest(const Source& source, xml::XmlResource* res, KeepSet* keepSet,
                                     bool mainDexOnly = false);
bool collectProguardRules(const Source& source, xml::XmlResource* res, KeepSet* keepSet);

bool writeKeepSet(std::ostream* out, const KeepSet& keepSet);
+31 −12
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ struct LinkOptions {
    Maybe<std::u16string> customJavaPackage;
    std::set<std::u16string> extraJavaPackages;
    Maybe<std::string> generateProguardRulesPath;
    Maybe<std::string> generateMainDexProguardRulesPath;
    bool noAutoVersion = false;
    bool noVersionVectors = false;
    bool staticLib = false;
@@ -282,6 +283,7 @@ struct ResourceFileFlattenerOptions {
    bool noVersionVectors = false;
    bool keepRawValues = false;
    bool doNotCompressAnything = false;
    bool updateProguardSpec = false;
    std::vector<std::string> extensionsToNotCompress;
};

@@ -363,8 +365,8 @@ bool ResourceFileFlattener::linkAndVersionXmlFile(const ResourceEntry* entry,
        return false;
    }

    if (!proguard::collectProguardRules(outFileOp->xmlToFlatten->file.source,
                                        outFileOp->xmlToFlatten.get(), mKeepSet)) {
    if (mOptions.updateProguardSpec && !proguard::collectProguardRules(
            outFileOp->xmlToFlatten->file.source, outFileOp->xmlToFlatten.get(), mKeepSet)) {
        return false;
    }

@@ -811,12 +813,12 @@ public:
        return true;
    }

    bool writeProguardFile(const proguard::KeepSet& keepSet) {
        if (!mOptions.generateProguardRulesPath) {
    bool writeProguardFile(const Maybe<std::string>& out, const proguard::KeepSet& keepSet) {
        if (!out) {
            return true;
        }

        const std::string& outPath = mOptions.generateProguardRulesPath.value();
        const std::string& outPath = out.value();
        std::ofstream fout(outPath, std::ofstream::binary);
        if (!fout) {
            mContext->getDiagnostics()->error(
@@ -1213,6 +1215,7 @@ public:
        }

        proguard::KeepSet proguardKeepSet;
        proguard::KeepSet proguardMainDexKeepSet;

        std::unique_ptr<IArchiveWriter> archiveWriter = makeArchiveWriter();
        if (!archiveWriter) {
@@ -1234,12 +1237,21 @@ public:

            XmlReferenceLinker manifestLinker;
            if (manifestLinker.consume(mContext, manifestXml.get())) {
                if (!proguard::collectProguardRulesForManifest(Source(mOptions.manifestPath),
                if (mOptions.generateProguardRulesPath &&
                        !proguard::collectProguardRulesForManifest(Source(mOptions.manifestPath),
                                                                   manifestXml.get(),
                                                                   &proguardKeepSet)) {
                    error = true;
                }

                if (mOptions.generateMainDexProguardRulesPath &&
                        !proguard::collectProguardRulesForManifest(Source(mOptions.manifestPath),
                                                                   manifestXml.get(),
                                                                   &proguardMainDexKeepSet,
                                                                   true)) {
                    error = true;
                }

                if (mOptions.generateJavaClassPath) {
                    if (!writeManifestJavaFile(manifestXml.get())) {
                        error = true;
@@ -1268,6 +1280,8 @@ public:
        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())) {
@@ -1338,10 +1352,12 @@ public:
            }
        }

        if (mOptions.generateProguardRulesPath) {
            if (!writeProguardFile(proguardKeepSet)) {
        if (!writeProguardFile(mOptions.generateProguardRulesPath, proguardKeepSet)) {
            return 1;
        }

        if (!writeProguardFile(mOptions.generateMainDexProguardRulesPath, proguardMainDexKeepSet)) {
            return 1;
        }

        if (mContext->verbose()) {
@@ -1397,6 +1413,9 @@ int link(const std::vector<StringPiece>& args) {
                          &options.generateJavaClassPath)
            .optionalFlag("--proguard", "Output file for generated Proguard rules",
                          &options.generateProguardRulesPath)
            .optionalFlag("--proguard-main-dex",
                          "Output file for generated Proguard rules for the main dex",
                          &options.generateMainDexProguardRulesPath)
            .optionalSwitch("--no-auto-version",
                            "Disables automatic style and layout SDK versioning",
                            &options.noAutoVersion)