Debian Codegraph @

tools/aapt2/link/ManifestFixer.cpp

raw symbols SCIP
1 /*
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
17 #include "link/ManifestFixer.h"
18
19 #include <unordered_set>
20
21 #include "android-base/logging.h"
22
23 #include "ResourceUtils.h"
24 #include "trace/TraceBuffer.h"
25 #include "util/Util.h"
26 #include "xml/XmlActionExecutor.h"
27 #include "xml/XmlDom.h"
28
29 using android::StringPiece;
30
31 namespace aapt {
32
33 static bool RequiredNameIsNotEmpty(xml::Element* el, SourcePathDiagnostics* diag) {
34 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
35 if (attr == nullptr) {
36 diag->Error(DiagMessage(el->line_number)
37 << "<" << el->name << "> is missing attribute 'android:name'");
38 return false;
39 }
40
41 if (attr->value.empty()) {
42 diag->Error(DiagMessage(el->line_number)
43 << "attribute 'android:name' in <" << el->name << "> tag must not be empty");
44 return false;
45 }
46 return true;
47 }
48
49 // This is how PackageManager builds class names from AndroidManifest.xml entries.
50 static bool NameIsJavaClassName(xml::Element* el, xml::Attribute* attr,
51 SourcePathDiagnostics* diag) {
52 // We allow unqualified class names (ie: .HelloActivity)
53 // Since we don't know the package name, we can just make a fake one here and
54 // the test will be identical as long as the real package name is valid too.
55 std::optional<std::string> fully_qualified_class_name =
56 util::GetFullyQualifiedClassName("a", attr->value);
57
58 StringPiece qualified_class_name = fully_qualified_class_name
59 ? fully_qualified_class_name.value()
60 : attr->value;
61
62 if (!util::IsJavaClassName(qualified_class_name)) {
63 diag->Error(DiagMessage(el->line_number)
64 << "attribute 'android:name' in <" << el->name
65 << "> tag must be a valid Java class name");
66 return false;
67 }
68 return true;
69 }
70
71 static bool OptionalNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) {
72 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) {
73 return NameIsJavaClassName(el, attr, diag);
74 }
75 return true;
76 }
77
78 static bool RequiredNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) {
79 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
80 if (attr == nullptr) {
81 diag->Error(DiagMessage(el->line_number)
82 << "<" << el->name << "> is missing attribute 'android:name'");
83 return false;
84 }
85 return NameIsJavaClassName(el, attr, diag);
86 }
87
88 static bool RequiredNameIsJavaPackage(xml::Element* el, SourcePathDiagnostics* diag) {
89 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name");
90 if (attr == nullptr) {
91 diag->Error(DiagMessage(el->line_number)
92 << "<" << el->name << "> is missing attribute 'android:name'");
93 return false;
94 }
95
96 if (!util::IsJavaPackageName(attr->value)) {
97 diag->Error(DiagMessage(el->line_number) << "attribute 'android:name' in <" << el->name
98 << "> tag must be a valid Java package name");
99 return false;
100 }
101 return true;
102 }
103
104 static xml::XmlNodeAction::ActionFuncWithDiag RequiredAndroidAttribute(const std::string& attr) {
105 return [=](xml::Element* el, SourcePathDiagnostics* diag) -> bool {
106 if (el->FindAttribute(xml::kSchemaAndroid, attr) == nullptr) {
107 diag->Error(DiagMessage(el->line_number)
108 << "<" << el->name << "> is missing required attribute 'android:" << attr << "'");
109 return false;
110 }
111 return true;
112 };
113 }
114
115 static xml::XmlNodeAction::ActionFuncWithDiag RequiredOneAndroidAttribute(
116 const std::string& attrName1, const std::string& attrName2) {
117 return [=](xml::Element* el, SourcePathDiagnostics* diag) -> bool {
118 xml::Attribute* attr1 = el->FindAttribute(xml::kSchemaAndroid, attrName1);
119 xml::Attribute* attr2 = el->FindAttribute(xml::kSchemaAndroid, attrName2);
120 if (attr1 == nullptr && attr2 == nullptr) {
121 diag->Error(DiagMessage(el->line_number)
122 << "<" << el->name << "> is missing required attribute 'android:" << attrName1
123 << "' or 'android:" << attrName2 << "'");
124 return false;
125 }
126 if (attr1 != nullptr && attr2 != nullptr) {
127 diag->Error(DiagMessage(el->line_number)
128 << "<" << el->name << "> can only specify one of attribute 'android:" << attrName1
129 << "' or 'android:" << attrName2 << "'");
130 return false;
131 }
132 return true;
133 };
134 }
135
136 static bool AutoGenerateIsFeatureSplit(xml::Element* el, SourcePathDiagnostics* diag) {
137 constexpr const char* kFeatureSplit = "featureSplit";
138 constexpr const char* kIsFeatureSplit = "isFeatureSplit";
139
140 xml::Attribute* attr = el->FindAttribute({}, kFeatureSplit);
141 if (attr != nullptr) {
142 // Rewrite the featureSplit attribute to be "split". This is what the
143 // platform recognizes.
144 attr->name = "split";
145
146 // Now inject the android:isFeatureSplit="true" attribute.
147 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, kIsFeatureSplit);
148 if (attr != nullptr) {
149 if (!ResourceUtils::ParseBool(attr->value).value_or(false)) {
150 // The isFeatureSplit attribute is false, which conflicts with the use
151 // of "featureSplit".
152 diag->Error(DiagMessage(el->line_number)
153 << "attribute 'featureSplit' used in <manifest> but 'android:isFeatureSplit' "
154 "is not 'true'");
155 return false;
156 }
157
158 // The attribute is already there and set to true, nothing to do.
159 } else {
160 el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, kIsFeatureSplit, "true"});
161 }
162 }
163 return true;
164 }
165
166 static bool AutoGenerateIsSplitRequired(xml::Element* el, SourcePathDiagnostics* diag) {
167 constexpr const char* kRequiredSplitTypes = "requiredSplitTypes";
168 constexpr const char* kIsSplitRequired = "isSplitRequired";
169
170 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, kRequiredSplitTypes);
171 if (attr != nullptr) {
172 // Now inject the android:isSplitRequired="true" attribute.
173 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, kIsSplitRequired);
174 if (attr != nullptr) {
175 if (!ResourceUtils::ParseBool(attr->value).value_or(false)) {
176 // The isFeatureSplit attribute is false, which conflicts with the use
177 // of "featureSplit".
178 diag->Error(DiagMessage(el->line_number)
179 << "attribute 'requiredSplitTypes' used in <manifest> but "
180 "'android:isSplitRequired' is not 'true'");
181 return false;
182 }
183 // The attribute is already there and set to true, nothing to do.
184 } else {
185 el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, kIsSplitRequired, "true"});
186 }
187 }
188 return true;
189 }
190
191 static bool VerifyManifest(xml::Element* el, xml::XmlActionExecutorPolicy policy,
192 SourcePathDiagnostics* diag) {
193 xml::Attribute* attr = el->FindAttribute({}, "package");
194 if (!attr) {
195 diag->Error(DiagMessage(el->line_number)
196 << "<manifest> tag is missing 'package' attribute");
197 return false;
198 } else if (ResourceUtils::IsReference(attr->value)) {
199 diag->Error(DiagMessage(el->line_number)
200 << "attribute 'package' in <manifest> tag must not be a reference");
201 return false;
202 } else if (!util::IsAndroidPackageName(attr->value)) {
203 DiagMessage error_msg(el->line_number);
204 error_msg << "attribute 'package' in <manifest> tag is not a valid Android package name: '"
205 << attr->value << "'";
206 if (policy == xml::XmlActionExecutorPolicy::kAllowListWarning) {
207 // Treat the error only as a warning.
208 diag->Warn(error_msg);
209 } else {
210 diag->Error(error_msg);
211 return false;
212 }
213 }
214
215 attr = el->FindAttribute({}, "split");
216 if (attr) {
217 if (!util::IsJavaPackageName(attr->value)) {
218 diag->Error(DiagMessage(el->line_number) << "attribute 'split' in <manifest> tag is not a "
219 "valid split name");
220 return false;
221 }
222 }
223 return true;
224 }
225
226 // The coreApp attribute in <manifest> is not a regular AAPT attribute, so type
227 // checking on it is manual.
228 static bool FixCoreAppAttribute(xml::Element* el, SourcePathDiagnostics* diag) {
229 if (xml::Attribute* attr = el->FindAttribute("", "coreApp")) {
230 std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseBool(attr->value);
231 if (!result) {
232 diag->Error(DiagMessage(el->line_number) << "attribute coreApp must be a boolean");
233 return false;
234 }
235 attr->compiled_value = std::move(result);
236 }
237 return true;
238 }
239
240 // Checks that <uses-feature> has android:glEsVersion or android:name, not both (or neither).
241 static bool VerifyUsesFeature(xml::Element* el, SourcePathDiagnostics* diag) {
242 bool has_name = false;
243 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) {
244 if (attr->value.empty()) {
245 diag->Error(DiagMessage(el->line_number)
246 << "android:name in <uses-feature> must not be empty");
247 return false;
248 }
249 has_name = true;
250 }
251
252 bool has_gl_es_version = false;
253 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "glEsVersion")) {
254 if (has_name) {
255 diag->Error(DiagMessage(el->line_number)
256 << "cannot define both android:name and android:glEsVersion in <uses-feature>");
257 return false;
258 }
259 has_gl_es_version = true;
260 }
261
262 if (!has_name && !has_gl_es_version) {
263 diag->Error(DiagMessage(el->line_number)
264 << "<uses-feature> must have either android:name or android:glEsVersion attribute");
265 return false;
266 }
267 return true;
268 }
269
270 // Ensure that 'ns_decls' contains a declaration for 'uri', using 'prefix' as
271 // the xmlns prefix if possible.
272 static void EnsureNamespaceIsDeclared(const std::string& prefix, const std::string& uri,
273 std::vector<xml::NamespaceDecl>* ns_decls) {
274 if (std::find_if(ns_decls->begin(), ns_decls->end(), [&](const xml::NamespaceDecl& ns_decl) {
275 return ns_decl.uri == uri;
276 }) != ns_decls->end()) {
277 return;
278 }
279
280 std::set<std::string> used_prefixes;
281 for (const auto& ns_decl : *ns_decls) {
282 used_prefixes.insert(ns_decl.prefix);
283 }
284
285 // Make multiple attempts in the unlikely event that 'prefix' is already taken.
286 std::string disambiguator;
287 for (int i = 0; i < used_prefixes.size() + 1; i++) {
288 std::string attempted_prefix = prefix + disambiguator;
289 if (used_prefixes.find(attempted_prefix) == used_prefixes.end()) {
290 ns_decls->push_back(xml::NamespaceDecl{attempted_prefix, uri});
291 return;
292 }
293 disambiguator = std::to_string(i);
294 }
295 }
296
297 bool ManifestFixer::BuildRules(xml::XmlActionExecutor* executor,
298 IDiagnostics* diag) {
299 // First verify some options.
300 if (options_.rename_manifest_package) {
301 if (!util::IsJavaPackageName(options_.rename_manifest_package.value())) {
302 diag->Error(DiagMessage() << "invalid manifest package override '"
303 << options_.rename_manifest_package.value()
304 << "'");
305 return false;
306 }
307 }
308
309 if (options_.rename_instrumentation_target_package) {
310 if (!util::IsJavaPackageName(options_.rename_instrumentation_target_package.value())) {
311 diag->Error(DiagMessage()
312 << "invalid instrumentation target package override '"
313 << options_.rename_instrumentation_target_package.value()
314 << "'");
315 return false;
316 }
317 }
318
319 if (options_.rename_overlay_target_package) {
320 if (!util::IsJavaPackageName(options_.rename_overlay_target_package.value())) {
321 diag->Error(DiagMessage()
322 << "invalid overlay target package override '"
323 << options_.rename_overlay_target_package.value()
324 << "'");
325 return false;
326 }
327 }
328
329 // Common <intent-filter> actions.
330 xml::XmlNodeAction intent_filter_action;
331 intent_filter_action["action"].Action(RequiredNameIsNotEmpty);
332 intent_filter_action["category"].Action(RequiredNameIsNotEmpty);
333 intent_filter_action["data"];
334
335 // Common <meta-data> actions.
336 xml::XmlNodeAction meta_data_action;
337
338 // Common <property> actions.
339 xml::XmlNodeAction property_action;
340 property_action.Action(RequiredOneAndroidAttribute("resource", "value"));
341
342 // Common <uses-feature> actions.
343 xml::XmlNodeAction uses_feature_action;
344 uses_feature_action.Action(VerifyUsesFeature);
345
346 // Common component actions.
347 xml::XmlNodeAction component_action;
348 component_action.Action(RequiredNameIsJavaClassName);
349 component_action["intent-filter"] = intent_filter_action;
350 component_action["preferred"] = intent_filter_action;
351 component_action["meta-data"] = meta_data_action;
352 component_action["property"] = property_action;
353
354 // Manifest actions.
355 xml::XmlNodeAction& manifest_action = (*executor)["manifest"];
356 manifest_action.Action(AutoGenerateIsFeatureSplit);
357 manifest_action.Action(AutoGenerateIsSplitRequired);
358 manifest_action.Action(VerifyManifest);
359 manifest_action.Action(FixCoreAppAttribute);
360 manifest_action.Action([&](xml::Element* el) -> bool {
361 EnsureNamespaceIsDeclared("android", xml::kSchemaAndroid, &el->namespace_decls);
362
363 if (options_.version_name_default) {
364 if (options_.replace_version) {
365 el->RemoveAttribute(xml::kSchemaAndroid, "versionName");
366 }
367 if (el->FindAttribute(xml::kSchemaAndroid, "versionName") == nullptr) {
368 el->attributes.push_back(
369 xml::Attribute{xml::kSchemaAndroid, "versionName",
370 options_.version_name_default.value()});
371 }
372 }
373
374 if (options_.version_code_default) {
375 if (options_.replace_version) {
376 el->RemoveAttribute(xml::kSchemaAndroid, "versionCode");
377 }
378 if (el->FindAttribute(xml::kSchemaAndroid, "versionCode") == nullptr) {
379 el->attributes.push_back(
380 xml::Attribute{xml::kSchemaAndroid, "versionCode",
381 options_.version_code_default.value()});
382 }
383 }
384
385 if (options_.version_code_major_default) {
386 if (options_.replace_version) {
387 el->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
388 }
389 if (el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor") == nullptr) {
390 el->attributes.push_back(
391 xml::Attribute{xml::kSchemaAndroid, "versionCodeMajor",
392 options_.version_code_major_default.value()});
393 }
394 }
395
396 if (options_.revision_code_default) {
397 if (options_.replace_version) {
398 el->RemoveAttribute(xml::kSchemaAndroid, "revisionCode");
399 }
400 if (el->FindAttribute(xml::kSchemaAndroid, "revisionCode") == nullptr) {
401 el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "revisionCode",
402 options_.revision_code_default.value()});
403 }
404 }
405
406 return true;
407 });
408
409 // Meta tags.
410 manifest_action["eat-comment"];
411
412 // Uses-sdk actions.
413 manifest_action["uses-sdk"].Action([&](xml::Element* el) -> bool {
414 if (options_.min_sdk_version_default &&
415 el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion") == nullptr) {
416 // There was no minSdkVersion defined and we have a default to assign.
417 el->attributes.push_back(
418 xml::Attribute{xml::kSchemaAndroid, "minSdkVersion",
419 options_.min_sdk_version_default.value()});
420 }
421
422 if (options_.target_sdk_version_default &&
423 el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion") == nullptr) {
424 // There was no targetSdkVersion defined and we have a default to assign.
425 el->attributes.push_back(
426 xml::Attribute{xml::kSchemaAndroid, "targetSdkVersion",
427 options_.target_sdk_version_default.value()});
428 }
429 return true;
430 });
431 manifest_action["uses-sdk"]["extension-sdk"];
432
433 // Instrumentation actions.
434 manifest_action["instrumentation"].Action(RequiredNameIsJavaClassName);
435 manifest_action["instrumentation"].Action([&](xml::Element* el) -> bool {
436 if (!options_.rename_instrumentation_target_package) {
437 return true;
438 }
439
440 if (xml::Attribute* attr =
441 el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) {
442 attr->value = options_.rename_instrumentation_target_package.value();
443 }
444 return true;
445 });
446 manifest_action["instrumentation"]["meta-data"] = meta_data_action;
447
448 manifest_action["attribution"];
449 manifest_action["attribution"]["inherit-from"];
450 manifest_action["original-package"];
451 manifest_action["overlay"].Action([&](xml::Element* el) -> bool {
452 if (options_.rename_overlay_target_package) {
453 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) {
454 attr->value = options_.rename_overlay_target_package.value();
455 }
456 }
457 if (options_.rename_overlay_category) {
458 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "category")) {
459 attr->value = options_.rename_overlay_category.value();
460 } else {
461 el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "category",
462 options_.rename_overlay_category.value()});
463 }
464 }
465 return true;
466 });
467 manifest_action["protected-broadcast"];
468 manifest_action["adopt-permissions"];
469 manifest_action["uses-permission"];
470 manifest_action["uses-permission"]["required-feature"].Action(RequiredNameIsNotEmpty);
471 manifest_action["uses-permission"]["required-not-feature"].Action(RequiredNameIsNotEmpty);
472 manifest_action["uses-permission-sdk-23"];
473 manifest_action["permission"];
474 manifest_action["permission"]["meta-data"] = meta_data_action;
475 manifest_action["permission-tree"];
476 manifest_action["permission-group"];
477 manifest_action["uses-configuration"];
478 manifest_action["supports-screens"];
479 manifest_action["uses-feature"] = uses_feature_action;
480 manifest_action["feature-group"]["uses-feature"] = uses_feature_action;
481 manifest_action["compatible-screens"];
482 manifest_action["compatible-screens"]["screen"];
483 manifest_action["supports-gl-texture"];
484 manifest_action["restrict-update"];
485 manifest_action["install-constraints"]["fingerprint-prefix"];
486 manifest_action["package-verifier"];
487 manifest_action["meta-data"] = meta_data_action;
488 manifest_action["uses-split"].Action(RequiredNameIsJavaPackage);
489 manifest_action["queries"]["package"].Action(RequiredNameIsJavaPackage);
490 manifest_action["queries"]["intent"] = intent_filter_action;
491 manifest_action["queries"]["provider"].Action(RequiredAndroidAttribute("authorities"));
492 // TODO: more complicated component name tag
493
494 manifest_action["key-sets"]["key-set"]["public-key"];
495 manifest_action["key-sets"]["upgrade-key-set"];
496
497 // Application actions.
498 xml::XmlNodeAction& application_action = manifest_action["application"];
499 application_action.Action(OptionalNameIsJavaClassName);
500
501 application_action["uses-library"].Action(RequiredNameIsNotEmpty);
502 application_action["uses-native-library"].Action(RequiredNameIsNotEmpty);
503 application_action["library"].Action(RequiredNameIsNotEmpty);
504 application_action["profileable"];
505 application_action["property"] = property_action;
506
507 xml::XmlNodeAction& static_library_action = application_action["static-library"];
508 static_library_action.Action(RequiredNameIsJavaPackage);
509 static_library_action.Action(RequiredAndroidAttribute("version"));
510
511 xml::XmlNodeAction& uses_static_library_action = application_action["uses-static-library"];
512 uses_static_library_action.Action(RequiredNameIsJavaPackage);
513 uses_static_library_action.Action(RequiredAndroidAttribute("version"));
514 uses_static_library_action.Action(RequiredAndroidAttribute("certDigest"));
515 uses_static_library_action["additional-certificate"];
516
517 xml::XmlNodeAction& sdk_library_action = application_action["sdk-library"];
518 sdk_library_action.Action(RequiredNameIsJavaPackage);
519 sdk_library_action.Action(RequiredAndroidAttribute("versionMajor"));
520
521 xml::XmlNodeAction& uses_sdk_library_action = application_action["uses-sdk-library"];
522 uses_sdk_library_action.Action(RequiredNameIsJavaPackage);
523 uses_sdk_library_action.Action(RequiredAndroidAttribute("versionMajor"));
524 uses_sdk_library_action.Action(RequiredAndroidAttribute("certDigest"));
525 uses_sdk_library_action["additional-certificate"];
526
527 xml::XmlNodeAction& uses_package_action = application_action["uses-package"];
528 uses_package_action.Action(RequiredNameIsJavaPackage);
529 uses_package_action["additional-certificate"];
530
531 if (options_.debug_mode) {
532 application_action.Action([&](xml::Element* el) -> bool {
533 xml::Attribute *attr = el->FindOrCreateAttribute(xml::kSchemaAndroid, "debuggable");
534 attr->value = "true";
535 return true;
536 });
537 }
538
539 application_action["meta-data"] = meta_data_action;
540
541 application_action["processes"];
542 application_action["processes"]["deny-permission"];
543 application_action["processes"]["allow-permission"];
544 application_action["processes"]["process"]["deny-permission"];
545 application_action["processes"]["process"]["allow-permission"];
546
547 application_action["activity"] = component_action;
548 application_action["activity"]["layout"];
549
550 application_action["activity-alias"] = component_action;
551 application_action["service"] = component_action;
552 application_action["receiver"] = component_action;
553 application_action["apex-system-service"] = component_action;
554
555 // Provider actions.
556 application_action["provider"] = component_action;
557 application_action["provider"]["grant-uri-permission"];
558 application_action["provider"]["path-permission"];
559
560 manifest_action["package"] = manifest_action;
561
562 return true;
563 }
564
565 static void FullyQualifyClassName(const StringPiece& package, const StringPiece& attr_ns,
566 const StringPiece& attr_name, xml::Element* el) {
567 xml::Attribute* attr = el->FindAttribute(attr_ns, attr_name);
568 if (attr != nullptr) {
569 if (std::optional<std::string> new_value =
570 util::GetFullyQualifiedClassName(package, attr->value)) {
571 attr->value = std::move(new_value.value());
572 }
573 }
574 }
575
576 static bool RenameManifestPackage(const StringPiece& package_override, xml::Element* manifest_el) {
577 xml::Attribute* attr = manifest_el->FindAttribute({}, "package");
578
579 // We've already verified that the manifest element is present, with a package
580 // name specified.
581 CHECK(attr != nullptr);
582
583 std::string original_package = std::move(attr->value);
584 attr->value = package_override.to_string();
585
586 xml::Element* application_el = manifest_el->FindChild({}, "application");
587 if (application_el != nullptr) {
588 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", application_el);
589 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "backupAgent", application_el);
590
591 for (xml::Element* child_el : application_el->GetChildElements()) {
592 if (child_el->namespace_uri.empty()) {
593 if (child_el->name == "activity" || child_el->name == "activity-alias" ||
594 child_el->name == "provider" || child_el->name == "receiver" ||
595 child_el->name == "service") {
596 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", child_el);
597 continue;
598 }
599
600 if (child_el->name == "activity-alias") {
601 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "targetActivity", child_el);
602 continue;
603 }
604
605 if (child_el->name == "processes") {
606 for (xml::Element* grand_child_el : child_el->GetChildElements()) {
607 if (grand_child_el->name == "process") {
608 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", grand_child_el);
609 }
610 }
611 continue;
612 }
613 }
614 }
615 }
616 return true;
617 }
618
619 bool ManifestFixer::Consume(IAaptContext* context, xml::XmlResource* doc) {
620 TRACE_CALL();
621 xml::Element* root = xml::FindRootElement(doc->root.get());
622 if (!root || !root->namespace_uri.empty() || root->name != "manifest") {
623 context->GetDiagnostics()->Error(DiagMessage(doc->file.source)
624 << "root tag must be <manifest>");
625 return false;
626 }
627
628 if ((options_.min_sdk_version_default || options_.target_sdk_version_default) &&
629 root->FindChild({}, "uses-sdk") == nullptr) {
630 // Auto insert a <uses-sdk> element. This must be inserted before the
631 // <application> tag. The device runtime PackageParser will make SDK version
632 // decisions while parsing <application>.
633 std::unique_ptr<xml::Element> uses_sdk = util::make_unique<xml::Element>();
634 uses_sdk->name = "uses-sdk";
635 root->InsertChild(0, std::move(uses_sdk));
636 }
637
638 if (options_.compile_sdk_version) {
639 xml::Attribute* attr = root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersion");
640
641 // Make sure we un-compile the value if it was set to something else.
642 attr->compiled_value = {};
643 attr->value = options_.compile_sdk_version.value();
644
645 attr = root->FindOrCreateAttribute("", "platformBuildVersionCode");
646
647 // Make sure we un-compile the value if it was set to something else.
648 attr->compiled_value = {};
649 attr->value = options_.compile_sdk_version.value();
650
651 }
652
653 if (options_.compile_sdk_version_codename) {
654 xml::Attribute* attr =
655 root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
656
657 // Make sure we un-compile the value if it was set to something else.
658 attr->compiled_value = {};
659 attr->value = options_.compile_sdk_version_codename.value();
660
661 attr = root->FindOrCreateAttribute("", "platformBuildVersionName");
662
663 // Make sure we un-compile the value if it was set to something else.
664 attr->compiled_value = {};
665 attr->value = options_.compile_sdk_version_codename.value();
666 }
667
668 xml::XmlActionExecutor executor;
669 if (!BuildRules(&executor, context->GetDiagnostics())) {
670 return false;
671 }
672
673 xml::XmlActionExecutorPolicy policy = options_.warn_validation
674 ? xml::XmlActionExecutorPolicy::kAllowListWarning
675 : xml::XmlActionExecutorPolicy::kAllowList;
676 if (!executor.Execute(policy, context->GetDiagnostics(), doc)) {
677 return false;
678 }
679
680 if (options_.rename_manifest_package) {
681 // Rename manifest package outside of the XmlActionExecutor.
682 // We need to extract the old package name and FullyQualify all class
683 // names.
684 if (!RenameManifestPackage(options_.rename_manifest_package.value(), root)) {
685 return false;
686 }
687 }
688 return true;
689 }
690
691 } // namespace aapt