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