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