blob: 4722358be8f733327565fae5f8cb029b70c06373 [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;
Jean-Luc Coelho181cbfd2019-11-27 12:37:48 -080059 bool no_proguard_location_reference = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070060 std::vector<std::string> javadoc_annotations;
61 Maybe<std::string> private_symbols;
62
63 // Optimizations/features.
64 bool no_auto_version = false;
65 bool no_version_vectors = false;
66 bool no_version_transitions = false;
67 bool no_resource_deduping = false;
Mårten Kongstadd8d29012018-06-11 14:13:37 +020068 bool no_resource_removal = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070069 bool no_xml_namespaces = false;
70 bool do_not_compress_anything = false;
71 std::unordered_set<std::string> extensions_to_not_compress;
Izabela Orlowska0faba5f2018-06-01 12:06:31 +010072 Maybe<std::regex> regex_to_not_compress;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070073
74 // Static lib options.
75 bool no_static_lib_packages = false;
Izabela Orlowska84febea2019-06-03 18:34:12 +010076 bool merge_only = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070077
78 // AndroidManifest.xml massaging options.
79 ManifestFixerOptions manifest_fixer_options;
80
81 // Products to use/filter on.
82 std::unordered_set<std::string> products;
83
84 // Flattening options.
85 TableFlattenerOptions table_flattener_options;
Ryan Mitchellef9e6882019-06-24 11:53:33 -070086 SerializeTableOptions proto_table_flattener_options;
Ryan Mitchell479fa392019-01-02 17:15:39 -080087 bool keep_raw_values = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070088
89 // Split APK options.
90 TableSplitterOptions table_splitter_options;
91 std::vector<SplitConstraints> split_constraints;
92 std::vector<std::string> split_paths;
93
Winson3c918b82019-01-25 14:25:37 -080094 // Configurations to exclude
95 std::vector<std::string> exclude_configs_;
96
Ryan Mitchell833a1a62018-07-10 13:51:36 -070097 // Stable ID options.
98 std::unordered_map<ResourceName, ResourceId> stable_id_map;
99 Maybe<std::string> resource_id_map_path;
100
101 // When 'true', allow reserved package IDs to be used for applications. Pre-O, the platform
102 // treats negative resource IDs [those with a package ID of 0x80 or higher] as invalid.
103 // In order to work around this limitation, we allow the use of traditionally reserved
104 // resource IDs [those between 0x02 and 0x7E].
105 bool allow_reserved_package_id = false;
106
107 // Whether we should fail on definitions of a resource with conflicting visibility.
108 bool strict_visibility = false;
109};
110
111class LinkCommand : public Command {
112 public:
113 explicit LinkCommand(IDiagnostics* diag) : Command("link", "l"),
114 diag_(diag) {
115 SetDescription("Links resources into an apk.");
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800116 AddRequiredFlag("-o", "Output path.", &options_.output_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700117 AddRequiredFlag("--manifest", "Path to the Android manifest to build.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800118 &options_.manifest_path, Command::kPath);
119 AddOptionalFlagList("-I", "Adds an Android APK to link against.", &options_.include_paths,
120 Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700121 AddOptionalFlagList("-A", "An assets directory to include in the APK. These are unprocessed.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800122 &options_.assets_dirs, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700123 AddOptionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n"
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800124 "The last conflicting resource given takes precedence.", &overlay_arg_list_,
125 Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700126 AddOptionalFlag("--package-id",
127 "Specify the package ID to use for this app. Must be greater or equal to\n"
128 "0x7f and can't be used with --static-lib or --shared-lib.", &package_id_);
129 AddOptionalFlag("--java", "Directory in which to generate R.java.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800130 &options_.generate_java_class_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700131 AddOptionalFlag("--proguard", "Output file for generated Proguard rules.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800132 &options_.generate_proguard_rules_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700133 AddOptionalFlag("--proguard-main-dex",
134 "Output file for generated Proguard rules for the main dex.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800135 &options_.generate_main_dex_proguard_rules_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700136 AddOptionalSwitch("--proguard-conditional-keep-rules",
137 "Generate conditional Proguard keep rules.",
138 &options_.generate_conditional_proguard_rules);
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700139 AddOptionalSwitch("--proguard-minimal-keep-rules",
140 "Generate a minimal set of Proguard keep rules.",
141 &options_.generate_minimal_proguard_rules);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700142 AddOptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning.",
143 &options_.no_auto_version);
144 AddOptionalSwitch("--no-version-vectors",
145 "Disables automatic versioning of vector drawables. Use this only\n"
146 "when building with vector drawable support library.",
147 &options_.no_version_vectors);
148 AddOptionalSwitch("--no-version-transitions",
149 "Disables automatic versioning of transition resources. Use this only\n"
150 "when building with transition support library.",
151 &options_.no_version_transitions);
152 AddOptionalSwitch("--no-resource-deduping", "Disables automatic deduping of resources with\n"
153 "identical values across compatible configurations.",
154 &options_.no_resource_deduping);
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200155 AddOptionalSwitch("--no-resource-removal", "Disables automatic removal of resources without\n"
156 "defaults. Use this only when building runtime resource overlay packages.",
157 &options_.no_resource_removal);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700158 AddOptionalSwitch("--enable-sparse-encoding",
159 "This decreases APK size at the cost of resource retrieval performance.",
160 &options_.table_flattener_options.use_sparse_entries);
161 AddOptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
162 &legacy_x_flag_);
163 AddOptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
164 &require_localization_);
165 AddOptionalFlagList("-c",
166 "Comma separated list of configurations to include. The default\n"
167 "is all configurations.", &configs_);
168 AddOptionalFlag("--preferred-density",
169 "Selects the closest matching density and strips out all others.",
170 &preferred_density_);
171 AddOptionalFlag("--product", "Comma separated list of product names to keep", &product_list_);
172 AddOptionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified by -o.",
173 &options_.output_to_directory);
174 AddOptionalSwitch("--no-xml-namespaces", "Removes XML namespace prefix and URI information\n"
175 "from AndroidManifest.xml and XML binaries in res/*.",
176 &options_.no_xml_namespaces);
177 AddOptionalFlag("--min-sdk-version",
178 "Default minimum SDK version to use for AndroidManifest.xml.",
179 &options_.manifest_fixer_options.min_sdk_version_default);
180 AddOptionalFlag("--target-sdk-version",
181 "Default target SDK version to use for AndroidManifest.xml.",
182 &options_.manifest_fixer_options.target_sdk_version_default);
183 AddOptionalFlag("--version-code",
184 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
Ryan Mitchell704090e2018-07-31 14:59:25 -0700185 "present.", &options_.manifest_fixer_options.version_code_default);
186 AddOptionalFlag("--version-code-major",
187 "Version code major (integer) to inject into the AndroidManifest.xml if none is\n"
188 "present.", &options_.manifest_fixer_options.version_code_major_default);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700189 AddOptionalFlag("--version-name",
190 "Version name to inject into the AndroidManifest.xml if none is present.",
191 &options_.manifest_fixer_options.version_name_default);
192 AddOptionalSwitch("--replace-version",
193 "If --version-code and/or --version-name are specified, these\n"
194 "values will replace any value already in the manifest. By\n"
195 "default, nothing is changed if the manifest already defines\n"
196 "these attributes.",
197 &options_.manifest_fixer_options.replace_version);
198 AddOptionalFlag("--compile-sdk-version-code",
199 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
200 "present.",
201 &options_.manifest_fixer_options.compile_sdk_version);
202 AddOptionalFlag("--compile-sdk-version-name",
203 "Version name to inject into the AndroidManifest.xml if none is present.",
204 &options_.manifest_fixer_options.compile_sdk_version_codename);
205 AddOptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
206 &shared_lib_);
207 AddOptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib_);
208 AddOptionalSwitch("--proto-format",
209 "Generates compiled resources in Protobuf format.\n"
210 "Suitable as input to the bundle tool for generating an App Bundle.",
211 &proto_format_);
212 AddOptionalSwitch("--no-static-lib-packages",
213 "Merge all library resources under the app's package.",
214 &options_.no_static_lib_packages);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700215 AddOptionalSwitch("--non-final-ids",
216 "Generates R.java without the final modifier. This is implied when\n"
217 "--static-lib is specified.",
218 &options_.generate_non_final_ids);
Jean-Luc Coelho181cbfd2019-11-27 12:37:48 -0800219 AddOptionalSwitch("--no-proguard-location-reference",
220 "Keep proguard rules files from having a reference to the source file",
221 &options_.no_proguard_location_reference);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700222 AddOptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
223 &stable_id_file_path_);
224 AddOptionalFlag("--emit-ids",
225 "Emit a file at the given path with a list of name to ID mappings,\n"
226 "suitable for use with --stable-ids.",
227 &options_.resource_id_map_path);
228 AddOptionalFlag("--private-symbols",
229 "Package name to use when generating R.java for private symbols.\n"
230 "If not specified, public and private symbols will use the application's\n"
231 "package name.",
232 &options_.private_symbols);
233 AddOptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
234 &options_.custom_java_package);
235 AddOptionalFlagList("--extra-packages",
236 "Generate the same R.java but with different package names.",
237 &extra_java_packages_);
238 AddOptionalFlagList("--add-javadoc-annotation",
239 "Adds a JavaDoc annotation to all generated Java classes.",
240 &options_.javadoc_annotations);
241 AddOptionalFlag("--output-text-symbols",
242 "Generates a text file containing the resource symbols of the R class in\n"
243 "the specified folder.",
244 &options_.generate_text_symbols_path);
245 AddOptionalSwitch("--allow-reserved-package-id",
246 "Allows the use of a reserved package ID. This should on be used for\n"
247 "packages with a pre-O min-sdk\n",
248 &options_.allow_reserved_package_id);
249 AddOptionalSwitch("--auto-add-overlay",
250 "Allows the addition of new resources in overlays without\n"
251 "<add-resource> tags.",
252 &options_.auto_add_overlay);
Donald Chai121c6e82019-06-12 12:51:57 -0700253 AddOptionalSwitch("--override-styles-instead-of-overlaying",
Ryan Mitchellef9e6882019-06-24 11:53:33 -0700254 "Causes styles defined in -R resources to replace previous definitions\n"
255 "instead of merging into them\n",
256 &options_.override_styles_instead_of_overlaying);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700257 AddOptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
258 &options_.manifest_fixer_options.rename_manifest_package);
259 AddOptionalFlag("--rename-instrumentation-target-package",
260 "Changes the name of the target package for instrumentation. Most useful\n"
261 "when used in conjunction with --rename-manifest-package.",
262 &options_.manifest_fixer_options.rename_instrumentation_target_package);
Ryan Mitchell81dfca02019-06-07 10:20:27 -0700263 AddOptionalFlagList("-0", "File suffix not to compress.",
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700264 &options_.extensions_to_not_compress);
265 AddOptionalSwitch("--no-compress", "Do not compress any resources.",
266 &options_.do_not_compress_anything);
Ryan Mitchell479fa392019-01-02 17:15:39 -0800267 AddOptionalSwitch("--keep-raw-values", "Preserve raw attribute values in xml files.",
268 &options_.keep_raw_values);
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100269 AddOptionalFlag("--no-compress-regex",
270 "Do not compress extensions matching the regular expression. Remember to\n"
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000271 "use the '$' symbol for end of line. Uses a case-sensitive ECMAScript"
272 "regular expression grammar.",
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100273 &no_compress_regex);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700274 AddOptionalSwitch("--warn-manifest-validation",
275 "Treat manifest validation errors as warnings.",
276 &options_.manifest_fixer_options.warn_validation);
277 AddOptionalFlagList("--split",
278 "Split resources matching a set of configs out to a Split APK.\n"
279 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
280 "On Windows, use a semicolon ';' separator instead.",
281 &split_args_);
Winson3c918b82019-01-25 14:25:37 -0800282 AddOptionalFlagList("--exclude-configs",
283 "Excludes values of resources whose configs contain the specified qualifiers.",
284 &options_.exclude_configs_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700285 AddOptionalSwitch("--debug-mode",
286 "Inserts android:debuggable=\"true\" in to the application node of the\n"
287 "manifest, making the application debuggable even on production devices.",
288 &options_.manifest_fixer_options.debug_mode);
289 AddOptionalSwitch("--strict-visibility",
290 "Do not allow overlays with different visibility levels.",
291 &options_.strict_visibility);
Ryan Mitchellef9e6882019-06-24 11:53:33 -0700292 AddOptionalSwitch("--exclude-sources",
293 "Do not serialize source file information when generating resources in\n"
294 "Protobuf format.",
295 &options_.proto_table_flattener_options.exclude_sources);
296 AddOptionalFlag("--trace-folder",
297 "Generate systrace json trace fragment to specified folder.",
298 &trace_folder_);
Izabela Orlowska84febea2019-06-03 18:34:12 +0100299 AddOptionalSwitch("--merge-only",
Ryan Mitchellef9e6882019-06-24 11:53:33 -0700300 "Only merge the resources, without verifying resource references. This flag\n"
301 "should only be used together with the --static-lib flag.",
302 &options_.merge_only);
303 AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700304 }
305
306 int Action(const std::vector<std::string>& args) override;
307
308 private:
309 IDiagnostics* diag_;
310 LinkOptions options_;
311
312 std::vector<std::string> overlay_arg_list_;
313 std::vector<std::string> extra_java_packages_;
314 Maybe<std::string> package_id_;
315 std::vector<std::string> configs_;
316 Maybe<std::string> preferred_density_;
317 Maybe<std::string> product_list_;
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100318 Maybe<std::string> no_compress_regex;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700319 bool legacy_x_flag_ = false;
320 bool require_localization_ = false;
321 bool verbose_ = false;
322 bool shared_lib_ = false;
323 bool static_lib_ = false;
324 bool proto_format_ = false;
325 Maybe<std::string> stable_id_file_path_;
326 std::vector<std::string> split_args_;
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800327 Maybe<std::string> trace_folder_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700328};
329
330}// namespace aapt
331
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200332#endif //AAPT2_LINK_H