blob: 012b0f230ca23cdf518f314e1eea3260e6ca2688 [file] [log] [blame]
Ryan Mitchell833a1a62018-07-10 13:51:36 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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
26namespace aapt {
27
28struct OptimizeOptions {
Ryan Mitchell833a1a62018-07-10 13:51:36 -070029 // Path to the output APK.
Ryan Mitchell4382e442021-07-14 12:53:01 -070030 std::optional<std::string> output_path;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070031 // Path to the output APK directory for splits.
Ryan Mitchell4382e442021-07-14 12:53:01 -070032 std::optional<std::string> output_dir;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070033
34 // Details of the app extracted from the AndroidManifest.xml
35 AppInfo app_info;
36
Ryan Mitchell4ea90752020-07-31 08:21:43 -070037 // Exclude list of unused resources that should be removed from the apk.
38 std::unordered_set<ResourceName> resources_exclude_list;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070039
40 // Split APK options.
41 TableSplitterOptions table_splitter_options;
42
43 // List of output split paths. These are in the same order as `split_constraints`.
44 std::vector<std::string> split_paths;
45
46 // List of SplitConstraints governing what resources go into each split. Ordered by `split_paths`.
47 std::vector<SplitConstraints> split_constraints;
48
49 TableFlattenerOptions table_flattener_options;
50
Ryan Mitchell4382e442021-07-14 12:53:01 -070051 std::optional<std::vector<aapt::configuration::OutputArtifact>> apk_artifacts;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070052
53 // Set of artifacts to keep when generating multi-APK splits. If the list is empty, all artifacts
54 // are kept and will be written as output.
55 std::unordered_set<std::string> kept_artifacts;
Mohamed Heikalc7694032018-11-07 16:49:02 -050056
57 // Whether or not to shorten resource paths in the APK.
Donald Chaid554f7c2019-07-10 18:46:34 -070058 bool shorten_resource_paths = false;
Mohamed Heikalc7694032018-11-07 16:49:02 -050059
60 // Path to the output map of original resource paths to shortened paths.
felkachang78a8d372022-09-14 15:17:29 +080061 // TODO(b/246489170): keep the old option and format until transform to the new one
Ryan Mitchell4382e442021-07-14 12:53:01 -070062 std::optional<std::string> shortened_paths_map_path;
Iurii Makhnoda06e4d2022-08-24 14:17:32 +000063
64 // Whether sparse encoding should be used for O+ resources.
65 bool enable_sparse_encoding = false;
66
67 // Whether sparse encoding should be used for all resources.
68 bool force_sparse_encoding = false;
felkachang78a8d372022-09-14 15:17:29 +080069
70 // Path to the output map of original resource paths/names to obfuscated paths/names.
71 std::optional<std::string> obfuscation_map_path;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070072};
73
74class OptimizeCommand : public Command {
75 public:
76 explicit OptimizeCommand() : Command("optimize") {
77 SetDescription("Preforms resource optimizations on an apk.");
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080078 AddOptionalFlag("-o", "Path to the output APK.", &options_.output_path, Command::kPath);
79 AddOptionalFlag("-d", "Path to the output directory (for splits).", &options_.output_dir,
80 Command::kPath);
81 AddOptionalFlag("-x", "Path to XML configuration file.", &config_path_, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070082 AddOptionalSwitch("-p", "Print the multi APK artifacts and exit.", &print_only_);
83 AddOptionalFlag(
84 "--target-densities",
85 "Comma separated list of the screen densities that the APK will be optimized for.\n"
86 "All the resources that would be unused on devices of the given densities will be \n"
87 "removed from the APK.",
88 &target_densities_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070089 AddOptionalFlag("--resources-config-path",
90 "Path to the resources.cfg file containing the list of resources and \n"
91 "directives to each resource. \n"
92 "Format: type/resource_name#[directive][,directive]",
93 &resources_config_path_);
94 AddOptionalFlagList("-c",
95 "Comma separated list of configurations to include. The default\n"
96 "is all configurations.",
97 &configs_);
98 AddOptionalFlagList("--split",
99 "Split resources matching a set of configs out to a "
100 "Split APK.\nSyntax: path/to/output.apk;<config>[,<config>[...]].\n"
101 "On Windows, use a semicolon ';' separator instead.",
102 &split_args_);
103 AddOptionalFlagList("--keep-artifacts",
104 "Comma separated list of artifacts to keep. If none are specified,\n"
105 "all artifacts will be kept.",
106 &kept_artifacts_);
Iurii Makhnoda06e4d2022-08-24 14:17:32 +0000107 AddOptionalSwitch(
108 "--enable-sparse-encoding",
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700109 "Enables encoding sparse entries using a binary search tree.\n"
Iurii Makhnoda06e4d2022-08-24 14:17:32 +0000110 "This decreases APK size at the cost of resource retrieval performance.\n"
111 "Only applies sparse encoding to Android O+ resources or all resources if minSdk of "
112 "the APK is O+",
113 &options_.enable_sparse_encoding);
114 AddOptionalSwitch("--force-sparse-encoding",
115 "Enables encoding sparse entries using a binary search tree.\n"
116 "This decreases APK size at the cost of resource retrieval performance.\n"
117 "Applies sparse encoding to all resources regardless of minSdk.",
118 &options_.force_sparse_encoding);
Yurii Zubrytskyia8bfabe2024-07-10 12:25:24 -0700119 AddOptionalSwitch(
120 "--enable-compact-entries",
121 "This decreases APK size by using compact resource entries for simple data types.",
122 &options_.table_flattener_options.use_compact_entries);
Brian Changd882ba42019-09-16 11:03:53 -0700123 AddOptionalSwitch("--collapse-resource-names",
124 "Collapses resource names to a single value in the key string pool. Resources can \n"
125 "be exempted using the \"no_collapse\" directive in a file specified by "
126 "--resources-config-path.",
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700127 &options_.table_flattener_options.collapse_key_stringpool);
Brian Changd882ba42019-09-16 11:03:53 -0700128 AddOptionalSwitch("--shorten-resource-paths",
branliuf1ed5232022-12-16 19:02:29 +0800129 "Shortens the paths of resources inside the APK. Resources can be exempted using the \n"
130 "\"no_path_shorten\" directive in a file specified by --resources-config-path.",
Mohamed Heikalc7694032018-11-07 16:49:02 -0500131 &options_.shorten_resource_paths);
felkachang78a8d372022-09-14 15:17:29 +0800132 // TODO(b/246489170): keep the old option and format until transform to the new one
Mohamed Heikalc7694032018-11-07 16:49:02 -0500133 AddOptionalFlag("--resource-path-shortening-map",
felkachang78a8d372022-09-14 15:17:29 +0800134 "[Deprecated]Path to output the map of old resource paths to shortened paths.",
135 &options_.shortened_paths_map_path);
136 AddOptionalFlag("--save-obfuscation-map",
137 "Path to output the map of original paths/names to obfuscated paths/names.",
138 &options_.obfuscation_map_path);
Iurii Makhno054e4332022-10-12 16:03:03 +0000139 AddOptionalSwitch(
140 "--deduplicate-entry-values",
141 "Whether to deduplicate pairs of resource entry and value for simple resources.\n"
142 "This is recommended to be used together with '--collapse-resource-names' flag or for\n"
143 "APKs where resource names are manually collapsed. For such APKs this flag allows to\n"
144 "store the same resource value only once in resource table which decreases APK size.\n"
145 "Has no effect on APKs where resource names are kept.",
146 &options_.table_flattener_options.deduplicate_entry_values);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700147 AddOptionalSwitch("-v", "Enables verbose logging", &verbose_);
148 }
149
150 int Action(const std::vector<std::string>& args) override;
151
152 private:
153 OptimizeOptions options_;
154
Mohamed Heikalc7694032018-11-07 16:49:02 -0500155 bool WriteObfuscatedPathsMap(const std::map<std::string, std::string> &path_map,
156 const std::string &file_path);
157
Ryan Mitchell4382e442021-07-14 12:53:01 -0700158 std::optional<std::string> config_path_;
159 std::optional<std::string> resources_config_path_;
160 std::optional<std::string> target_densities_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700161 std::vector<std::string> configs_;
162 std::vector<std::string> split_args_;
163 std::unordered_set<std::string> kept_artifacts_;
164 bool print_only_ = false;
165 bool verbose_ = false;
166};
167
168}// namespace aapt
169
Mohamed Heikalc7694032018-11-07 16:49:02 -0500170#endif //AAPT2_OPTIMIZE_H