blob: 53f0abee1cf25177bd2c288cb25764ef7c34cc9f [file] [log] [blame]
Adam Lesinski2ae4a872015-11-02 16:10:55 -08001/*
2 * Copyright (C) 2015 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
Adam Lesinski2ae4a872015-11-02 16:10:55 -080017#include "link/ManifestFixer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <unordered_set>
20
21#include "android-base/logging.h"
22
Adam Lesinskicacb28f2016-10-19 12:18:14 -070023#include "ResourceUtils.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080024#include "trace/TraceBuffer.h"
Adam Lesinski2ae4a872015-11-02 16:10:55 -080025#include "util/Util.h"
Adam Lesinskicc5609d2016-04-05 12:41:07 -070026#include "xml/XmlActionExecutor.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080027#include "xml/XmlDom.h"
Adam Lesinski2ae4a872015-11-02 16:10:55 -080028
Adam Lesinskid5083f62017-01-16 15:07:21 -080029using android::StringPiece;
30
Adam Lesinski2ae4a872015-11-02 16:10:55 -080031namespace aapt {
32
Brandon Liu3cff2552022-10-19 18:51:07 +000033// This is to detect whether an <intent-filter> contains deeplink.
34// See https://developer.android.com/training/app-links/deep-linking.
35static bool HasDeepLink(xml::Element* intent_filter_el) {
36 xml::Element* action_el = intent_filter_el->FindChild({}, "action");
37 xml::Element* category_el = intent_filter_el->FindChild({}, "category");
38 xml::Element* data_el = intent_filter_el->FindChild({}, "data");
39 if (action_el == nullptr || category_el == nullptr || data_el == nullptr) {
40 return false;
41 }
42
43 // Deeplinks must specify the ACTION_VIEW intent action.
44 constexpr const char* action_view = "android.intent.action.VIEW";
45 if (intent_filter_el->FindChildWithAttribute({}, "action", xml::kSchemaAndroid, "name",
46 action_view) == nullptr) {
47 return false;
48 }
49
50 // Deeplinks must have scheme included in <data> tag.
51 xml::Attribute* data_scheme_attr = data_el->FindAttribute(xml::kSchemaAndroid, "scheme");
52 if (data_scheme_attr == nullptr || data_scheme_attr->value.empty()) {
53 return false;
54 }
55
56 // Deeplinks must include BROWSABLE category.
57 constexpr const char* category_browsable = "android.intent.category.BROWSABLE";
58 if (intent_filter_el->FindChildWithAttribute({}, "category", xml::kSchemaAndroid, "name",
59 category_browsable) == nullptr) {
60 return false;
61 }
62 return true;
63}
64
65static bool VerifyDeeplinkPathAttribute(xml::Element* data_el, android::SourcePathDiagnostics* diag,
66 const std::string& attr_name) {
67 xml::Attribute* attr = data_el->FindAttribute(xml::kSchemaAndroid, attr_name);
68 if (attr != nullptr && !attr->value.empty()) {
69 StringPiece attr_value = attr->value;
70 const char* startChar = attr_value.begin();
71 if (attr_name == "pathPattern") {
Brandon Liua35bf782022-11-03 19:04:31 +000072 // pathPattern starts with '.' or '*' does not need leading slash.
73 // Reference starts with @ does not need leading slash.
74 if (*startChar == '/' || *startChar == '.' || *startChar == '*' || *startChar == '@') {
Brandon Liu3cff2552022-10-19 18:51:07 +000075 return true;
76 } else {
77 diag->Error(android::DiagMessage(data_el->line_number)
78 << "attribute 'android:" << attr_name << "' in <" << data_el->name
79 << "> tag has value of '" << attr_value
80 << "', it must be in a pattern start with '.' or '*', otherwise must start "
81 "with a leading slash '/'");
82 return false;
83 }
84 } else {
Brandon Liua35bf782022-11-03 19:04:31 +000085 // Reference starts with @ does not need leading slash.
86 if (*startChar == '/' || *startChar == '@') {
Brandon Liu3cff2552022-10-19 18:51:07 +000087 return true;
88 } else {
89 diag->Error(android::DiagMessage(data_el->line_number)
90 << "attribute 'android:" << attr_name << "' in <" << data_el->name
91 << "> tag has value of '" << attr_value
92 << "', it must start with a leading slash '/'");
93 return false;
94 }
95 }
96 }
97 return true;
98}
99
100static bool VerifyDeepLinkIntentAction(xml::Element* intent_filter_el,
101 android::SourcePathDiagnostics* diag) {
102 if (!HasDeepLink(intent_filter_el)) {
103 return true;
104 }
105
106 xml::Element* data_el = intent_filter_el->FindChild({}, "data");
107 if (data_el != nullptr) {
108 if (!VerifyDeeplinkPathAttribute(data_el, diag, "path")) {
109 return false;
110 }
111 if (!VerifyDeeplinkPathAttribute(data_el, diag, "pathPrefix")) {
112 return false;
113 }
114 if (!VerifyDeeplinkPathAttribute(data_el, diag, "pathPattern")) {
115 return false;
116 }
117 }
118 return true;
119}
120
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000121static bool RequiredNameIsNotEmpty(xml::Element* el, android::SourcePathDiagnostics* diag) {
Adam Lesinskifca5e422017-12-20 15:03:36 -0800122 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
123 if (attr == nullptr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000124 diag->Error(android::DiagMessage(el->line_number)
Adam Lesinskifca5e422017-12-20 15:03:36 -0800125 << "<" << el->name << "> is missing attribute 'android:name'");
126 return false;
127 }
128
129 if (attr->value.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000130 diag->Error(android::DiagMessage(el->line_number)
Adam Lesinskifca5e422017-12-20 15:03:36 -0800131 << "attribute 'android:name' in <" << el->name << "> tag must not be empty");
132 return false;
133 }
134 return true;
135}
136
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800137// This is how PackageManager builds class names from AndroidManifest.xml entries.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138static bool NameIsJavaClassName(xml::Element* el, xml::Attribute* attr,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000139 android::SourcePathDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 // We allow unqualified class names (ie: .HelloActivity)
141 // Since we don't know the package name, we can just make a fake one here and
142 // the test will be identical as long as the real package name is valid too.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700143 std::optional<std::string> fully_qualified_class_name =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 util::GetFullyQualifiedClassName("a", attr->value);
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 StringPiece qualified_class_name = fully_qualified_class_name
147 ? fully_qualified_class_name.value()
148 : attr->value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 if (!util::IsJavaClassName(qualified_class_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000151 diag->Error(android::DiagMessage(el->line_number) << "attribute 'android:name' in <" << el->name
152 << "> tag must be a valid Java class name");
Adam Lesinski52364f72016-01-11 13:10:24 -0800153 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 }
155 return true;
156}
157
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000158static bool OptionalNameIsJavaClassName(xml::Element* el, android::SourcePathDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) {
160 return NameIsJavaClassName(el, attr, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 }
162 return true;
163}
164
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000165static bool RequiredNameIsJavaClassName(xml::Element* el, android::SourcePathDiagnostics* diag) {
Adam Lesinskifca5e422017-12-20 15:03:36 -0800166 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
167 if (attr == nullptr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000168 diag->Error(android::DiagMessage(el->line_number)
Adam Lesinskifca5e422017-12-20 15:03:36 -0800169 << "<" << el->name << "> is missing attribute 'android:name'");
170 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 }
Adam Lesinskifca5e422017-12-20 15:03:36 -0800172 return NameIsJavaClassName(el, attr, diag);
Adam Lesinski52364f72016-01-11 13:10:24 -0800173}
174
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000175static bool RequiredNameIsJavaPackage(xml::Element* el, android::SourcePathDiagnostics* diag) {
Adam Lesinskifca5e422017-12-20 15:03:36 -0800176 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
177 if (attr == nullptr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000178 diag->Error(android::DiagMessage(el->line_number)
Adam Lesinskifca5e422017-12-20 15:03:36 -0800179 << "<" << el->name << "> is missing attribute 'android:name'");
180 return false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800181 }
Adam Lesinskifca5e422017-12-20 15:03:36 -0800182
183 if (!util::IsJavaPackageName(attr->value)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000184 diag->Error(android::DiagMessage(el->line_number) << "attribute 'android:name' in <" << el->name
185 << "> tag must be a valid Java package name");
Adam Lesinskifca5e422017-12-20 15:03:36 -0800186 return false;
187 }
188 return true;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800189}
190
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800191static xml::XmlNodeAction::ActionFuncWithDiag RequiredAndroidAttribute(const std::string& attr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000192 return [=](xml::Element* el, android::SourcePathDiagnostics* diag) -> bool {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800193 if (el->FindAttribute(xml::kSchemaAndroid, attr) == nullptr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000194 diag->Error(android::DiagMessage(el->line_number)
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800195 << "<" << el->name << "> is missing required attribute 'android:" << attr << "'");
196 return false;
197 }
198 return true;
199 };
200}
201
Todd Kennedyce3e1292020-10-29 17:14:24 -0700202static xml::XmlNodeAction::ActionFuncWithDiag RequiredOneAndroidAttribute(
203 const std::string& attrName1, const std::string& attrName2) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000204 return [=](xml::Element* el, android::SourcePathDiagnostics* diag) -> bool {
Todd Kennedyce3e1292020-10-29 17:14:24 -0700205 xml::Attribute* attr1 = el->FindAttribute(xml::kSchemaAndroid, attrName1);
206 xml::Attribute* attr2 = el->FindAttribute(xml::kSchemaAndroid, attrName2);
207 if (attr1 == nullptr && attr2 == nullptr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000208 diag->Error(android::DiagMessage(el->line_number)
Todd Kennedyce3e1292020-10-29 17:14:24 -0700209 << "<" << el->name << "> is missing required attribute 'android:" << attrName1
210 << "' or 'android:" << attrName2 << "'");
211 return false;
212 }
213 if (attr1 != nullptr && attr2 != nullptr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000214 diag->Error(android::DiagMessage(el->line_number)
Todd Kennedyce3e1292020-10-29 17:14:24 -0700215 << "<" << el->name << "> can only specify one of attribute 'android:" << attrName1
216 << "' or 'android:" << attrName2 << "'");
217 return false;
218 }
219 return true;
220 };
221}
222
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000223static bool AutoGenerateIsFeatureSplit(xml::Element* el, android::SourcePathDiagnostics* diag) {
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800224 constexpr const char* kFeatureSplit = "featureSplit";
225 constexpr const char* kIsFeatureSplit = "isFeatureSplit";
226
227 xml::Attribute* attr = el->FindAttribute({}, kFeatureSplit);
228 if (attr != nullptr) {
229 // Rewrite the featureSplit attribute to be "split". This is what the
230 // platform recognizes.
231 attr->name = "split";
232
233 // Now inject the android:isFeatureSplit="true" attribute.
234 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, kIsFeatureSplit);
235 if (attr != nullptr) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700236 if (!ResourceUtils::ParseBool(attr->value).value_or(false)) {
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800237 // The isFeatureSplit attribute is false, which conflicts with the use
238 // of "featureSplit".
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000239 diag->Error(android::DiagMessage(el->line_number)
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800240 << "attribute 'featureSplit' used in <manifest> but 'android:isFeatureSplit' "
241 "is not 'true'");
242 return false;
243 }
244
245 // The attribute is already there and set to true, nothing to do.
246 } else {
247 el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, kIsFeatureSplit, "true"});
248 }
249 }
250 return true;
251}
252
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000253static bool AutoGenerateIsSplitRequired(xml::Element* el, android::SourcePathDiagnostics* diag) {
Jackal Guo0c01abb2021-07-30 22:17:43 +0800254 constexpr const char* kRequiredSplitTypes = "requiredSplitTypes";
255 constexpr const char* kIsSplitRequired = "isSplitRequired";
256
257 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, kRequiredSplitTypes);
258 if (attr != nullptr) {
259 // Now inject the android:isSplitRequired="true" attribute.
260 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, kIsSplitRequired);
261 if (attr != nullptr) {
262 if (!ResourceUtils::ParseBool(attr->value).value_or(false)) {
263 // The isFeatureSplit attribute is false, which conflicts with the use
264 // of "featureSplit".
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000265 diag->Error(android::DiagMessage(el->line_number)
Jackal Guo0c01abb2021-07-30 22:17:43 +0800266 << "attribute 'requiredSplitTypes' used in <manifest> but "
267 "'android:isSplitRequired' is not 'true'");
268 return false;
269 }
270 // The attribute is already there and set to true, nothing to do.
271 } else {
272 el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, kIsSplitRequired, "true"});
273 }
274 }
275 return true;
276}
277
Rhed Jao2c434422020-11-12 10:48:03 +0800278static bool VerifyManifest(xml::Element* el, xml::XmlActionExecutorPolicy policy,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000279 android::SourcePathDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 xml::Attribute* attr = el->FindAttribute({}, "package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 if (!attr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000282 diag->Error(android::DiagMessage(el->line_number)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 << "<manifest> tag is missing 'package' attribute");
284 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 } else if (ResourceUtils::IsReference(attr->value)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000286 diag->Error(android::DiagMessage(el->line_number)
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800287 << "attribute 'package' in <manifest> tag must not be a reference");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 return false;
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800289 } else if (!util::IsAndroidPackageName(attr->value)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000290 android::DiagMessage error_msg(el->line_number);
Rhed Jao2c434422020-11-12 10:48:03 +0800291 error_msg << "attribute 'package' in <manifest> tag is not a valid Android package name: '"
292 << attr->value << "'";
293 if (policy == xml::XmlActionExecutorPolicy::kAllowListWarning) {
294 // Treat the error only as a warning.
295 diag->Warn(error_msg);
296 } else {
297 diag->Error(error_msg);
298 return false;
299 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 }
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800301
302 attr = el->FindAttribute({}, "split");
303 if (attr) {
304 if (!util::IsJavaPackageName(attr->value)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000305 diag->Error(android::DiagMessage(el->line_number)
306 << "attribute 'split' in <manifest> tag is not a "
307 "valid split name");
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800308 return false;
309 }
310 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 return true;
Adam Lesinski52364f72016-01-11 13:10:24 -0800312}
313
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800314// The coreApp attribute in <manifest> is not a regular AAPT attribute, so type
315// checking on it is manual.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000316static bool FixCoreAppAttribute(xml::Element* el, android::SourcePathDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 if (xml::Attribute* attr = el->FindAttribute("", "coreApp")) {
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800318 std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseBool(attr->value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700319 if (!result) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000320 diag->Error(android::DiagMessage(el->line_number) << "attribute coreApp must be a boolean");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 return false;
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700322 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 attr->compiled_value = std::move(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324 }
325 return true;
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700326}
327
Adam Lesinski86d67df2017-01-31 13:47:27 -0800328// Checks that <uses-feature> has android:glEsVersion or android:name, not both (or neither).
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000329static bool VerifyUsesFeature(xml::Element* el, android::SourcePathDiagnostics* diag) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800330 bool has_name = false;
331 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) {
332 if (attr->value.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000333 diag->Error(android::DiagMessage(el->line_number)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800334 << "android:name in <uses-feature> must not be empty");
335 return false;
336 }
337 has_name = true;
338 }
339
340 bool has_gl_es_version = false;
341 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "glEsVersion")) {
342 if (has_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000343 diag->Error(android::DiagMessage(el->line_number)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800344 << "cannot define both android:name and android:glEsVersion in <uses-feature>");
345 return false;
346 }
347 has_gl_es_version = true;
348 }
349
350 if (!has_name && !has_gl_es_version) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000351 diag->Error(android::DiagMessage(el->line_number)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800352 << "<uses-feature> must have either android:name or android:glEsVersion attribute");
353 return false;
354 }
355 return true;
356}
357
Donald Chai6e497352019-05-19 21:07:50 -0700358// Ensure that 'ns_decls' contains a declaration for 'uri', using 'prefix' as
359// the xmlns prefix if possible.
360static void EnsureNamespaceIsDeclared(const std::string& prefix, const std::string& uri,
361 std::vector<xml::NamespaceDecl>* ns_decls) {
362 if (std::find_if(ns_decls->begin(), ns_decls->end(), [&](const xml::NamespaceDecl& ns_decl) {
363 return ns_decl.uri == uri;
364 }) != ns_decls->end()) {
365 return;
366 }
367
368 std::set<std::string> used_prefixes;
369 for (const auto& ns_decl : *ns_decls) {
370 used_prefixes.insert(ns_decl.prefix);
371 }
372
373 // Make multiple attempts in the unlikely event that 'prefix' is already taken.
374 std::string disambiguator;
375 for (int i = 0; i < used_prefixes.size() + 1; i++) {
376 std::string attempted_prefix = prefix + disambiguator;
377 if (used_prefixes.find(attempted_prefix) == used_prefixes.end()) {
378 ns_decls->push_back(xml::NamespaceDecl{attempted_prefix, uri});
379 return;
380 }
381 disambiguator = std::to_string(i);
382 }
383}
384
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000385bool ManifestFixer::BuildRules(xml::XmlActionExecutor* executor, android::IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 // First verify some options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 if (options_.rename_manifest_package) {
388 if (!util::IsJavaPackageName(options_.rename_manifest_package.value())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000389 diag->Error(android::DiagMessage() << "invalid manifest package override '"
390 << options_.rename_manifest_package.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 return false;
392 }
393 }
394
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 if (options_.rename_instrumentation_target_package) {
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800396 if (!util::IsJavaPackageName(options_.rename_instrumentation_target_package.value())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000397 diag->Error(android::DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 << "invalid instrumentation target package override '"
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000399 << options_.rename_instrumentation_target_package.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 return false;
401 }
402 }
403
Roshan Piusae12ce42020-04-25 16:08:55 -0700404 if (options_.rename_overlay_target_package) {
405 if (!util::IsJavaPackageName(options_.rename_overlay_target_package.value())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000406 diag->Error(android::DiagMessage() << "invalid overlay target package override '"
407 << options_.rename_overlay_target_package.value() << "'");
Roshan Piusae12ce42020-04-25 16:08:55 -0700408 return false;
409 }
410 }
411
Adam Lesinski86d67df2017-01-31 13:47:27 -0800412 // Common <intent-filter> actions.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 xml::XmlNodeAction intent_filter_action;
Brandon Liu3cff2552022-10-19 18:51:07 +0000414 intent_filter_action.Action(VerifyDeepLinkIntentAction);
Adam Lesinskifca5e422017-12-20 15:03:36 -0800415 intent_filter_action["action"].Action(RequiredNameIsNotEmpty);
416 intent_filter_action["category"].Action(RequiredNameIsNotEmpty);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 intent_filter_action["data"];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418
Adam Lesinski86d67df2017-01-31 13:47:27 -0800419 // Common <meta-data> actions.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420 xml::XmlNodeAction meta_data_action;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421
Todd Kennedyce3e1292020-10-29 17:14:24 -0700422 // Common <property> actions.
423 xml::XmlNodeAction property_action;
424 property_action.Action(RequiredOneAndroidAttribute("resource", "value"));
425
Adam Lesinski86d67df2017-01-31 13:47:27 -0800426 // Common <uses-feature> actions.
427 xml::XmlNodeAction uses_feature_action;
428 uses_feature_action.Action(VerifyUsesFeature);
429
430 // Common component actions.
431 xml::XmlNodeAction component_action;
432 component_action.Action(RequiredNameIsJavaClassName);
433 component_action["intent-filter"] = intent_filter_action;
Ryan Mitchell28afe682018-09-07 14:33:14 -0700434 component_action["preferred"] = intent_filter_action;
Adam Lesinski86d67df2017-01-31 13:47:27 -0800435 component_action["meta-data"] = meta_data_action;
Todd Kennedyce3e1292020-10-29 17:14:24 -0700436 component_action["property"] = property_action;
Adam Lesinski86d67df2017-01-31 13:47:27 -0800437
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 // Manifest actions.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 xml::XmlNodeAction& manifest_action = (*executor)["manifest"];
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800440 manifest_action.Action(AutoGenerateIsFeatureSplit);
Jackal Guo0c01abb2021-07-30 22:17:43 +0800441 manifest_action.Action(AutoGenerateIsSplitRequired);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 manifest_action.Action(VerifyManifest);
443 manifest_action.Action(FixCoreAppAttribute);
444 manifest_action.Action([&](xml::Element* el) -> bool {
Donald Chai6e497352019-05-19 21:07:50 -0700445 EnsureNamespaceIsDeclared("android", xml::kSchemaAndroid, &el->namespace_decls);
446
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 if (options_.version_name_default) {
Colin Crossdcd58c42018-05-25 22:46:35 -0700448 if (options_.replace_version) {
449 el->RemoveAttribute(xml::kSchemaAndroid, "versionName");
450 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451 if (el->FindAttribute(xml::kSchemaAndroid, "versionName") == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700452 el->attributes.push_back(
453 xml::Attribute{xml::kSchemaAndroid, "versionName",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 options_.version_name_default.value()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700456 }
457
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458 if (options_.version_code_default) {
Colin Crossdcd58c42018-05-25 22:46:35 -0700459 if (options_.replace_version) {
460 el->RemoveAttribute(xml::kSchemaAndroid, "versionCode");
461 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700462 if (el->FindAttribute(xml::kSchemaAndroid, "versionCode") == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463 el->attributes.push_back(
464 xml::Attribute{xml::kSchemaAndroid, "versionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 options_.version_code_default.value()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700467 }
Ryan Mitchell7cb82a82018-05-10 15:35:31 -0700468
Ryan Mitchell704090e2018-07-31 14:59:25 -0700469 if (options_.version_code_major_default) {
470 if (options_.replace_version) {
471 el->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
472 }
473 if (el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor") == nullptr) {
474 el->attributes.push_back(
475 xml::Attribute{xml::kSchemaAndroid, "versionCodeMajor",
476 options_.version_code_major_default.value()});
477 }
478 }
479
Rhed Jaob9ccb8a42020-11-30 21:42:16 +0800480 if (options_.revision_code_default) {
481 if (options_.replace_version) {
482 el->RemoveAttribute(xml::kSchemaAndroid, "revisionCode");
483 }
484 if (el->FindAttribute(xml::kSchemaAndroid, "revisionCode") == nullptr) {
485 el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "revisionCode",
486 options_.revision_code_default.value()});
487 }
488 }
489
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800490 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 });
492
493 // Meta tags.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700494 manifest_action["eat-comment"];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495
496 // Uses-sdk actions.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497 manifest_action["uses-sdk"].Action([&](xml::Element* el) -> bool {
498 if (options_.min_sdk_version_default &&
499 el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion") == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 // There was no minSdkVersion defined and we have a default to assign.
501 el->attributes.push_back(
502 xml::Attribute{xml::kSchemaAndroid, "minSdkVersion",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700503 options_.min_sdk_version_default.value()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 }
505
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 if (options_.target_sdk_version_default &&
507 el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion") == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 // There was no targetSdkVersion defined and we have a default to assign.
509 el->attributes.push_back(
510 xml::Attribute{xml::kSchemaAndroid, "targetSdkVersion",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 options_.target_sdk_version_default.value()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 }
513 return true;
514 });
Anton Hanssonb2f709d2020-01-09 10:25:23 +0000515 manifest_action["uses-sdk"]["extension-sdk"];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516
517 // Instrumentation actions.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800518 manifest_action["instrumentation"].Action(RequiredNameIsJavaClassName);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 manifest_action["instrumentation"].Action([&](xml::Element* el) -> bool {
520 if (!options_.rename_instrumentation_target_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 return true;
522 }
523
524 if (xml::Attribute* attr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700525 el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) {
526 attr->value = options_.rename_instrumentation_target_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527 }
528 return true;
529 });
Adam Lesinski86d67df2017-01-31 13:47:27 -0800530 manifest_action["instrumentation"]["meta-data"] = meta_data_action;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800532 manifest_action["attribution"];
533 manifest_action["attribution"]["inherit-from"];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534 manifest_action["original-package"];
Roshan Piusae12ce42020-04-25 16:08:55 -0700535 manifest_action["overlay"].Action([&](xml::Element* el) -> bool {
Jeremy Meyer6b05b7d2022-09-30 22:22:24 +0000536 if (options_.rename_overlay_target_package) {
537 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) {
538 attr->value = options_.rename_overlay_target_package.value();
539 }
Roshan Piusae12ce42020-04-25 16:08:55 -0700540 }
Jeremy Meyer6b05b7d2022-09-30 22:22:24 +0000541 if (options_.rename_overlay_category) {
542 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "category")) {
543 attr->value = options_.rename_overlay_category.value();
544 } else {
545 el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "category",
546 options_.rename_overlay_category.value()});
547 }
Roshan Piusae12ce42020-04-25 16:08:55 -0700548 }
549 return true;
550 });
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 manifest_action["protected-broadcast"];
Alan Viverettecf5326f2018-01-05 16:03:50 -0500552 manifest_action["adopt-permissions"];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700553 manifest_action["uses-permission"];
Sergey Nikolaienkov65f90992020-09-30 08:17:09 +0000554 manifest_action["uses-permission"]["required-feature"].Action(RequiredNameIsNotEmpty);
555 manifest_action["uses-permission"]["required-not-feature"].Action(RequiredNameIsNotEmpty);
Adam Lesinski4b585db2017-05-12 15:25:50 -0700556 manifest_action["uses-permission-sdk-23"];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700557 manifest_action["permission"];
Ryan Mitchell66f6cfb2018-07-25 16:15:17 -0700558 manifest_action["permission"]["meta-data"] = meta_data_action;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700559 manifest_action["permission-tree"];
560 manifest_action["permission-group"];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 manifest_action["uses-configuration"];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 manifest_action["supports-screens"];
Adam Lesinski86d67df2017-01-31 13:47:27 -0800563 manifest_action["uses-feature"] = uses_feature_action;
564 manifest_action["feature-group"]["uses-feature"] = uses_feature_action;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 manifest_action["compatible-screens"];
566 manifest_action["compatible-screens"]["screen"];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700567 manifest_action["supports-gl-texture"];
Ryan Mitchell66f6cfb2018-07-25 16:15:17 -0700568 manifest_action["restrict-update"];
MÃ¥rten Kongstadb0502bb2022-05-16 15:23:40 +0200569 manifest_action["install-constraints"]["fingerprint-prefix"];
Ryan Mitchell66f6cfb2018-07-25 16:15:17 -0700570 manifest_action["package-verifier"];
Adam Lesinski5119e512016-12-05 19:48:20 -0800571 manifest_action["meta-data"] = meta_data_action;
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800572 manifest_action["uses-split"].Action(RequiredNameIsJavaPackage);
Patrick Baumanna4ffb452019-06-24 15:01:49 -0700573 manifest_action["queries"]["package"].Action(RequiredNameIsJavaPackage);
574 manifest_action["queries"]["intent"] = intent_filter_action;
Patrick Baumann99181232020-01-28 10:55:25 -0800575 manifest_action["queries"]["provider"].Action(RequiredAndroidAttribute("authorities"));
Patrick Baumanna4ffb452019-06-24 15:01:49 -0700576 // TODO: more complicated component name tag
Adam Lesinski5119e512016-12-05 19:48:20 -0800577
Adam Lesinski87f1e0f2017-06-27 16:21:58 -0700578 manifest_action["key-sets"]["key-set"]["public-key"];
579 manifest_action["key-sets"]["upgrade-key-set"];
580
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 // Application actions.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582 xml::XmlNodeAction& application_action = manifest_action["application"];
583 application_action.Action(OptionalNameIsJavaClassName);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584
Adam Lesinskifca5e422017-12-20 15:03:36 -0800585 application_action["uses-library"].Action(RequiredNameIsNotEmpty);
Jiyong Park6a5b8b12020-06-30 13:23:36 +0900586 application_action["uses-native-library"].Action(RequiredNameIsNotEmpty);
Adam Lesinskifca5e422017-12-20 15:03:36 -0800587 application_action["library"].Action(RequiredNameIsNotEmpty);
Chris Craik335b5652019-04-04 12:46:47 -0700588 application_action["profileable"];
Todd Kennedyce3e1292020-10-29 17:14:24 -0700589 application_action["property"] = property_action;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800590
591 xml::XmlNodeAction& static_library_action = application_action["static-library"];
592 static_library_action.Action(RequiredNameIsJavaPackage);
593 static_library_action.Action(RequiredAndroidAttribute("version"));
594
595 xml::XmlNodeAction& uses_static_library_action = application_action["uses-static-library"];
596 uses_static_library_action.Action(RequiredNameIsJavaPackage);
597 uses_static_library_action.Action(RequiredAndroidAttribute("version"));
598 uses_static_library_action.Action(RequiredAndroidAttribute("certDigest"));
Ryan Mitchell66f6cfb2018-07-25 16:15:17 -0700599 uses_static_library_action["additional-certificate"];
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800600
Alex Buynytskyya16137052021-12-02 13:26:54 +0000601 xml::XmlNodeAction& sdk_library_action = application_action["sdk-library"];
602 sdk_library_action.Action(RequiredNameIsJavaPackage);
603 sdk_library_action.Action(RequiredAndroidAttribute("versionMajor"));
604
605 xml::XmlNodeAction& uses_sdk_library_action = application_action["uses-sdk-library"];
606 uses_sdk_library_action.Action(RequiredNameIsJavaPackage);
607 uses_sdk_library_action.Action(RequiredAndroidAttribute("versionMajor"));
608 uses_sdk_library_action.Action(RequiredAndroidAttribute("certDigest"));
609 uses_sdk_library_action["additional-certificate"];
610
Dianne Hackborn813d7502018-10-02 16:59:46 -0700611 xml::XmlNodeAction& uses_package_action = application_action["uses-package"];
612 uses_package_action.Action(RequiredNameIsJavaPackage);
613 uses_package_action["additional-certificate"];
614
Ryan Mitchelle5b38a62018-03-23 13:35:00 -0700615 if (options_.debug_mode) {
616 application_action.Action([&](xml::Element* el) -> bool {
617 xml::Attribute *attr = el->FindOrCreateAttribute(xml::kSchemaAndroid, "debuggable");
618 attr->value = "true";
619 return true;
620 });
621 }
622
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700623 application_action["meta-data"] = meta_data_action;
Adam Lesinski7a917a22017-06-02 12:55:24 -0700624
Dianne Hackbornfc0839a2020-01-31 11:02:28 -0800625 application_action["processes"];
626 application_action["processes"]["deny-permission"];
627 application_action["processes"]["allow-permission"];
628 application_action["processes"]["process"]["deny-permission"];
629 application_action["processes"]["process"]["allow-permission"];
630
Adam Lesinski86d67df2017-01-31 13:47:27 -0800631 application_action["activity"] = component_action;
Adam Lesinski7a917a22017-06-02 12:55:24 -0700632 application_action["activity"]["layout"];
633
Adam Lesinski86d67df2017-01-31 13:47:27 -0800634 application_action["activity-alias"] = component_action;
635 application_action["service"] = component_action;
636 application_action["receiver"] = component_action;
Gavin Corkeryad5d4ba2021-10-26 12:24:43 +0100637 application_action["apex-system-service"] = component_action;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638
639 // Provider actions.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800640 application_action["provider"] = component_action;
Adam Lesinskic10c0d02017-04-28 12:54:08 -0700641 application_action["provider"]["grant-uri-permission"];
Adam Lesinski25783ca2017-04-24 13:33:47 -0700642 application_action["provider"]["path-permission"];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643
Ryan Mitchell28afe682018-09-07 14:33:14 -0700644 manifest_action["package"] = manifest_action;
645
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 return true;
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800647}
648
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700649static void FullyQualifyClassName(StringPiece package, StringPiece attr_ns, StringPiece attr_name,
650 xml::Element* el) {
Adam Lesinski23034b92017-11-29 16:27:44 -0800651 xml::Attribute* attr = el->FindAttribute(attr_ns, attr_name);
652 if (attr != nullptr) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700653 if (std::optional<std::string> new_value =
654 util::GetFullyQualifiedClassName(package, attr->value)) {
Adam Lesinski23034b92017-11-29 16:27:44 -0800655 attr->value = std::move(new_value.value());
Adam Lesinski52364f72016-01-11 13:10:24 -0800656 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 }
Adam Lesinski23034b92017-11-29 16:27:44 -0800658}
Adam Lesinski52364f72016-01-11 13:10:24 -0800659
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700660static bool RenameManifestPackage(StringPiece package_override, xml::Element* manifest_el) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700661 xml::Attribute* attr = manifest_el->FindAttribute({}, "package");
Adam Lesinski52364f72016-01-11 13:10:24 -0800662
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 // We've already verified that the manifest element is present, with a package
664 // name specified.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 CHECK(attr != nullptr);
Adam Lesinski52364f72016-01-11 13:10:24 -0800666
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700667 std::string original_package = std::move(attr->value);
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700668 attr->value.assign(package_override);
Adam Lesinski52364f72016-01-11 13:10:24 -0800669
Adam Lesinski23034b92017-11-29 16:27:44 -0800670 xml::Element* application_el = manifest_el->FindChild({}, "application");
671 if (application_el != nullptr) {
672 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", application_el);
673 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "backupAgent", application_el);
674
675 for (xml::Element* child_el : application_el->GetChildElements()) {
676 if (child_el->namespace_uri.empty()) {
677 if (child_el->name == "activity" || child_el->name == "activity-alias" ||
678 child_el->name == "provider" || child_el->name == "receiver" ||
679 child_el->name == "service") {
680 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", child_el);
Makoto Onuki808fd192021-12-10 09:40:54 -0800681 continue;
Adam Lesinski23034b92017-11-29 16:27:44 -0800682 }
683
684 if (child_el->name == "activity-alias") {
685 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "targetActivity", child_el);
Makoto Onuki808fd192021-12-10 09:40:54 -0800686 continue;
687 }
688
689 if (child_el->name == "processes") {
690 for (xml::Element* grand_child_el : child_el->GetChildElements()) {
691 if (grand_child_el->name == "process") {
692 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", grand_child_el);
693 }
694 }
695 continue;
Adam Lesinski23034b92017-11-29 16:27:44 -0800696 }
697 }
698 }
699 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700700 return true;
Adam Lesinski52364f72016-01-11 13:10:24 -0800701}
702
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700703bool ManifestFixer::Consume(IAaptContext* context, xml::XmlResource* doc) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800704 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 xml::Element* root = xml::FindRootElement(doc->root.get());
706 if (!root || !root->namespace_uri.empty() || root->name != "manifest") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000707 context->GetDiagnostics()->Error(android::DiagMessage(doc->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700708 << "root tag must be <manifest>");
709 return false;
710 }
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800711
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800712 if ((options_.min_sdk_version_default || options_.target_sdk_version_default) &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700713 root->FindChild({}, "uses-sdk") == nullptr) {
Adam Lesinskie343eb12016-10-27 16:31:58 -0700714 // Auto insert a <uses-sdk> element. This must be inserted before the
715 // <application> tag. The device runtime PackageParser will make SDK version
716 // decisions while parsing <application>.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 std::unique_ptr<xml::Element> uses_sdk = util::make_unique<xml::Element>();
718 uses_sdk->name = "uses-sdk";
Adam Lesinskie343eb12016-10-27 16:31:58 -0700719 root->InsertChild(0, std::move(uses_sdk));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720 }
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800721
Adam Lesinskic6284372017-12-04 13:46:23 -0800722 if (options_.compile_sdk_version) {
723 xml::Attribute* attr = root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersion");
724
725 // Make sure we un-compile the value if it was set to something else.
726 attr->compiled_value = {};
Adam Lesinskic6284372017-12-04 13:46:23 -0800727 attr->value = options_.compile_sdk_version.value();
Ryan Mitchellaada89c2019-02-12 08:06:26 -0800728
729 attr = root->FindOrCreateAttribute("", "platformBuildVersionCode");
730
731 // Make sure we un-compile the value if it was set to something else.
732 attr->compiled_value = {};
733 attr->value = options_.compile_sdk_version.value();
734
Adam Lesinskic6284372017-12-04 13:46:23 -0800735 }
736
737 if (options_.compile_sdk_version_codename) {
738 xml::Attribute* attr =
739 root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
740
741 // Make sure we un-compile the value if it was set to something else.
742 attr->compiled_value = {};
Ryan Mitchellaada89c2019-02-12 08:06:26 -0800743 attr->value = options_.compile_sdk_version_codename.value();
Adam Lesinskic6284372017-12-04 13:46:23 -0800744
Ryan Mitchellaada89c2019-02-12 08:06:26 -0800745 attr = root->FindOrCreateAttribute("", "platformBuildVersionName");
746
747 // Make sure we un-compile the value if it was set to something else.
748 attr->compiled_value = {};
Adam Lesinskic6284372017-12-04 13:46:23 -0800749 attr->value = options_.compile_sdk_version_codename.value();
750 }
751
Andrei Onea7109b702023-02-23 17:43:25 +0000752 if (!options_.fingerprint_prefixes.empty()) {
753 xml::Element* install_constraints_el = root->FindChild({}, "install-constraints");
754 if (install_constraints_el == nullptr) {
755 std::unique_ptr<xml::Element> install_constraints = std::make_unique<xml::Element>();
756 install_constraints->name = "install-constraints";
757 install_constraints_el = install_constraints.get();
758 root->AppendChild(std::move(install_constraints));
759 }
760 for (const std::string& prefix : options_.fingerprint_prefixes) {
761 std::unique_ptr<xml::Element> prefix_el = std::make_unique<xml::Element>();
762 prefix_el->name = "fingerprint-prefix";
763 xml::Attribute* attr = prefix_el->FindOrCreateAttribute(xml::kSchemaAndroid, "value");
764 attr->value = prefix;
765 install_constraints_el->AppendChild(std::move(prefix_el));
766 }
767 }
768
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 xml::XmlActionExecutor executor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700770 if (!BuildRules(&executor, context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700771 return false;
772 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700773
Izabela Orlowskaad9e1322017-12-19 16:22:42 +0000774 xml::XmlActionExecutorPolicy policy = options_.warn_validation
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700775 ? xml::XmlActionExecutorPolicy::kAllowListWarning
776 : xml::XmlActionExecutorPolicy::kAllowList;
Izabela Orlowskaad9e1322017-12-19 16:22:42 +0000777 if (!executor.Execute(policy, context->GetDiagnostics(), doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 return false;
779 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700780
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700781 if (options_.rename_manifest_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700782 // Rename manifest package outside of the XmlActionExecutor.
Adam Lesinskie343eb12016-10-27 16:31:58 -0700783 // We need to extract the old package name and FullyQualify all class
784 // names.
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800785 if (!RenameManifestPackage(options_.rename_manifest_package.value(), root)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700786 return false;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700787 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700788 }
789 return true;
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800790}
791
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700792} // namespace aapt