blob: f740d538b326f747f384c7931c08d8cd0a8d94ef [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;
78
79 // Split APK options.
80 TableSplitterOptions table_splitter_options;
81 std::vector<SplitConstraints> split_constraints;
82 std::vector<std::string> split_paths;
83
84 // Stable ID options.
85 std::unordered_map<ResourceName, ResourceId> stable_id_map;
86 Maybe<std::string> resource_id_map_path;
87
88 // When 'true', allow reserved package IDs to be used for applications. Pre-O, the platform
89 // treats negative resource IDs [those with a package ID of 0x80 or higher] as invalid.
90 // In order to work around this limitation, we allow the use of traditionally reserved
91 // resource IDs [those between 0x02 and 0x7E].
92 bool allow_reserved_package_id = false;
93
94 // Whether we should fail on definitions of a resource with conflicting visibility.
95 bool strict_visibility = false;
96};
97
98class LinkCommand : public Command {
99 public:
100 explicit LinkCommand(IDiagnostics* diag) : Command("link", "l"),
101 diag_(diag) {
102 SetDescription("Links resources into an apk.");
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800103 AddRequiredFlag("-o", "Output path.", &options_.output_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700104 AddRequiredFlag("--manifest", "Path to the Android manifest to build.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800105 &options_.manifest_path, Command::kPath);
106 AddOptionalFlagList("-I", "Adds an Android APK to link against.", &options_.include_paths,
107 Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700108 AddOptionalFlagList("-A", "An assets directory to include in the APK. These are unprocessed.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800109 &options_.assets_dirs, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700110 AddOptionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n"
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800111 "The last conflicting resource given takes precedence.", &overlay_arg_list_,
112 Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700113 AddOptionalFlag("--package-id",
114 "Specify the package ID to use for this app. Must be greater or equal to\n"
115 "0x7f and can't be used with --static-lib or --shared-lib.", &package_id_);
116 AddOptionalFlag("--java", "Directory in which to generate R.java.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800117 &options_.generate_java_class_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700118 AddOptionalFlag("--proguard", "Output file for generated Proguard rules.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800119 &options_.generate_proguard_rules_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700120 AddOptionalFlag("--proguard-main-dex",
121 "Output file for generated Proguard rules for the main dex.",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800122 &options_.generate_main_dex_proguard_rules_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700123 AddOptionalSwitch("--proguard-conditional-keep-rules",
124 "Generate conditional Proguard keep rules.",
125 &options_.generate_conditional_proguard_rules);
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700126 AddOptionalSwitch("--proguard-minimal-keep-rules",
127 "Generate a minimal set of Proguard keep rules.",
128 &options_.generate_minimal_proguard_rules);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700129 AddOptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning.",
130 &options_.no_auto_version);
131 AddOptionalSwitch("--no-version-vectors",
132 "Disables automatic versioning of vector drawables. Use this only\n"
133 "when building with vector drawable support library.",
134 &options_.no_version_vectors);
135 AddOptionalSwitch("--no-version-transitions",
136 "Disables automatic versioning of transition resources. Use this only\n"
137 "when building with transition support library.",
138 &options_.no_version_transitions);
139 AddOptionalSwitch("--no-resource-deduping", "Disables automatic deduping of resources with\n"
140 "identical values across compatible configurations.",
141 &options_.no_resource_deduping);
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200142 AddOptionalSwitch("--no-resource-removal", "Disables automatic removal of resources without\n"
143 "defaults. Use this only when building runtime resource overlay packages.",
144 &options_.no_resource_removal);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700145 AddOptionalSwitch("--enable-sparse-encoding",
146 "This decreases APK size at the cost of resource retrieval performance.",
147 &options_.table_flattener_options.use_sparse_entries);
148 AddOptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
149 &legacy_x_flag_);
150 AddOptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
151 &require_localization_);
152 AddOptionalFlagList("-c",
153 "Comma separated list of configurations to include. The default\n"
154 "is all configurations.", &configs_);
155 AddOptionalFlag("--preferred-density",
156 "Selects the closest matching density and strips out all others.",
157 &preferred_density_);
158 AddOptionalFlag("--product", "Comma separated list of product names to keep", &product_list_);
159 AddOptionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified by -o.",
160 &options_.output_to_directory);
161 AddOptionalSwitch("--no-xml-namespaces", "Removes XML namespace prefix and URI information\n"
162 "from AndroidManifest.xml and XML binaries in res/*.",
163 &options_.no_xml_namespaces);
164 AddOptionalFlag("--min-sdk-version",
165 "Default minimum SDK version to use for AndroidManifest.xml.",
166 &options_.manifest_fixer_options.min_sdk_version_default);
167 AddOptionalFlag("--target-sdk-version",
168 "Default target SDK version to use for AndroidManifest.xml.",
169 &options_.manifest_fixer_options.target_sdk_version_default);
170 AddOptionalFlag("--version-code",
171 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
Ryan Mitchell704090e2018-07-31 14:59:25 -0700172 "present.", &options_.manifest_fixer_options.version_code_default);
173 AddOptionalFlag("--version-code-major",
174 "Version code major (integer) to inject into the AndroidManifest.xml if none is\n"
175 "present.", &options_.manifest_fixer_options.version_code_major_default);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700176 AddOptionalFlag("--version-name",
177 "Version name to inject into the AndroidManifest.xml if none is present.",
178 &options_.manifest_fixer_options.version_name_default);
179 AddOptionalSwitch("--replace-version",
180 "If --version-code and/or --version-name are specified, these\n"
181 "values will replace any value already in the manifest. By\n"
182 "default, nothing is changed if the manifest already defines\n"
183 "these attributes.",
184 &options_.manifest_fixer_options.replace_version);
185 AddOptionalFlag("--compile-sdk-version-code",
186 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
187 "present.",
188 &options_.manifest_fixer_options.compile_sdk_version);
189 AddOptionalFlag("--compile-sdk-version-name",
190 "Version name to inject into the AndroidManifest.xml if none is present.",
191 &options_.manifest_fixer_options.compile_sdk_version_codename);
192 AddOptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
193 &shared_lib_);
194 AddOptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib_);
195 AddOptionalSwitch("--proto-format",
196 "Generates compiled resources in Protobuf format.\n"
197 "Suitable as input to the bundle tool for generating an App Bundle.",
198 &proto_format_);
199 AddOptionalSwitch("--no-static-lib-packages",
200 "Merge all library resources under the app's package.",
201 &options_.no_static_lib_packages);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700202 AddOptionalSwitch("--non-final-ids",
203 "Generates R.java without the final modifier. This is implied when\n"
204 "--static-lib is specified.",
205 &options_.generate_non_final_ids);
206 AddOptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
207 &stable_id_file_path_);
208 AddOptionalFlag("--emit-ids",
209 "Emit a file at the given path with a list of name to ID mappings,\n"
210 "suitable for use with --stable-ids.",
211 &options_.resource_id_map_path);
212 AddOptionalFlag("--private-symbols",
213 "Package name to use when generating R.java for private symbols.\n"
214 "If not specified, public and private symbols will use the application's\n"
215 "package name.",
216 &options_.private_symbols);
217 AddOptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
218 &options_.custom_java_package);
219 AddOptionalFlagList("--extra-packages",
220 "Generate the same R.java but with different package names.",
221 &extra_java_packages_);
222 AddOptionalFlagList("--add-javadoc-annotation",
223 "Adds a JavaDoc annotation to all generated Java classes.",
224 &options_.javadoc_annotations);
225 AddOptionalFlag("--output-text-symbols",
226 "Generates a text file containing the resource symbols of the R class in\n"
227 "the specified folder.",
228 &options_.generate_text_symbols_path);
229 AddOptionalSwitch("--allow-reserved-package-id",
230 "Allows the use of a reserved package ID. This should on be used for\n"
231 "packages with a pre-O min-sdk\n",
232 &options_.allow_reserved_package_id);
233 AddOptionalSwitch("--auto-add-overlay",
234 "Allows the addition of new resources in overlays without\n"
235 "<add-resource> tags.",
236 &options_.auto_add_overlay);
237 AddOptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
238 &options_.manifest_fixer_options.rename_manifest_package);
239 AddOptionalFlag("--rename-instrumentation-target-package",
240 "Changes the name of the target package for instrumentation. Most useful\n"
241 "when used in conjunction with --rename-manifest-package.",
242 &options_.manifest_fixer_options.rename_instrumentation_target_package);
243 AddOptionalFlagList("-0", "File extensions not to compress.",
244 &options_.extensions_to_not_compress);
245 AddOptionalSwitch("--no-compress", "Do not compress any resources.",
246 &options_.do_not_compress_anything);
247 AddOptionalSwitch("--warn-manifest-validation",
248 "Treat manifest validation errors as warnings.",
249 &options_.manifest_fixer_options.warn_validation);
250 AddOptionalFlagList("--split",
251 "Split resources matching a set of configs out to a Split APK.\n"
252 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
253 "On Windows, use a semicolon ';' separator instead.",
254 &split_args_);
255 AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
256 AddOptionalSwitch("--debug-mode",
257 "Inserts android:debuggable=\"true\" in to the application node of the\n"
258 "manifest, making the application debuggable even on production devices.",
259 &options_.manifest_fixer_options.debug_mode);
260 AddOptionalSwitch("--strict-visibility",
261 "Do not allow overlays with different visibility levels.",
262 &options_.strict_visibility);
263 }
264
265 int Action(const std::vector<std::string>& args) override;
266
267 private:
268 IDiagnostics* diag_;
269 LinkOptions options_;
270
271 std::vector<std::string> overlay_arg_list_;
272 std::vector<std::string> extra_java_packages_;
273 Maybe<std::string> package_id_;
274 std::vector<std::string> configs_;
275 Maybe<std::string> preferred_density_;
276 Maybe<std::string> product_list_;
277 bool legacy_x_flag_ = false;
278 bool require_localization_ = false;
279 bool verbose_ = false;
280 bool shared_lib_ = false;
281 bool static_lib_ = false;
282 bool proto_format_ = false;
283 Maybe<std::string> stable_id_file_path_;
284 std::vector<std::string> split_args_;
285};
286
287}// namespace aapt
288
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200289#endif //AAPT2_LINK_H