blob: 1fc149ab41afcc3ee425f7d1c7513f34e021b0aa [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"
28
29namespace aapt {
30
31enum class OutputFormat {
32 kApk,
33 kProto,
34};
35
36struct LinkOptions {
37 std::string output_path;
38 std::string manifest_path;
39 std::vector<std::string> include_paths;
40 std::vector<std::string> overlay_files;
41 std::vector<std::string> assets_dirs;
42 bool output_to_directory = false;
43 bool auto_add_overlay = false;
44 OutputFormat output_format = OutputFormat::kApk;
45
46 // Java/Proguard options.
47 Maybe<std::string> generate_java_class_path;
48 Maybe<std::string> custom_java_package;
49 std::set<std::string> extra_java_packages;
50 Maybe<std::string> generate_text_symbols_path;
51 Maybe<std::string> generate_proguard_rules_path;
52 Maybe<std::string> generate_main_dex_proguard_rules_path;
53 bool generate_conditional_proguard_rules = false;
Ryan Mitchell7e5236d2018-09-25 15:20:59 -070054 bool generate_minimal_proguard_rules = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070055 bool generate_non_final_ids = false;
56 std::vector<std::string> javadoc_annotations;
57 Maybe<std::string> private_symbols;
58
59 // Optimizations/features.
60 bool no_auto_version = false;
61 bool no_version_vectors = false;
62 bool no_version_transitions = false;
63 bool no_resource_deduping = false;
Mårten Kongstadd8d29012018-06-11 14:13:37 +020064 bool no_resource_removal = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070065 bool no_xml_namespaces = false;
66 bool do_not_compress_anything = false;
67 std::unordered_set<std::string> extensions_to_not_compress;
Izabela Orlowska0faba5f2018-06-01 12:06:31 +010068 Maybe<std::regex> regex_to_not_compress;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070069
70 // Static lib options.
71 bool no_static_lib_packages = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070072
73 // AndroidManifest.xml massaging options.
74 ManifestFixerOptions manifest_fixer_options;
75
76 // Products to use/filter on.
77 std::unordered_set<std::string> products;
78
79 // Flattening options.
80 TableFlattenerOptions table_flattener_options;
Ryan Mitchell479fa392019-01-02 17:15:39 -080081 bool keep_raw_values = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070082
83 // Split APK options.
84 TableSplitterOptions table_splitter_options;
85 std::vector<SplitConstraints> split_constraints;
86 std::vector<std::string> split_paths;
87
Winson3c918b82019-01-25 14:25:37 -080088 // Configurations to exclude
89 std::vector<std::string> exclude_configs_;
90
Ryan Mitchell833a1a62018-07-10 13:51:36 -070091 // Stable ID options.
92 std::unordered_map<ResourceName, ResourceId> stable_id_map;
93 Maybe<std::string> resource_id_map_path;
94
95 // When 'true', allow reserved package IDs to be used for applications. Pre-O, the platform
96 // treats negative resource IDs [those with a package ID of 0x80 or higher] as invalid.
97 // In order to work around this limitation, we allow the use of traditionally reserved
98 // resource IDs [those between 0x02 and 0x7E].
99 bool allow_reserved_package_id = false;
100
101 // Whether we should fail on definitions of a resource with conflicting visibility.
102 bool strict_visibility = false;
103};
104
105class LinkCommand : public Command {
106 public:
107 explicit LinkCommand(IDiagnostics* diag) : Command("link", "l"),
108 diag_(diag) {
109 SetDescription("Links resources into an apk.");
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800110 AddRequiredFlag("-o", "Output path.", &options_.output_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700111 AddRequiredFlag("--manifest", "Path to the Android manifest to build.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800112 &options_.manifest_path, Command::kPath);
113 AddOptionalFlagList("-I", "Adds an Android APK to link against.", &options_.include_paths,
114 Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700115 AddOptionalFlagList("-A", "An assets directory to include in the APK. These are unprocessed.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800116 &options_.assets_dirs, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700117 AddOptionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n"
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800118 "The last conflicting resource given takes precedence.", &overlay_arg_list_,
119 Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700120 AddOptionalFlag("--package-id",
121 "Specify the package ID to use for this app. Must be greater or equal to\n"
122 "0x7f and can't be used with --static-lib or --shared-lib.", &package_id_);
123 AddOptionalFlag("--java", "Directory in which to generate R.java.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800124 &options_.generate_java_class_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700125 AddOptionalFlag("--proguard", "Output file for generated Proguard rules.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800126 &options_.generate_proguard_rules_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700127 AddOptionalFlag("--proguard-main-dex",
128 "Output file for generated Proguard rules for the main dex.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800129 &options_.generate_main_dex_proguard_rules_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700130 AddOptionalSwitch("--proguard-conditional-keep-rules",
131 "Generate conditional Proguard keep rules.",
132 &options_.generate_conditional_proguard_rules);
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700133 AddOptionalSwitch("--proguard-minimal-keep-rules",
134 "Generate a minimal set of Proguard keep rules.",
135 &options_.generate_minimal_proguard_rules);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700136 AddOptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning.",
137 &options_.no_auto_version);
138 AddOptionalSwitch("--no-version-vectors",
139 "Disables automatic versioning of vector drawables. Use this only\n"
140 "when building with vector drawable support library.",
141 &options_.no_version_vectors);
142 AddOptionalSwitch("--no-version-transitions",
143 "Disables automatic versioning of transition resources. Use this only\n"
144 "when building with transition support library.",
145 &options_.no_version_transitions);
146 AddOptionalSwitch("--no-resource-deduping", "Disables automatic deduping of resources with\n"
147 "identical values across compatible configurations.",
148 &options_.no_resource_deduping);
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200149 AddOptionalSwitch("--no-resource-removal", "Disables automatic removal of resources without\n"
150 "defaults. Use this only when building runtime resource overlay packages.",
151 &options_.no_resource_removal);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700152 AddOptionalSwitch("--enable-sparse-encoding",
153 "This decreases APK size at the cost of resource retrieval performance.",
154 &options_.table_flattener_options.use_sparse_entries);
155 AddOptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
156 &legacy_x_flag_);
157 AddOptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
158 &require_localization_);
159 AddOptionalFlagList("-c",
160 "Comma separated list of configurations to include. The default\n"
161 "is all configurations.", &configs_);
162 AddOptionalFlag("--preferred-density",
163 "Selects the closest matching density and strips out all others.",
164 &preferred_density_);
165 AddOptionalFlag("--product", "Comma separated list of product names to keep", &product_list_);
166 AddOptionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified by -o.",
167 &options_.output_to_directory);
168 AddOptionalSwitch("--no-xml-namespaces", "Removes XML namespace prefix and URI information\n"
169 "from AndroidManifest.xml and XML binaries in res/*.",
170 &options_.no_xml_namespaces);
171 AddOptionalFlag("--min-sdk-version",
172 "Default minimum SDK version to use for AndroidManifest.xml.",
173 &options_.manifest_fixer_options.min_sdk_version_default);
174 AddOptionalFlag("--target-sdk-version",
175 "Default target SDK version to use for AndroidManifest.xml.",
176 &options_.manifest_fixer_options.target_sdk_version_default);
177 AddOptionalFlag("--version-code",
178 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
Ryan Mitchell704090e2018-07-31 14:59:25 -0700179 "present.", &options_.manifest_fixer_options.version_code_default);
180 AddOptionalFlag("--version-code-major",
181 "Version code major (integer) to inject into the AndroidManifest.xml if none is\n"
182 "present.", &options_.manifest_fixer_options.version_code_major_default);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700183 AddOptionalFlag("--version-name",
184 "Version name to inject into the AndroidManifest.xml if none is present.",
185 &options_.manifest_fixer_options.version_name_default);
186 AddOptionalSwitch("--replace-version",
187 "If --version-code and/or --version-name are specified, these\n"
188 "values will replace any value already in the manifest. By\n"
189 "default, nothing is changed if the manifest already defines\n"
190 "these attributes.",
191 &options_.manifest_fixer_options.replace_version);
192 AddOptionalFlag("--compile-sdk-version-code",
193 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
194 "present.",
195 &options_.manifest_fixer_options.compile_sdk_version);
196 AddOptionalFlag("--compile-sdk-version-name",
197 "Version name to inject into the AndroidManifest.xml if none is present.",
198 &options_.manifest_fixer_options.compile_sdk_version_codename);
199 AddOptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
200 &shared_lib_);
201 AddOptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib_);
202 AddOptionalSwitch("--proto-format",
203 "Generates compiled resources in Protobuf format.\n"
204 "Suitable as input to the bundle tool for generating an App Bundle.",
205 &proto_format_);
206 AddOptionalSwitch("--no-static-lib-packages",
207 "Merge all library resources under the app's package.",
208 &options_.no_static_lib_packages);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700209 AddOptionalSwitch("--non-final-ids",
210 "Generates R.java without the final modifier. This is implied when\n"
211 "--static-lib is specified.",
212 &options_.generate_non_final_ids);
213 AddOptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
214 &stable_id_file_path_);
215 AddOptionalFlag("--emit-ids",
216 "Emit a file at the given path with a list of name to ID mappings,\n"
217 "suitable for use with --stable-ids.",
218 &options_.resource_id_map_path);
219 AddOptionalFlag("--private-symbols",
220 "Package name to use when generating R.java for private symbols.\n"
221 "If not specified, public and private symbols will use the application's\n"
222 "package name.",
223 &options_.private_symbols);
224 AddOptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
225 &options_.custom_java_package);
226 AddOptionalFlagList("--extra-packages",
227 "Generate the same R.java but with different package names.",
228 &extra_java_packages_);
229 AddOptionalFlagList("--add-javadoc-annotation",
230 "Adds a JavaDoc annotation to all generated Java classes.",
231 &options_.javadoc_annotations);
232 AddOptionalFlag("--output-text-symbols",
233 "Generates a text file containing the resource symbols of the R class in\n"
234 "the specified folder.",
235 &options_.generate_text_symbols_path);
236 AddOptionalSwitch("--allow-reserved-package-id",
237 "Allows the use of a reserved package ID. This should on be used for\n"
238 "packages with a pre-O min-sdk\n",
239 &options_.allow_reserved_package_id);
240 AddOptionalSwitch("--auto-add-overlay",
241 "Allows the addition of new resources in overlays without\n"
242 "<add-resource> tags.",
243 &options_.auto_add_overlay);
244 AddOptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
245 &options_.manifest_fixer_options.rename_manifest_package);
246 AddOptionalFlag("--rename-instrumentation-target-package",
247 "Changes the name of the target package for instrumentation. Most useful\n"
248 "when used in conjunction with --rename-manifest-package.",
249 &options_.manifest_fixer_options.rename_instrumentation_target_package);
250 AddOptionalFlagList("-0", "File extensions not to compress.",
251 &options_.extensions_to_not_compress);
252 AddOptionalSwitch("--no-compress", "Do not compress any resources.",
253 &options_.do_not_compress_anything);
Ryan Mitchell479fa392019-01-02 17:15:39 -0800254 AddOptionalSwitch("--keep-raw-values", "Preserve raw attribute values in xml files.",
255 &options_.keep_raw_values);
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100256 AddOptionalFlag("--no-compress-regex",
257 "Do not compress extensions matching the regular expression. Remember to\n"
258 " use the '$' symbol for end of line. Uses a non case-sensitive\n"
259 " ECMAScript regular expression grammar.",
260 &no_compress_regex);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700261 AddOptionalSwitch("--warn-manifest-validation",
262 "Treat manifest validation errors as warnings.",
263 &options_.manifest_fixer_options.warn_validation);
264 AddOptionalFlagList("--split",
265 "Split resources matching a set of configs out to a Split APK.\n"
266 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
267 "On Windows, use a semicolon ';' separator instead.",
268 &split_args_);
Winson3c918b82019-01-25 14:25:37 -0800269 AddOptionalFlagList("--exclude-configs",
270 "Excludes values of resources whose configs contain the specified qualifiers.",
271 &options_.exclude_configs_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700272 AddOptionalSwitch("--debug-mode",
273 "Inserts android:debuggable=\"true\" in to the application node of the\n"
274 "manifest, making the application debuggable even on production devices.",
275 &options_.manifest_fixer_options.debug_mode);
276 AddOptionalSwitch("--strict-visibility",
277 "Do not allow overlays with different visibility levels.",
278 &options_.strict_visibility);
Ryan Mitchell479fa392019-01-02 17:15:39 -0800279 AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700280 }
281
282 int Action(const std::vector<std::string>& args) override;
283
284 private:
285 IDiagnostics* diag_;
286 LinkOptions options_;
287
288 std::vector<std::string> overlay_arg_list_;
289 std::vector<std::string> extra_java_packages_;
290 Maybe<std::string> package_id_;
291 std::vector<std::string> configs_;
292 Maybe<std::string> preferred_density_;
293 Maybe<std::string> product_list_;
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100294 Maybe<std::string> no_compress_regex;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700295 bool legacy_x_flag_ = false;
296 bool require_localization_ = false;
297 bool verbose_ = false;
298 bool shared_lib_ = false;
299 bool static_lib_ = false;
300 bool proto_format_ = false;
301 Maybe<std::string> stable_id_file_path_;
302 std::vector<std::string> split_args_;
303};
304
305}// namespace aapt
306
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200307#endif //AAPT2_LINK_H