blob: 10b84b0c6bdac6a6132d61b98bfadc387dd03af4 [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 {
29 friend class OptimizeCommand;
30
31 // Path to the output APK.
Ryan Mitchell4382e442021-07-14 12:53:01 -070032 std::optional<std::string> output_path;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070033 // Path to the output APK directory for splits.
Ryan Mitchell4382e442021-07-14 12:53:01 -070034 std::optional<std::string> output_dir;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070035
36 // Details of the app extracted from the AndroidManifest.xml
37 AppInfo app_info;
38
Ryan Mitchell4ea90752020-07-31 08:21:43 -070039 // Exclude list of unused resources that should be removed from the apk.
40 std::unordered_set<ResourceName> resources_exclude_list;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070041
42 // Split APK options.
43 TableSplitterOptions table_splitter_options;
44
45 // List of output split paths. These are in the same order as `split_constraints`.
46 std::vector<std::string> split_paths;
47
48 // List of SplitConstraints governing what resources go into each split. Ordered by `split_paths`.
49 std::vector<SplitConstraints> split_constraints;
50
51 TableFlattenerOptions table_flattener_options;
52
Ryan Mitchell4382e442021-07-14 12:53:01 -070053 std::optional<std::vector<aapt::configuration::OutputArtifact>> apk_artifacts;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070054
55 // Set of artifacts to keep when generating multi-APK splits. If the list is empty, all artifacts
56 // are kept and will be written as output.
57 std::unordered_set<std::string> kept_artifacts;
Mohamed Heikalc7694032018-11-07 16:49:02 -050058
59 // Whether or not to shorten resource paths in the APK.
Donald Chaid554f7c2019-07-10 18:46:34 -070060 bool shorten_resource_paths = false;
Mohamed Heikalc7694032018-11-07 16:49:02 -050061
62 // Path to the output map of original resource paths to shortened paths.
Ryan Mitchell4382e442021-07-14 12:53:01 -070063 std::optional<std::string> shortened_paths_map_path;
Iurii Makhnoda06e4d2022-08-24 14:17:32 +000064
65 // Whether sparse encoding should be used for O+ resources.
66 bool enable_sparse_encoding = false;
67
68 // Whether sparse encoding should be used for all resources.
69 bool force_sparse_encoding = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070070};
71
72class OptimizeCommand : public Command {
73 public:
74 explicit OptimizeCommand() : Command("optimize") {
75 SetDescription("Preforms resource optimizations on an apk.");
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080076 AddOptionalFlag("-o", "Path to the output APK.", &options_.output_path, Command::kPath);
77 AddOptionalFlag("-d", "Path to the output directory (for splits).", &options_.output_dir,
78 Command::kPath);
79 AddOptionalFlag("-x", "Path to XML configuration file.", &config_path_, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070080 AddOptionalSwitch("-p", "Print the multi APK artifacts and exit.", &print_only_);
81 AddOptionalFlag(
82 "--target-densities",
83 "Comma separated list of the screen densities that the APK will be optimized for.\n"
84 "All the resources that would be unused on devices of the given densities will be \n"
85 "removed from the APK.",
86 &target_densities_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070087 AddOptionalFlag("--resources-config-path",
88 "Path to the resources.cfg file containing the list of resources and \n"
89 "directives to each resource. \n"
90 "Format: type/resource_name#[directive][,directive]",
91 &resources_config_path_);
92 AddOptionalFlagList("-c",
93 "Comma separated list of configurations to include. The default\n"
94 "is all configurations.",
95 &configs_);
96 AddOptionalFlagList("--split",
97 "Split resources matching a set of configs out to a "
98 "Split APK.\nSyntax: path/to/output.apk;<config>[,<config>[...]].\n"
99 "On Windows, use a semicolon ';' separator instead.",
100 &split_args_);
101 AddOptionalFlagList("--keep-artifacts",
102 "Comma separated list of artifacts to keep. If none are specified,\n"
103 "all artifacts will be kept.",
104 &kept_artifacts_);
Iurii Makhnoda06e4d2022-08-24 14:17:32 +0000105 AddOptionalSwitch(
106 "--enable-sparse-encoding",
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700107 "Enables encoding sparse entries using a binary search tree.\n"
Iurii Makhnoda06e4d2022-08-24 14:17:32 +0000108 "This decreases APK size at the cost of resource retrieval performance.\n"
109 "Only applies sparse encoding to Android O+ resources or all resources if minSdk of "
110 "the APK is O+",
111 &options_.enable_sparse_encoding);
112 AddOptionalSwitch("--force-sparse-encoding",
113 "Enables encoding sparse entries using a binary search tree.\n"
114 "This decreases APK size at the cost of resource retrieval performance.\n"
115 "Applies sparse encoding to all resources regardless of minSdk.",
116 &options_.force_sparse_encoding);
Brian Changd882ba42019-09-16 11:03:53 -0700117 AddOptionalSwitch("--collapse-resource-names",
118 "Collapses resource names to a single value in the key string pool. Resources can \n"
119 "be exempted using the \"no_collapse\" directive in a file specified by "
120 "--resources-config-path.",
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700121 &options_.table_flattener_options.collapse_key_stringpool);
Brian Changd882ba42019-09-16 11:03:53 -0700122 AddOptionalSwitch("--shorten-resource-paths",
123 "Shortens the paths of resources inside the APK.",
Mohamed Heikalc7694032018-11-07 16:49:02 -0500124 &options_.shorten_resource_paths);
125 AddOptionalFlag("--resource-path-shortening-map",
126 "Path to output the map of old resource paths to shortened paths.",
127 &options_.shortened_paths_map_path);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700128 AddOptionalSwitch("-v", "Enables verbose logging", &verbose_);
129 }
130
131 int Action(const std::vector<std::string>& args) override;
132
133 private:
134 OptimizeOptions options_;
135
Mohamed Heikalc7694032018-11-07 16:49:02 -0500136 bool WriteObfuscatedPathsMap(const std::map<std::string, std::string> &path_map,
137 const std::string &file_path);
138
Ryan Mitchell4382e442021-07-14 12:53:01 -0700139 std::optional<std::string> config_path_;
140 std::optional<std::string> resources_config_path_;
141 std::optional<std::string> target_densities_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700142 std::vector<std::string> configs_;
143 std::vector<std::string> split_args_;
144 std::unordered_set<std::string> kept_artifacts_;
145 bool print_only_ = false;
146 bool verbose_ = false;
147};
148
149}// namespace aapt
150
Mohamed Heikalc7694032018-11-07 16:49:02 -0500151#endif //AAPT2_OPTIMIZE_H