Resource Path Obfuscation
This CL allows aapt2 to obfuscate resource paths within the output apk
and move resources to shorter obfuscated paths. This reduces apk size
when there is a large number of resources since the path metadata exists
in 4 places in the apk.
This CL adds two arguments to aapt2, one to enable resource path
obfuscation and one to point to a path to output the path map to (for
later debugging).
Test: make aapt2_tests
Bug: b/75965637
Change-Id: I9cacafe1d17800d673566b2d61b0b88f3fb8d60c
diff --git a/tools/aapt2/LoadedApk.cpp b/tools/aapt2/LoadedApk.cpp
index b353ff0..45719ef 100644
--- a/tools/aapt2/LoadedApk.cpp
+++ b/tools/aapt2/LoadedApk.cpp
@@ -223,8 +223,17 @@
io::IFile* file = iterator->Next();
std::string path = file->GetSource().path;
+ std::string output_path = path;
+ bool is_resource = path.find("res/") == 0;
+ if (is_resource) {
+ auto it = options.shortened_path_map.find(path);
+ if (it != options.shortened_path_map.end()) {
+ output_path = it->second;
+ }
+ }
+
// Skip resources that are not referenced if requested.
- if (path.find("res/") == 0 && referenced_resources.find(path) == referenced_resources.end()) {
+ if (is_resource && referenced_resources.find(output_path) == referenced_resources.end()) {
if (context->IsVerbose()) {
context->GetDiagnostics()->Note(DiagMessage()
<< "Removing resource '" << path << "' from APK.");
@@ -283,7 +292,8 @@
return false;
}
} else {
- if (!io::CopyFileToArchivePreserveCompression(context, file, path, writer)) {
+ if (!io::CopyFileToArchivePreserveCompression(
+ context, file, output_path, writer)) {
return false;
}
}