blob: 324807c55215e841cd6411aa376818ee6f219df4 [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"
27#include "link/ManifestFixer.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080028#include "trace/TraceBuffer.h"
Ryan Mitchell833a1a62018-07-10 13:51:36 -070029
30namespace aapt {
31
32enum class OutputFormat {
33 kApk,
34 kProto,
35};
36
37struct LinkOptions {
38 std::string output_path;
39 std::string manifest_path;
40 std::vector<std::string> include_paths;
41 std::vector<std::string> overlay_files;
42 std::vector<std::string> assets_dirs;
43 bool output_to_directory = false;
44 bool auto_add_overlay = false;
Donald Chai121c6e82019-06-12 12:51:57 -070045 bool override_styles_instead_of_overlaying = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070046 OutputFormat output_format = OutputFormat::kApk;
47
48 // Java/Proguard options.
49 Maybe<std::string> generate_java_class_path;
50 Maybe<std::string> custom_java_package;
51 std::set<std::string> extra_java_packages;
52 Maybe<std::string> generate_text_symbols_path;
53 Maybe<std::string> generate_proguard_rules_path;
54 Maybe<std::string> generate_main_dex_proguard_rules_path;
55 bool generate_conditional_proguard_rules = false;
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070056 bool generate_minimal_proguard_rules = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070057 bool generate_non_final_ids = false;
58 std::vector<std::string> javadoc_annotations;
59 Maybe<std::string> private_symbols;
60
61 // Optimizations/features.
62 bool no_auto_version = false;
63 bool no_version_vectors = false;
64 bool no_version_transitions = false;
65 bool no_resource_deduping = false;
Mårten Kongstadd8d29012018-06-11 14:13:37 +020066 bool no_resource_removal = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070067 bool no_xml_namespaces = false;
68 bool do_not_compress_anything = false;
69 std::unordered_set<std::string> extensions_to_not_compress;
Izabela Orlowska0faba5f2018-06-01 12:06:31 +010070 Maybe<std::regex> regex_to_not_compress;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070071
72 // Static lib options.
73 bool no_static_lib_packages = false;
Izabela Orlowska84febea2019-06-03 18:34:12 +010074 bool merge_only = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070075
76 // AndroidManifest.xml massaging options.
77 ManifestFixerOptions manifest_fixer_options;
78
79 // Products to use/filter on.
80 std::unordered_set<std::string> products;
81
82 // Flattening options.
83 TableFlattenerOptions table_flattener_options;
Ryan Mitchell479fa392019-01-02 17:15:39 -080084 bool keep_raw_values = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070085
86 // Split APK options.
87 TableSplitterOptions table_splitter_options;
88 std::vector<SplitConstraints> split_constraints;
89 std::vector<std::string> split_paths;
90
Winson3c918b82019-01-25 14:25:37 -080091 // Configurations to exclude
92 std::vector<std::string> exclude_configs_;
93
Ryan Mitchell833a1a62018-07-10 13:51:36 -070094 // Stable ID options.
95 std::unordered_map<ResourceName, ResourceId> stable_id_map;
96 Maybe<std::string> resource_id_map_path;
97
98 // When 'true', allow reserved package IDs to be used for applications. Pre-O, the platform
99 // treats negative resource IDs [those with a package ID of 0x80 or higher] as invalid.
100 // In order to work around this limitation, we allow the use of traditionally reserved
101 // resource IDs [those between 0x02 and 0x7E].
102 bool allow_reserved_package_id = false;
103
104 // Whether we should fail on definitions of a resource with conflicting visibility.
105 bool strict_visibility = false;
106};
107
108class LinkCommand : public Command {
109 public:
110 explicit LinkCommand(IDiagnostics* diag) : Command("link", "l"),
111 diag_(diag) {
112 SetDescription("Links resources into an apk.");
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800113 AddRequiredFlag("-o", "Output path.", &options_.output_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700114 AddRequiredFlag("--manifest", "Path to the Android manifest to build.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800115 &options_.manifest_path, Command::kPath);
116 AddOptionalFlagList("-I", "Adds an Android APK to link against.", &options_.include_paths,
117 Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700118 AddOptionalFlagList("-A", "An assets directory to include in the APK. These are unprocessed.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800119 &options_.assets_dirs, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700120 AddOptionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n"
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800121 "The last conflicting resource given takes precedence.", &overlay_arg_list_,
122 Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700123 AddOptionalFlag("--package-id",
124 "Specify the package ID to use for this app. Must be greater or equal to\n"
125 "0x7f and can't be used with --static-lib or --shared-lib.", &package_id_);
126 AddOptionalFlag("--java", "Directory in which to generate R.java.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800127 &options_.generate_java_class_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700128 AddOptionalFlag("--proguard", "Output file for generated Proguard rules.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800129 &options_.generate_proguard_rules_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700130 AddOptionalFlag("--proguard-main-dex",
131 "Output file for generated Proguard rules for the main dex.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800132 &options_.generate_main_dex_proguard_rules_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700133 AddOptionalSwitch("--proguard-conditional-keep-rules",
134 "Generate conditional Proguard keep rules.",
135 &options_.generate_conditional_proguard_rules);
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700136 AddOptionalSwitch("--proguard-minimal-keep-rules",
137 "Generate a minimal set of Proguard keep rules.",
138 &options_.generate_minimal_proguard_rules);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700139 AddOptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning.",
140 &options_.no_auto_version);
141 AddOptionalSwitch("--no-version-vectors",
142 "Disables automatic versioning of vector drawables. Use this only\n"
143 "when building with vector drawable support library.",
144 &options_.no_version_vectors);
145 AddOptionalSwitch("--no-version-transitions",
146 "Disables automatic versioning of transition resources. Use this only\n"
147 "when building with transition support library.",
148 &options_.no_version_transitions);
149 AddOptionalSwitch("--no-resource-deduping", "Disables automatic deduping of resources with\n"
150 "identical values across compatible configurations.",
151 &options_.no_resource_deduping);
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200152 AddOptionalSwitch("--no-resource-removal", "Disables automatic removal of resources without\n"
153 "defaults. Use this only when building runtime resource overlay packages.",
154 &options_.no_resource_removal);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700155 AddOptionalSwitch("--enable-sparse-encoding",
156 "This decreases APK size at the cost of resource retrieval performance.",
157 &options_.table_flattener_options.use_sparse_entries);
158 AddOptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
159 &legacy_x_flag_);
160 AddOptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
161 &require_localization_);
162 AddOptionalFlagList("-c",
163 "Comma separated list of configurations to include. The default\n"
164 "is all configurations.", &configs_);
165 AddOptionalFlag("--preferred-density",
166 "Selects the closest matching density and strips out all others.",
167 &preferred_density_);
168 AddOptionalFlag("--product", "Comma separated list of product names to keep", &product_list_);
169 AddOptionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified by -o.",
170 &options_.output_to_directory);
171 AddOptionalSwitch("--no-xml-namespaces", "Removes XML namespace prefix and URI information\n"
172 "from AndroidManifest.xml and XML binaries in res/*.",
173 &options_.no_xml_namespaces);
174 AddOptionalFlag("--min-sdk-version",
175 "Default minimum SDK version to use for AndroidManifest.xml.",
176 &options_.manifest_fixer_options.min_sdk_version_default);
177 AddOptionalFlag("--target-sdk-version",
178 "Default target SDK version to use for AndroidManifest.xml.",
179 &options_.manifest_fixer_options.target_sdk_version_default);
180 AddOptionalFlag("--version-code",
181 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
Ryan Mitchell704090e2018-07-31 14:59:25 -0700182 "present.", &options_.manifest_fixer_options.version_code_default);
183 AddOptionalFlag("--version-code-major",
184 "Version code major (integer) to inject into the AndroidManifest.xml if none is\n"
185 "present.", &options_.manifest_fixer_options.version_code_major_default);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700186 AddOptionalFlag("--version-name",
187 "Version name to inject into the AndroidManifest.xml if none is present.",
188 &options_.manifest_fixer_options.version_name_default);
189 AddOptionalSwitch("--replace-version",
190 "If --version-code and/or --version-name are specified, these\n"
191 "values will replace any value already in the manifest. By\n"
192 "default, nothing is changed if the manifest already defines\n"
193 "these attributes.",
194 &options_.manifest_fixer_options.replace_version);
195 AddOptionalFlag("--compile-sdk-version-code",
196 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
197 "present.",
198 &options_.manifest_fixer_options.compile_sdk_version);
199 AddOptionalFlag("--compile-sdk-version-name",
200 "Version name to inject into the AndroidManifest.xml if none is present.",
201 &options_.manifest_fixer_options.compile_sdk_version_codename);
202 AddOptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
203 &shared_lib_);
204 AddOptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib_);
205 AddOptionalSwitch("--proto-format",
206 "Generates compiled resources in Protobuf format.\n"
207 "Suitable as input to the bundle tool for generating an App Bundle.",
208 &proto_format_);
209 AddOptionalSwitch("--no-static-lib-packages",
210 "Merge all library resources under the app's package.",
211 &options_.no_static_lib_packages);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700212 AddOptionalSwitch("--non-final-ids",
213 "Generates R.java without the final modifier. This is implied when\n"
214 "--static-lib is specified.",
215 &options_.generate_non_final_ids);
216 AddOptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
217 &stable_id_file_path_);
218 AddOptionalFlag("--emit-ids",
219 "Emit a file at the given path with a list of name to ID mappings,\n"
220 "suitable for use with --stable-ids.",
221 &options_.resource_id_map_path);
222 AddOptionalFlag("--private-symbols",
223 "Package name to use when generating R.java for private symbols.\n"
224 "If not specified, public and private symbols will use the application's\n"
225 "package name.",
226 &options_.private_symbols);
227 AddOptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
228 &options_.custom_java_package);
229 AddOptionalFlagList("--extra-packages",
230 "Generate the same R.java but with different package names.",
231 &extra_java_packages_);
232 AddOptionalFlagList("--add-javadoc-annotation",
233 "Adds a JavaDoc annotation to all generated Java classes.",
234 &options_.javadoc_annotations);
235 AddOptionalFlag("--output-text-symbols",
236 "Generates a text file containing the resource symbols of the R class in\n"
237 "the specified folder.",
238 &options_.generate_text_symbols_path);
239 AddOptionalSwitch("--allow-reserved-package-id",
240 "Allows the use of a reserved package ID. This should on be used for\n"
241 "packages with a pre-O min-sdk\n",
242 &options_.allow_reserved_package_id);
243 AddOptionalSwitch("--auto-add-overlay",
244 "Allows the addition of new resources in overlays without\n"
245 "<add-resource> tags.",
246 &options_.auto_add_overlay);
Donald Chai121c6e82019-06-12 12:51:57 -0700247 AddOptionalSwitch("--override-styles-instead-of-overlaying",
248 "Causes styles defined in -R resources to replace previous definitions\n"
249 "instead of merging into them\n",
250 &options_.override_styles_instead_of_overlaying);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700251 AddOptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
252 &options_.manifest_fixer_options.rename_manifest_package);
253 AddOptionalFlag("--rename-instrumentation-target-package",
254 "Changes the name of the target package for instrumentation. Most useful\n"
255 "when used in conjunction with --rename-manifest-package.",
256 &options_.manifest_fixer_options.rename_instrumentation_target_package);
Ryan Mitchell81dfca02019-06-07 10:20:27 -0700257 AddOptionalFlagList("-0", "File suffix not to compress.",
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700258 &options_.extensions_to_not_compress);
259 AddOptionalSwitch("--no-compress", "Do not compress any resources.",
260 &options_.do_not_compress_anything);
Ryan Mitchell479fa392019-01-02 17:15:39 -0800261 AddOptionalSwitch("--keep-raw-values", "Preserve raw attribute values in xml files.",
262 &options_.keep_raw_values);
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100263 AddOptionalFlag("--no-compress-regex",
264 "Do not compress extensions matching the regular expression. Remember to\n"
265 " use the '$' symbol for end of line. Uses a non case-sensitive\n"
266 " ECMAScript regular expression grammar.",
267 &no_compress_regex);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700268 AddOptionalSwitch("--warn-manifest-validation",
269 "Treat manifest validation errors as warnings.",
270 &options_.manifest_fixer_options.warn_validation);
271 AddOptionalFlagList("--split",
272 "Split resources matching a set of configs out to a Split APK.\n"
273 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
274 "On Windows, use a semicolon ';' separator instead.",
275 &split_args_);
Winson3c918b82019-01-25 14:25:37 -0800276 AddOptionalFlagList("--exclude-configs",
277 "Excludes values of resources whose configs contain the specified qualifiers.",
278 &options_.exclude_configs_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700279 AddOptionalSwitch("--debug-mode",
280 "Inserts android:debuggable=\"true\" in to the application node of the\n"
281 "manifest, making the application debuggable even on production devices.",
282 &options_.manifest_fixer_options.debug_mode);
283 AddOptionalSwitch("--strict-visibility",
284 "Do not allow overlays with different visibility levels.",
285 &options_.strict_visibility);
Ryan Mitchell479fa392019-01-02 17:15:39 -0800286 AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800287 AddOptionalFlag("--trace-folder", "Generate systrace json trace fragment to specified folder.",
288 &trace_folder_);
Izabela Orlowska84febea2019-06-03 18:34:12 +0100289 AddOptionalSwitch("--merge-only",
290 "Only merge the resources, without verifying resource references. This flag\n"
291 "should only be used together with the --static-lib flag.",
292 &options_.merge_only);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700293 }
294
295 int Action(const std::vector<std::string>& args) override;
296
297 private:
298 IDiagnostics* diag_;
299 LinkOptions options_;
300
301 std::vector<std::string> overlay_arg_list_;
302 std::vector<std::string> extra_java_packages_;
303 Maybe<std::string> package_id_;
304 std::vector<std::string> configs_;
305 Maybe<std::string> preferred_density_;
306 Maybe<std::string> product_list_;
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100307 Maybe<std::string> no_compress_regex;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700308 bool legacy_x_flag_ = false;
309 bool require_localization_ = false;
310 bool verbose_ = false;
311 bool shared_lib_ = false;
312 bool static_lib_ = false;
313 bool proto_format_ = false;
314 Maybe<std::string> stable_id_file_path_;
315 std::vector<std::string> split_args_;
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800316 Maybe<std::string> trace_folder_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700317};
318
319}// namespace aapt
320
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200321#endif //AAPT2_LINK_H