blob: 7df9cf788cde76b21ec2a65c08077863792196a1 [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.
32 Maybe<std::string> output_path;
33 // Path to the output APK directory for splits.
34 Maybe<std::string> output_dir;
35
36 // Details of the app extracted from the AndroidManifest.xml
37 AppInfo app_info;
38
39 // Blacklist of unused resources that should be removed from the apk.
40 std::unordered_set<ResourceName> resources_blacklist;
41
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
53 Maybe<std::vector<aapt::configuration::OutputArtifact>> apk_artifacts;
54
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.
60 bool shorten_resource_paths;
61
62 // Path to the output map of original resource paths to shortened paths.
63 Maybe<std::string> shortened_paths_map_path;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070064};
65
66class 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);
71 AddOptionalFlag("-d", "Path to the output directory (for splits).", &options_.output_dir);
72 AddOptionalFlag("-x", "Path to XML configuration file.", &config_path_);
73 AddOptionalSwitch("-p", "Print the multi APK artifacts and exit.", &print_only_);
74 AddOptionalFlag(
75 "--target-densities",
76 "Comma separated list of the screen densities that the APK will be optimized for.\n"
77 "All the resources that would be unused on devices of the given densities will be \n"
78 "removed from the APK.",
79 &target_densities_);
80 AddOptionalFlag("--whitelist-path",
81 "Path to the whitelist.cfg file containing whitelisted resources \n"
82 "whose names should not be altered in final resource tables.",
83 &whitelist_path_);
84 AddOptionalFlag("--resources-config-path",
85 "Path to the resources.cfg file containing the list of resources and \n"
86 "directives to each resource. \n"
87 "Format: type/resource_name#[directive][,directive]",
88 &resources_config_path_);
89 AddOptionalFlagList("-c",
90 "Comma separated list of configurations to include. The default\n"
91 "is all configurations.",
92 &configs_);
93 AddOptionalFlagList("--split",
94 "Split resources matching a set of configs out to a "
95 "Split APK.\nSyntax: path/to/output.apk;<config>[,<config>[...]].\n"
96 "On Windows, use a semicolon ';' separator instead.",
97 &split_args_);
98 AddOptionalFlagList("--keep-artifacts",
99 "Comma separated list of artifacts to keep. If none are specified,\n"
100 "all artifacts will be kept.",
101 &kept_artifacts_);
102 AddOptionalSwitch("--enable-sparse-encoding",
103 "Enables encoding sparse entries using a binary search tree.\n"
104 "This decreases APK size at the cost of resource retrieval performance.",
105 &options_.table_flattener_options.use_sparse_entries);
106 AddOptionalSwitch("--enable-resource-obfuscation",
107 "Enables obfuscation of key string pool to single value",
108 &options_.table_flattener_options.collapse_key_stringpool);
Mohamed Heikalc7694032018-11-07 16:49:02 -0500109 AddOptionalSwitch("--enable-resource-path-shortening",
110 "Enables shortening of the path of the resources inside the APK.",
111 &options_.shorten_resource_paths);
112 AddOptionalFlag("--resource-path-shortening-map",
113 "Path to output the map of old resource paths to shortened paths.",
114 &options_.shortened_paths_map_path);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700115 AddOptionalSwitch("-v", "Enables verbose logging", &verbose_);
116 }
117
118 int Action(const std::vector<std::string>& args) override;
119
120 private:
121 OptimizeOptions options_;
122
Mohamed Heikalc7694032018-11-07 16:49:02 -0500123 bool WriteObfuscatedPathsMap(const std::map<std::string, std::string> &path_map,
124 const std::string &file_path);
125
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700126 Maybe<std::string> config_path_;
127 Maybe<std::string> whitelist_path_;
128 Maybe<std::string> resources_config_path_;
129 Maybe<std::string> target_densities_;
130 std::vector<std::string> configs_;
131 std::vector<std::string> split_args_;
132 std::unordered_set<std::string> kept_artifacts_;
133 bool print_only_ = false;
134 bool verbose_ = false;
135};
136
137}// namespace aapt
138
Mohamed Heikalc7694032018-11-07 16:49:02 -0500139#endif //AAPT2_OPTIMIZE_H