blob: a5623cb1f1e16e6a840a30a01640a67a427a2bc2 [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"
Ryan Mitchell833a1a62018-07-10 13:51:36 -070023#include "Resource.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000024#include "androidfw/IDiagnostics.h"
Ryan Mitchell833a1a62018-07-10 13:51:36 -070025#include "format/binary/TableFlattener.h"
Ryan Mitchellef9e6882019-06-24 11:53:33 -070026#include "format/proto/ProtoSerialize.h"
Ryan Mitchell833a1a62018-07-10 13:51:36 -070027#include "link/ManifestFixer.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000028#include "split/TableSplitter.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;
Ryan Mitchell4382e442021-07-14 12:53:01 -070048 std::optional<std::string> rename_resources_package;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070049
50 // Java/Proguard options.
Ryan Mitchell4382e442021-07-14 12:53:01 -070051 std::optional<std::string> generate_java_class_path;
52 std::optional<std::string> custom_java_package;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070053 std::set<std::string> extra_java_packages;
Ryan Mitchell4382e442021-07-14 12:53:01 -070054 std::optional<std::string> generate_text_symbols_path;
55 std::optional<std::string> generate_proguard_rules_path;
56 std::optional<std::string> generate_main_dex_proguard_rules_path;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070057 bool generate_conditional_proguard_rules = false;
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070058 bool generate_minimal_proguard_rules = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070059 bool generate_non_final_ids = false;
Jean-Luc Coelho181cbfd2019-11-27 12:37:48 -080060 bool no_proguard_location_reference = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070061 std::vector<std::string> javadoc_annotations;
Ryan Mitchell4382e442021-07-14 12:53:01 -070062 std::optional<std::string> private_symbols;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070063
64 // Optimizations/features.
65 bool no_auto_version = false;
66 bool no_version_vectors = false;
67 bool no_version_transitions = false;
68 bool no_resource_deduping = false;
Mårten Kongstadd8d29012018-06-11 14:13:37 +020069 bool no_resource_removal = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070070 bool no_xml_namespaces = false;
71 bool do_not_compress_anything = false;
72 std::unordered_set<std::string> extensions_to_not_compress;
Ryan Mitchell4382e442021-07-14 12:53:01 -070073 std::optional<std::regex> regex_to_not_compress;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070074
75 // Static lib options.
76 bool no_static_lib_packages = false;
Izabela Orlowska84febea2019-06-03 18:34:12 +010077 bool merge_only = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070078
79 // AndroidManifest.xml massaging options.
80 ManifestFixerOptions manifest_fixer_options;
81
82 // Products to use/filter on.
83 std::unordered_set<std::string> products;
84
85 // Flattening options.
86 TableFlattenerOptions table_flattener_options;
Ryan Mitchellef9e6882019-06-24 11:53:33 -070087 SerializeTableOptions proto_table_flattener_options;
Ryan Mitchell479fa392019-01-02 17:15:39 -080088 bool keep_raw_values = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070089
90 // Split APK options.
91 TableSplitterOptions table_splitter_options;
92 std::vector<SplitConstraints> split_constraints;
93 std::vector<std::string> split_paths;
94
Winson3c918b82019-01-25 14:25:37 -080095 // Configurations to exclude
96 std::vector<std::string> exclude_configs_;
97
Ryan Mitchell833a1a62018-07-10 13:51:36 -070098 // Stable ID options.
99 std::unordered_map<ResourceName, ResourceId> stable_id_map;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700100 std::optional<std::string> resource_id_map_path;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700101
102 // When 'true', allow reserved package IDs to be used for applications. Pre-O, the platform
103 // treats negative resource IDs [those with a package ID of 0x80 or higher] as invalid.
104 // In order to work around this limitation, we allow the use of traditionally reserved
105 // resource IDs [those between 0x02 and 0x7E].
106 bool allow_reserved_package_id = false;
107
108 // Whether we should fail on definitions of a resource with conflicting visibility.
109 bool strict_visibility = false;
110};
111
112class LinkCommand : public Command {
113 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000114 explicit LinkCommand(android::IDiagnostics* diag) : Command("link", "l"), diag_(diag) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700115 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);
Rhed Jaob9ccb8a42020-11-30 21:42:16 +0800192 AddOptionalFlag("--revision-code",
193 "Revision code (integer) to inject into the AndroidManifest.xml if none is\n"
194 "present.", &options_.manifest_fixer_options.revision_code_default);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700195 AddOptionalSwitch("--replace-version",
Rhed Jaob9ccb8a42020-11-30 21:42:16 +0800196 "If --version-code, --version-name, and/or --revision-code are specified, these\n"
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700197 "values will replace any value already in the manifest. By\n"
198 "default, nothing is changed if the manifest already defines\n"
199 "these attributes.",
200 &options_.manifest_fixer_options.replace_version);
201 AddOptionalFlag("--compile-sdk-version-code",
202 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
203 "present.",
204 &options_.manifest_fixer_options.compile_sdk_version);
205 AddOptionalFlag("--compile-sdk-version-name",
206 "Version name to inject into the AndroidManifest.xml if none is present.",
207 &options_.manifest_fixer_options.compile_sdk_version_codename);
208 AddOptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
209 &shared_lib_);
210 AddOptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib_);
211 AddOptionalSwitch("--proto-format",
212 "Generates compiled resources in Protobuf format.\n"
213 "Suitable as input to the bundle tool for generating an App Bundle.",
214 &proto_format_);
215 AddOptionalSwitch("--no-static-lib-packages",
216 "Merge all library resources under the app's package.",
217 &options_.no_static_lib_packages);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700218 AddOptionalSwitch("--non-final-ids",
219 "Generates R.java without the final modifier. This is implied when\n"
220 "--static-lib is specified.",
221 &options_.generate_non_final_ids);
Jean-Luc Coelho181cbfd2019-11-27 12:37:48 -0800222 AddOptionalSwitch("--no-proguard-location-reference",
223 "Keep proguard rules files from having a reference to the source file",
224 &options_.no_proguard_location_reference);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700225 AddOptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
226 &stable_id_file_path_);
227 AddOptionalFlag("--emit-ids",
228 "Emit a file at the given path with a list of name to ID mappings,\n"
229 "suitable for use with --stable-ids.",
230 &options_.resource_id_map_path);
231 AddOptionalFlag("--private-symbols",
232 "Package name to use when generating R.java for private symbols.\n"
233 "If not specified, public and private symbols will use the application's\n"
234 "package name.",
235 &options_.private_symbols);
236 AddOptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
237 &options_.custom_java_package);
238 AddOptionalFlagList("--extra-packages",
239 "Generate the same R.java but with different package names.",
240 &extra_java_packages_);
241 AddOptionalFlagList("--add-javadoc-annotation",
242 "Adds a JavaDoc annotation to all generated Java classes.",
243 &options_.javadoc_annotations);
244 AddOptionalFlag("--output-text-symbols",
245 "Generates a text file containing the resource symbols of the R class in\n"
246 "the specified folder.",
247 &options_.generate_text_symbols_path);
248 AddOptionalSwitch("--allow-reserved-package-id",
249 "Allows the use of a reserved package ID. This should on be used for\n"
250 "packages with a pre-O min-sdk\n",
251 &options_.allow_reserved_package_id);
252 AddOptionalSwitch("--auto-add-overlay",
253 "Allows the addition of new resources in overlays without\n"
254 "<add-resource> tags.",
255 &options_.auto_add_overlay);
Donald Chai121c6e82019-06-12 12:51:57 -0700256 AddOptionalSwitch("--override-styles-instead-of-overlaying",
Ryan Mitchellef9e6882019-06-24 11:53:33 -0700257 "Causes styles defined in -R resources to replace previous definitions\n"
258 "instead of merging into them\n",
259 &options_.override_styles_instead_of_overlaying);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700260 AddOptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
261 &options_.manifest_fixer_options.rename_manifest_package);
Ryan Mitchell60b74fb2020-02-26 12:45:39 -0800262 AddOptionalFlag("--rename-resources-package", "Renames the package in resources table",
263 &options_.rename_resources_package);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700264 AddOptionalFlag("--rename-instrumentation-target-package",
265 "Changes the name of the target package for instrumentation. Most useful\n"
266 "when used in conjunction with --rename-manifest-package.",
267 &options_.manifest_fixer_options.rename_instrumentation_target_package);
Roshan Piusae12ce42020-04-25 16:08:55 -0700268 AddOptionalFlag("--rename-overlay-target-package",
269 "Changes the name of the target package for overlay. Most useful\n"
270 "when used in conjunction with --rename-manifest-package.",
271 &options_.manifest_fixer_options.rename_overlay_target_package);
Ryan Mitchell81dfca0e2019-06-07 10:20:27 -0700272 AddOptionalFlagList("-0", "File suffix not to compress.",
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700273 &options_.extensions_to_not_compress);
274 AddOptionalSwitch("--no-compress", "Do not compress any resources.",
275 &options_.do_not_compress_anything);
Ryan Mitchell479fa392019-01-02 17:15:39 -0800276 AddOptionalSwitch("--keep-raw-values", "Preserve raw attribute values in xml files.",
277 &options_.keep_raw_values);
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100278 AddOptionalFlag("--no-compress-regex",
279 "Do not compress extensions matching the regular expression. Remember to\n"
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000280 "use the '$' symbol for end of line. Uses a case-sensitive ECMAScript"
281 "regular expression grammar.",
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100282 &no_compress_regex);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700283 AddOptionalSwitch("--warn-manifest-validation",
284 "Treat manifest validation errors as warnings.",
285 &options_.manifest_fixer_options.warn_validation);
286 AddOptionalFlagList("--split",
287 "Split resources matching a set of configs out to a Split APK.\n"
288 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
289 "On Windows, use a semicolon ';' separator instead.",
290 &split_args_);
Winson3c918b82019-01-25 14:25:37 -0800291 AddOptionalFlagList("--exclude-configs",
292 "Excludes values of resources whose configs contain the specified qualifiers.",
293 &options_.exclude_configs_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700294 AddOptionalSwitch("--debug-mode",
295 "Inserts android:debuggable=\"true\" in to the application node of the\n"
296 "manifest, making the application debuggable even on production devices.",
297 &options_.manifest_fixer_options.debug_mode);
298 AddOptionalSwitch("--strict-visibility",
299 "Do not allow overlays with different visibility levels.",
300 &options_.strict_visibility);
Ryan Mitchellef9e6882019-06-24 11:53:33 -0700301 AddOptionalSwitch("--exclude-sources",
302 "Do not serialize source file information when generating resources in\n"
303 "Protobuf format.",
304 &options_.proto_table_flattener_options.exclude_sources);
305 AddOptionalFlag("--trace-folder",
306 "Generate systrace json trace fragment to specified folder.",
307 &trace_folder_);
Izabela Orlowska84febea2019-06-03 18:34:12 +0100308 AddOptionalSwitch("--merge-only",
Ryan Mitchellef9e6882019-06-24 11:53:33 -0700309 "Only merge the resources, without verifying resource references. This flag\n"
310 "should only be used together with the --static-lib flag.",
311 &options_.merge_only);
312 AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700313 }
314
315 int Action(const std::vector<std::string>& args) override;
316
317 private:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000318 android::IDiagnostics* diag_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700319 LinkOptions options_;
320
321 std::vector<std::string> overlay_arg_list_;
322 std::vector<std::string> extra_java_packages_;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700323 std::optional<std::string> package_id_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700324 std::vector<std::string> configs_;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700325 std::optional<std::string> preferred_density_;
326 std::optional<std::string> product_list_;
327 std::optional<std::string> no_compress_regex;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700328 bool legacy_x_flag_ = false;
329 bool require_localization_ = false;
330 bool verbose_ = false;
331 bool shared_lib_ = false;
332 bool static_lib_ = false;
333 bool proto_format_ = false;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700334 std::optional<std::string> stable_id_file_path_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700335 std::vector<std::string> split_args_;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700336 std::optional<std::string> trace_folder_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700337};
338
339}// namespace aapt
340
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200341#endif //AAPT2_LINK_H