From e49bb30dab4c37926962e8b408045e5a45f52c40 Mon Sep 17 00:00:00 2001 From: Rohit Agrawal Date: Fri, 22 Apr 2016 12:27:55 -0700 Subject: AAPT2: ProGuard config for components in main dex. Create an analogue of "aapt2 --proguard" which outputs a proguard configuration that keeps only components which need to be in the main dex. Bug: 27383099 Change-Id: I61d652bfcdfc18e1614e852bd6f7540efd15f780 --- tools/aapt2/java/ProguardRules.cpp | 30 ++++++++++++++++++++++++++---- tools/aapt2/java/ProguardRules.h | 3 ++- 2 files changed, 28 insertions(+), 5 deletions(-) (limited to 'tools/aapt2/java') diff --git a/tools/aapt2/java/ProguardRules.cpp b/tools/aapt2/java/ProguardRules.cpp index c610bb0f2ff2..53ff96191c94 100644 --- a/tools/aapt2/java/ProguardRules.cpp +++ b/tools/aapt2/java/ProguardRules.cpp @@ -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 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; diff --git a/tools/aapt2/java/ProguardRules.h b/tools/aapt2/java/ProguardRules.h index aafffd39d84e..744ae5b90d27 100644 --- a/tools/aapt2/java/ProguardRules.h +++ b/tools/aapt2/java/ProguardRules.h @@ -46,7 +46,8 @@ private: std::map> 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); -- cgit v1.2.3-59-g8ed1b