blob: 434475ee1be51a3f1e54fbef375add335776eafe [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;
52 bool generate_non_final_ids = false;
53 std::vector<std::string> javadoc_annotations;
54 Maybe<std::string> private_symbols;
55
56 // Optimizations/features.
57 bool no_auto_version = false;
58 bool no_version_vectors = false;
59 bool no_version_transitions = false;
60 bool no_resource_deduping = false;
61 bool no_xml_namespaces = false;
62 bool do_not_compress_anything = false;
63 std::unordered_set<std::string> extensions_to_not_compress;
64
65 // Static lib options.
66 bool no_static_lib_packages = false;
67 bool auto_namespace_static_lib = false;
68
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);
123 AddOptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning.",
124 &options_.no_auto_version);
125 AddOptionalSwitch("--no-version-vectors",
126 "Disables automatic versioning of vector drawables. Use this only\n"
127 "when building with vector drawable support library.",
128 &options_.no_version_vectors);
129 AddOptionalSwitch("--no-version-transitions",
130 "Disables automatic versioning of transition resources. Use this only\n"
131 "when building with transition support library.",
132 &options_.no_version_transitions);
133 AddOptionalSwitch("--no-resource-deduping", "Disables automatic deduping of resources with\n"
134 "identical values across compatible configurations.",
135 &options_.no_resource_deduping);
136 AddOptionalSwitch("--enable-sparse-encoding",
137 "This decreases APK size at the cost of resource retrieval performance.",
138 &options_.table_flattener_options.use_sparse_entries);
139 AddOptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
140 &legacy_x_flag_);
141 AddOptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
142 &require_localization_);
143 AddOptionalFlagList("-c",
144 "Comma separated list of configurations to include. The default\n"
145 "is all configurations.", &configs_);
146 AddOptionalFlag("--preferred-density",
147 "Selects the closest matching density and strips out all others.",
148 &preferred_density_);
149 AddOptionalFlag("--product", "Comma separated list of product names to keep", &product_list_);
150 AddOptionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified by -o.",
151 &options_.output_to_directory);
152 AddOptionalSwitch("--no-xml-namespaces", "Removes XML namespace prefix and URI information\n"
153 "from AndroidManifest.xml and XML binaries in res/*.",
154 &options_.no_xml_namespaces);
155 AddOptionalFlag("--min-sdk-version",
156 "Default minimum SDK version to use for AndroidManifest.xml.",
157 &options_.manifest_fixer_options.min_sdk_version_default);
158 AddOptionalFlag("--target-sdk-version",
159 "Default target SDK version to use for AndroidManifest.xml.",
160 &options_.manifest_fixer_options.target_sdk_version_default);
161 AddOptionalFlag("--version-code",
162 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
163 "present.",
164 &options_.manifest_fixer_options.version_code_default);
165 AddOptionalFlag("--version-name",
166 "Version name to inject into the AndroidManifest.xml if none is present.",
167 &options_.manifest_fixer_options.version_name_default);
168 AddOptionalSwitch("--replace-version",
169 "If --version-code and/or --version-name are specified, these\n"
170 "values will replace any value already in the manifest. By\n"
171 "default, nothing is changed if the manifest already defines\n"
172 "these attributes.",
173 &options_.manifest_fixer_options.replace_version);
174 AddOptionalFlag("--compile-sdk-version-code",
175 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
176 "present.",
177 &options_.manifest_fixer_options.compile_sdk_version);
178 AddOptionalFlag("--compile-sdk-version-name",
179 "Version name to inject into the AndroidManifest.xml if none is present.",
180 &options_.manifest_fixer_options.compile_sdk_version_codename);
181 AddOptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
182 &shared_lib_);
183 AddOptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib_);
184 AddOptionalSwitch("--proto-format",
185 "Generates compiled resources in Protobuf format.\n"
186 "Suitable as input to the bundle tool for generating an App Bundle.",
187 &proto_format_);
188 AddOptionalSwitch("--no-static-lib-packages",
189 "Merge all library resources under the app's package.",
190 &options_.no_static_lib_packages);
191 AddOptionalSwitch("--auto-namespace-static-lib",
192 "Automatically namespace resource references when building a static\n"
193 "library.",
194 &options_.auto_namespace_static_lib);
195 AddOptionalSwitch("--non-final-ids",
196 "Generates R.java without the final modifier. This is implied when\n"
197 "--static-lib is specified.",
198 &options_.generate_non_final_ids);
199 AddOptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
200 &stable_id_file_path_);
201 AddOptionalFlag("--emit-ids",
202 "Emit a file at the given path with a list of name to ID mappings,\n"
203 "suitable for use with --stable-ids.",
204 &options_.resource_id_map_path);
205 AddOptionalFlag("--private-symbols",
206 "Package name to use when generating R.java for private symbols.\n"
207 "If not specified, public and private symbols will use the application's\n"
208 "package name.",
209 &options_.private_symbols);
210 AddOptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
211 &options_.custom_java_package);
212 AddOptionalFlagList("--extra-packages",
213 "Generate the same R.java but with different package names.",
214 &extra_java_packages_);
215 AddOptionalFlagList("--add-javadoc-annotation",
216 "Adds a JavaDoc annotation to all generated Java classes.",
217 &options_.javadoc_annotations);
218 AddOptionalFlag("--output-text-symbols",
219 "Generates a text file containing the resource symbols of the R class in\n"
220 "the specified folder.",
221 &options_.generate_text_symbols_path);
222 AddOptionalSwitch("--allow-reserved-package-id",
223 "Allows the use of a reserved package ID. This should on be used for\n"
224 "packages with a pre-O min-sdk\n",
225 &options_.allow_reserved_package_id);
226 AddOptionalSwitch("--auto-add-overlay",
227 "Allows the addition of new resources in overlays without\n"
228 "<add-resource> tags.",
229 &options_.auto_add_overlay);
230 AddOptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
231 &options_.manifest_fixer_options.rename_manifest_package);
232 AddOptionalFlag("--rename-instrumentation-target-package",
233 "Changes the name of the target package for instrumentation. Most useful\n"
234 "when used in conjunction with --rename-manifest-package.",
235 &options_.manifest_fixer_options.rename_instrumentation_target_package);
236 AddOptionalFlagList("-0", "File extensions not to compress.",
237 &options_.extensions_to_not_compress);
238 AddOptionalSwitch("--no-compress", "Do not compress any resources.",
239 &options_.do_not_compress_anything);
240 AddOptionalSwitch("--warn-manifest-validation",
241 "Treat manifest validation errors as warnings.",
242 &options_.manifest_fixer_options.warn_validation);
243 AddOptionalFlagList("--split",
244 "Split resources matching a set of configs out to a Split APK.\n"
245 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
246 "On Windows, use a semicolon ';' separator instead.",
247 &split_args_);
248 AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
249 AddOptionalSwitch("--debug-mode",
250 "Inserts android:debuggable=\"true\" in to the application node of the\n"
251 "manifest, making the application debuggable even on production devices.",
252 &options_.manifest_fixer_options.debug_mode);
253 AddOptionalSwitch("--strict-visibility",
254 "Do not allow overlays with different visibility levels.",
255 &options_.strict_visibility);
256 }
257
258 int Action(const std::vector<std::string>& args) override;
259
260 private:
261 IDiagnostics* diag_;
262 LinkOptions options_;
263
264 std::vector<std::string> overlay_arg_list_;
265 std::vector<std::string> extra_java_packages_;
266 Maybe<std::string> package_id_;
267 std::vector<std::string> configs_;
268 Maybe<std::string> preferred_density_;
269 Maybe<std::string> product_list_;
270 bool legacy_x_flag_ = false;
271 bool require_localization_ = false;
272 bool verbose_ = false;
273 bool shared_lib_ = false;
274 bool static_lib_ = false;
275 bool proto_format_ = false;
276 Maybe<std::string> stable_id_file_path_;
277 std::vector<std::string> split_args_;
278};
279
280}// namespace aapt
281
282#endif //AAPT2_LINK_H