Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 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 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 17 | #include "link/ManifestFixer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <unordered_set> |
| 20 | |
| 21 | #include "android-base/logging.h" |
| 22 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 23 | #include "ResourceUtils.h" |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 24 | #include "trace/TraceBuffer.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 25 | #include "util/Util.h" |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 26 | #include "xml/XmlActionExecutor.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 27 | #include "xml/XmlDom.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 28 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 29 | using android::StringPiece; |
| 30 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 31 | namespace aapt { |
| 32 | |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 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 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 49 | // This is how PackageManager builds class names from AndroidManifest.xml entries. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 50 | static bool NameIsJavaClassName(xml::Element* el, xml::Attribute* attr, |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 51 | SourcePathDiagnostics* diag) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 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. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 55 | std::optional<std::string> fully_qualified_class_name = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | util::GetFullyQualifiedClassName("a", attr->value); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 57 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | StringPiece qualified_class_name = fully_qualified_class_name |
| 59 | ? fully_qualified_class_name.value() |
| 60 | : attr->value; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | if (!util::IsJavaClassName(qualified_class_name)) { |
| 63 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | << "attribute 'android:name' in <" << el->name |
| 65 | << "> tag must be a valid Java class name"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 66 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 71 | static bool OptionalNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) { |
| 73 | return NameIsJavaClassName(el, attr, diag); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 78 | static bool RequiredNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) { |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 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; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | } |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 85 | return NameIsJavaClassName(el, attr, diag); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 88 | static bool RequiredNameIsJavaPackage(xml::Element* el, SourcePathDiagnostics* diag) { |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 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; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 94 | } |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 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; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 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 | |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 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) { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 149 | if (!ResourceUtils::ParseBool(attr->value).value_or(false)) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 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 | |
Jackal Guo | 0c01abb | 2021-07-30 22:17:43 +0800 | [diff] [blame] | 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 | |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 191 | static bool VerifyManifest(xml::Element* el, xml::XmlActionExecutorPolicy policy, |
| 192 | SourcePathDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | xml::Attribute* attr = el->FindAttribute({}, "package"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 194 | if (!attr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 195 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | << "<manifest> tag is missing 'package' attribute"); |
| 197 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 198 | } else if (ResourceUtils::IsReference(attr->value)) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 199 | diag->Error(DiagMessage(el->line_number) |
| 200 | << "attribute 'package' in <manifest> tag must not be a reference"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 201 | return false; |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 202 | } else if (!util::IsAndroidPackageName(attr->value)) { |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 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 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | } |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 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 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 223 | return true; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 226 | // The coreApp attribute in <manifest> is not a regular AAPT attribute, so type |
| 227 | // checking on it is manual. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 228 | static bool FixCoreAppAttribute(xml::Element* el, SourcePathDiagnostics* diag) { |
| 229 | if (xml::Attribute* attr = el->FindAttribute("", "coreApp")) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 230 | std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseBool(attr->value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 231 | if (!result) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 232 | diag->Error(DiagMessage(el->line_number) << "attribute coreApp must be a boolean"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 233 | return false; |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 234 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 235 | attr->compiled_value = std::move(result); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 236 | } |
| 237 | return true; |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 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 | |
Donald Chai | 6e49735 | 2019-05-19 21:07:50 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 297 | bool ManifestFixer::BuildRules(xml::XmlActionExecutor* executor, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 298 | IDiagnostics* diag) { |
| 299 | // First verify some options. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 304 | << "'"); |
| 305 | return false; |
| 306 | } |
| 307 | } |
| 308 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 309 | if (options_.rename_instrumentation_target_package) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 310 | if (!util::IsJavaPackageName(options_.rename_instrumentation_target_package.value())) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 311 | diag->Error(DiagMessage() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 312 | << "invalid instrumentation target package override '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 313 | << options_.rename_instrumentation_target_package.value() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 314 | << "'"); |
| 315 | return false; |
| 316 | } |
| 317 | } |
| 318 | |
Roshan Pius | ae12ce4 | 2020-04-25 16:08:55 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 329 | // Common <intent-filter> actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 330 | xml::XmlNodeAction intent_filter_action; |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 331 | intent_filter_action["action"].Action(RequiredNameIsNotEmpty); |
| 332 | intent_filter_action["category"].Action(RequiredNameIsNotEmpty); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | intent_filter_action["data"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 334 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 335 | // Common <meta-data> actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 336 | xml::XmlNodeAction meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 337 | |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 338 | // Common <property> actions. |
| 339 | xml::XmlNodeAction property_action; |
| 340 | property_action.Action(RequiredOneAndroidAttribute("resource", "value")); |
| 341 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 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; |
Ryan Mitchell | 28afe68 | 2018-09-07 14:33:14 -0700 | [diff] [blame] | 350 | component_action["preferred"] = intent_filter_action; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 351 | component_action["meta-data"] = meta_data_action; |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 352 | component_action["property"] = property_action; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 353 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 354 | // Manifest actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 355 | xml::XmlNodeAction& manifest_action = (*executor)["manifest"]; |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 356 | manifest_action.Action(AutoGenerateIsFeatureSplit); |
Jackal Guo | 0c01abb | 2021-07-30 22:17:43 +0800 | [diff] [blame] | 357 | manifest_action.Action(AutoGenerateIsSplitRequired); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | manifest_action.Action(VerifyManifest); |
| 359 | manifest_action.Action(FixCoreAppAttribute); |
| 360 | manifest_action.Action([&](xml::Element* el) -> bool { |
Donald Chai | 6e49735 | 2019-05-19 21:07:50 -0700 | [diff] [blame] | 361 | EnsureNamespaceIsDeclared("android", xml::kSchemaAndroid, &el->namespace_decls); |
| 362 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 363 | if (options_.version_name_default) { |
Colin Cross | dcd58c4 | 2018-05-25 22:46:35 -0700 | [diff] [blame] | 364 | if (options_.replace_version) { |
| 365 | el->RemoveAttribute(xml::kSchemaAndroid, "versionName"); |
| 366 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 367 | if (el->FindAttribute(xml::kSchemaAndroid, "versionName") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 368 | el->attributes.push_back( |
| 369 | xml::Attribute{xml::kSchemaAndroid, "versionName", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 370 | options_.version_name_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 371 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 374 | if (options_.version_code_default) { |
Colin Cross | dcd58c4 | 2018-05-25 22:46:35 -0700 | [diff] [blame] | 375 | if (options_.replace_version) { |
| 376 | el->RemoveAttribute(xml::kSchemaAndroid, "versionCode"); |
| 377 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 378 | if (el->FindAttribute(xml::kSchemaAndroid, "versionCode") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 379 | el->attributes.push_back( |
| 380 | xml::Attribute{xml::kSchemaAndroid, "versionCode", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 381 | options_.version_code_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 382 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 383 | } |
Ryan Mitchell | 7cb82a8 | 2018-05-10 15:35:31 -0700 | [diff] [blame] | 384 | |
Ryan Mitchell | 704090e | 2018-07-31 14:59:25 -0700 | [diff] [blame] | 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 | |
Rhed Jao | b9ccb8a4 | 2020-11-30 21:42:16 +0800 | [diff] [blame] | 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 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 406 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 407 | }); |
| 408 | |
| 409 | // Meta tags. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 410 | manifest_action["eat-comment"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 411 | |
| 412 | // Uses-sdk actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 413 | manifest_action["uses-sdk"].Action([&](xml::Element* el) -> bool { |
| 414 | if (options_.min_sdk_version_default && |
| 415 | el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 416 | // There was no minSdkVersion defined and we have a default to assign. |
| 417 | el->attributes.push_back( |
| 418 | xml::Attribute{xml::kSchemaAndroid, "minSdkVersion", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 419 | options_.min_sdk_version_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 422 | if (options_.target_sdk_version_default && |
| 423 | el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 424 | // There was no targetSdkVersion defined and we have a default to assign. |
| 425 | el->attributes.push_back( |
| 426 | xml::Attribute{xml::kSchemaAndroid, "targetSdkVersion", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 427 | options_.target_sdk_version_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 428 | } |
| 429 | return true; |
| 430 | }); |
Anton Hansson | b2f709d | 2020-01-09 10:25:23 +0000 | [diff] [blame] | 431 | manifest_action["uses-sdk"]["extension-sdk"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 432 | |
| 433 | // Instrumentation actions. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 434 | manifest_action["instrumentation"].Action(RequiredNameIsJavaClassName); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 435 | manifest_action["instrumentation"].Action([&](xml::Element* el) -> bool { |
| 436 | if (!options_.rename_instrumentation_target_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 437 | return true; |
| 438 | } |
| 439 | |
| 440 | if (xml::Attribute* attr = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 441 | el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) { |
| 442 | attr->value = options_.rename_instrumentation_target_package.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 443 | } |
| 444 | return true; |
| 445 | }); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 446 | manifest_action["instrumentation"]["meta-data"] = meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 447 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 448 | manifest_action["attribution"]; |
| 449 | manifest_action["attribution"]["inherit-from"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 450 | manifest_action["original-package"]; |
Roshan Pius | ae12ce4 | 2020-04-25 16:08:55 -0700 | [diff] [blame] | 451 | manifest_action["overlay"].Action([&](xml::Element* el) -> bool { |
| 452 | if (!options_.rename_overlay_target_package) { |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | if (xml::Attribute* attr = |
| 457 | el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) { |
| 458 | attr->value = options_.rename_overlay_target_package.value(); |
| 459 | } |
| 460 | return true; |
| 461 | }); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 462 | manifest_action["protected-broadcast"]; |
Alan Viverette | cf5326f | 2018-01-05 16:03:50 -0500 | [diff] [blame] | 463 | manifest_action["adopt-permissions"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 464 | manifest_action["uses-permission"]; |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 465 | manifest_action["uses-permission"]["required-feature"].Action(RequiredNameIsNotEmpty); |
| 466 | manifest_action["uses-permission"]["required-not-feature"].Action(RequiredNameIsNotEmpty); |
Adam Lesinski | 4b585db | 2017-05-12 15:25:50 -0700 | [diff] [blame] | 467 | manifest_action["uses-permission-sdk-23"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 468 | manifest_action["permission"]; |
Ryan Mitchell | 66f6cfb | 2018-07-25 16:15:17 -0700 | [diff] [blame] | 469 | manifest_action["permission"]["meta-data"] = meta_data_action; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 470 | manifest_action["permission-tree"]; |
| 471 | manifest_action["permission-group"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 472 | manifest_action["uses-configuration"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 473 | manifest_action["supports-screens"]; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 474 | manifest_action["uses-feature"] = uses_feature_action; |
| 475 | manifest_action["feature-group"]["uses-feature"] = uses_feature_action; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 476 | manifest_action["compatible-screens"]; |
| 477 | manifest_action["compatible-screens"]["screen"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 478 | manifest_action["supports-gl-texture"]; |
Ryan Mitchell | 66f6cfb | 2018-07-25 16:15:17 -0700 | [diff] [blame] | 479 | manifest_action["restrict-update"]; |
| 480 | manifest_action["package-verifier"]; |
Adam Lesinski | 5119e51 | 2016-12-05 19:48:20 -0800 | [diff] [blame] | 481 | manifest_action["meta-data"] = meta_data_action; |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 482 | manifest_action["uses-split"].Action(RequiredNameIsJavaPackage); |
Patrick Baumann | a4ffb45 | 2019-06-24 15:01:49 -0700 | [diff] [blame] | 483 | manifest_action["queries"]["package"].Action(RequiredNameIsJavaPackage); |
| 484 | manifest_action["queries"]["intent"] = intent_filter_action; |
Patrick Baumann | 9918123 | 2020-01-28 10:55:25 -0800 | [diff] [blame] | 485 | manifest_action["queries"]["provider"].Action(RequiredAndroidAttribute("authorities")); |
Patrick Baumann | a4ffb45 | 2019-06-24 15:01:49 -0700 | [diff] [blame] | 486 | // TODO: more complicated component name tag |
Adam Lesinski | 5119e51 | 2016-12-05 19:48:20 -0800 | [diff] [blame] | 487 | |
Adam Lesinski | 87f1e0f | 2017-06-27 16:21:58 -0700 | [diff] [blame] | 488 | manifest_action["key-sets"]["key-set"]["public-key"]; |
| 489 | manifest_action["key-sets"]["upgrade-key-set"]; |
| 490 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 491 | // Application actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 492 | xml::XmlNodeAction& application_action = manifest_action["application"]; |
| 493 | application_action.Action(OptionalNameIsJavaClassName); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 494 | |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 495 | application_action["uses-library"].Action(RequiredNameIsNotEmpty); |
Jiyong Park | 6a5b8b1 | 2020-06-30 13:23:36 +0900 | [diff] [blame] | 496 | application_action["uses-native-library"].Action(RequiredNameIsNotEmpty); |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 497 | application_action["library"].Action(RequiredNameIsNotEmpty); |
Chris Craik | 335b565 | 2019-04-04 12:46:47 -0700 | [diff] [blame] | 498 | application_action["profileable"]; |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 499 | application_action["property"] = property_action; |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 500 | |
| 501 | xml::XmlNodeAction& static_library_action = application_action["static-library"]; |
| 502 | static_library_action.Action(RequiredNameIsJavaPackage); |
| 503 | static_library_action.Action(RequiredAndroidAttribute("version")); |
| 504 | |
| 505 | xml::XmlNodeAction& uses_static_library_action = application_action["uses-static-library"]; |
| 506 | uses_static_library_action.Action(RequiredNameIsJavaPackage); |
| 507 | uses_static_library_action.Action(RequiredAndroidAttribute("version")); |
| 508 | uses_static_library_action.Action(RequiredAndroidAttribute("certDigest")); |
Ryan Mitchell | 66f6cfb | 2018-07-25 16:15:17 -0700 | [diff] [blame] | 509 | uses_static_library_action["additional-certificate"]; |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 510 | |
Alex Buynytskyy | a1613705 | 2021-12-02 13:26:54 +0000 | [diff] [blame] | 511 | xml::XmlNodeAction& sdk_library_action = application_action["sdk-library"]; |
| 512 | sdk_library_action.Action(RequiredNameIsJavaPackage); |
| 513 | sdk_library_action.Action(RequiredAndroidAttribute("versionMajor")); |
| 514 | |
| 515 | xml::XmlNodeAction& uses_sdk_library_action = application_action["uses-sdk-library"]; |
| 516 | uses_sdk_library_action.Action(RequiredNameIsJavaPackage); |
| 517 | uses_sdk_library_action.Action(RequiredAndroidAttribute("versionMajor")); |
| 518 | uses_sdk_library_action.Action(RequiredAndroidAttribute("certDigest")); |
| 519 | uses_sdk_library_action["additional-certificate"]; |
| 520 | |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 521 | xml::XmlNodeAction& uses_package_action = application_action["uses-package"]; |
| 522 | uses_package_action.Action(RequiredNameIsJavaPackage); |
| 523 | uses_package_action["additional-certificate"]; |
| 524 | |
Ryan Mitchell | e5b38a6 | 2018-03-23 13:35:00 -0700 | [diff] [blame] | 525 | if (options_.debug_mode) { |
| 526 | application_action.Action([&](xml::Element* el) -> bool { |
| 527 | xml::Attribute *attr = el->FindOrCreateAttribute(xml::kSchemaAndroid, "debuggable"); |
| 528 | attr->value = "true"; |
| 529 | return true; |
| 530 | }); |
| 531 | } |
| 532 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 533 | application_action["meta-data"] = meta_data_action; |
Adam Lesinski | 7a917a2 | 2017-06-02 12:55:24 -0700 | [diff] [blame] | 534 | |
Dianne Hackborn | fc0839a | 2020-01-31 11:02:28 -0800 | [diff] [blame] | 535 | application_action["processes"]; |
| 536 | application_action["processes"]["deny-permission"]; |
| 537 | application_action["processes"]["allow-permission"]; |
| 538 | application_action["processes"]["process"]["deny-permission"]; |
| 539 | application_action["processes"]["process"]["allow-permission"]; |
| 540 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 541 | application_action["activity"] = component_action; |
Adam Lesinski | 7a917a2 | 2017-06-02 12:55:24 -0700 | [diff] [blame] | 542 | application_action["activity"]["layout"]; |
| 543 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 544 | application_action["activity-alias"] = component_action; |
| 545 | application_action["service"] = component_action; |
| 546 | application_action["receiver"] = component_action; |
Gavin Corkery | ad5d4ba | 2021-10-26 12:24:43 +0100 | [diff] [blame^] | 547 | application_action["apex-system-service"] = component_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 548 | |
| 549 | // Provider actions. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 550 | application_action["provider"] = component_action; |
Adam Lesinski | c10c0d0 | 2017-04-28 12:54:08 -0700 | [diff] [blame] | 551 | application_action["provider"]["grant-uri-permission"]; |
Adam Lesinski | 25783ca | 2017-04-24 13:33:47 -0700 | [diff] [blame] | 552 | application_action["provider"]["path-permission"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 553 | |
Ryan Mitchell | 28afe68 | 2018-09-07 14:33:14 -0700 | [diff] [blame] | 554 | manifest_action["package"] = manifest_action; |
| 555 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 556 | return true; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 559 | static void FullyQualifyClassName(const StringPiece& package, const StringPiece& attr_ns, |
| 560 | const StringPiece& attr_name, xml::Element* el) { |
| 561 | xml::Attribute* attr = el->FindAttribute(attr_ns, attr_name); |
| 562 | if (attr != nullptr) { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 563 | if (std::optional<std::string> new_value = |
| 564 | util::GetFullyQualifiedClassName(package, attr->value)) { |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 565 | attr->value = std::move(new_value.value()); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 566 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 567 | } |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 568 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 569 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 570 | static bool RenameManifestPackage(const StringPiece& package_override, xml::Element* manifest_el) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 571 | xml::Attribute* attr = manifest_el->FindAttribute({}, "package"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 572 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 573 | // We've already verified that the manifest element is present, with a package |
| 574 | // name specified. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 575 | CHECK(attr != nullptr); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 576 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 577 | std::string original_package = std::move(attr->value); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 578 | attr->value = package_override.to_string(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 579 | |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 580 | xml::Element* application_el = manifest_el->FindChild({}, "application"); |
| 581 | if (application_el != nullptr) { |
| 582 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", application_el); |
| 583 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "backupAgent", application_el); |
| 584 | |
| 585 | for (xml::Element* child_el : application_el->GetChildElements()) { |
| 586 | if (child_el->namespace_uri.empty()) { |
| 587 | if (child_el->name == "activity" || child_el->name == "activity-alias" || |
| 588 | child_el->name == "provider" || child_el->name == "receiver" || |
| 589 | child_el->name == "service") { |
| 590 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", child_el); |
| 591 | } |
| 592 | |
| 593 | if (child_el->name == "activity-alias") { |
| 594 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "targetActivity", child_el); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 599 | return true; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 602 | bool ManifestFixer::Consume(IAaptContext* context, xml::XmlResource* doc) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 603 | TRACE_CALL(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 604 | xml::Element* root = xml::FindRootElement(doc->root.get()); |
| 605 | if (!root || !root->namespace_uri.empty() || root->name != "manifest") { |
| 606 | context->GetDiagnostics()->Error(DiagMessage(doc->file.source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 607 | << "root tag must be <manifest>"); |
| 608 | return false; |
| 609 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 610 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 611 | if ((options_.min_sdk_version_default || options_.target_sdk_version_default) && |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 612 | root->FindChild({}, "uses-sdk") == nullptr) { |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 613 | // Auto insert a <uses-sdk> element. This must be inserted before the |
| 614 | // <application> tag. The device runtime PackageParser will make SDK version |
| 615 | // decisions while parsing <application>. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 616 | std::unique_ptr<xml::Element> uses_sdk = util::make_unique<xml::Element>(); |
| 617 | uses_sdk->name = "uses-sdk"; |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 618 | root->InsertChild(0, std::move(uses_sdk)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 619 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 620 | |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 621 | if (options_.compile_sdk_version) { |
| 622 | xml::Attribute* attr = root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersion"); |
| 623 | |
| 624 | // Make sure we un-compile the value if it was set to something else. |
| 625 | attr->compiled_value = {}; |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 626 | attr->value = options_.compile_sdk_version.value(); |
Ryan Mitchell | aada89c | 2019-02-12 08:06:26 -0800 | [diff] [blame] | 627 | |
| 628 | attr = root->FindOrCreateAttribute("", "platformBuildVersionCode"); |
| 629 | |
| 630 | // Make sure we un-compile the value if it was set to something else. |
| 631 | attr->compiled_value = {}; |
| 632 | attr->value = options_.compile_sdk_version.value(); |
| 633 | |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | if (options_.compile_sdk_version_codename) { |
| 637 | xml::Attribute* attr = |
| 638 | root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename"); |
| 639 | |
| 640 | // Make sure we un-compile the value if it was set to something else. |
| 641 | attr->compiled_value = {}; |
Ryan Mitchell | aada89c | 2019-02-12 08:06:26 -0800 | [diff] [blame] | 642 | attr->value = options_.compile_sdk_version_codename.value(); |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 643 | |
Ryan Mitchell | aada89c | 2019-02-12 08:06:26 -0800 | [diff] [blame] | 644 | attr = root->FindOrCreateAttribute("", "platformBuildVersionName"); |
| 645 | |
| 646 | // Make sure we un-compile the value if it was set to something else. |
| 647 | attr->compiled_value = {}; |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 648 | attr->value = options_.compile_sdk_version_codename.value(); |
| 649 | } |
| 650 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 651 | xml::XmlActionExecutor executor; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 652 | if (!BuildRules(&executor, context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 653 | return false; |
| 654 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 655 | |
Izabela Orlowska | ad9e132 | 2017-12-19 16:22:42 +0000 | [diff] [blame] | 656 | xml::XmlActionExecutorPolicy policy = options_.warn_validation |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 657 | ? xml::XmlActionExecutorPolicy::kAllowListWarning |
| 658 | : xml::XmlActionExecutorPolicy::kAllowList; |
Izabela Orlowska | ad9e132 | 2017-12-19 16:22:42 +0000 | [diff] [blame] | 659 | if (!executor.Execute(policy, context->GetDiagnostics(), doc)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 660 | return false; |
| 661 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 662 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 663 | if (options_.rename_manifest_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 664 | // Rename manifest package outside of the XmlActionExecutor. |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 665 | // We need to extract the old package name and FullyQualify all class |
| 666 | // names. |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 667 | if (!RenameManifestPackage(options_.rename_manifest_package.value(), root)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 668 | return false; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 669 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 670 | } |
| 671 | return true; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 674 | } // namespace aapt |