From 6ba873faec0d165b74acfecf1533f58433d431e9 Mon Sep 17 00:00:00 2001 From: Rohit Agrawal Date: Thu, 21 Apr 2016 16:29:58 -0700 Subject: AAPT: ProGuard config for components in main dex. Create an analogue of "aapt -G" which outputs a proguard configuration that keeps only components which need to be in the main dex. BUG: 27383099 Change-Id: Ic18c8c563794ff27a5598a214111d1b446a005f1 (cherry picked from commit 86229cb622fccde8ab8cbe85eead91a34313a708) --- tools/aapt/Resource.cpp | 89 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 70 insertions(+), 19 deletions(-) (limited to 'tools/aapt/Resource.cpp') diff --git a/tools/aapt/Resource.cpp b/tools/aapt/Resource.cpp index 1b30d362a716..d05ae3cec028 100644 --- a/tools/aapt/Resource.cpp +++ b/tools/aapt/Resource.cpp @@ -2830,7 +2830,7 @@ addProguardKeepMethodRule(ProguardKeepSet* keep, const String8& memberName, } status_t -writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp& assets) +writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp& assets, bool mainDex) { status_t err; ResXMLTree tree; @@ -2842,6 +2842,7 @@ writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp& ass sp assGroup; sp assFile; String8 pkg; + String8 defaultProcess; // First, look for a package file to parse. This is required to // be able to generate the resource information. @@ -2898,6 +2899,15 @@ writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp& ass addProguardKeepRule(keep, agent, pkg.string(), assFile->getPrintableSource(), tree.getLineNumber()); } + + if (mainDex) { + defaultProcess = AaptXml::getAttribute(tree, + "http://schemas.android.com/apk/res/android", "process", &error); + if (error != "") { + fprintf(stderr, "ERROR: %s\n", error.string()); + return -1; + } + } } else if (tag == "instrumentation") { keepTag = true; } @@ -2914,7 +2924,23 @@ writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp& ass fprintf(stderr, "ERROR: %s\n", error.string()); return -1; } - if (name.length() > 0) { + + keepTag = name.length() > 0; + + if (keepTag && mainDex) { + String8 componentProcess = AaptXml::getAttribute(tree, + "http://schemas.android.com/apk/res/android", "process", &error); + if (error != "") { + fprintf(stderr, "ERROR: %s\n", error.string()); + return -1; + } + + const String8& process = + componentProcess.length() > 0 ? componentProcess : defaultProcess; + keepTag = process.length() > 0 && process.find(":") != 0; + } + + if (keepTag) { addProguardKeepRule(keep, name, pkg.string(), assFile->getPrintableSource(), tree.getLineNumber()); } @@ -3096,6 +3122,31 @@ writeProguardForLayouts(ProguardKeepSet* keep, const sp& assets) return NO_ERROR; } +status_t +writeProguardSpec(const char* filename, const ProguardKeepSet& keep, status_t err) +{ + FILE* fp = fopen(filename, "w+"); + if (fp == NULL) { + fprintf(stderr, "ERROR: Unable to open class file %s: %s\n", + filename, strerror(errno)); + return UNKNOWN_ERROR; + } + + const KeyedVector >& rules = keep.rules; + const size_t N = rules.size(); + for (size_t i=0; i& locations = rules.valueAt(i); + const size_t M = locations.size(); + for (size_t j=0; j& assets) { @@ -3107,7 +3158,7 @@ writeProguardFile(Bundle* bundle, const sp& assets) ProguardKeepSet keep; - err = writeProguardForAndroidManifest(&keep, assets); + err = writeProguardForAndroidManifest(&keep, assets, false); if (err < 0) { return err; } @@ -3117,26 +3168,26 @@ writeProguardFile(Bundle* bundle, const sp& assets) return err; } - FILE* fp = fopen(bundle->getProguardFile(), "w+"); - if (fp == NULL) { - fprintf(stderr, "ERROR: Unable to open class file %s: %s\n", - bundle->getProguardFile(), strerror(errno)); - return UNKNOWN_ERROR; + return writeProguardSpec(bundle->getProguardFile(), keep, err); +} + +status_t +writeMainDexProguardFile(Bundle* bundle, const sp& assets) +{ + status_t err = -1; + + if (!bundle->getMainDexProguardFile()) { + return NO_ERROR; } - const KeyedVector >& rules = keep.rules; - const size_t N = rules.size(); - for (size_t i=0; i& locations = rules.valueAt(i); - const size_t M = locations.size(); - for (size_t j=0; jgetMainDexProguardFile(), keep, err); } // Loops through the string paths and writes them to the file pointer -- cgit v1.2.3-59-g8ed1b