diff options
author | 2016-04-21 16:29:58 -0700 | |
---|---|---|
committer | 2016-04-21 18:00:53 -0700 | |
commit | 86229cb622fccde8ab8cbe85eead91a34313a708 (patch) | |
tree | e84bce78081f926623a45c5f222a79c31d207acb | |
parent | 6a86fbe69d48c18ad1414ee04941ebc7f6a4eb0b (diff) |
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
-rw-r--r-- | tools/aapt/Bundle.h | 5 | ||||
-rw-r--r-- | tools/aapt/Command.cpp | 6 | ||||
-rw-r--r-- | tools/aapt/Main.cpp | 13 | ||||
-rw-r--r-- | tools/aapt/Main.h | 1 | ||||
-rw-r--r-- | tools/aapt/Resource.cpp | 89 |
5 files changed, 94 insertions, 20 deletions
diff --git a/tools/aapt/Bundle.h b/tools/aapt/Bundle.h index ca06ac440731..653c1b4d6f97 100644 --- a/tools/aapt/Bundle.h +++ b/tools/aapt/Bundle.h @@ -55,7 +55,7 @@ public: mCompressionMethod(0), mJunkPath(false), mOutputAPKFile(NULL), mManifestPackageNameOverride(NULL), mInstrumentationPackageNameOverride(NULL), mAutoAddOverlay(false), mGenDependencies(false), mNoVersionVectors(false), - mCrunchedOutputDir(NULL), mProguardFile(NULL), + mCrunchedOutputDir(NULL), mProguardFile(NULL), mMainDexProguardFile(NULL), mAndroidManifestFile(NULL), mPublicOutputFile(NULL), mRClassDir(NULL), mResourceIntermediatesDir(NULL), mManifestMinSdkVersion(NULL), mMinSdkVersion(NULL), mTargetSdkVersion(NULL), mMaxSdkVersion(NULL), @@ -146,6 +146,8 @@ public: void setCrunchedOutputDir(const char* dir) { mCrunchedOutputDir = dir; } const char* getProguardFile() const { return mProguardFile; } void setProguardFile(const char* file) { mProguardFile = file; } + const char* getMainDexProguardFile() const { return mMainDexProguardFile; } + void setMainDexProguardFile(const char* file) { mMainDexProguardFile = file; } const android::Vector<const char*>& getResourceSourceDirs() const { return mResourceSourceDirs; } void addResourceSourceDir(const char* dir) { mResourceSourceDirs.insertAt(dir,0); } const char* getAndroidManifestFile() const { return mAndroidManifestFile; } @@ -299,6 +301,7 @@ private: bool mNoVersionVectors; const char* mCrunchedOutputDir; const char* mProguardFile; + const char* mMainDexProguardFile; const char* mAndroidManifestFile; const char* mPublicOutputFile; const char* mRClassDir; diff --git a/tools/aapt/Command.cpp b/tools/aapt/Command.cpp index db40416111d5..9976d00fa872 100644 --- a/tools/aapt/Command.cpp +++ b/tools/aapt/Command.cpp @@ -2631,6 +2631,12 @@ int doPackage(Bundle* bundle) goto bail; } + // Write out the Main Dex ProGuard file + err = writeMainDexProguardFile(bundle, assets); + if (err < 0) { + goto bail; + } + // Write the apk if (outputAPKFile) { // Gather all resources and add them to the APK Builder. The builder will then diff --git a/tools/aapt/Main.cpp b/tools/aapt/Main.cpp index c424cc516b56..984d98e30f29 100644 --- a/tools/aapt/Main.cpp +++ b/tools/aapt/Main.cpp @@ -67,6 +67,7 @@ void usage(void) " [--max-res-version VAL] \\\n" " [-I base-package [-I base-package ...]] \\\n" " [-A asset-source-dir] [-G class-list-file] [-P public-definitions-file] \\\n" + " [-D main-dex-class-list-file] \\\n" " [-S resource-sources [-S resource-sources ...]] \\\n" " [-F apk-file] [-J R-file-dir] \\\n" " [--product product1,product2,...] \\\n" @@ -120,6 +121,7 @@ void usage(void) " localization=\"suggested\"\n" " -A additional directory in which to find raw asset files\n" " -G A file to output proguard options into.\n" + " -D A file to output proguard options for the main dex into.\n" " -F specify the apk file to output\n" " -I add an existing package to base include set\n" " -J specify where to output R.java resource constant definitions\n" @@ -390,6 +392,17 @@ int main(int argc, char* const argv[]) convertPath(argv[0]); bundle.setProguardFile(argv[0]); break; + case 'D': + argc--; + argv++; + if (!argc) { + fprintf(stderr, "ERROR: No argument supplied for '-D' option\n"); + wantUsage = true; + goto bail; + } + convertPath(argv[0]); + bundle.setMainDexProguardFile(argv[0]); + break; case 'I': argc--; argv++; diff --git a/tools/aapt/Main.h b/tools/aapt/Main.h index e84c4c503cd2..a493842b8d10 100644 --- a/tools/aapt/Main.h +++ b/tools/aapt/Main.h @@ -54,6 +54,7 @@ extern android::status_t writeResourceSymbols(Bundle* bundle, bool includePrivate, bool emitCallback); extern android::status_t writeProguardFile(Bundle* bundle, const sp<AaptAssets>& assets); +extern android::status_t writeMainDexProguardFile(Bundle* bundle, const sp<AaptAssets>& assets); extern bool isValidResourceType(const String8& type); diff --git a/tools/aapt/Resource.cpp b/tools/aapt/Resource.cpp index 18a194326de7..8e7045bd07e1 100644 --- a/tools/aapt/Resource.cpp +++ b/tools/aapt/Resource.cpp @@ -2832,7 +2832,7 @@ addProguardKeepMethodRule(ProguardKeepSet* keep, const String8& memberName, } status_t -writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp<AaptAssets>& assets) +writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp<AaptAssets>& assets, bool mainDex) { status_t err; ResXMLTree tree; @@ -2844,6 +2844,7 @@ writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp<AaptAssets>& ass sp<AaptGroup> assGroup; sp<AaptFile> assFile; String8 pkg; + String8 defaultProcess; // First, look for a package file to parse. This is required to // be able to generate the resource information. @@ -2900,6 +2901,15 @@ writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp<AaptAssets>& 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; } @@ -2916,7 +2926,23 @@ writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp<AaptAssets>& 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()); } @@ -3099,6 +3125,31 @@ writeProguardForLayouts(ProguardKeepSet* keep, const sp<AaptAssets>& assets) } 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<String8, SortedVector<String8> >& rules = keep.rules; + const size_t N = rules.size(); + for (size_t i=0; i<N; i++) { + const SortedVector<String8>& locations = rules.valueAt(i); + const size_t M = locations.size(); + for (size_t j=0; j<M; j++) { + fprintf(fp, "# %s\n", locations.itemAt(j).string()); + } + fprintf(fp, "%s\n\n", rules.keyAt(i).string()); + } + fclose(fp); + + return err; +} + +status_t writeProguardFile(Bundle* bundle, const sp<AaptAssets>& assets) { status_t err = -1; @@ -3109,7 +3160,7 @@ writeProguardFile(Bundle* bundle, const sp<AaptAssets>& assets) ProguardKeepSet keep; - err = writeProguardForAndroidManifest(&keep, assets); + err = writeProguardForAndroidManifest(&keep, assets, false); if (err < 0) { return err; } @@ -3119,26 +3170,26 @@ writeProguardFile(Bundle* bundle, const sp<AaptAssets>& 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<AaptAssets>& assets) +{ + status_t err = -1; + + if (!bundle->getMainDexProguardFile()) { + return NO_ERROR; } - const KeyedVector<String8, SortedVector<String8> >& rules = keep.rules; - const size_t N = rules.size(); - for (size_t i=0; i<N; i++) { - const SortedVector<String8>& locations = rules.valueAt(i); - const size_t M = locations.size(); - for (size_t j=0; j<M; j++) { - fprintf(fp, "# %s\n", locations.itemAt(j).string()); - } - fprintf(fp, "%s\n\n", rules.keyAt(i).string()); + ProguardKeepSet keep; + + err = writeProguardForAndroidManifest(&keep, assets, true); + if (err < 0) { + return err; } - fclose(fp); - return err; + return writeProguardSpec(bundle->getMainDexProguardFile(), keep, err); } // Loops through the string paths and writes them to the file pointer |