blob: 4beb0ad2bd46345b2e80cb0cf39af13740cedb8a [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.");
104 AddRequiredFlag("-o", "Output path.", &options_.output_path);
105 AddRequiredFlag("--manifest", "Path to the Android manifest to build.",
106 &options_.manifest_path);
107 AddOptionalFlagList("-I", "Adds an Android APK to link against.", &options_.include_paths);
108 AddOptionalFlagList("-A", "An assets directory to include in the APK. These are unprocessed.",
109 &options_.assets_dirs);
110 AddOptionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n"
111 "The last conflicting resource given takes precedence.", &overlay_arg_list_);
112 AddOptionalFlag("--package-id",
113 "Specify the package ID to use for this app. Must be greater or equal to\n"
114 "0x7f and can't be used with --static-lib or --shared-lib.", &package_id_);
115 AddOptionalFlag("--java", "Directory in which to generate R.java.",
116 &options_.generate_java_class_path);
117 AddOptionalFlag("--proguard", "Output file for generated Proguard rules.",
118 &options_.generate_proguard_rules_path);
119 AddOptionalFlag("--proguard-main-dex",
120 "Output file for generated Proguard rules for the main dex.",
121 &options_.generate_main_dex_proguard_rules_path);
122 AddOptionalSwitch("--proguard-conditional-keep-rules",
123 "Generate conditional Proguard keep rules.",
124 &options_.generate_conditional_proguard_rules);
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700125 AddOptionalSwitch("--proguard-minimal-keep-rules",
126 "Generate a minimal set of Proguard keep rules.",
127 &options_.generate_minimal_proguard_rules);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700128 AddOptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning.",
129 &options_.no_auto_version);
130 AddOptionalSwitch("--no-version-vectors",
131 "Disables automatic versioning of vector drawables. Use this only\n"
132 "when building with vector drawable support library.",
133 &options_.no_version_vectors);
134 AddOptionalSwitch("--no-version-transitions",
135 "Disables automatic versioning of transition resources. Use this only\n"
136 "when building with transition support library.",
137 &options_.no_version_transitions);
138 AddOptionalSwitch("--no-resource-deduping", "Disables automatic deduping of resources with\n"
139 "identical values across compatible configurations.",
140 &options_.no_resource_deduping);
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200141 AddOptionalSwitch("--no-resource-removal", "Disables automatic removal of resources without\n"
142 "defaults. Use this only when building runtime resource overlay packages.",
143 &options_.no_resource_removal);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700144 AddOptionalSwitch("--enable-sparse-encoding",
145 "This decreases APK size at the cost of resource retrieval performance.",
146 &options_.table_flattener_options.use_sparse_entries);
147 AddOptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
148 &legacy_x_flag_);
149 AddOptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
150 &require_localization_);
151 AddOptionalFlagList("-c",
152 "Comma separated list of configurations to include. The default\n"
153 "is all configurations.", &configs_);
154 AddOptionalFlag("--preferred-density",
155 "Selects the closest matching density and strips out all others.",
156 &preferred_density_);
157 AddOptionalFlag("--product", "Comma separated list of product names to keep", &product_list_);
158 AddOptionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified by -o.",
159 &options_.output_to_directory);
160 AddOptionalSwitch("--no-xml-namespaces", "Removes XML namespace prefix and URI information\n"
161 "from AndroidManifest.xml and XML binaries in res/*.",
162 &options_.no_xml_namespaces);
163 AddOptionalFlag("--min-sdk-version",
164 "Default minimum SDK version to use for AndroidManifest.xml.",
165 &options_.manifest_fixer_options.min_sdk_version_default);
166 AddOptionalFlag("--target-sdk-version",
167 "Default target SDK version to use for AndroidManifest.xml.",
168 &options_.manifest_fixer_options.target_sdk_version_default);
169 AddOptionalFlag("--version-code",
170 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
Ryan Mitchell704090e2018-07-31 14:59:25 -0700171 "present.", &options_.manifest_fixer_options.version_code_default);
172 AddOptionalFlag("--version-code-major",
173 "Version code major (integer) to inject into the AndroidManifest.xml if none is\n"
174 "present.", &options_.manifest_fixer_options.version_code_major_default);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700175 AddOptionalFlag("--version-name",
176 "Version name to inject into the AndroidManifest.xml if none is present.",
177 &options_.manifest_fixer_options.version_name_default);
178 AddOptionalSwitch("--replace-version",
179 "If --version-code and/or --version-name are specified, these\n"
180 "values will replace any value already in the manifest. By\n"
181 "default, nothing is changed if the manifest already defines\n"
182 "these attributes.",
183 &options_.manifest_fixer_options.replace_version);
184 AddOptionalFlag("--compile-sdk-version-code",
185 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
186 "present.",
187 &options_.manifest_fixer_options.compile_sdk_version);
188 AddOptionalFlag("--compile-sdk-version-name",
189 "Version name to inject into the AndroidManifest.xml if none is present.",
190 &options_.manifest_fixer_options.compile_sdk_version_codename);
191 AddOptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
192 &shared_lib_);
193 AddOptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib_);
194 AddOptionalSwitch("--proto-format",
195 "Generates compiled resources in Protobuf format.\n"
196 "Suitable as input to the bundle tool for generating an App Bundle.",
197 &proto_format_);
198 AddOptionalSwitch("--no-static-lib-packages",
199 "Merge all library resources under the app's package.",
200 &options_.no_static_lib_packages);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700201 AddOptionalSwitch("--non-final-ids",
202 "Generates R.java without the final modifier. This is implied when\n"
203 "--static-lib is specified.",
204 &options_.generate_non_final_ids);
205 AddOptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
206 &stable_id_file_path_);
207 AddOptionalFlag("--emit-ids",
208 "Emit a file at the given path with a list of name to ID mappings,\n"
209 "suitable for use with --stable-ids.",
210 &options_.resource_id_map_path);
211 AddOptionalFlag("--private-symbols",
212 "Package name to use when generating R.java for private symbols.\n"
213 "If not specified, public and private symbols will use the application's\n"
214 "package name.",
215 &options_.private_symbols);
216 AddOptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
217 &options_.custom_java_package);
218 AddOptionalFlagList("--extra-packages",
219 "Generate the same R.java but with different package names.",
220 &extra_java_packages_);
221 AddOptionalFlagList("--add-javadoc-annotation",
222 "Adds a JavaDoc annotation to all generated Java classes.",
223 &options_.javadoc_annotations);
224 AddOptionalFlag("--output-text-symbols",
225 "Generates a text file containing the resource symbols of the R class in\n"
226 "the specified folder.",
227 &options_.generate_text_symbols_path);
228 AddOptionalSwitch("--allow-reserved-package-id",
229 "Allows the use of a reserved package ID. This should on be used for\n"
230 "packages with a pre-O min-sdk\n",
231 &options_.allow_reserved_package_id);
232 AddOptionalSwitch("--auto-add-overlay",
233 "Allows the addition of new resources in overlays without\n"
234 "<add-resource> tags.",
235 &options_.auto_add_overlay);
236 AddOptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
237 &options_.manifest_fixer_options.rename_manifest_package);
238 AddOptionalFlag("--rename-instrumentation-target-package",
239 "Changes the name of the target package for instrumentation. Most useful\n"
240 "when used in conjunction with --rename-manifest-package.",
241 &options_.manifest_fixer_options.rename_instrumentation_target_package);
242 AddOptionalFlagList("-0", "File extensions not to compress.",
243 &options_.extensions_to_not_compress);
244 AddOptionalSwitch("--no-compress", "Do not compress any resources.",
245 &options_.do_not_compress_anything);
Ryan Mitchell479fa392019-01-02 17:15:39 -0800246 AddOptionalSwitch("--keep-raw-values", "Preserve raw attribute values in xml files.",
247 &options_.keep_raw_values);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700248 AddOptionalSwitch("--warn-manifest-validation",
249 "Treat manifest validation errors as warnings.",
250 &options_.manifest_fixer_options.warn_validation);
251 AddOptionalFlagList("--split",
252 "Split resources matching a set of configs out to a Split APK.\n"
253 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
254 "On Windows, use a semicolon ';' separator instead.",
255 &split_args_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700256 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);
Ryan Mitchell479fa392019-01-02 17:15:39 -0800263 AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700264 }
265
266 int Action(const std::vector<std::string>& args) override;
267
268 private:
269 IDiagnostics* diag_;
270 LinkOptions options_;
271
272 std::vector<std::string> overlay_arg_list_;
273 std::vector<std::string> extra_java_packages_;
274 Maybe<std::string> package_id_;
275 std::vector<std::string> configs_;
276 Maybe<std::string> preferred_density_;
277 Maybe<std::string> product_list_;
278 bool legacy_x_flag_ = false;
279 bool require_localization_ = false;
280 bool verbose_ = false;
281 bool shared_lib_ = false;
282 bool static_lib_ = false;
283 bool proto_format_ = false;
284 Maybe<std::string> stable_id_file_path_;
285 std::vector<std::string> split_args_;
286};
287
288}// namespace aapt
289
Mårten Kongstadd8d29012018-06-11 14:13:37 +0200290#endif //AAPT2_LINK_H