xref: /aosp_15_r20/frameworks/base/tools/aapt2/cmd/Optimize.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1*d57664e9SAndroid Build Coastguard Worker /*
2*d57664e9SAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*d57664e9SAndroid Build Coastguard Worker  *
4*d57664e9SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*d57664e9SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*d57664e9SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*d57664e9SAndroid Build Coastguard Worker  *
8*d57664e9SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*d57664e9SAndroid Build Coastguard Worker  *
10*d57664e9SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*d57664e9SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*d57664e9SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*d57664e9SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*d57664e9SAndroid Build Coastguard Worker  * limitations under the License.
15*d57664e9SAndroid Build Coastguard Worker  */
16*d57664e9SAndroid Build Coastguard Worker 
17*d57664e9SAndroid Build Coastguard Worker #ifndef AAPT2_OPTIMIZE_H
18*d57664e9SAndroid Build Coastguard Worker #define AAPT2_OPTIMIZE_H
19*d57664e9SAndroid Build Coastguard Worker 
20*d57664e9SAndroid Build Coastguard Worker #include "AppInfo.h"
21*d57664e9SAndroid Build Coastguard Worker #include "Command.h"
22*d57664e9SAndroid Build Coastguard Worker #include "configuration/ConfigurationParser.h"
23*d57664e9SAndroid Build Coastguard Worker #include "format/binary/TableFlattener.h"
24*d57664e9SAndroid Build Coastguard Worker #include "split/TableSplitter.h"
25*d57664e9SAndroid Build Coastguard Worker 
26*d57664e9SAndroid Build Coastguard Worker namespace aapt {
27*d57664e9SAndroid Build Coastguard Worker 
28*d57664e9SAndroid Build Coastguard Worker struct OptimizeOptions {
29*d57664e9SAndroid Build Coastguard Worker   // Path to the output APK.
30*d57664e9SAndroid Build Coastguard Worker   std::optional<std::string> output_path;
31*d57664e9SAndroid Build Coastguard Worker   // Path to the output APK directory for splits.
32*d57664e9SAndroid Build Coastguard Worker   std::optional<std::string> output_dir;
33*d57664e9SAndroid Build Coastguard Worker 
34*d57664e9SAndroid Build Coastguard Worker   // Details of the app extracted from the AndroidManifest.xml
35*d57664e9SAndroid Build Coastguard Worker   AppInfo app_info;
36*d57664e9SAndroid Build Coastguard Worker 
37*d57664e9SAndroid Build Coastguard Worker   // Exclude list of unused resources that should be removed from the apk.
38*d57664e9SAndroid Build Coastguard Worker   std::unordered_set<ResourceName> resources_exclude_list;
39*d57664e9SAndroid Build Coastguard Worker 
40*d57664e9SAndroid Build Coastguard Worker   // Split APK options.
41*d57664e9SAndroid Build Coastguard Worker   TableSplitterOptions table_splitter_options;
42*d57664e9SAndroid Build Coastguard Worker 
43*d57664e9SAndroid Build Coastguard Worker   // List of output split paths. These are in the same order as `split_constraints`.
44*d57664e9SAndroid Build Coastguard Worker   std::vector<std::string> split_paths;
45*d57664e9SAndroid Build Coastguard Worker 
46*d57664e9SAndroid Build Coastguard Worker   // List of SplitConstraints governing what resources go into each split. Ordered by `split_paths`.
47*d57664e9SAndroid Build Coastguard Worker   std::vector<SplitConstraints> split_constraints;
48*d57664e9SAndroid Build Coastguard Worker 
49*d57664e9SAndroid Build Coastguard Worker   TableFlattenerOptions table_flattener_options;
50*d57664e9SAndroid Build Coastguard Worker 
51*d57664e9SAndroid Build Coastguard Worker   std::optional<std::vector<aapt::configuration::OutputArtifact>> apk_artifacts;
52*d57664e9SAndroid Build Coastguard Worker 
53*d57664e9SAndroid Build Coastguard Worker   // Set of artifacts to keep when generating multi-APK splits. If the list is empty, all artifacts
54*d57664e9SAndroid Build Coastguard Worker   // are kept and will be written as output.
55*d57664e9SAndroid Build Coastguard Worker   std::unordered_set<std::string> kept_artifacts;
56*d57664e9SAndroid Build Coastguard Worker 
57*d57664e9SAndroid Build Coastguard Worker   // Whether or not to shorten resource paths in the APK.
58*d57664e9SAndroid Build Coastguard Worker   bool shorten_resource_paths = false;
59*d57664e9SAndroid Build Coastguard Worker 
60*d57664e9SAndroid Build Coastguard Worker   // Path to the output map of original resource paths to shortened paths.
61*d57664e9SAndroid Build Coastguard Worker   // TODO(b/246489170): keep the old option and format until transform to the new one
62*d57664e9SAndroid Build Coastguard Worker   std::optional<std::string> shortened_paths_map_path;
63*d57664e9SAndroid Build Coastguard Worker 
64*d57664e9SAndroid Build Coastguard Worker   // Whether sparse encoding should be used for O+ resources.
65*d57664e9SAndroid Build Coastguard Worker   bool enable_sparse_encoding = false;
66*d57664e9SAndroid Build Coastguard Worker 
67*d57664e9SAndroid Build Coastguard Worker   // Whether sparse encoding should be used for all resources.
68*d57664e9SAndroid Build Coastguard Worker   bool force_sparse_encoding = false;
69*d57664e9SAndroid Build Coastguard Worker 
70*d57664e9SAndroid Build Coastguard Worker   // Path to the output map of original resource paths/names to obfuscated paths/names.
71*d57664e9SAndroid Build Coastguard Worker   std::optional<std::string> obfuscation_map_path;
72*d57664e9SAndroid Build Coastguard Worker };
73*d57664e9SAndroid Build Coastguard Worker 
74*d57664e9SAndroid Build Coastguard Worker class OptimizeCommand : public Command {
75*d57664e9SAndroid Build Coastguard Worker  public:
OptimizeCommand()76*d57664e9SAndroid Build Coastguard Worker   explicit OptimizeCommand() : Command("optimize") {
77*d57664e9SAndroid Build Coastguard Worker     SetDescription("Preforms resource optimizations on an apk.");
78*d57664e9SAndroid Build Coastguard Worker     AddOptionalFlag("-o", "Path to the output APK.", &options_.output_path, Command::kPath);
79*d57664e9SAndroid Build Coastguard Worker     AddOptionalFlag("-d", "Path to the output directory (for splits).", &options_.output_dir,
80*d57664e9SAndroid Build Coastguard Worker         Command::kPath);
81*d57664e9SAndroid Build Coastguard Worker     AddOptionalFlag("-x", "Path to XML configuration file.", &config_path_, Command::kPath);
82*d57664e9SAndroid Build Coastguard Worker     AddOptionalSwitch("-p", "Print the multi APK artifacts and exit.", &print_only_);
83*d57664e9SAndroid Build Coastguard Worker     AddOptionalFlag(
84*d57664e9SAndroid Build Coastguard Worker         "--target-densities",
85*d57664e9SAndroid Build Coastguard Worker         "Comma separated list of the screen densities that the APK will be optimized for.\n"
86*d57664e9SAndroid Build Coastguard Worker             "All the resources that would be unused on devices of the given densities will be \n"
87*d57664e9SAndroid Build Coastguard Worker             "removed from the APK.",
88*d57664e9SAndroid Build Coastguard Worker         &target_densities_);
89*d57664e9SAndroid Build Coastguard Worker     AddOptionalFlag("--resources-config-path",
90*d57664e9SAndroid Build Coastguard Worker         "Path to the resources.cfg file containing the list of resources and \n"
91*d57664e9SAndroid Build Coastguard Worker             "directives to each resource. \n"
92*d57664e9SAndroid Build Coastguard Worker             "Format: type/resource_name#[directive][,directive]",
93*d57664e9SAndroid Build Coastguard Worker         &resources_config_path_);
94*d57664e9SAndroid Build Coastguard Worker     AddOptionalFlagList("-c",
95*d57664e9SAndroid Build Coastguard Worker         "Comma separated list of configurations to include. The default\n"
96*d57664e9SAndroid Build Coastguard Worker             "is all configurations.",
97*d57664e9SAndroid Build Coastguard Worker         &configs_);
98*d57664e9SAndroid Build Coastguard Worker     AddOptionalFlagList("--split",
99*d57664e9SAndroid Build Coastguard Worker         "Split resources matching a set of configs out to a "
100*d57664e9SAndroid Build Coastguard Worker             "Split APK.\nSyntax: path/to/output.apk;<config>[,<config>[...]].\n"
101*d57664e9SAndroid Build Coastguard Worker             "On Windows, use a semicolon ';' separator instead.",
102*d57664e9SAndroid Build Coastguard Worker         &split_args_);
103*d57664e9SAndroid Build Coastguard Worker     AddOptionalFlagList("--keep-artifacts",
104*d57664e9SAndroid Build Coastguard Worker         "Comma separated list of artifacts to keep. If none are specified,\n"
105*d57664e9SAndroid Build Coastguard Worker             "all artifacts will be kept.",
106*d57664e9SAndroid Build Coastguard Worker         &kept_artifacts_);
107*d57664e9SAndroid Build Coastguard Worker     AddOptionalSwitch(
108*d57664e9SAndroid Build Coastguard Worker         "--enable-sparse-encoding",
109*d57664e9SAndroid Build Coastguard Worker         "Enables encoding sparse entries using a binary search tree.\n"
110*d57664e9SAndroid Build Coastguard Worker         "This decreases APK size at the cost of resource retrieval performance.\n"
111*d57664e9SAndroid Build Coastguard Worker         "Only applies sparse encoding to Android O+ resources or all resources if minSdk of "
112*d57664e9SAndroid Build Coastguard Worker         "the APK is O+",
113*d57664e9SAndroid Build Coastguard Worker         &options_.enable_sparse_encoding);
114*d57664e9SAndroid Build Coastguard Worker     AddOptionalSwitch("--force-sparse-encoding",
115*d57664e9SAndroid Build Coastguard Worker                       "Enables encoding sparse entries using a binary search tree.\n"
116*d57664e9SAndroid Build Coastguard Worker                       "This decreases APK size at the cost of resource retrieval performance.\n"
117*d57664e9SAndroid Build Coastguard Worker                       "Applies sparse encoding to all resources regardless of minSdk.",
118*d57664e9SAndroid Build Coastguard Worker                       &options_.force_sparse_encoding);
119*d57664e9SAndroid Build Coastguard Worker     AddOptionalSwitch(
120*d57664e9SAndroid Build Coastguard Worker         "--enable-compact-entries",
121*d57664e9SAndroid Build Coastguard Worker         "This decreases APK size by using compact resource entries for simple data types.",
122*d57664e9SAndroid Build Coastguard Worker         &options_.table_flattener_options.use_compact_entries);
123*d57664e9SAndroid Build Coastguard Worker     AddOptionalSwitch("--collapse-resource-names",
124*d57664e9SAndroid Build Coastguard Worker         "Collapses resource names to a single value in the key string pool. Resources can \n"
125*d57664e9SAndroid Build Coastguard Worker             "be exempted using the \"no_collapse\" directive in a file specified by "
126*d57664e9SAndroid Build Coastguard Worker             "--resources-config-path.",
127*d57664e9SAndroid Build Coastguard Worker         &options_.table_flattener_options.collapse_key_stringpool);
128*d57664e9SAndroid Build Coastguard Worker     AddOptionalSwitch("--shorten-resource-paths",
129*d57664e9SAndroid Build Coastguard Worker         "Shortens the paths of resources inside the APK. Resources can be exempted using the \n"
130*d57664e9SAndroid Build Coastguard Worker         "\"no_path_shorten\" directive in a file specified by --resources-config-path.",
131*d57664e9SAndroid Build Coastguard Worker         &options_.shorten_resource_paths);
132*d57664e9SAndroid Build Coastguard Worker     // TODO(b/246489170): keep the old option and format until transform to the new one
133*d57664e9SAndroid Build Coastguard Worker     AddOptionalFlag("--resource-path-shortening-map",
134*d57664e9SAndroid Build Coastguard Worker                     "[Deprecated]Path to output the map of old resource paths to shortened paths.",
135*d57664e9SAndroid Build Coastguard Worker                     &options_.shortened_paths_map_path);
136*d57664e9SAndroid Build Coastguard Worker     AddOptionalFlag("--save-obfuscation-map",
137*d57664e9SAndroid Build Coastguard Worker                     "Path to output the map of original paths/names to obfuscated paths/names.",
138*d57664e9SAndroid Build Coastguard Worker                     &options_.obfuscation_map_path);
139*d57664e9SAndroid Build Coastguard Worker     AddOptionalSwitch(
140*d57664e9SAndroid Build Coastguard Worker         "--deduplicate-entry-values",
141*d57664e9SAndroid Build Coastguard Worker         "Whether to deduplicate pairs of resource entry and value for simple resources.\n"
142*d57664e9SAndroid Build Coastguard Worker         "This is recommended to be used together with '--collapse-resource-names' flag or for\n"
143*d57664e9SAndroid Build Coastguard Worker         "APKs where resource names are manually collapsed. For such APKs this flag allows to\n"
144*d57664e9SAndroid Build Coastguard Worker         "store the same resource value only once in resource table which decreases APK size.\n"
145*d57664e9SAndroid Build Coastguard Worker         "Has no effect on APKs where resource names are kept.",
146*d57664e9SAndroid Build Coastguard Worker         &options_.table_flattener_options.deduplicate_entry_values);
147*d57664e9SAndroid Build Coastguard Worker     AddOptionalSwitch("-v", "Enables verbose logging", &verbose_);
148*d57664e9SAndroid Build Coastguard Worker   }
149*d57664e9SAndroid Build Coastguard Worker 
150*d57664e9SAndroid Build Coastguard Worker   int Action(const std::vector<std::string>& args) override;
151*d57664e9SAndroid Build Coastguard Worker 
152*d57664e9SAndroid Build Coastguard Worker  private:
153*d57664e9SAndroid Build Coastguard Worker   OptimizeOptions options_;
154*d57664e9SAndroid Build Coastguard Worker 
155*d57664e9SAndroid Build Coastguard Worker   bool WriteObfuscatedPathsMap(const std::map<std::string, std::string> &path_map,
156*d57664e9SAndroid Build Coastguard Worker                                const std::string &file_path);
157*d57664e9SAndroid Build Coastguard Worker 
158*d57664e9SAndroid Build Coastguard Worker   std::optional<std::string> config_path_;
159*d57664e9SAndroid Build Coastguard Worker   std::optional<std::string> resources_config_path_;
160*d57664e9SAndroid Build Coastguard Worker   std::optional<std::string> target_densities_;
161*d57664e9SAndroid Build Coastguard Worker   std::vector<std::string> configs_;
162*d57664e9SAndroid Build Coastguard Worker   std::vector<std::string> split_args_;
163*d57664e9SAndroid Build Coastguard Worker   std::unordered_set<std::string> kept_artifacts_;
164*d57664e9SAndroid Build Coastguard Worker   bool print_only_ = false;
165*d57664e9SAndroid Build Coastguard Worker   bool verbose_ = false;
166*d57664e9SAndroid Build Coastguard Worker };
167*d57664e9SAndroid Build Coastguard Worker 
168*d57664e9SAndroid Build Coastguard Worker }// namespace aapt
169*d57664e9SAndroid Build Coastguard Worker 
170*d57664e9SAndroid Build Coastguard Worker #endif //AAPT2_OPTIMIZE_H
171