• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <regex>
21 
22 #include "Command.h"
23 #include "Resource.h"
24 #include "androidfw/IDiagnostics.h"
25 #include "format/binary/TableFlattener.h"
26 #include "format/proto/ProtoSerialize.h"
27 #include "link/ManifestFixer.h"
28 #include "split/TableSplitter.h"
29 #include "trace/TraceBuffer.h"
30 
31 namespace aapt {
32 
33 enum class OutputFormat {
34   kApk,
35   kProto,
36 };
37 
38 struct LinkOptions {
39   std::string output_path;
40   std::string manifest_path;
41   std::vector<std::string> include_paths;
42   std::vector<std::string> overlay_files;
43   std::vector<std::string> assets_dirs;
44   bool output_to_directory = false;
45   bool auto_add_overlay = false;
46   bool override_styles_instead_of_overlaying = false;
47   OutputFormat output_format = OutputFormat::kApk;
48   std::optional<std::string> rename_resources_package;
49 
50   // Java/Proguard options.
51   std::optional<std::string> generate_java_class_path;
52   std::optional<std::string> custom_java_package;
53   std::set<std::string> extra_java_packages;
54   std::optional<std::string> generate_text_symbols_path;
55   std::optional<std::string> generate_proguard_rules_path;
56   std::optional<std::string> generate_main_dex_proguard_rules_path;
57   bool generate_conditional_proguard_rules = false;
58   bool generate_minimal_proguard_rules = false;
59   bool generate_non_final_ids = false;
60   bool no_proguard_location_reference = false;
61   std::vector<std::string> javadoc_annotations;
62   std::optional<std::string> private_symbols;
63 
64   // Optimizations/features.
65   bool no_auto_version = false;
66   bool no_version_vectors = false;
67   bool no_version_transitions = false;
68   bool no_resource_deduping = false;
69   bool no_resource_removal = false;
70   bool no_xml_namespaces = false;
71   bool do_not_compress_anything = false;
72   bool use_sparse_encoding = false;
73   std::unordered_set<std::string> extensions_to_not_compress;
74   std::optional<std::regex> regex_to_not_compress;
75 
76   // Static lib options.
77   bool no_static_lib_packages = false;
78   bool merge_only = false;
79 
80   // AndroidManifest.xml massaging options.
81   ManifestFixerOptions manifest_fixer_options;
82 
83   // Products to use/filter on.
84   std::unordered_set<std::string> products;
85 
86   // Flattening options.
87   TableFlattenerOptions table_flattener_options;
88   SerializeTableOptions proto_table_flattener_options;
89   bool keep_raw_values = false;
90 
91   // Split APK options.
92   TableSplitterOptions table_splitter_options;
93   std::vector<SplitConstraints> split_constraints;
94   std::vector<std::string> split_paths;
95 
96   // Configurations to exclude
97   std::vector<std::string> exclude_configs_;
98 
99   // Stable ID options.
100   std::unordered_map<ResourceName, ResourceId> stable_id_map;
101   std::optional<std::string> resource_id_map_path;
102 
103   // When 'true', allow reserved package IDs to be used for applications. Pre-O, the platform
104   // treats negative resource IDs [those with a package ID of 0x80 or higher] as invalid.
105   // In order to work around this limitation, we allow the use of traditionally reserved
106   // resource IDs [those between 0x02 and 0x7E].
107   bool allow_reserved_package_id = false;
108 
109   // Whether we should fail on definitions of a resource with conflicting visibility.
110   bool strict_visibility = false;
111 };
112 
113 class LinkCommand : public Command {
114  public:
LinkCommand(android::IDiagnostics * diag)115   explicit LinkCommand(android::IDiagnostics* diag) : Command("link", "l"), diag_(diag) {
116     SetDescription("Links resources into an apk.");
117     AddRequiredFlag("-o", "Output path.", &options_.output_path, Command::kPath);
118     AddRequiredFlag("--manifest", "Path to the Android manifest to build.",
119         &options_.manifest_path, Command::kPath);
120     AddOptionalFlagList("-I", "Adds an Android APK to link against.", &options_.include_paths,
121          Command::kPath);
122     AddOptionalFlagList("-A", "An assets directory to include in the APK. These are unprocessed.",
123         &options_.assets_dirs, Command::kPath);
124     AddOptionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n"
125         "The last conflicting resource given takes precedence.", &overlay_arg_list_,
126         Command::kPath);
127     AddOptionalFlag("--package-id",
128         "Specify the package ID to use for this app. Must be greater or equal to\n"
129             "0x7f and can't be used with --static-lib or --shared-lib.", &package_id_);
130     AddOptionalFlag("--java", "Directory in which to generate R.java.",
131         &options_.generate_java_class_path, Command::kPath);
132     AddOptionalFlag("--proguard", "Output file for generated Proguard rules.",
133         &options_.generate_proguard_rules_path, Command::kPath);
134     AddOptionalFlag("--proguard-main-dex",
135         "Output file for generated Proguard rules for the main dex.",
136         &options_.generate_main_dex_proguard_rules_path, Command::kPath);
137     AddOptionalSwitch("--proguard-conditional-keep-rules",
138         "Generate conditional Proguard keep rules.",
139         &options_.generate_conditional_proguard_rules);
140     AddOptionalSwitch("--proguard-minimal-keep-rules",
141         "Generate a minimal set of Proguard keep rules.",
142         &options_.generate_minimal_proguard_rules);
143     AddOptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning.",
144         &options_.no_auto_version);
145     AddOptionalSwitch("--no-version-vectors",
146         "Disables automatic versioning of vector drawables. Use this only\n"
147             "when building with vector drawable support library.",
148         &options_.no_version_vectors);
149     AddOptionalSwitch("--no-version-transitions",
150         "Disables automatic versioning of transition resources. Use this only\n"
151             "when building with transition support library.",
152         &options_.no_version_transitions);
153     AddOptionalSwitch("--no-resource-deduping", "Disables automatic deduping of resources with\n"
154             "identical values across compatible configurations.",
155         &options_.no_resource_deduping);
156     AddOptionalSwitch("--no-resource-removal", "Disables automatic removal of resources without\n"
157             "defaults. Use this only when building runtime resource overlay packages.",
158         &options_.no_resource_removal);
159     AddOptionalSwitch("--enable-sparse-encoding",
160                       "This decreases APK size at the cost of resource retrieval performance.",
161                       &options_.use_sparse_encoding);
162     AddOptionalSwitch("--enable-compact-entries",
163         "This decreases APK size by using compact resource entries for simple data types.",
164         &options_.table_flattener_options.use_compact_entries);
165     AddOptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
166         &legacy_x_flag_);
167     AddOptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
168         &require_localization_);
169     AddOptionalFlagList("-c",
170         "Comma separated list of configurations to include. The default\n"
171             "is all configurations.", &configs_);
172     AddOptionalFlag("--preferred-density",
173         "Selects the closest matching density and strips out all others.",
174         &preferred_density_);
175     AddOptionalFlag("--product", "Comma separated list of product names to keep", &product_list_);
176     AddOptionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified by -o.",
177         &options_.output_to_directory);
178     AddOptionalSwitch("--no-xml-namespaces", "Removes XML namespace prefix and URI information\n"
179             "from AndroidManifest.xml and XML binaries in res/*.",
180         &options_.no_xml_namespaces);
181     AddOptionalFlag("--min-sdk-version",
182         "Default minimum SDK version to use for AndroidManifest.xml.",
183         &options_.manifest_fixer_options.min_sdk_version_default);
184     AddOptionalFlag("--target-sdk-version",
185         "Default target SDK version to use for AndroidManifest.xml.",
186         &options_.manifest_fixer_options.target_sdk_version_default);
187     AddOptionalFlag("--version-code",
188         "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
189             "present.", &options_.manifest_fixer_options.version_code_default);
190     AddOptionalFlag("--version-code-major",
191         "Version code major (integer) to inject into the AndroidManifest.xml if none is\n"
192             "present.", &options_.manifest_fixer_options.version_code_major_default);
193     AddOptionalFlag("--version-name",
194         "Version name to inject into the AndroidManifest.xml if none is present.",
195         &options_.manifest_fixer_options.version_name_default);
196     AddOptionalFlag("--revision-code",
197         "Revision code (integer) to inject into the AndroidManifest.xml if none is\n"
198             "present.", &options_.manifest_fixer_options.revision_code_default);
199     AddOptionalSwitch("--replace-version",
200         "If --version-code, --version-name, and/or --revision-code are specified, these\n"
201             "values will replace any value already in the manifest. By\n"
202             "default, nothing is changed if the manifest already defines\n"
203             "these attributes.",
204         &options_.manifest_fixer_options.replace_version);
205     AddOptionalFlag("--compile-sdk-version-code",
206         "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
207             "present.",
208         &options_.manifest_fixer_options.compile_sdk_version);
209     AddOptionalFlag("--compile-sdk-version-name",
210         "Version name to inject into the AndroidManifest.xml if none is present.",
211         &options_.manifest_fixer_options.compile_sdk_version_codename);
212     AddOptionalSwitch(
213         "--no-compile-sdk-metadata",
214         "Suppresses output of compile SDK-related attributes in AndroidManifest.xml,\n"
215         "including android:compileSdkVersion and platformBuildVersion.",
216         &options_.manifest_fixer_options.no_compile_sdk_metadata);
217     AddOptionalFlagList("--fingerprint-prefix", "Fingerprint prefix to add to install constraints.",
218                         &options_.manifest_fixer_options.fingerprint_prefixes);
219     AddOptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
220         &shared_lib_);
221     AddOptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib_);
222     AddOptionalSwitch("--proto-format",
223         "Generates compiled resources in Protobuf format.\n"
224             "Suitable as input to the bundle tool for generating an App Bundle.",
225         &proto_format_);
226     AddOptionalSwitch("--no-static-lib-packages",
227         "Merge all library resources under the app's package.",
228         &options_.no_static_lib_packages);
229     AddOptionalSwitch("--non-final-ids",
230         "Generates R.java without the final modifier. This is implied when\n"
231             "--static-lib is specified.",
232         &options_.generate_non_final_ids);
233     AddOptionalSwitch("--no-proguard-location-reference",
234         "Keep proguard rules files from having a reference to the source file",
235         &options_.no_proguard_location_reference);
236     AddOptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
237         &stable_id_file_path_);
238     AddOptionalFlag("--emit-ids",
239         "Emit a file at the given path with a list of name to ID mappings,\n"
240             "suitable for use with --stable-ids.",
241         &options_.resource_id_map_path);
242     AddOptionalFlag("--private-symbols",
243         "Package name to use when generating R.java for private symbols.\n"
244             "If not specified, public and private symbols will use the application's\n"
245             "package name.",
246         &options_.private_symbols);
247     AddOptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
248         &options_.custom_java_package);
249     AddOptionalFlagList("--extra-packages",
250         "Generate the same R.java but with different package names.",
251         &extra_java_packages_);
252     AddOptionalFlagList("--add-javadoc-annotation",
253         "Adds a JavaDoc annotation to all generated Java classes.",
254         &options_.javadoc_annotations);
255     AddOptionalFlag("--output-text-symbols",
256         "Generates a text file containing the resource symbols of the R class in\n"
257             "the specified folder.",
258         &options_.generate_text_symbols_path);
259     AddOptionalSwitch("--allow-reserved-package-id",
260         "Allows the use of a reserved package ID. This should on be used for\n"
261             "packages with a pre-O min-sdk\n",
262         &options_.allow_reserved_package_id);
263     AddOptionalSwitch("--auto-add-overlay",
264         "Allows the addition of new resources in overlays without\n"
265             "<add-resource> tags.",
266         &options_.auto_add_overlay);
267     AddOptionalSwitch("--override-styles-instead-of-overlaying",
268         "Causes styles defined in -R resources to replace previous definitions\n"
269             "instead of merging into them\n",
270         &options_.override_styles_instead_of_overlaying);
271     AddOptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
272         &options_.manifest_fixer_options.rename_manifest_package);
273     AddOptionalFlag("--rename-resources-package", "Renames the package in resources table",
274         &options_.rename_resources_package);
275     AddOptionalFlag("--rename-instrumentation-target-package",
276         "Changes the name of the target package for instrumentation. Most useful\n"
277             "when used in conjunction with --rename-manifest-package.",
278         &options_.manifest_fixer_options.rename_instrumentation_target_package);
279     AddOptionalFlag("--rename-overlay-target-package",
280         "Changes the name of the target package for overlay. Most useful\n"
281             "when used in conjunction with --rename-manifest-package.",
282         &options_.manifest_fixer_options.rename_overlay_target_package);
283     AddOptionalFlag("--rename-overlay-category", "Changes the category for the overlay.",
284                     &options_.manifest_fixer_options.rename_overlay_category);
285     AddOptionalFlagList("-0", "File suffix not to compress.",
286         &options_.extensions_to_not_compress);
287     AddOptionalSwitch("--no-compress", "Do not compress any resources.",
288         &options_.do_not_compress_anything);
289     AddOptionalSwitch("--keep-raw-values", "Preserve raw attribute values in xml files.",
290         &options_.keep_raw_values);
291     AddOptionalFlag("--no-compress-regex",
292         "Do not compress extensions matching the regular expression. Remember to\n"
293             "use the '$' symbol for end of line. Uses a case-sensitive ECMAScript"
294             "regular expression grammar.",
295         &no_compress_regex);
296     AddOptionalSwitch("--warn-manifest-validation",
297         "Treat manifest validation errors as warnings.",
298         &options_.manifest_fixer_options.warn_validation);
299     AddOptionalFlagList("--split",
300         "Split resources matching a set of configs out to a Split APK.\n"
301             "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
302             "On Windows, use a semicolon ';' separator instead.",
303         &split_args_);
304     AddOptionalFlagList("--exclude-configs",
305         "Excludes values of resources whose configs contain the specified qualifiers.",
306         &options_.exclude_configs_);
307     AddOptionalSwitch("--debug-mode",
308         "Inserts android:debuggable=\"true\" in to the application node of the\n"
309             "manifest, making the application debuggable even on production devices.",
310         &options_.manifest_fixer_options.debug_mode);
311     AddOptionalSwitch("--strict-visibility",
312         "Do not allow overlays with different visibility levels.",
313         &options_.strict_visibility);
314     AddOptionalSwitch("--exclude-sources",
315         "Do not serialize source file information when generating resources in\n"
316             "Protobuf format.",
317         &options_.proto_table_flattener_options.exclude_sources);
318     AddOptionalFlag("--trace-folder",
319         "Generate systrace json trace fragment to specified folder.",
320         &trace_folder_);
321     AddOptionalSwitch("--merge-only",
322         "Only merge the resources, without verifying resource references. This flag\n"
323             "should only be used together with the --static-lib flag.",
324         &options_.merge_only);
325     AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
326   }
327 
328   int Action(const std::vector<std::string>& args) override;
329 
330  private:
331   android::IDiagnostics* diag_;
332   LinkOptions options_;
333 
334   std::vector<std::string> overlay_arg_list_;
335   std::vector<std::string> extra_java_packages_;
336   std::optional<std::string> package_id_;
337   std::vector<std::string> configs_;
338   std::optional<std::string> preferred_density_;
339   std::optional<std::string> product_list_;
340   std::optional<std::string> no_compress_regex;
341   bool legacy_x_flag_ = false;
342   bool require_localization_ = false;
343   bool verbose_ = false;
344   bool shared_lib_ = false;
345   bool static_lib_ = false;
346   bool proto_format_ = false;
347   std::optional<std::string> stable_id_file_path_;
348   std::vector<std::string> split_args_;
349   std::optional<std::string> trace_folder_;
350 };
351 
352 }// namespace aapt
353 
354 #endif //AAPT2_LINK_H
355