blob: 56bff8fe0fe807ecf2d7944db4c3fdf9c9e599bd [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_LINK_H
18#define AAPT2_LINK_H
19
Izabela Orlowska0faba5f2018-06-01 12:06:31 +010020#include <regex>
21
Ryan Mitchell833a1a62018-07-10 13:51:36 -070022#include "Command.h"
23#include "Diagnostics.h"
24#include "Resource.h"
25#include "split/TableSplitter.h"
26#include "format/binary/TableFlattener.h"
Ryan Mitchellef9e6882019-06-24 11:53:33 -070027#include "format/proto/ProtoSerialize.h"
Ryan Mitchell833a1a62018-07-10 13:51:36 -070028#include "link/ManifestFixer.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080029#include "trace/TraceBuffer.h"
Ryan Mitchell833a1a62018-07-10 13:51:36 -070030
31namespace aapt {
32
33enum class OutputFormat {
34 kApk,
35 kProto,
36};
37
38struct LinkOptions {
39 std::string output_path;
40 std::string manifest_path;
41 std::vector<std::string> include_paths;
42 std::vector<std::string> overlay_files;
43 std::vector<std::string> assets_dirs;
44 bool output_to_directory = false;
45 bool auto_add_overlay = false;
Donald Chai121c6e82019-06-12 12:51:57 -070046 bool override_styles_instead_of_overlaying = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070047 OutputFormat output_format = OutputFormat::kApk;
48
49 // Java/Proguard options.
50 Maybe<std::string> generate_java_class_path;
51 Maybe<std::string> custom_java_package;
52 std::set<std::string> extra_java_packages;
53 Maybe<std::string> generate_text_symbols_path;
54 Maybe<std::string> generate_proguard_rules_path;
55 Maybe<std::string> generate_main_dex_proguard_rules_path;
56 bool generate_conditional_proguard_rules = false;
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070057 bool generate_minimal_proguard_rules = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070058 bool generate_non_final_ids = false;
59 std::vector<std::string> javadoc_annotations;
60 Maybe<std::string> private_symbols;
61
62 // Optimizations/features.
63 bool no_auto_version = false;
64 bool no_version_vectors = false;
65 bool no_version_transitions = false;
66 bool no_resource_deduping = false;
Mårten Kongstadd8d29012018-06-11 14:13:37 +020067 bool no_resource_removal = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070068 bool no_xml_namespaces = false;
69 bool do_not_compress_anything = false;
70 std::unordered_set<std::string> extensions_to_not_compress;
Izabela Orlowska0faba5f2018-06-01 12:06:31 +010071 Maybe<std::regex> regex_to_not_compress;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070072
73 // Static lib options.
74 bool no_static_lib_packages = false;
Izabela Orlowska84febea2019-06-03 18:34:12 +010075 bool merge_only = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070076
77 // AndroidManifest.xml massaging options.
78 ManifestFixerOptions manifest_fixer_options;
79
80 // Products to use/filter on.
81 std::unordered_set<std::string> products;
82
83 // Flattening options.
84 TableFlattenerOptions table_flattener_options;
Ryan Mitchellef9e6882019-06-24 11:53:33 -070085 SerializeTableOptions proto_table_flattener_options;
Ryan Mitchell479fa392019-01-02 17:15:39 -080086 bool keep_raw_values = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070087
88 // Split APK options.
89 TableSplitterOptions table_splitter_options;
90 std::vector<SplitConstraints> split_constraints;
91 std::vector<std::string> split_paths;
92
Winson3c918b82019-01-25 14:25:37 -080093 // Configurations to exclude
94 std::vector<std::string> exclude_configs_;
95
Ryan Mitchell833a1a62018-07-10 13:51:36 -070096 // Stable ID options.
97 std::unordered_map<ResourceName, ResourceId> stable_id_map;
98 Maybe<std::string> resource_id_map_path;
99
100 // When 'true', allow reserved package IDs to be used for applications. Pre-O, the platform
101 // treats negative resource IDs [those with a package ID of 0x80 or higher] as invalid.
102 // In order to work around this limitation, we allow the use of traditionally reserved
103 // resource IDs [those between 0x02 and 0x7E].
104 bool allow_reserved_package_id = false;
105
106 // Whether we should fail on definitions of a resource with conflicting visibility.
107 bool strict_visibility = false;
108};
109
110class LinkCommand : public Command {
111 public:
112 explicit LinkCommand(IDiagnostics* diag) : Command("link", "l"),
113 diag_(diag) {
114 SetDescription("Links resources into an apk.");
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800115 AddRequiredFlag("-o", "Output path.", &options_.output_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700116 AddRequiredFlag("--manifest", "Path to the Android manifest to build.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800117 &options_.manifest_path, Command::kPath);
118 AddOptionalFlagList("-I", "Adds an Android APK to link against.", &options_.include_paths,
119 Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700120 AddOptionalFlagList("-A", "An assets directory to include in the APK. These are unprocessed.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800121 &options_.assets_dirs, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700122 AddOptionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n"
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800123 "The last conflicting resource given takes precedence.", &overlay_arg_list_,
124 Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700125 AddOptionalFlag("--package-id",
126 "Specify the package ID to use for this app. Must be greater or equal to\n"
127 "0x7f and can't be used with --static-lib or --shared-lib.", &package_id_);
128 AddOptionalFlag("--java", "Directory in which to generate R.java.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800129 &options_.generate_java_class_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700130 AddOptionalFlag("--proguard", "Output file for generated Proguard rules.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800131 &options_.generate_proguard_rules_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700132 AddOptionalFlag("--proguard-main-dex",
133 "Output file for generated Proguard rules for the main dex.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800134 &options_.generate_main_dex_proguard_rules_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700135 AddOptionalSwitch("--proguard-conditional-keep-rules",
136 "Generate conditional Proguard keep rules.",
137 &options_.generate_conditional_proguard_rules);
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700138 AddOptionalSwitch("--proguard-minimal-keep-rules",
139 "Generate a minimal set of Proguard keep rules.",
140 &options_.generate_minimal_proguard_rules);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700141 AddOptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning.",
142 &options_.no_auto_version);
143 AddOptionalSwitch("--no-version-vectors",
144 "Disables automatic versioning of vector drawables. Use this only\n"
145 "when building with vector drawable support library.",
146 &options_.no_version_vectors);
147 AddOptionalSwitch("--no-version-transitions",
148 "Disables automatic versioning of transition resources. Use this only\n"
149 "when building with transition support library.",
150 &options_.no_version_transitions);
151 AddOptionalSwitch("--no-resource-deduping", "Disables automatic deduping of resources with\n"
152 "identical values across compatible configurations.",
153 &options_.no_resource_deduping);
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200154 AddOptionalSwitch("--no-resource-removal", "Disables automatic removal of resources without\n"
155 "defaults. Use this only when building runtime resource overlay packages.",
156 &options_.no_resource_removal);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700157 AddOptionalSwitch("--enable-sparse-encoding",
158 "This decreases APK size at the cost of resource retrieval performance.",
159 &options_.table_flattener_options.use_sparse_entries);
160 AddOptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
161 &legacy_x_flag_);
162 AddOptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
163 &require_localization_);
164 AddOptionalFlagList("-c",
165 "Comma separated list of configurations to include. The default\n"
166 "is all configurations.", &configs_);
167 AddOptionalFlag("--preferred-density",
168 "Selects the closest matching density and strips out all others.",
169 &preferred_density_);
170 AddOptionalFlag("--product", "Comma separated list of product names to keep", &product_list_);
171 AddOptionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified by -o.",
172 &options_.output_to_directory);
173 AddOptionalSwitch("--no-xml-namespaces", "Removes XML namespace prefix and URI information\n"
174 "from AndroidManifest.xml and XML binaries in res/*.",
175 &options_.no_xml_namespaces);
176 AddOptionalFlag("--min-sdk-version",
177 "Default minimum SDK version to use for AndroidManifest.xml.",
178 &options_.manifest_fixer_options.min_sdk_version_default);
179 AddOptionalFlag("--target-sdk-version",
180 "Default target SDK version to use for AndroidManifest.xml.",
181 &options_.manifest_fixer_options.target_sdk_version_default);
182 AddOptionalFlag("--version-code",
183 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
Ryan Mitchell704090e2018-07-31 14:59:25 -0700184 "present.", &options_.manifest_fixer_options.version_code_default);
185 AddOptionalFlag("--version-code-major",
186 "Version code major (integer) to inject into the AndroidManifest.xml if none is\n"
187 "present.", &options_.manifest_fixer_options.version_code_major_default);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700188 AddOptionalFlag("--version-name",
189 "Version name to inject into the AndroidManifest.xml if none is present.",
190 &options_.manifest_fixer_options.version_name_default);
191 AddOptionalSwitch("--replace-version",
192 "If --version-code and/or --version-name are specified, these\n"
193 "values will replace any value already in the manifest. By\n"
194 "default, nothing is changed if the manifest already defines\n"
195 "these attributes.",
196 &options_.manifest_fixer_options.replace_version);
197 AddOptionalFlag("--compile-sdk-version-code",
198 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
199 "present.",
200 &options_.manifest_fixer_options.compile_sdk_version);
201 AddOptionalFlag("--compile-sdk-version-name",
202 "Version name to inject into the AndroidManifest.xml if none is present.",
203 &options_.manifest_fixer_options.compile_sdk_version_codename);
204 AddOptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
205 &shared_lib_);
206 AddOptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib_);
207 AddOptionalSwitch("--proto-format",
208 "Generates compiled resources in Protobuf format.\n"
209 "Suitable as input to the bundle tool for generating an App Bundle.",
210 &proto_format_);
211 AddOptionalSwitch("--no-static-lib-packages",
212 "Merge all library resources under the app's package.",
213 &options_.no_static_lib_packages);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700214 AddOptionalSwitch("--non-final-ids",
215 "Generates R.java without the final modifier. This is implied when\n"
216 "--static-lib is specified.",
217 &options_.generate_non_final_ids);
218 AddOptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
219 &stable_id_file_path_);
220 AddOptionalFlag("--emit-ids",
221 "Emit a file at the given path with a list of name to ID mappings,\n"
222 "suitable for use with --stable-ids.",
223 &options_.resource_id_map_path);
224 AddOptionalFlag("--private-symbols",
225 "Package name to use when generating R.java for private symbols.\n"
226 "If not specified, public and private symbols will use the application's\n"
227 "package name.",
228 &options_.private_symbols);
229 AddOptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
230 &options_.custom_java_package);
231 AddOptionalFlagList("--extra-packages",
232 "Generate the same R.java but with different package names.",
233 &extra_java_packages_);
234 AddOptionalFlagList("--add-javadoc-annotation",
235 "Adds a JavaDoc annotation to all generated Java classes.",
236 &options_.javadoc_annotations);
237 AddOptionalFlag("--output-text-symbols",
238 "Generates a text file containing the resource symbols of the R class in\n"
239 "the specified folder.",
240 &options_.generate_text_symbols_path);
241 AddOptionalSwitch("--allow-reserved-package-id",
242 "Allows the use of a reserved package ID. This should on be used for\n"
243 "packages with a pre-O min-sdk\n",
244 &options_.allow_reserved_package_id);
245 AddOptionalSwitch("--auto-add-overlay",
246 "Allows the addition of new resources in overlays without\n"
247 "<add-resource> tags.",
248 &options_.auto_add_overlay);
Donald Chai121c6e82019-06-12 12:51:57 -0700249 AddOptionalSwitch("--override-styles-instead-of-overlaying",
Ryan Mitchellef9e6882019-06-24 11:53:33 -0700250 "Causes styles defined in -R resources to replace previous definitions\n"
251 "instead of merging into them\n",
252 &options_.override_styles_instead_of_overlaying);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700253 AddOptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
254 &options_.manifest_fixer_options.rename_manifest_package);
255 AddOptionalFlag("--rename-instrumentation-target-package",
256 "Changes the name of the target package for instrumentation. Most useful\n"
257 "when used in conjunction with --rename-manifest-package.",
258 &options_.manifest_fixer_options.rename_instrumentation_target_package);
Ryan Mitchell81dfca02019-06-07 10:20:27 -0700259 AddOptionalFlagList("-0", "File suffix not to compress.",
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700260 &options_.extensions_to_not_compress);
261 AddOptionalSwitch("--no-compress", "Do not compress any resources.",
262 &options_.do_not_compress_anything);
Ryan Mitchell479fa392019-01-02 17:15:39 -0800263 AddOptionalSwitch("--keep-raw-values", "Preserve raw attribute values in xml files.",
264 &options_.keep_raw_values);
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100265 AddOptionalFlag("--no-compress-regex",
266 "Do not compress extensions matching the regular expression. Remember to\n"
267 " use the '$' symbol for end of line. Uses a non case-sensitive\n"
268 " ECMAScript regular expression grammar.",
269 &no_compress_regex);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700270 AddOptionalSwitch("--warn-manifest-validation",
271 "Treat manifest validation errors as warnings.",
272 &options_.manifest_fixer_options.warn_validation);
273 AddOptionalFlagList("--split",
274 "Split resources matching a set of configs out to a Split APK.\n"
275 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
276 "On Windows, use a semicolon ';' separator instead.",
277 &split_args_);
Winson3c918b82019-01-25 14:25:37 -0800278 AddOptionalFlagList("--exclude-configs",
279 "Excludes values of resources whose configs contain the specified qualifiers.",
280 &options_.exclude_configs_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700281 AddOptionalSwitch("--debug-mode",
282 "Inserts android:debuggable=\"true\" in to the application node of the\n"
283 "manifest, making the application debuggable even on production devices.",
284 &options_.manifest_fixer_options.debug_mode);
285 AddOptionalSwitch("--strict-visibility",
286 "Do not allow overlays with different visibility levels.",
287 &options_.strict_visibility);
Ryan Mitchellef9e6882019-06-24 11:53:33 -0700288 AddOptionalSwitch("--exclude-sources",
289 "Do not serialize source file information when generating resources in\n"
290 "Protobuf format.",
291 &options_.proto_table_flattener_options.exclude_sources);
292 AddOptionalFlag("--trace-folder",
293 "Generate systrace json trace fragment to specified folder.",
294 &trace_folder_);
Izabela Orlowska84febea2019-06-03 18:34:12 +0100295 AddOptionalSwitch("--merge-only",
Ryan Mitchellef9e6882019-06-24 11:53:33 -0700296 "Only merge the resources, without verifying resource references. This flag\n"
297 "should only be used together with the --static-lib flag.",
298 &options_.merge_only);
299 AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700300 }
301
302 int Action(const std::vector<std::string>& args) override;
303
304 private:
305 IDiagnostics* diag_;
306 LinkOptions options_;
307
308 std::vector<std::string> overlay_arg_list_;
309 std::vector<std::string> extra_java_packages_;
310 Maybe<std::string> package_id_;
311 std::vector<std::string> configs_;
312 Maybe<std::string> preferred_density_;
313 Maybe<std::string> product_list_;
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100314 Maybe<std::string> no_compress_regex;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700315 bool legacy_x_flag_ = false;
316 bool require_localization_ = false;
317 bool verbose_ = false;
318 bool shared_lib_ = false;
319 bool static_lib_ = false;
320 bool proto_format_ = false;
321 Maybe<std::string> stable_id_file_path_;
322 std::vector<std::string> split_args_;
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800323 Maybe<std::string> trace_folder_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700324};
325
326}// namespace aapt
327
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200328#endif //AAPT2_LINK_H