blob: 37765f6b5d0880638a22ada315810cfa69544dce [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;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070074
75 // AndroidManifest.xml massaging options.
76 ManifestFixerOptions manifest_fixer_options;
77
78 // Products to use/filter on.
79 std::unordered_set<std::string> products;
80
81 // Flattening options.
82 TableFlattenerOptions table_flattener_options;
Ryan Mitchell479fa392019-01-02 17:15:39 -080083 bool keep_raw_values = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070084
85 // Split APK options.
86 TableSplitterOptions table_splitter_options;
87 std::vector<SplitConstraints> split_constraints;
88 std::vector<std::string> split_paths;
89
Winson3c918b82019-01-25 14:25:37 -080090 // Configurations to exclude
91 std::vector<std::string> exclude_configs_;
92
Ryan Mitchell833a1a62018-07-10 13:51:36 -070093 // Stable ID options.
94 std::unordered_map<ResourceName, ResourceId> stable_id_map;
95 Maybe<std::string> resource_id_map_path;
96
97 // When 'true', allow reserved package IDs to be used for applications. Pre-O, the platform
98 // treats negative resource IDs [those with a package ID of 0x80 or higher] as invalid.
99 // In order to work around this limitation, we allow the use of traditionally reserved
100 // resource IDs [those between 0x02 and 0x7E].
101 bool allow_reserved_package_id = false;
102
103 // Whether we should fail on definitions of a resource with conflicting visibility.
104 bool strict_visibility = false;
105};
106
107class LinkCommand : public Command {
108 public:
109 explicit LinkCommand(IDiagnostics* diag) : Command("link", "l"),
110 diag_(diag) {
111 SetDescription("Links resources into an apk.");
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800112 AddRequiredFlag("-o", "Output path.", &options_.output_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700113 AddRequiredFlag("--manifest", "Path to the Android manifest to build.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800114 &options_.manifest_path, Command::kPath);
115 AddOptionalFlagList("-I", "Adds an Android APK to link against.", &options_.include_paths,
116 Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700117 AddOptionalFlagList("-A", "An assets directory to include in the APK. These are unprocessed.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800118 &options_.assets_dirs, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700119 AddOptionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n"
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800120 "The last conflicting resource given takes precedence.", &overlay_arg_list_,
121 Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700122 AddOptionalFlag("--package-id",
123 "Specify the package ID to use for this app. Must be greater or equal to\n"
124 "0x7f and can't be used with --static-lib or --shared-lib.", &package_id_);
125 AddOptionalFlag("--java", "Directory in which to generate R.java.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800126 &options_.generate_java_class_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700127 AddOptionalFlag("--proguard", "Output file for generated Proguard rules.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800128 &options_.generate_proguard_rules_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700129 AddOptionalFlag("--proguard-main-dex",
130 "Output file for generated Proguard rules for the main dex.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800131 &options_.generate_main_dex_proguard_rules_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700132 AddOptionalSwitch("--proguard-conditional-keep-rules",
133 "Generate conditional Proguard keep rules.",
134 &options_.generate_conditional_proguard_rules);
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700135 AddOptionalSwitch("--proguard-minimal-keep-rules",
136 "Generate a minimal set of Proguard keep rules.",
137 &options_.generate_minimal_proguard_rules);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700138 AddOptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning.",
139 &options_.no_auto_version);
140 AddOptionalSwitch("--no-version-vectors",
141 "Disables automatic versioning of vector drawables. Use this only\n"
142 "when building with vector drawable support library.",
143 &options_.no_version_vectors);
144 AddOptionalSwitch("--no-version-transitions",
145 "Disables automatic versioning of transition resources. Use this only\n"
146 "when building with transition support library.",
147 &options_.no_version_transitions);
148 AddOptionalSwitch("--no-resource-deduping", "Disables automatic deduping of resources with\n"
149 "identical values across compatible configurations.",
150 &options_.no_resource_deduping);
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200151 AddOptionalSwitch("--no-resource-removal", "Disables automatic removal of resources without\n"
152 "defaults. Use this only when building runtime resource overlay packages.",
153 &options_.no_resource_removal);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700154 AddOptionalSwitch("--enable-sparse-encoding",
155 "This decreases APK size at the cost of resource retrieval performance.",
156 &options_.table_flattener_options.use_sparse_entries);
157 AddOptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
158 &legacy_x_flag_);
159 AddOptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
160 &require_localization_);
161 AddOptionalFlagList("-c",
162 "Comma separated list of configurations to include. The default\n"
163 "is all configurations.", &configs_);
164 AddOptionalFlag("--preferred-density",
165 "Selects the closest matching density and strips out all others.",
166 &preferred_density_);
167 AddOptionalFlag("--product", "Comma separated list of product names to keep", &product_list_);
168 AddOptionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified by -o.",
169 &options_.output_to_directory);
170 AddOptionalSwitch("--no-xml-namespaces", "Removes XML namespace prefix and URI information\n"
171 "from AndroidManifest.xml and XML binaries in res/*.",
172 &options_.no_xml_namespaces);
173 AddOptionalFlag("--min-sdk-version",
174 "Default minimum SDK version to use for AndroidManifest.xml.",
175 &options_.manifest_fixer_options.min_sdk_version_default);
176 AddOptionalFlag("--target-sdk-version",
177 "Default target SDK version to use for AndroidManifest.xml.",
178 &options_.manifest_fixer_options.target_sdk_version_default);
179 AddOptionalFlag("--version-code",
180 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
Ryan Mitchell704090e2018-07-31 14:59:25 -0700181 "present.", &options_.manifest_fixer_options.version_code_default);
182 AddOptionalFlag("--version-code-major",
183 "Version code major (integer) to inject into the AndroidManifest.xml if none is\n"
184 "present.", &options_.manifest_fixer_options.version_code_major_default);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700185 AddOptionalFlag("--version-name",
186 "Version name to inject into the AndroidManifest.xml if none is present.",
187 &options_.manifest_fixer_options.version_name_default);
188 AddOptionalSwitch("--replace-version",
189 "If --version-code and/or --version-name are specified, these\n"
190 "values will replace any value already in the manifest. By\n"
191 "default, nothing is changed if the manifest already defines\n"
192 "these attributes.",
193 &options_.manifest_fixer_options.replace_version);
194 AddOptionalFlag("--compile-sdk-version-code",
195 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
196 "present.",
197 &options_.manifest_fixer_options.compile_sdk_version);
198 AddOptionalFlag("--compile-sdk-version-name",
199 "Version name to inject into the AndroidManifest.xml if none is present.",
200 &options_.manifest_fixer_options.compile_sdk_version_codename);
201 AddOptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
202 &shared_lib_);
203 AddOptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib_);
204 AddOptionalSwitch("--proto-format",
205 "Generates compiled resources in Protobuf format.\n"
206 "Suitable as input to the bundle tool for generating an App Bundle.",
207 &proto_format_);
208 AddOptionalSwitch("--no-static-lib-packages",
209 "Merge all library resources under the app's package.",
210 &options_.no_static_lib_packages);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700211 AddOptionalSwitch("--non-final-ids",
212 "Generates R.java without the final modifier. This is implied when\n"
213 "--static-lib is specified.",
214 &options_.generate_non_final_ids);
215 AddOptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
216 &stable_id_file_path_);
217 AddOptionalFlag("--emit-ids",
218 "Emit a file at the given path with a list of name to ID mappings,\n"
219 "suitable for use with --stable-ids.",
220 &options_.resource_id_map_path);
221 AddOptionalFlag("--private-symbols",
222 "Package name to use when generating R.java for private symbols.\n"
223 "If not specified, public and private symbols will use the application's\n"
224 "package name.",
225 &options_.private_symbols);
226 AddOptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
227 &options_.custom_java_package);
228 AddOptionalFlagList("--extra-packages",
229 "Generate the same R.java but with different package names.",
230 &extra_java_packages_);
231 AddOptionalFlagList("--add-javadoc-annotation",
232 "Adds a JavaDoc annotation to all generated Java classes.",
233 &options_.javadoc_annotations);
234 AddOptionalFlag("--output-text-symbols",
235 "Generates a text file containing the resource symbols of the R class in\n"
236 "the specified folder.",
237 &options_.generate_text_symbols_path);
238 AddOptionalSwitch("--allow-reserved-package-id",
239 "Allows the use of a reserved package ID. This should on be used for\n"
240 "packages with a pre-O min-sdk\n",
241 &options_.allow_reserved_package_id);
242 AddOptionalSwitch("--auto-add-overlay",
243 "Allows the addition of new resources in overlays without\n"
244 "<add-resource> tags.",
245 &options_.auto_add_overlay);
Donald Chai121c6e82019-06-12 12:51:57 -0700246 AddOptionalSwitch("--override-styles-instead-of-overlaying",
247 "Causes styles defined in -R resources to replace previous definitions\n"
248 "instead of merging into them\n",
249 &options_.override_styles_instead_of_overlaying);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700250 AddOptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
251 &options_.manifest_fixer_options.rename_manifest_package);
252 AddOptionalFlag("--rename-instrumentation-target-package",
253 "Changes the name of the target package for instrumentation. Most useful\n"
254 "when used in conjunction with --rename-manifest-package.",
255 &options_.manifest_fixer_options.rename_instrumentation_target_package);
Ryan Mitchell81dfca02019-06-07 10:20:27 -0700256 AddOptionalFlagList("-0", "File suffix not to compress.",
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700257 &options_.extensions_to_not_compress);
258 AddOptionalSwitch("--no-compress", "Do not compress any resources.",
259 &options_.do_not_compress_anything);
Ryan Mitchell479fa392019-01-02 17:15:39 -0800260 AddOptionalSwitch("--keep-raw-values", "Preserve raw attribute values in xml files.",
261 &options_.keep_raw_values);
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100262 AddOptionalFlag("--no-compress-regex",
263 "Do not compress extensions matching the regular expression. Remember to\n"
264 " use the '$' symbol for end of line. Uses a non case-sensitive\n"
265 " ECMAScript regular expression grammar.",
266 &no_compress_regex);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700267 AddOptionalSwitch("--warn-manifest-validation",
268 "Treat manifest validation errors as warnings.",
269 &options_.manifest_fixer_options.warn_validation);
270 AddOptionalFlagList("--split",
271 "Split resources matching a set of configs out to a Split APK.\n"
272 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
273 "On Windows, use a semicolon ';' separator instead.",
274 &split_args_);
Winson3c918b82019-01-25 14:25:37 -0800275 AddOptionalFlagList("--exclude-configs",
276 "Excludes values of resources whose configs contain the specified qualifiers.",
277 &options_.exclude_configs_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700278 AddOptionalSwitch("--debug-mode",
279 "Inserts android:debuggable=\"true\" in to the application node of the\n"
280 "manifest, making the application debuggable even on production devices.",
281 &options_.manifest_fixer_options.debug_mode);
282 AddOptionalSwitch("--strict-visibility",
283 "Do not allow overlays with different visibility levels.",
284 &options_.strict_visibility);
Ryan Mitchell479fa392019-01-02 17:15:39 -0800285 AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800286 AddOptionalFlag("--trace-folder", "Generate systrace json trace fragment to specified folder.",
287 &trace_folder_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700288 }
289
290 int Action(const std::vector<std::string>& args) override;
291
292 private:
293 IDiagnostics* diag_;
294 LinkOptions options_;
295
296 std::vector<std::string> overlay_arg_list_;
297 std::vector<std::string> extra_java_packages_;
298 Maybe<std::string> package_id_;
299 std::vector<std::string> configs_;
300 Maybe<std::string> preferred_density_;
301 Maybe<std::string> product_list_;
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100302 Maybe<std::string> no_compress_regex;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700303 bool legacy_x_flag_ = false;
304 bool require_localization_ = false;
305 bool verbose_ = false;
306 bool shared_lib_ = false;
307 bool static_lib_ = false;
308 bool proto_format_ = false;
309 Maybe<std::string> stable_id_file_path_;
310 std::vector<std::string> split_args_;
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800311 Maybe<std::string> trace_folder_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700312};
313
314}// namespace aapt
315
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200316#endif //AAPT2_LINK_H