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