Loading tools/aapt2/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -115,6 +115,7 @@ cc_library_host_static { "optimize/MultiApkGenerator.cpp", "optimize/ResourceDeduper.cpp", "optimize/ResourceFilter.cpp", "optimize/ResourcePathShortener.cpp", "optimize/VersionCollapser.cpp", "process/SymbolTable.cpp", "split/TableSplitter.cpp", Loading tools/aapt2/LoadedApk.cpp +12 −2 Original line number Diff line number Diff line Loading @@ -223,8 +223,17 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table 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."); Loading Loading @@ -283,7 +292,8 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table return false; } } else { if (!io::CopyFileToArchivePreserveCompression(context, file, path, writer)) { if (!io::CopyFileToArchivePreserveCompression( context, file, output_path, writer)) { return false; } } Loading tools/aapt2/cmd/Optimize.cpp +26 −0 Original line number Diff line number Diff line Loading @@ -41,6 +41,7 @@ #include "optimize/MultiApkGenerator.h" #include "optimize/ResourceDeduper.h" #include "optimize/ResourceFilter.h" #include "optimize/ResourcePathShortener.h" #include "optimize/VersionCollapser.h" #include "split/TableSplitter.h" #include "util/Files.h" Loading @@ -52,6 +53,7 @@ using ::android::ConfigDescription; using ::android::ResTable_config; using ::android::StringPiece; using ::android::base::ReadFileToString; using ::android::base::WriteStringToFile; using ::android::base::StringAppendF; using ::android::base::StringPrintf; Loading Loading @@ -143,6 +145,21 @@ class Optimizer { return 1; } if (options_.shorten_resource_paths) { ResourcePathShortener shortener(options_.table_flattener_options.shortened_path_map); if (!shortener.Consume(context_, apk->GetResourceTable())) { context_->GetDiagnostics()->Error(DiagMessage() << "failed shortening resource paths"); return 1; } if (options_.shortened_paths_map_path && !WriteShortenedPathsMap(options_.table_flattener_options.shortened_path_map, options_.shortened_paths_map_path.value())) { context_->GetDiagnostics()->Error(DiagMessage() << "failed to write shortened resource paths to file"); return 1; } } // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or // equal to the minSdk. options_.split_constraints = Loading Loading @@ -264,6 +281,15 @@ class Optimizer { ArchiveEntry::kAlign, writer); } bool WriteShortenedPathsMap(const std::map<std::string, std::string> &path_map, const std::string &file_path) { std::stringstream ss; for (auto it = path_map.cbegin(); it != path_map.cend(); ++it) { ss << it->first << " -> " << it->second << "\n"; } return WriteStringToFile(ss.str(), file_path); } OptimizeOptions options_; OptimizeContext* context_; }; Loading tools/aapt2/cmd/Optimize.h +16 −1 Original line number Diff line number Diff line Loading @@ -55,6 +55,12 @@ struct OptimizeOptions { // Set of artifacts to keep when generating multi-APK splits. If the list is empty, all artifacts // are kept and will be written as output. std::unordered_set<std::string> kept_artifacts; // Whether or not to shorten resource paths in the APK. bool shorten_resource_paths; // Path to the output map of original resource paths to shortened paths. Maybe<std::string> shortened_paths_map_path; }; class OptimizeCommand : public Command { Loading Loading @@ -101,6 +107,12 @@ class OptimizeCommand : public Command { AddOptionalSwitch("--enable-resource-obfuscation", "Enables obfuscation of key string pool to single value", &options_.table_flattener_options.collapse_key_stringpool); AddOptionalSwitch("--enable-resource-path-shortening", "Enables shortening of the path of the resources inside the APK.", &options_.shorten_resource_paths); AddOptionalFlag("--resource-path-shortening-map", "Path to output the map of old resource paths to shortened paths.", &options_.shortened_paths_map_path); AddOptionalSwitch("-v", "Enables verbose logging", &verbose_); } Loading @@ -109,6 +121,9 @@ class OptimizeCommand : public Command { private: OptimizeOptions options_; bool WriteObfuscatedPathsMap(const std::map<std::string, std::string> &path_map, const std::string &file_path); Maybe<std::string> config_path_; Maybe<std::string> whitelist_path_; Maybe<std::string> resources_config_path_; Loading tools/aapt2/format/binary/TableFlattener.h +3 −0 Original line number Diff line number Diff line Loading @@ -46,6 +46,9 @@ struct TableFlattenerOptions { // When true, sort the entries in the values string pool by priority and configuration. bool sort_stringpool_entries = true; // Map from original resource paths to shortened resource paths. std::map<std::string, std::string> shortened_path_map; }; class TableFlattener : public IResourceTableConsumer { Loading Loading
tools/aapt2/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -115,6 +115,7 @@ cc_library_host_static { "optimize/MultiApkGenerator.cpp", "optimize/ResourceDeduper.cpp", "optimize/ResourceFilter.cpp", "optimize/ResourcePathShortener.cpp", "optimize/VersionCollapser.cpp", "process/SymbolTable.cpp", "split/TableSplitter.cpp", Loading
tools/aapt2/LoadedApk.cpp +12 −2 Original line number Diff line number Diff line Loading @@ -223,8 +223,17 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table 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."); Loading Loading @@ -283,7 +292,8 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table return false; } } else { if (!io::CopyFileToArchivePreserveCompression(context, file, path, writer)) { if (!io::CopyFileToArchivePreserveCompression( context, file, output_path, writer)) { return false; } } Loading
tools/aapt2/cmd/Optimize.cpp +26 −0 Original line number Diff line number Diff line Loading @@ -41,6 +41,7 @@ #include "optimize/MultiApkGenerator.h" #include "optimize/ResourceDeduper.h" #include "optimize/ResourceFilter.h" #include "optimize/ResourcePathShortener.h" #include "optimize/VersionCollapser.h" #include "split/TableSplitter.h" #include "util/Files.h" Loading @@ -52,6 +53,7 @@ using ::android::ConfigDescription; using ::android::ResTable_config; using ::android::StringPiece; using ::android::base::ReadFileToString; using ::android::base::WriteStringToFile; using ::android::base::StringAppendF; using ::android::base::StringPrintf; Loading Loading @@ -143,6 +145,21 @@ class Optimizer { return 1; } if (options_.shorten_resource_paths) { ResourcePathShortener shortener(options_.table_flattener_options.shortened_path_map); if (!shortener.Consume(context_, apk->GetResourceTable())) { context_->GetDiagnostics()->Error(DiagMessage() << "failed shortening resource paths"); return 1; } if (options_.shortened_paths_map_path && !WriteShortenedPathsMap(options_.table_flattener_options.shortened_path_map, options_.shortened_paths_map_path.value())) { context_->GetDiagnostics()->Error(DiagMessage() << "failed to write shortened resource paths to file"); return 1; } } // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or // equal to the minSdk. options_.split_constraints = Loading Loading @@ -264,6 +281,15 @@ class Optimizer { ArchiveEntry::kAlign, writer); } bool WriteShortenedPathsMap(const std::map<std::string, std::string> &path_map, const std::string &file_path) { std::stringstream ss; for (auto it = path_map.cbegin(); it != path_map.cend(); ++it) { ss << it->first << " -> " << it->second << "\n"; } return WriteStringToFile(ss.str(), file_path); } OptimizeOptions options_; OptimizeContext* context_; }; Loading
tools/aapt2/cmd/Optimize.h +16 −1 Original line number Diff line number Diff line Loading @@ -55,6 +55,12 @@ struct OptimizeOptions { // Set of artifacts to keep when generating multi-APK splits. If the list is empty, all artifacts // are kept and will be written as output. std::unordered_set<std::string> kept_artifacts; // Whether or not to shorten resource paths in the APK. bool shorten_resource_paths; // Path to the output map of original resource paths to shortened paths. Maybe<std::string> shortened_paths_map_path; }; class OptimizeCommand : public Command { Loading Loading @@ -101,6 +107,12 @@ class OptimizeCommand : public Command { AddOptionalSwitch("--enable-resource-obfuscation", "Enables obfuscation of key string pool to single value", &options_.table_flattener_options.collapse_key_stringpool); AddOptionalSwitch("--enable-resource-path-shortening", "Enables shortening of the path of the resources inside the APK.", &options_.shorten_resource_paths); AddOptionalFlag("--resource-path-shortening-map", "Path to output the map of old resource paths to shortened paths.", &options_.shortened_paths_map_path); AddOptionalSwitch("-v", "Enables verbose logging", &verbose_); } Loading @@ -109,6 +121,9 @@ class OptimizeCommand : public Command { private: OptimizeOptions options_; bool WriteObfuscatedPathsMap(const std::map<std::string, std::string> &path_map, const std::string &file_path); Maybe<std::string> config_path_; Maybe<std::string> whitelist_path_; Maybe<std::string> resources_config_path_; Loading
tools/aapt2/format/binary/TableFlattener.h +3 −0 Original line number Diff line number Diff line Loading @@ -46,6 +46,9 @@ struct TableFlattenerOptions { // When true, sort the entries in the values string pool by priority and configuration. bool sort_stringpool_entries = true; // Map from original resource paths to shortened resource paths. std::map<std::string, std::string> shortened_path_map; }; class TableFlattener : public IResourceTableConsumer { Loading