| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef AAPT2_OPTIMIZE_H |
| 18 |
#define AAPT2_OPTIMIZE_H |
| 19 |
|
| 20 |
#include "AppInfo.h" |
| 21 |
#include "Command.h" |
| 22 |
#include "configuration/ConfigurationParser.h" |
| 23 |
#include "format/binary/TableFlattener.h" |
| 24 |
#include "split/TableSplitter.h" |
| 25 |
|
| 26 |
namespace aapt { |
| 27 |
|
| 28 |
struct OptimizeOptions { |
| 29 |
friend class OptimizeCommand; |
| 30 |
|
| 31 |
|
| 32 |
std::optional<std::string> output_path; |
| 33 |
|
| 34 |
std::optional<std::string> output_dir; |
| 35 |
|
| 36 |
|
| 37 |
AppInfo app_info; |
| 38 |
|
| 39 |
|
| 40 |
std::unordered_set<ResourceName> resources_exclude_list; |
| 41 |
|
| 42 |
|
| 43 |
TableSplitterOptions table_splitter_options; |
| 44 |
|
| 45 |
|
| 46 |
std::vector<std::string> split_paths; |
| 47 |
|
| 48 |
|
| 49 |
std::vector<SplitConstraints> split_constraints; |
| 50 |
|
| 51 |
TableFlattenerOptions table_flattener_options; |
| 52 |
|
| 53 |
std::optional<std::vector<aapt::configuration::OutputArtifact>> apk_artifacts; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
std::unordered_set<std::string> kept_artifacts; |
| 58 |
|
| 59 |
|
| 60 |
bool shorten_resource_paths = false; |
| 61 |
|
| 62 |
|
| 63 |
std::optional<std::string> shortened_paths_map_path; |
| 64 |
}; |
| 65 |
|
| 66 |
class OptimizeCommand : public Command { |
| 67 |
public: |
| 68 |
explicit OptimizeCommand() : Command("optimize") { |
| 69 |
SetDescription("Preforms resource optimizations on an apk."); |
| 70 |
AddOptionalFlag("-o", "Path to the output APK.", &options_.output_path, Command::kPath); |
| 71 |
AddOptionalFlag("-d", "Path to the output directory (for splits).", &options_.output_dir, |
| 72 |
Command::kPath); |
| 73 |
AddOptionalFlag("-x", "Path to XML configuration file.", &config_path_, Command::kPath); |
| 74 |
AddOptionalSwitch("-p", "Print the multi APK artifacts and exit.", &print_only_); |
| 75 |
AddOptionalFlag( |
| 76 |
"--target-densities", |
| 77 |
"Comma separated list of the screen densities that the APK will be optimized for.\n" |
| 78 |
"All the resources that would be unused on devices of the given densities will be \n" |
| 79 |
"removed from the APK.", |
| 80 |
&target_densities_); |
| 81 |
AddOptionalFlag("--resources-config-path", |
| 82 |
"Path to the resources.cfg file containing the list of resources and \n" |
| 83 |
"directives to each resource. \n" |
| 84 |
"Format: type/resource_name#[directive][,directive]", |
| 85 |
&resources_config_path_); |
| 86 |
AddOptionalFlagList("-c", |
| 87 |
"Comma separated list of configurations to include. The default\n" |
| 88 |
"is all configurations.", |
| 89 |
&configs_); |
| 90 |
AddOptionalFlagList("--split", |
| 91 |
"Split resources matching a set of configs out to a " |
| 92 |
"Split APK.\nSyntax: path/to/output.apk;<config>[,<config>[...]].\n" |
| 93 |
"On Windows, use a semicolon ';' separator instead.", |
| 94 |
&split_args_); |
| 95 |
AddOptionalFlagList("--keep-artifacts", |
| 96 |
"Comma separated list of artifacts to keep. If none are specified,\n" |
| 97 |
"all artifacts will be kept.", |
| 98 |
&kept_artifacts_); |
| 99 |
AddOptionalSwitch("--enable-sparse-encoding", |
| 100 |
"Enables encoding sparse entries using a binary search tree.\n" |
| 101 |
"This decreases APK size at the cost of resource retrieval performance.", |
| 102 |
&options_.table_flattener_options.use_sparse_entries); |
| 103 |
AddOptionalSwitch("--collapse-resource-names", |
| 104 |
"Collapses resource names to a single value in the key string pool. Resources can \n" |
| 105 |
"be exempted using the \"no_collapse\" directive in a file specified by " |
| 106 |
"--resources-config-path.", |
| 107 |
&options_.table_flattener_options.collapse_key_stringpool); |
| 108 |
AddOptionalSwitch("--shorten-resource-paths", |
| 109 |
"Shortens the paths of resources inside the APK.", |
| 110 |
&options_.shorten_resource_paths); |
| 111 |
AddOptionalFlag("--resource-path-shortening-map", |
| 112 |
"Path to output the map of old resource paths to shortened paths.", |
| 113 |
&options_.shortened_paths_map_path); |
| 114 |
AddOptionalSwitch("-v", "Enables verbose logging", &verbose_); |
| 115 |
} |
| 116 |
|
| 117 |
int Action(const std::vector<std::string>& args) override; |
| 118 |
|
| 119 |
private: |
| 120 |
OptimizeOptions options_; |
| 121 |
|
| 122 |
bool WriteObfuscatedPathsMap(const std::map<std::string, std::string> &path_map, |
| 123 |
const std::string &file_path); |
| 124 |
|
| 125 |
std::optional<std::string> config_path_; |
| 126 |
std::optional<std::string> resources_config_path_; |
| 127 |
std::optional<std::string> target_densities_; |
| 128 |
std::vector<std::string> configs_; |
| 129 |
std::vector<std::string> split_args_; |
| 130 |
std::unordered_set<std::string> kept_artifacts_; |
| 131 |
bool print_only_ = false; |
| 132 |
bool verbose_ = false; |
| 133 |
}; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
#endif |