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