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