blob: c813a446b8dbb524cd29c16d6f22c26e14d59fe2 [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
Adam Lesinskifca5e422017-12-20 15:03:36 -080033static 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
Adam Lesinskib0c47ef2017-03-06 20:05:57 -080049// This is how PackageManager builds class names from AndroidManifest.xml entries.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050static bool NameIsJavaClassName(xml::Element* el, xml::Attribute* attr,
Adam Lesinskicc5609d2016-04-05 12:41:07 -070051 SourcePathDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 // 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.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 Maybe<std::string> fully_qualified_class_name =
56 util::GetFullyQualifiedClassName("a", attr->value);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070057
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 StringPiece qualified_class_name = fully_qualified_class_name
59 ? fully_qualified_class_name.value()
60 : attr->value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070061
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 if (!util::IsJavaClassName(qualified_class_name)) {
63 diag->Error(DiagMessage(el->line_number)
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 << "attribute 'android:name' in <" << el->name
65 << "> tag must be a valid Java class name");
Adam Lesinski52364f72016-01-11 13:10:24 -080066 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 }
68 return true;
69}
70
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080071static bool OptionalNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) {
73 return NameIsJavaClassName(el, attr, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 }
75 return true;
76}
77
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080078static bool RequiredNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) {
Adam Lesinskifca5e422017-12-20 15:03:36 -080079 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;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 }
Adam Lesinskifca5e422017-12-20 15:03:36 -080085 return NameIsJavaClassName(el, attr, diag);
Adam Lesinski52364f72016-01-11 13:10:24 -080086}
87
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080088static bool RequiredNameIsJavaPackage(xml::Element* el, SourcePathDiagnostics* diag) {
Adam Lesinskifca5e422017-12-20 15:03:36 -080089 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;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080094 }
Adam Lesinskifca5e422017-12-20 15:03:36 -080095
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;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800102}
103
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800104static 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
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800115static bool AutoGenerateIsFeatureSplit(xml::Element* el, SourcePathDiagnostics* diag) {
116 constexpr const char* kFeatureSplit = "featureSplit";
117 constexpr const char* kIsFeatureSplit = "isFeatureSplit";
118
119 xml::Attribute* attr = el->FindAttribute({}, kFeatureSplit);
120 if (attr != nullptr) {
121 // Rewrite the featureSplit attribute to be "split". This is what the
122 // platform recognizes.
123 attr->name = "split";
124
125 // Now inject the android:isFeatureSplit="true" attribute.
126 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, kIsFeatureSplit);
127 if (attr != nullptr) {
128 if (!ResourceUtils::ParseBool(attr->value).value_or_default(false)) {
129 // The isFeatureSplit attribute is false, which conflicts with the use
130 // of "featureSplit".
131 diag->Error(DiagMessage(el->line_number)
132 << "attribute 'featureSplit' used in <manifest> but 'android:isFeatureSplit' "
133 "is not 'true'");
134 return false;
135 }
136
137 // The attribute is already there and set to true, nothing to do.
138 } else {
139 el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, kIsFeatureSplit, "true"});
140 }
141 }
142 return true;
143}
144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145static bool VerifyManifest(xml::Element* el, SourcePathDiagnostics* diag) {
146 xml::Attribute* attr = el->FindAttribute({}, "package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 if (!attr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 diag->Error(DiagMessage(el->line_number)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 << "<manifest> tag is missing 'package' attribute");
150 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 } else if (ResourceUtils::IsReference(attr->value)) {
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800152 diag->Error(DiagMessage(el->line_number)
153 << "attribute 'package' in <manifest> tag must not be a reference");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 return false;
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800155 } else if (!util::IsAndroidPackageName(attr->value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 diag->Error(DiagMessage(el->line_number)
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800157 << "attribute 'package' in <manifest> tag is not a valid Android package name: '"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 << attr->value << "'");
159 return false;
160 }
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800161
162 attr = el->FindAttribute({}, "split");
163 if (attr) {
164 if (!util::IsJavaPackageName(attr->value)) {
165 diag->Error(DiagMessage(el->line_number) << "attribute 'split' in <manifest> tag is not a "
166 "valid split name");
167 return false;
168 }
169 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 return true;
Adam Lesinski52364f72016-01-11 13:10:24 -0800171}
172
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800173// The coreApp attribute in <manifest> is not a regular AAPT attribute, so type
174// checking on it is manual.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175static bool FixCoreAppAttribute(xml::Element* el, SourcePathDiagnostics* diag) {
176 if (xml::Attribute* attr = el->FindAttribute("", "coreApp")) {
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800177 std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseBool(attr->value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 if (!result) {
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800179 diag->Error(DiagMessage(el->line_number) << "attribute coreApp must be a boolean");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 return false;
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700181 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 attr->compiled_value = std::move(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 }
184 return true;
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700185}
186
Adam Lesinski86d67df2017-01-31 13:47:27 -0800187// Checks that <uses-feature> has android:glEsVersion or android:name, not both (or neither).
188static bool VerifyUsesFeature(xml::Element* el, SourcePathDiagnostics* diag) {
189 bool has_name = false;
190 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) {
191 if (attr->value.empty()) {
192 diag->Error(DiagMessage(el->line_number)
193 << "android:name in <uses-feature> must not be empty");
194 return false;
195 }
196 has_name = true;
197 }
198
199 bool has_gl_es_version = false;
200 if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "glEsVersion")) {
201 if (has_name) {
202 diag->Error(DiagMessage(el->line_number)
203 << "cannot define both android:name and android:glEsVersion in <uses-feature>");
204 return false;
205 }
206 has_gl_es_version = true;
207 }
208
209 if (!has_name && !has_gl_es_version) {
210 diag->Error(DiagMessage(el->line_number)
211 << "<uses-feature> must have either android:name or android:glEsVersion attribute");
212 return false;
213 }
214 return true;
215}
216
Donald Chai6e497352019-05-19 21:07:50 -0700217// Ensure that 'ns_decls' contains a declaration for 'uri', using 'prefix' as
218// the xmlns prefix if possible.
219static void EnsureNamespaceIsDeclared(const std::string& prefix, const std::string& uri,
220 std::vector<xml::NamespaceDecl>* ns_decls) {
221 if (std::find_if(ns_decls->begin(), ns_decls->end(), [&](const xml::NamespaceDecl& ns_decl) {
222 return ns_decl.uri == uri;
223 }) != ns_decls->end()) {
224 return;
225 }
226
227 std::set<std::string> used_prefixes;
228 for (const auto& ns_decl : *ns_decls) {
229 used_prefixes.insert(ns_decl.prefix);
230 }
231
232 // Make multiple attempts in the unlikely event that 'prefix' is already taken.
233 std::string disambiguator;
234 for (int i = 0; i < used_prefixes.size() + 1; i++) {
235 std::string attempted_prefix = prefix + disambiguator;
236 if (used_prefixes.find(attempted_prefix) == used_prefixes.end()) {
237 ns_decls->push_back(xml::NamespaceDecl{attempted_prefix, uri});
238 return;
239 }
240 disambiguator = std::to_string(i);
241 }
242}
243
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244bool ManifestFixer::BuildRules(xml::XmlActionExecutor* executor,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 IDiagnostics* diag) {
246 // First verify some options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 if (options_.rename_manifest_package) {
248 if (!util::IsJavaPackageName(options_.rename_manifest_package.value())) {
249 diag->Error(DiagMessage() << "invalid manifest package override '"
250 << options_.rename_manifest_package.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 << "'");
252 return false;
253 }
254 }
255
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 if (options_.rename_instrumentation_target_package) {
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800257 if (!util::IsJavaPackageName(options_.rename_instrumentation_target_package.value())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 diag->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 << "invalid instrumentation target package override '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 << options_.rename_instrumentation_target_package.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 << "'");
262 return false;
263 }
264 }
265
Roshan Piusae12ce42020-04-25 16:08:55 -0700266 if (options_.rename_overlay_target_package) {
267 if (!util::IsJavaPackageName(options_.rename_overlay_target_package.value())) {
268 diag->Error(DiagMessage()
269 << "invalid overlay target package override '"
270 << options_.rename_overlay_target_package.value()
271 << "'");
272 return false;
273 }
274 }
275
Adam Lesinski86d67df2017-01-31 13:47:27 -0800276 // Common <intent-filter> actions.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 xml::XmlNodeAction intent_filter_action;
Adam Lesinskifca5e422017-12-20 15:03:36 -0800278 intent_filter_action["action"].Action(RequiredNameIsNotEmpty);
279 intent_filter_action["category"].Action(RequiredNameIsNotEmpty);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 intent_filter_action["data"];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281
Adam Lesinski86d67df2017-01-31 13:47:27 -0800282 // Common <meta-data> actions.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 xml::XmlNodeAction meta_data_action;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284
Adam Lesinski86d67df2017-01-31 13:47:27 -0800285 // Common <uses-feature> actions.
286 xml::XmlNodeAction uses_feature_action;
287 uses_feature_action.Action(VerifyUsesFeature);
288
289 // Common component actions.
290 xml::XmlNodeAction component_action;
291 component_action.Action(RequiredNameIsJavaClassName);
292 component_action["intent-filter"] = intent_filter_action;
Ryan Mitchell28afe682018-09-07 14:33:14 -0700293 component_action["preferred"] = intent_filter_action;
Adam Lesinski86d67df2017-01-31 13:47:27 -0800294 component_action["meta-data"] = meta_data_action;
295
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 // Manifest actions.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 xml::XmlNodeAction& manifest_action = (*executor)["manifest"];
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800298 manifest_action.Action(AutoGenerateIsFeatureSplit);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 manifest_action.Action(VerifyManifest);
300 manifest_action.Action(FixCoreAppAttribute);
301 manifest_action.Action([&](xml::Element* el) -> bool {
Donald Chai6e497352019-05-19 21:07:50 -0700302 EnsureNamespaceIsDeclared("android", xml::kSchemaAndroid, &el->namespace_decls);
303
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 if (options_.version_name_default) {
Colin Crossdcd58c42018-05-25 22:46:35 -0700305 if (options_.replace_version) {
306 el->RemoveAttribute(xml::kSchemaAndroid, "versionName");
307 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 if (el->FindAttribute(xml::kSchemaAndroid, "versionName") == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700309 el->attributes.push_back(
310 xml::Attribute{xml::kSchemaAndroid, "versionName",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 options_.version_name_default.value()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700313 }
314
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 if (options_.version_code_default) {
Colin Crossdcd58c42018-05-25 22:46:35 -0700316 if (options_.replace_version) {
317 el->RemoveAttribute(xml::kSchemaAndroid, "versionCode");
318 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 if (el->FindAttribute(xml::kSchemaAndroid, "versionCode") == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 el->attributes.push_back(
321 xml::Attribute{xml::kSchemaAndroid, "versionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 options_.version_code_default.value()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700324 }
Ryan Mitchell7cb82a82018-05-10 15:35:31 -0700325
Ryan Mitchell704090e2018-07-31 14:59:25 -0700326 if (options_.version_code_major_default) {
327 if (options_.replace_version) {
328 el->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
329 }
330 if (el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor") == nullptr) {
331 el->attributes.push_back(
332 xml::Attribute{xml::kSchemaAndroid, "versionCodeMajor",
333 options_.version_code_major_default.value()});
334 }
335 }
336
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800337 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 });
339
340 // Meta tags.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341 manifest_action["eat-comment"];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342
343 // Uses-sdk actions.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 manifest_action["uses-sdk"].Action([&](xml::Element* el) -> bool {
345 if (options_.min_sdk_version_default &&
346 el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion") == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 // There was no minSdkVersion defined and we have a default to assign.
348 el->attributes.push_back(
349 xml::Attribute{xml::kSchemaAndroid, "minSdkVersion",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 options_.min_sdk_version_default.value()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 }
352
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 if (options_.target_sdk_version_default &&
354 el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion") == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 // There was no targetSdkVersion defined and we have a default to assign.
356 el->attributes.push_back(
357 xml::Attribute{xml::kSchemaAndroid, "targetSdkVersion",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 options_.target_sdk_version_default.value()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 }
360 return true;
361 });
Anton Hanssonb2f709d2020-01-09 10:25:23 +0000362 manifest_action["uses-sdk"]["extension-sdk"];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363
364 // Instrumentation actions.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800365 manifest_action["instrumentation"].Action(RequiredNameIsJavaClassName);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366 manifest_action["instrumentation"].Action([&](xml::Element* el) -> bool {
367 if (!options_.rename_instrumentation_target_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 return true;
369 }
370
371 if (xml::Attribute* attr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) {
373 attr->value = options_.rename_instrumentation_target_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 }
375 return true;
376 });
Adam Lesinski86d67df2017-01-31 13:47:27 -0800377 manifest_action["instrumentation"]["meta-data"] = meta_data_action;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800379 // TODO moltmann: Remove
Philip P. Moltmann9046d822019-12-13 15:59:49 -0800380 manifest_action["feature"];
381 manifest_action["feature"]["inherit-from"];
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800382
383 manifest_action["attribution"];
384 manifest_action["attribution"]["inherit-from"];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 manifest_action["original-package"];
Roshan Piusae12ce42020-04-25 16:08:55 -0700386 manifest_action["overlay"].Action([&](xml::Element* el) -> bool {
387 if (!options_.rename_overlay_target_package) {
388 return true;
389 }
390
391 if (xml::Attribute* attr =
392 el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) {
393 attr->value = options_.rename_overlay_target_package.value();
394 }
395 return true;
396 });
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 manifest_action["protected-broadcast"];
Alan Viverettecf5326f2018-01-05 16:03:50 -0500398 manifest_action["adopt-permissions"];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 manifest_action["uses-permission"];
Adam Lesinski4b585db2017-05-12 15:25:50 -0700400 manifest_action["uses-permission-sdk-23"];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 manifest_action["permission"];
Ryan Mitchell66f6cfb2018-07-25 16:15:17 -0700402 manifest_action["permission"]["meta-data"] = meta_data_action;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 manifest_action["permission-tree"];
404 manifest_action["permission-group"];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 manifest_action["uses-configuration"];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 manifest_action["supports-screens"];
Adam Lesinski86d67df2017-01-31 13:47:27 -0800407 manifest_action["uses-feature"] = uses_feature_action;
408 manifest_action["feature-group"]["uses-feature"] = uses_feature_action;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 manifest_action["compatible-screens"];
410 manifest_action["compatible-screens"]["screen"];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 manifest_action["supports-gl-texture"];
Ryan Mitchell66f6cfb2018-07-25 16:15:17 -0700412 manifest_action["restrict-update"];
413 manifest_action["package-verifier"];
Adam Lesinski5119e512016-12-05 19:48:20 -0800414 manifest_action["meta-data"] = meta_data_action;
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800415 manifest_action["uses-split"].Action(RequiredNameIsJavaPackage);
Patrick Baumanna4ffb452019-06-24 15:01:49 -0700416 manifest_action["queries"]["package"].Action(RequiredNameIsJavaPackage);
417 manifest_action["queries"]["intent"] = intent_filter_action;
Patrick Baumann99181232020-01-28 10:55:25 -0800418 manifest_action["queries"]["provider"].Action(RequiredAndroidAttribute("authorities"));
Patrick Baumanna4ffb452019-06-24 15:01:49 -0700419 // TODO: more complicated component name tag
Adam Lesinski5119e512016-12-05 19:48:20 -0800420
Adam Lesinski87f1e0f2017-06-27 16:21:58 -0700421 manifest_action["key-sets"]["key-set"]["public-key"];
422 manifest_action["key-sets"]["upgrade-key-set"];
423
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 // Application actions.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 xml::XmlNodeAction& application_action = manifest_action["application"];
426 application_action.Action(OptionalNameIsJavaClassName);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427
Adam Lesinskifca5e422017-12-20 15:03:36 -0800428 application_action["uses-library"].Action(RequiredNameIsNotEmpty);
429 application_action["library"].Action(RequiredNameIsNotEmpty);
Chris Craik335b5652019-04-04 12:46:47 -0700430 application_action["profileable"];
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800431
432 xml::XmlNodeAction& static_library_action = application_action["static-library"];
433 static_library_action.Action(RequiredNameIsJavaPackage);
434 static_library_action.Action(RequiredAndroidAttribute("version"));
435
436 xml::XmlNodeAction& uses_static_library_action = application_action["uses-static-library"];
437 uses_static_library_action.Action(RequiredNameIsJavaPackage);
438 uses_static_library_action.Action(RequiredAndroidAttribute("version"));
439 uses_static_library_action.Action(RequiredAndroidAttribute("certDigest"));
Ryan Mitchell66f6cfb2018-07-25 16:15:17 -0700440 uses_static_library_action["additional-certificate"];
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800441
Dianne Hackborn813d7502018-10-02 16:59:46 -0700442 xml::XmlNodeAction& uses_package_action = application_action["uses-package"];
443 uses_package_action.Action(RequiredNameIsJavaPackage);
444 uses_package_action["additional-certificate"];
445
Ryan Mitchelle5b38a62018-03-23 13:35:00 -0700446 if (options_.debug_mode) {
447 application_action.Action([&](xml::Element* el) -> bool {
448 xml::Attribute *attr = el->FindOrCreateAttribute(xml::kSchemaAndroid, "debuggable");
449 attr->value = "true";
450 return true;
451 });
452 }
453
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 application_action["meta-data"] = meta_data_action;
Adam Lesinski7a917a22017-06-02 12:55:24 -0700455
Dianne Hackbornfc0839a2020-01-31 11:02:28 -0800456 application_action["processes"];
457 application_action["processes"]["deny-permission"];
458 application_action["processes"]["allow-permission"];
459 application_action["processes"]["process"]["deny-permission"];
460 application_action["processes"]["process"]["allow-permission"];
461
Adam Lesinski86d67df2017-01-31 13:47:27 -0800462 application_action["activity"] = component_action;
Adam Lesinski7a917a22017-06-02 12:55:24 -0700463 application_action["activity"]["layout"];
464
Adam Lesinski86d67df2017-01-31 13:47:27 -0800465 application_action["activity-alias"] = component_action;
466 application_action["service"] = component_action;
467 application_action["receiver"] = component_action;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468
469 // Provider actions.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800470 application_action["provider"] = component_action;
Adam Lesinskic10c0d02017-04-28 12:54:08 -0700471 application_action["provider"]["grant-uri-permission"];
Adam Lesinski25783ca2017-04-24 13:33:47 -0700472 application_action["provider"]["path-permission"];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473
Ryan Mitchell28afe682018-09-07 14:33:14 -0700474 manifest_action["package"] = manifest_action;
475
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 return true;
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800477}
478
Adam Lesinski23034b92017-11-29 16:27:44 -0800479static void FullyQualifyClassName(const StringPiece& package, const StringPiece& attr_ns,
480 const StringPiece& attr_name, xml::Element* el) {
481 xml::Attribute* attr = el->FindAttribute(attr_ns, attr_name);
482 if (attr != nullptr) {
483 if (Maybe<std::string> new_value = util::GetFullyQualifiedClassName(package, attr->value)) {
484 attr->value = std::move(new_value.value());
Adam Lesinski52364f72016-01-11 13:10:24 -0800485 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 }
Adam Lesinski23034b92017-11-29 16:27:44 -0800487}
Adam Lesinski52364f72016-01-11 13:10:24 -0800488
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800489static bool RenameManifestPackage(const StringPiece& package_override, xml::Element* manifest_el) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 xml::Attribute* attr = manifest_el->FindAttribute({}, "package");
Adam Lesinski52364f72016-01-11 13:10:24 -0800491
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 // We've already verified that the manifest element is present, with a package
493 // name specified.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700494 CHECK(attr != nullptr);
Adam Lesinski52364f72016-01-11 13:10:24 -0800495
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 std::string original_package = std::move(attr->value);
Adam Lesinskid5083f62017-01-16 15:07:21 -0800497 attr->value = package_override.to_string();
Adam Lesinski52364f72016-01-11 13:10:24 -0800498
Adam Lesinski23034b92017-11-29 16:27:44 -0800499 xml::Element* application_el = manifest_el->FindChild({}, "application");
500 if (application_el != nullptr) {
501 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", application_el);
502 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "backupAgent", application_el);
503
504 for (xml::Element* child_el : application_el->GetChildElements()) {
505 if (child_el->namespace_uri.empty()) {
506 if (child_el->name == "activity" || child_el->name == "activity-alias" ||
507 child_el->name == "provider" || child_el->name == "receiver" ||
508 child_el->name == "service") {
509 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", child_el);
510 }
511
512 if (child_el->name == "activity-alias") {
513 FullyQualifyClassName(original_package, xml::kSchemaAndroid, "targetActivity", child_el);
514 }
515 }
516 }
517 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 return true;
Adam Lesinski52364f72016-01-11 13:10:24 -0800519}
520
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521bool ManifestFixer::Consume(IAaptContext* context, xml::XmlResource* doc) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800522 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 xml::Element* root = xml::FindRootElement(doc->root.get());
524 if (!root || !root->namespace_uri.empty() || root->name != "manifest") {
525 context->GetDiagnostics()->Error(DiagMessage(doc->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 << "root tag must be <manifest>");
527 return false;
528 }
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800529
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800530 if ((options_.min_sdk_version_default || options_.target_sdk_version_default) &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 root->FindChild({}, "uses-sdk") == nullptr) {
Adam Lesinskie343eb12016-10-27 16:31:58 -0700532 // Auto insert a <uses-sdk> element. This must be inserted before the
533 // <application> tag. The device runtime PackageParser will make SDK version
534 // decisions while parsing <application>.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 std::unique_ptr<xml::Element> uses_sdk = util::make_unique<xml::Element>();
536 uses_sdk->name = "uses-sdk";
Adam Lesinskie343eb12016-10-27 16:31:58 -0700537 root->InsertChild(0, std::move(uses_sdk));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 }
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800539
Adam Lesinskic6284372017-12-04 13:46:23 -0800540 if (options_.compile_sdk_version) {
541 xml::Attribute* attr = root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersion");
542
543 // Make sure we un-compile the value if it was set to something else.
544 attr->compiled_value = {};
Adam Lesinskic6284372017-12-04 13:46:23 -0800545 attr->value = options_.compile_sdk_version.value();
Ryan Mitchellaada89c2019-02-12 08:06:26 -0800546
547 attr = root->FindOrCreateAttribute("", "platformBuildVersionCode");
548
549 // Make sure we un-compile the value if it was set to something else.
550 attr->compiled_value = {};
551 attr->value = options_.compile_sdk_version.value();
552
Adam Lesinskic6284372017-12-04 13:46:23 -0800553 }
554
555 if (options_.compile_sdk_version_codename) {
556 xml::Attribute* attr =
557 root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
558
559 // Make sure we un-compile the value if it was set to something else.
560 attr->compiled_value = {};
Ryan Mitchellaada89c2019-02-12 08:06:26 -0800561 attr->value = options_.compile_sdk_version_codename.value();
Adam Lesinskic6284372017-12-04 13:46:23 -0800562
Ryan Mitchellaada89c2019-02-12 08:06:26 -0800563 attr = root->FindOrCreateAttribute("", "platformBuildVersionName");
564
565 // Make sure we un-compile the value if it was set to something else.
566 attr->compiled_value = {};
Adam Lesinskic6284372017-12-04 13:46:23 -0800567 attr->value = options_.compile_sdk_version_codename.value();
568 }
569
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 xml::XmlActionExecutor executor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571 if (!BuildRules(&executor, context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 return false;
573 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700574
Izabela Orlowskaad9e1322017-12-19 16:22:42 +0000575 xml::XmlActionExecutorPolicy policy = options_.warn_validation
576 ? xml::XmlActionExecutorPolicy::kWhitelistWarning
577 : xml::XmlActionExecutorPolicy::kWhitelist;
578 if (!executor.Execute(policy, context->GetDiagnostics(), doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 return false;
580 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700581
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582 if (options_.rename_manifest_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700583 // Rename manifest package outside of the XmlActionExecutor.
Adam Lesinskie343eb12016-10-27 16:31:58 -0700584 // We need to extract the old package name and FullyQualify all class
585 // names.
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800586 if (!RenameManifestPackage(options_.rename_manifest_package.value(), root)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 return false;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700588 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 }
590 return true;
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800591}
592
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593} // namespace aapt