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
Bug: 28425556
Change-Id: Ic18c8c563794ff27a5598a214111d1b446a005f1
(cherry picked from commit 86229cb622fccde8ab8cbe85eead91a34313a708)
diff --git a/tools/aapt/Resource.cpp b/tools/aapt/Resource.cpp
index 18a1943..8e7045b 100644
--- a/tools/aapt/Resource.cpp
+++ b/tools/aapt/Resource.cpp
@@ -2832,7 +2832,7 @@
}
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 @@
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 @@
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 @@
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,30 +3125,12 @@
}
status_t
-writeProguardFile(Bundle* bundle, const sp<AaptAssets>& assets)
+writeProguardSpec(const char* filename, const ProguardKeepSet& keep, status_t err)
{
- status_t err = -1;
-
- if (!bundle->getProguardFile()) {
- return NO_ERROR;
- }
-
- ProguardKeepSet keep;
-
- err = writeProguardForAndroidManifest(&keep, assets);
- if (err < 0) {
- return err;
- }
-
- err = writeProguardForLayouts(&keep, assets);
- if (err < 0) {
- return err;
- }
-
- FILE* fp = fopen(bundle->getProguardFile(), "w+");
+ FILE* fp = fopen(filename, "w+");
if (fp == NULL) {
fprintf(stderr, "ERROR: Unable to open class file %s: %s\n",
- bundle->getProguardFile(), strerror(errno));
+ filename, strerror(errno));
return UNKNOWN_ERROR;
}
@@ -3141,6 +3149,49 @@
return err;
}
+status_t
+writeProguardFile(Bundle* bundle, const sp<AaptAssets>& assets)
+{
+ status_t err = -1;
+
+ if (!bundle->getProguardFile()) {
+ return NO_ERROR;
+ }
+
+ ProguardKeepSet keep;
+
+ err = writeProguardForAndroidManifest(&keep, assets, false);
+ if (err < 0) {
+ return err;
+ }
+
+ err = writeProguardForLayouts(&keep, assets);
+ if (err < 0) {
+ return err;
+ }
+
+ 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;
+ }
+
+ ProguardKeepSet keep;
+
+ err = writeProguardForAndroidManifest(&keep, assets, true);
+ if (err < 0) {
+ return err;
+ }
+
+ return writeProguardSpec(bundle->getMainDexProguardFile(), keep, err);
+}
+
// Loops through the string paths and writes them to the file pointer
// Each file path is written on its own line with a terminating backslash.
status_t writePathsToFile(const sp<FilePathStore>& files, FILE* fp)