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 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 21 | #include "ResourceUtils.h" |
Brandon Liu | 7de701f | 2024-06-11 18:51:39 +0000 | [diff] [blame] | 22 | #include "android-base/logging.h" |
| 23 | #include "process/SymbolTable.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 | |
Brandon Liu | 3cff255 | 2022-10-19 18:51:07 +0000 | [diff] [blame] | 33 | // This is to detect whether an <intent-filter> contains deeplink. |
| 34 | // See https://developer.android.com/training/app-links/deep-linking. |
| 35 | static bool HasDeepLink(xml::Element* intent_filter_el) { |
| 36 | xml::Element* action_el = intent_filter_el->FindChild({}, "action"); |
| 37 | xml::Element* category_el = intent_filter_el->FindChild({}, "category"); |
| 38 | xml::Element* data_el = intent_filter_el->FindChild({}, "data"); |
| 39 | if (action_el == nullptr || category_el == nullptr || data_el == nullptr) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | // Deeplinks must specify the ACTION_VIEW intent action. |
| 44 | constexpr const char* action_view = "android.intent.action.VIEW"; |
| 45 | if (intent_filter_el->FindChildWithAttribute({}, "action", xml::kSchemaAndroid, "name", |
| 46 | action_view) == nullptr) { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | // Deeplinks must have scheme included in <data> tag. |
| 51 | xml::Attribute* data_scheme_attr = data_el->FindAttribute(xml::kSchemaAndroid, "scheme"); |
| 52 | if (data_scheme_attr == nullptr || data_scheme_attr->value.empty()) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | // Deeplinks must include BROWSABLE category. |
| 57 | constexpr const char* category_browsable = "android.intent.category.BROWSABLE"; |
| 58 | if (intent_filter_el->FindChildWithAttribute({}, "category", xml::kSchemaAndroid, "name", |
| 59 | category_browsable) == nullptr) { |
| 60 | return false; |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | static bool VerifyDeeplinkPathAttribute(xml::Element* data_el, android::SourcePathDiagnostics* diag, |
| 66 | const std::string& attr_name) { |
| 67 | xml::Attribute* attr = data_el->FindAttribute(xml::kSchemaAndroid, attr_name); |
| 68 | if (attr != nullptr && !attr->value.empty()) { |
| 69 | StringPiece attr_value = attr->value; |
| 70 | const char* startChar = attr_value.begin(); |
| 71 | if (attr_name == "pathPattern") { |
Brandon Liu | a35bf78 | 2022-11-03 19:04:31 +0000 | [diff] [blame] | 72 | // pathPattern starts with '.' or '*' does not need leading slash. |
| 73 | // Reference starts with @ does not need leading slash. |
| 74 | if (*startChar == '/' || *startChar == '.' || *startChar == '*' || *startChar == '@') { |
Brandon Liu | 3cff255 | 2022-10-19 18:51:07 +0000 | [diff] [blame] | 75 | return true; |
| 76 | } else { |
| 77 | diag->Error(android::DiagMessage(data_el->line_number) |
| 78 | << "attribute 'android:" << attr_name << "' in <" << data_el->name |
| 79 | << "> tag has value of '" << attr_value |
| 80 | << "', it must be in a pattern start with '.' or '*', otherwise must start " |
| 81 | "with a leading slash '/'"); |
| 82 | return false; |
| 83 | } |
| 84 | } else { |
Brandon Liu | a35bf78 | 2022-11-03 19:04:31 +0000 | [diff] [blame] | 85 | // Reference starts with @ does not need leading slash. |
| 86 | if (*startChar == '/' || *startChar == '@') { |
Brandon Liu | 3cff255 | 2022-10-19 18:51:07 +0000 | [diff] [blame] | 87 | return true; |
| 88 | } else { |
| 89 | diag->Error(android::DiagMessage(data_el->line_number) |
| 90 | << "attribute 'android:" << attr_name << "' in <" << data_el->name |
| 91 | << "> tag has value of '" << attr_value |
| 92 | << "', it must start with a leading slash '/'"); |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | static bool VerifyDeepLinkIntentAction(xml::Element* intent_filter_el, |
| 101 | android::SourcePathDiagnostics* diag) { |
| 102 | if (!HasDeepLink(intent_filter_el)) { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | xml::Element* data_el = intent_filter_el->FindChild({}, "data"); |
| 107 | if (data_el != nullptr) { |
| 108 | if (!VerifyDeeplinkPathAttribute(data_el, diag, "path")) { |
| 109 | return false; |
| 110 | } |
| 111 | if (!VerifyDeeplinkPathAttribute(data_el, diag, "pathPrefix")) { |
| 112 | return false; |
| 113 | } |
| 114 | if (!VerifyDeeplinkPathAttribute(data_el, diag, "pathPattern")) { |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 121 | static bool RequiredNameIsNotEmpty(xml::Element* el, android::SourcePathDiagnostics* diag) { |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 122 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name"); |
| 123 | if (attr == nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 124 | diag->Error(android::DiagMessage(el->line_number) |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 125 | << "<" << el->name << "> is missing attribute 'android:name'"); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | if (attr->value.empty()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 130 | diag->Error(android::DiagMessage(el->line_number) |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 131 | << "attribute 'android:name' in <" << el->name << "> tag must not be empty"); |
| 132 | return false; |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 137 | // This is how PackageManager builds class names from AndroidManifest.xml entries. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | static bool NameIsJavaClassName(xml::Element* el, xml::Attribute* attr, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 139 | android::SourcePathDiagnostics* diag) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | // We allow unqualified class names (ie: .HelloActivity) |
| 141 | // Since we don't know the package name, we can just make a fake one here and |
| 142 | // the test will be identical as long as the real package name is valid too. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 143 | std::optional<std::string> fully_qualified_class_name = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 144 | util::GetFullyQualifiedClassName("a", attr->value); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 145 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 146 | StringPiece qualified_class_name = fully_qualified_class_name |
| 147 | ? fully_qualified_class_name.value() |
| 148 | : attr->value; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 149 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 150 | if (!util::IsJavaClassName(qualified_class_name)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 151 | diag->Error(android::DiagMessage(el->line_number) << "attribute 'android:name' in <" << el->name |
| 152 | << "> tag must be a valid Java class name"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 153 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 158 | static bool OptionalNameIsJavaClassName(xml::Element* el, android::SourcePathDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 159 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) { |
| 160 | return NameIsJavaClassName(el, attr, diag); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | } |
| 162 | return true; |
| 163 | } |
| 164 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 165 | static bool RequiredNameIsJavaClassName(xml::Element* el, android::SourcePathDiagnostics* diag) { |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 166 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name"); |
| 167 | if (attr == nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 168 | diag->Error(android::DiagMessage(el->line_number) |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 169 | << "<" << el->name << "> is missing attribute 'android:name'"); |
| 170 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | } |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 172 | return NameIsJavaClassName(el, attr, diag); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Brandon Liu | 7de701f | 2024-06-11 18:51:39 +0000 | [diff] [blame] | 175 | static bool UpdateConfigChangesIfNeeded(xml::Element* el, IAaptContext* context) { |
| 176 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "configChanges"); |
| 177 | if (attr == nullptr) { |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | if (attr->value != "allKnown" && attr->value.find("allKnown") != std::string::npos) { |
| 182 | context->GetDiagnostics()->Error( |
| 183 | android::DiagMessage(el->line_number) |
| 184 | << "If you want to declare 'allKnown' in attribute 'android:configChanges' in <" << el->name |
| 185 | << ">, " << attr->value << " is not allowed', allKnown has to be used " |
| 186 | << "by itself, for example: 'android:configChanges=allKnown', it cannot be combined with " |
| 187 | << "the other flags"); |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | if (attr->value == "allKnown") { |
| 192 | SymbolTable* symbol_table = context->GetExternalSymbols(); |
| 193 | const SymbolTable::Symbol* symbol = |
| 194 | symbol_table->FindByName(ResourceName("android", ResourceType::kAttr, "configChanges")); |
| 195 | |
| 196 | if (symbol == nullptr) { |
| 197 | context->GetDiagnostics()->Error( |
| 198 | android::DiagMessage(el->line_number) |
| 199 | << "Cannot find symbol for android:configChanges with min sdk: " |
| 200 | << context->GetMinSdkVersion()); |
Brandon Liu | b9b08c4 | 2024-07-12 17:48:56 +0000 | [diff] [blame] | 201 | return false; |
Brandon Liu | 7de701f | 2024-06-11 18:51:39 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | std::stringstream new_value; |
| 205 | |
| 206 | const auto& symbols = symbol->attribute->symbols; |
| 207 | for (auto it = symbols.begin(); it != symbols.end(); ++it) { |
| 208 | // Skip 'resourcesUnused' which is the flag to fully disable activity restart specifically |
| 209 | // for games. |
| 210 | if (it->symbol.name.value().entry == "resourcesUnused") { |
| 211 | continue; |
| 212 | } |
| 213 | if (it != symbols.begin()) { |
| 214 | new_value << "|"; |
| 215 | } |
| 216 | new_value << it->symbol.name.value().entry; |
| 217 | } |
| 218 | const auto& old_value = attr->value; |
| 219 | auto new_value_str = new_value.str(); |
| 220 | context->GetDiagnostics()->Note(android::DiagMessage(el->line_number) |
| 221 | << "Updating value of 'android:configChanges' from " |
| 222 | << old_value << " to " << new_value_str); |
| 223 | attr->value = std::move(new_value_str); |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 228 | static bool RequiredNameIsJavaPackage(xml::Element* el, android::SourcePathDiagnostics* diag) { |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 229 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name"); |
| 230 | if (attr == nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 231 | diag->Error(android::DiagMessage(el->line_number) |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 232 | << "<" << el->name << "> is missing attribute 'android:name'"); |
| 233 | return false; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 234 | } |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 235 | |
| 236 | if (!util::IsJavaPackageName(attr->value)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 237 | diag->Error(android::DiagMessage(el->line_number) << "attribute 'android:name' in <" << el->name |
| 238 | << "> tag must be a valid Java package name"); |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 239 | return false; |
| 240 | } |
| 241 | return true; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 244 | static xml::XmlNodeAction::ActionFuncWithDiag RequiredAndroidAttribute(const std::string& attr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 245 | return [=](xml::Element* el, android::SourcePathDiagnostics* diag) -> bool { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 246 | if (el->FindAttribute(xml::kSchemaAndroid, attr) == nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 247 | diag->Error(android::DiagMessage(el->line_number) |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 248 | << "<" << el->name << "> is missing required attribute 'android:" << attr << "'"); |
| 249 | return false; |
| 250 | } |
| 251 | return true; |
| 252 | }; |
| 253 | } |
| 254 | |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 255 | static xml::XmlNodeAction::ActionFuncWithDiag RequiredOneAndroidAttribute( |
| 256 | const std::string& attrName1, const std::string& attrName2) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 257 | return [=](xml::Element* el, android::SourcePathDiagnostics* diag) -> bool { |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 258 | xml::Attribute* attr1 = el->FindAttribute(xml::kSchemaAndroid, attrName1); |
| 259 | xml::Attribute* attr2 = el->FindAttribute(xml::kSchemaAndroid, attrName2); |
| 260 | if (attr1 == nullptr && attr2 == nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 261 | diag->Error(android::DiagMessage(el->line_number) |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 262 | << "<" << el->name << "> is missing required attribute 'android:" << attrName1 |
| 263 | << "' or 'android:" << attrName2 << "'"); |
| 264 | return false; |
| 265 | } |
| 266 | if (attr1 != nullptr && attr2 != nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 267 | diag->Error(android::DiagMessage(el->line_number) |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 268 | << "<" << el->name << "> can only specify one of attribute 'android:" << attrName1 |
| 269 | << "' or 'android:" << attrName2 << "'"); |
| 270 | return false; |
| 271 | } |
| 272 | return true; |
| 273 | }; |
| 274 | } |
| 275 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 276 | static bool AutoGenerateIsFeatureSplit(xml::Element* el, android::SourcePathDiagnostics* diag) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 277 | constexpr const char* kFeatureSplit = "featureSplit"; |
| 278 | constexpr const char* kIsFeatureSplit = "isFeatureSplit"; |
| 279 | |
| 280 | xml::Attribute* attr = el->FindAttribute({}, kFeatureSplit); |
| 281 | if (attr != nullptr) { |
| 282 | // Rewrite the featureSplit attribute to be "split". This is what the |
| 283 | // platform recognizes. |
| 284 | attr->name = "split"; |
| 285 | |
| 286 | // Now inject the android:isFeatureSplit="true" attribute. |
| 287 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, kIsFeatureSplit); |
| 288 | if (attr != nullptr) { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 289 | if (!ResourceUtils::ParseBool(attr->value).value_or(false)) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 290 | // The isFeatureSplit attribute is false, which conflicts with the use |
| 291 | // of "featureSplit". |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 292 | diag->Error(android::DiagMessage(el->line_number) |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 293 | << "attribute 'featureSplit' used in <manifest> but 'android:isFeatureSplit' " |
| 294 | "is not 'true'"); |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | // The attribute is already there and set to true, nothing to do. |
| 299 | } else { |
| 300 | el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, kIsFeatureSplit, "true"}); |
| 301 | } |
| 302 | } |
| 303 | return true; |
| 304 | } |
| 305 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 306 | static bool AutoGenerateIsSplitRequired(xml::Element* el, android::SourcePathDiagnostics* diag) { |
Jackal Guo | 0c01abb | 2021-07-30 22:17:43 +0800 | [diff] [blame] | 307 | constexpr const char* kRequiredSplitTypes = "requiredSplitTypes"; |
| 308 | constexpr const char* kIsSplitRequired = "isSplitRequired"; |
| 309 | |
| 310 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, kRequiredSplitTypes); |
| 311 | if (attr != nullptr) { |
| 312 | // Now inject the android:isSplitRequired="true" attribute. |
| 313 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, kIsSplitRequired); |
| 314 | if (attr != nullptr) { |
| 315 | if (!ResourceUtils::ParseBool(attr->value).value_or(false)) { |
| 316 | // The isFeatureSplit attribute is false, which conflicts with the use |
| 317 | // of "featureSplit". |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 318 | diag->Error(android::DiagMessage(el->line_number) |
Jackal Guo | 0c01abb | 2021-07-30 22:17:43 +0800 | [diff] [blame] | 319 | << "attribute 'requiredSplitTypes' used in <manifest> but " |
| 320 | "'android:isSplitRequired' is not 'true'"); |
| 321 | return false; |
| 322 | } |
| 323 | // The attribute is already there and set to true, nothing to do. |
| 324 | } else { |
| 325 | el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, kIsSplitRequired, "true"}); |
| 326 | } |
| 327 | } |
| 328 | return true; |
| 329 | } |
| 330 | |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 331 | static bool VerifyManifest(xml::Element* el, xml::XmlActionExecutorPolicy policy, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 332 | android::SourcePathDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | xml::Attribute* attr = el->FindAttribute({}, "package"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 334 | if (!attr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 335 | diag->Error(android::DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 336 | << "<manifest> tag is missing 'package' attribute"); |
| 337 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 338 | } else if (ResourceUtils::IsReference(attr->value)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 339 | diag->Error(android::DiagMessage(el->line_number) |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 340 | << "attribute 'package' in <manifest> tag must not be a reference"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 341 | return false; |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 342 | } else if (!util::IsAndroidPackageName(attr->value)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 343 | android::DiagMessage error_msg(el->line_number); |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 344 | error_msg << "attribute 'package' in <manifest> tag is not a valid Android package name: '" |
| 345 | << attr->value << "'"; |
| 346 | if (policy == xml::XmlActionExecutorPolicy::kAllowListWarning) { |
| 347 | // Treat the error only as a warning. |
| 348 | diag->Warn(error_msg); |
| 349 | } else { |
| 350 | diag->Error(error_msg); |
| 351 | return false; |
| 352 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 353 | } |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 354 | |
| 355 | attr = el->FindAttribute({}, "split"); |
| 356 | if (attr) { |
| 357 | if (!util::IsJavaPackageName(attr->value)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 358 | diag->Error(android::DiagMessage(el->line_number) |
| 359 | << "attribute 'split' in <manifest> tag is not a " |
| 360 | "valid split name"); |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 361 | return false; |
| 362 | } |
| 363 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 364 | return true; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 367 | // The coreApp attribute in <manifest> is not a regular AAPT attribute, so type |
| 368 | // checking on it is manual. |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 369 | static bool FixCoreAppAttribute(xml::Element* el, android::SourcePathDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 370 | if (xml::Attribute* attr = el->FindAttribute("", "coreApp")) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 371 | std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseBool(attr->value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 372 | if (!result) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 373 | diag->Error(android::DiagMessage(el->line_number) << "attribute coreApp must be a boolean"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 374 | return false; |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 375 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 376 | attr->compiled_value = std::move(result); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 377 | } |
| 378 | return true; |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 381 | // Checks that <uses-feature> has android:glEsVersion or android:name, not both (or neither). |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 382 | static bool VerifyUsesFeature(xml::Element* el, android::SourcePathDiagnostics* diag) { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 383 | bool has_name = false; |
| 384 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) { |
| 385 | if (attr->value.empty()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 386 | diag->Error(android::DiagMessage(el->line_number) |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 387 | << "android:name in <uses-feature> must not be empty"); |
| 388 | return false; |
| 389 | } |
| 390 | has_name = true; |
| 391 | } |
| 392 | |
| 393 | bool has_gl_es_version = false; |
Yi Kong | d650788 | 2023-12-05 17:06:08 +0900 | [diff] [blame] | 394 | if (el->FindAttribute(xml::kSchemaAndroid, "glEsVersion")) { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 395 | if (has_name) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 396 | diag->Error(android::DiagMessage(el->line_number) |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 397 | << "cannot define both android:name and android:glEsVersion in <uses-feature>"); |
| 398 | return false; |
| 399 | } |
| 400 | has_gl_es_version = true; |
| 401 | } |
| 402 | |
| 403 | if (!has_name && !has_gl_es_version) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 404 | diag->Error(android::DiagMessage(el->line_number) |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 405 | << "<uses-feature> must have either android:name or android:glEsVersion attribute"); |
| 406 | return false; |
| 407 | } |
| 408 | return true; |
| 409 | } |
| 410 | |
Donald Chai | 6e49735 | 2019-05-19 21:07:50 -0700 | [diff] [blame] | 411 | // Ensure that 'ns_decls' contains a declaration for 'uri', using 'prefix' as |
| 412 | // the xmlns prefix if possible. |
| 413 | static void EnsureNamespaceIsDeclared(const std::string& prefix, const std::string& uri, |
| 414 | std::vector<xml::NamespaceDecl>* ns_decls) { |
| 415 | if (std::find_if(ns_decls->begin(), ns_decls->end(), [&](const xml::NamespaceDecl& ns_decl) { |
| 416 | return ns_decl.uri == uri; |
| 417 | }) != ns_decls->end()) { |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | std::set<std::string> used_prefixes; |
| 422 | for (const auto& ns_decl : *ns_decls) { |
| 423 | used_prefixes.insert(ns_decl.prefix); |
| 424 | } |
| 425 | |
| 426 | // Make multiple attempts in the unlikely event that 'prefix' is already taken. |
| 427 | std::string disambiguator; |
| 428 | for (int i = 0; i < used_prefixes.size() + 1; i++) { |
| 429 | std::string attempted_prefix = prefix + disambiguator; |
| 430 | if (used_prefixes.find(attempted_prefix) == used_prefixes.end()) { |
| 431 | ns_decls->push_back(xml::NamespaceDecl{attempted_prefix, uri}); |
| 432 | return; |
| 433 | } |
| 434 | disambiguator = std::to_string(i); |
| 435 | } |
| 436 | } |
| 437 | |
Brandon Liu | 7de701f | 2024-06-11 18:51:39 +0000 | [diff] [blame] | 438 | bool ManifestFixer::BuildRules(xml::XmlActionExecutor* executor, IAaptContext* context) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 439 | // First verify some options. |
Brandon Liu | 7de701f | 2024-06-11 18:51:39 +0000 | [diff] [blame] | 440 | android::IDiagnostics* diag = context->GetDiagnostics(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 441 | if (options_.rename_manifest_package) { |
| 442 | if (!util::IsJavaPackageName(options_.rename_manifest_package.value())) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 443 | diag->Error(android::DiagMessage() << "invalid manifest package override '" |
| 444 | << options_.rename_manifest_package.value() << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 445 | return false; |
| 446 | } |
| 447 | } |
| 448 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 449 | if (options_.rename_instrumentation_target_package) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 450 | if (!util::IsJavaPackageName(options_.rename_instrumentation_target_package.value())) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 451 | diag->Error(android::DiagMessage() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 452 | << "invalid instrumentation target package override '" |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 453 | << options_.rename_instrumentation_target_package.value() << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 454 | return false; |
| 455 | } |
| 456 | } |
| 457 | |
Roshan Pius | ae12ce4 | 2020-04-25 16:08:55 -0700 | [diff] [blame] | 458 | if (options_.rename_overlay_target_package) { |
| 459 | if (!util::IsJavaPackageName(options_.rename_overlay_target_package.value())) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 460 | diag->Error(android::DiagMessage() << "invalid overlay target package override '" |
| 461 | << options_.rename_overlay_target_package.value() << "'"); |
Roshan Pius | ae12ce4 | 2020-04-25 16:08:55 -0700 | [diff] [blame] | 462 | return false; |
| 463 | } |
| 464 | } |
| 465 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 466 | // Common <intent-filter> actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 467 | xml::XmlNodeAction intent_filter_action; |
Brandon Liu | 3cff255 | 2022-10-19 18:51:07 +0000 | [diff] [blame] | 468 | intent_filter_action.Action(VerifyDeepLinkIntentAction); |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 469 | intent_filter_action["action"].Action(RequiredNameIsNotEmpty); |
| 470 | intent_filter_action["category"].Action(RequiredNameIsNotEmpty); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 471 | intent_filter_action["data"]; |
William Loh | d314584 | 2023-12-06 11:11:19 -0800 | [diff] [blame] | 472 | intent_filter_action["uri-relative-filter-group"]; |
| 473 | intent_filter_action["uri-relative-filter-group"]["data"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 474 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 475 | // Common <meta-data> actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 476 | xml::XmlNodeAction meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 477 | |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 478 | // Common <property> actions. |
| 479 | xml::XmlNodeAction property_action; |
| 480 | property_action.Action(RequiredOneAndroidAttribute("resource", "value")); |
| 481 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 482 | // Common <uses-feature> actions. |
| 483 | xml::XmlNodeAction uses_feature_action; |
| 484 | uses_feature_action.Action(VerifyUsesFeature); |
| 485 | |
| 486 | // Common component actions. |
| 487 | xml::XmlNodeAction component_action; |
| 488 | component_action.Action(RequiredNameIsJavaClassName); |
Brandon Liu | 7de701f | 2024-06-11 18:51:39 +0000 | [diff] [blame] | 489 | component_action.Action( |
| 490 | [context](xml::Element* el) -> bool { return UpdateConfigChangesIfNeeded(el, context); }); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 491 | component_action["intent-filter"] = intent_filter_action; |
Ryan Mitchell | 28afe68 | 2018-09-07 14:33:14 -0700 | [diff] [blame] | 492 | component_action["preferred"] = intent_filter_action; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 493 | component_action["meta-data"] = meta_data_action; |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 494 | component_action["property"] = property_action; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 495 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 496 | // Manifest actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 497 | xml::XmlNodeAction& manifest_action = (*executor)["manifest"]; |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 498 | manifest_action.Action(AutoGenerateIsFeatureSplit); |
Jackal Guo | 0c01abb | 2021-07-30 22:17:43 +0800 | [diff] [blame] | 499 | manifest_action.Action(AutoGenerateIsSplitRequired); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 500 | manifest_action.Action(VerifyManifest); |
| 501 | manifest_action.Action(FixCoreAppAttribute); |
Ryan Prichard | cbcff74 | 2024-05-06 20:25:16 -0700 | [diff] [blame] | 502 | manifest_action.Action([this, diag](xml::Element* el) -> bool { |
Donald Chai | 6e49735 | 2019-05-19 21:07:50 -0700 | [diff] [blame] | 503 | EnsureNamespaceIsDeclared("android", xml::kSchemaAndroid, &el->namespace_decls); |
| 504 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 505 | if (options_.version_name_default) { |
Colin Cross | dcd58c4 | 2018-05-25 22:46:35 -0700 | [diff] [blame] | 506 | if (options_.replace_version) { |
| 507 | el->RemoveAttribute(xml::kSchemaAndroid, "versionName"); |
| 508 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 509 | if (el->FindAttribute(xml::kSchemaAndroid, "versionName") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 510 | el->attributes.push_back( |
| 511 | xml::Attribute{xml::kSchemaAndroid, "versionName", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 512 | options_.version_name_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 513 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 516 | if (options_.version_code_default) { |
Colin Cross | dcd58c4 | 2018-05-25 22:46:35 -0700 | [diff] [blame] | 517 | if (options_.replace_version) { |
| 518 | el->RemoveAttribute(xml::kSchemaAndroid, "versionCode"); |
| 519 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 520 | if (el->FindAttribute(xml::kSchemaAndroid, "versionCode") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 521 | el->attributes.push_back( |
| 522 | xml::Attribute{xml::kSchemaAndroid, "versionCode", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 523 | options_.version_code_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 524 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 525 | } |
Ryan Mitchell | 7cb82a8 | 2018-05-10 15:35:31 -0700 | [diff] [blame] | 526 | |
Ryan Mitchell | 704090e | 2018-07-31 14:59:25 -0700 | [diff] [blame] | 527 | if (options_.version_code_major_default) { |
| 528 | if (options_.replace_version) { |
| 529 | el->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor"); |
| 530 | } |
| 531 | if (el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor") == nullptr) { |
| 532 | el->attributes.push_back( |
| 533 | xml::Attribute{xml::kSchemaAndroid, "versionCodeMajor", |
| 534 | options_.version_code_major_default.value()}); |
| 535 | } |
| 536 | } |
| 537 | |
Rhed Jao | b9ccb8a4 | 2020-11-30 21:42:16 +0800 | [diff] [blame] | 538 | if (options_.revision_code_default) { |
| 539 | if (options_.replace_version) { |
| 540 | el->RemoveAttribute(xml::kSchemaAndroid, "revisionCode"); |
| 541 | } |
| 542 | if (el->FindAttribute(xml::kSchemaAndroid, "revisionCode") == nullptr) { |
| 543 | el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "revisionCode", |
| 544 | options_.revision_code_default.value()}); |
| 545 | } |
| 546 | } |
| 547 | |
Mark Punzalan | cc694e5 | 2024-04-02 20:43:23 +0000 | [diff] [blame] | 548 | if (options_.non_updatable_system) { |
| 549 | if (el->FindAttribute(xml::kSchemaAndroid, "versionCode") == nullptr) { |
| 550 | el->RemoveAttribute("", "updatableSystem"); |
| 551 | el->attributes.push_back(xml::Attribute{"", "updatableSystem", "false"}); |
| 552 | } else { |
| 553 | diag->Note(android::DiagMessage(el->line_number) |
| 554 | << "Ignoring --non-updatable-system because the manifest has a versionCode"); |
| 555 | } |
| 556 | } |
| 557 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 558 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 559 | }); |
| 560 | |
| 561 | // Meta tags. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 562 | manifest_action["eat-comment"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 563 | |
| 564 | // Uses-sdk actions. |
Ryan Prichard | cbcff74 | 2024-05-06 20:25:16 -0700 | [diff] [blame] | 565 | manifest_action["uses-sdk"].Action([this](xml::Element* el) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 566 | if (options_.min_sdk_version_default && |
| 567 | el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 568 | // There was no minSdkVersion defined and we have a default to assign. |
| 569 | el->attributes.push_back( |
| 570 | xml::Attribute{xml::kSchemaAndroid, "minSdkVersion", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 571 | options_.min_sdk_version_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 574 | if (options_.target_sdk_version_default && |
| 575 | el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 576 | // There was no targetSdkVersion defined and we have a default to assign. |
| 577 | el->attributes.push_back( |
| 578 | xml::Attribute{xml::kSchemaAndroid, "targetSdkVersion", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 579 | options_.target_sdk_version_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 580 | } |
| 581 | return true; |
| 582 | }); |
Anton Hansson | b2f709d | 2020-01-09 10:25:23 +0000 | [diff] [blame] | 583 | manifest_action["uses-sdk"]["extension-sdk"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 584 | |
| 585 | // Instrumentation actions. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 586 | manifest_action["instrumentation"].Action(RequiredNameIsJavaClassName); |
Ryan Prichard | cbcff74 | 2024-05-06 20:25:16 -0700 | [diff] [blame] | 587 | manifest_action["instrumentation"].Action([this](xml::Element* el) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 588 | if (!options_.rename_instrumentation_target_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 589 | return true; |
| 590 | } |
| 591 | |
| 592 | if (xml::Attribute* attr = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 593 | el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) { |
| 594 | attr->value = options_.rename_instrumentation_target_package.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 595 | } |
| 596 | return true; |
| 597 | }); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 598 | manifest_action["instrumentation"]["meta-data"] = meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 599 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 600 | manifest_action["attribution"]; |
| 601 | manifest_action["attribution"]["inherit-from"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 602 | manifest_action["original-package"]; |
Ryan Prichard | cbcff74 | 2024-05-06 20:25:16 -0700 | [diff] [blame] | 603 | manifest_action["overlay"].Action([this](xml::Element* el) -> bool { |
Jeremy Meyer | 6b05b7d | 2022-09-30 22:22:24 +0000 | [diff] [blame] | 604 | if (options_.rename_overlay_target_package) { |
| 605 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) { |
| 606 | attr->value = options_.rename_overlay_target_package.value(); |
| 607 | } |
Roshan Pius | ae12ce4 | 2020-04-25 16:08:55 -0700 | [diff] [blame] | 608 | } |
Jeremy Meyer | 6b05b7d | 2022-09-30 22:22:24 +0000 | [diff] [blame] | 609 | if (options_.rename_overlay_category) { |
| 610 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "category")) { |
| 611 | attr->value = options_.rename_overlay_category.value(); |
| 612 | } else { |
| 613 | el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "category", |
| 614 | options_.rename_overlay_category.value()}); |
| 615 | } |
Roshan Pius | ae12ce4 | 2020-04-25 16:08:55 -0700 | [diff] [blame] | 616 | } |
| 617 | return true; |
| 618 | }); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 619 | manifest_action["protected-broadcast"]; |
Alan Viverette | cf5326f | 2018-01-05 16:03:50 -0500 | [diff] [blame] | 620 | manifest_action["adopt-permissions"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 621 | manifest_action["uses-permission"]; |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 622 | manifest_action["uses-permission"]["required-feature"].Action(RequiredNameIsNotEmpty); |
| 623 | manifest_action["uses-permission"]["required-not-feature"].Action(RequiredNameIsNotEmpty); |
Adam Lesinski | 4b585db | 2017-05-12 15:25:50 -0700 | [diff] [blame] | 624 | manifest_action["uses-permission-sdk-23"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 625 | manifest_action["permission"]; |
Ryan Mitchell | 66f6cfb | 2018-07-25 16:15:17 -0700 | [diff] [blame] | 626 | manifest_action["permission"]["meta-data"] = meta_data_action; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 627 | manifest_action["permission-tree"]; |
| 628 | manifest_action["permission-group"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 629 | manifest_action["uses-configuration"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 630 | manifest_action["supports-screens"]; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 631 | manifest_action["uses-feature"] = uses_feature_action; |
| 632 | manifest_action["feature-group"]["uses-feature"] = uses_feature_action; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 633 | manifest_action["compatible-screens"]; |
| 634 | manifest_action["compatible-screens"]["screen"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 635 | manifest_action["supports-gl-texture"]; |
Ryan Mitchell | 66f6cfb | 2018-07-25 16:15:17 -0700 | [diff] [blame] | 636 | manifest_action["restrict-update"]; |
MÃ¥rten Kongstad | b0502bb | 2022-05-16 15:23:40 +0200 | [diff] [blame] | 637 | manifest_action["install-constraints"]["fingerprint-prefix"]; |
Ryan Mitchell | 66f6cfb | 2018-07-25 16:15:17 -0700 | [diff] [blame] | 638 | manifest_action["package-verifier"]; |
Adam Lesinski | 5119e51 | 2016-12-05 19:48:20 -0800 | [diff] [blame] | 639 | manifest_action["meta-data"] = meta_data_action; |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 640 | manifest_action["uses-split"].Action(RequiredNameIsJavaPackage); |
Patrick Baumann | a4ffb45 | 2019-06-24 15:01:49 -0700 | [diff] [blame] | 641 | manifest_action["queries"]["package"].Action(RequiredNameIsJavaPackage); |
| 642 | manifest_action["queries"]["intent"] = intent_filter_action; |
Patrick Baumann | 9918123 | 2020-01-28 10:55:25 -0800 | [diff] [blame] | 643 | manifest_action["queries"]["provider"].Action(RequiredAndroidAttribute("authorities")); |
Patrick Baumann | a4ffb45 | 2019-06-24 15:01:49 -0700 | [diff] [blame] | 644 | // TODO: more complicated component name tag |
Adam Lesinski | 5119e51 | 2016-12-05 19:48:20 -0800 | [diff] [blame] | 645 | |
Adam Lesinski | 87f1e0f | 2017-06-27 16:21:58 -0700 | [diff] [blame] | 646 | manifest_action["key-sets"]["key-set"]["public-key"]; |
| 647 | manifest_action["key-sets"]["upgrade-key-set"]; |
| 648 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 649 | // Application actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 650 | xml::XmlNodeAction& application_action = manifest_action["application"]; |
| 651 | application_action.Action(OptionalNameIsJavaClassName); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 652 | |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 653 | application_action["uses-library"].Action(RequiredNameIsNotEmpty); |
Jiyong Park | 6a5b8b1 | 2020-06-30 13:23:36 +0900 | [diff] [blame] | 654 | application_action["uses-native-library"].Action(RequiredNameIsNotEmpty); |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 655 | application_action["library"].Action(RequiredNameIsNotEmpty); |
Chris Craik | 335b565 | 2019-04-04 12:46:47 -0700 | [diff] [blame] | 656 | application_action["profileable"]; |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 657 | application_action["property"] = property_action; |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 658 | |
| 659 | xml::XmlNodeAction& static_library_action = application_action["static-library"]; |
| 660 | static_library_action.Action(RequiredNameIsJavaPackage); |
| 661 | static_library_action.Action(RequiredAndroidAttribute("version")); |
| 662 | |
| 663 | xml::XmlNodeAction& uses_static_library_action = application_action["uses-static-library"]; |
| 664 | uses_static_library_action.Action(RequiredNameIsJavaPackage); |
| 665 | uses_static_library_action.Action(RequiredAndroidAttribute("version")); |
| 666 | uses_static_library_action.Action(RequiredAndroidAttribute("certDigest")); |
Ryan Mitchell | 66f6cfb | 2018-07-25 16:15:17 -0700 | [diff] [blame] | 667 | uses_static_library_action["additional-certificate"]; |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 668 | |
Alex Buynytskyy | a1613705 | 2021-12-02 13:26:54 +0000 | [diff] [blame] | 669 | xml::XmlNodeAction& sdk_library_action = application_action["sdk-library"]; |
| 670 | sdk_library_action.Action(RequiredNameIsJavaPackage); |
| 671 | sdk_library_action.Action(RequiredAndroidAttribute("versionMajor")); |
| 672 | |
| 673 | xml::XmlNodeAction& uses_sdk_library_action = application_action["uses-sdk-library"]; |
| 674 | uses_sdk_library_action.Action(RequiredNameIsJavaPackage); |
| 675 | uses_sdk_library_action.Action(RequiredAndroidAttribute("versionMajor")); |
| 676 | uses_sdk_library_action.Action(RequiredAndroidAttribute("certDigest")); |
| 677 | uses_sdk_library_action["additional-certificate"]; |
| 678 | |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 679 | xml::XmlNodeAction& uses_package_action = application_action["uses-package"]; |
| 680 | uses_package_action.Action(RequiredNameIsJavaPackage); |
| 681 | uses_package_action["additional-certificate"]; |
| 682 | |
Ryan Mitchell | e5b38a6 | 2018-03-23 13:35:00 -0700 | [diff] [blame] | 683 | if (options_.debug_mode) { |
Ryan Prichard | cbcff74 | 2024-05-06 20:25:16 -0700 | [diff] [blame] | 684 | application_action.Action([](xml::Element* el) -> bool { |
Ryan Mitchell | e5b38a6 | 2018-03-23 13:35:00 -0700 | [diff] [blame] | 685 | xml::Attribute *attr = el->FindOrCreateAttribute(xml::kSchemaAndroid, "debuggable"); |
| 686 | attr->value = "true"; |
| 687 | return true; |
| 688 | }); |
| 689 | } |
| 690 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 691 | application_action["meta-data"] = meta_data_action; |
Adam Lesinski | 7a917a2 | 2017-06-02 12:55:24 -0700 | [diff] [blame] | 692 | |
Dianne Hackborn | fc0839a | 2020-01-31 11:02:28 -0800 | [diff] [blame] | 693 | application_action["processes"]; |
| 694 | application_action["processes"]["deny-permission"]; |
| 695 | application_action["processes"]["allow-permission"]; |
| 696 | application_action["processes"]["process"]["deny-permission"]; |
| 697 | application_action["processes"]["process"]["allow-permission"]; |
| 698 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 699 | application_action["activity"] = component_action; |
Adam Lesinski | 7a917a2 | 2017-06-02 12:55:24 -0700 | [diff] [blame] | 700 | application_action["activity"]["layout"]; |
| 701 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 702 | application_action["activity-alias"] = component_action; |
| 703 | application_action["service"] = component_action; |
| 704 | application_action["receiver"] = component_action; |
Gavin Corkery | ad5d4ba | 2021-10-26 12:24:43 +0100 | [diff] [blame] | 705 | application_action["apex-system-service"] = component_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 706 | |
| 707 | // Provider actions. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 708 | application_action["provider"] = component_action; |
Adam Lesinski | c10c0d0 | 2017-04-28 12:54:08 -0700 | [diff] [blame] | 709 | application_action["provider"]["grant-uri-permission"]; |
Adam Lesinski | 25783ca | 2017-04-24 13:33:47 -0700 | [diff] [blame] | 710 | application_action["provider"]["path-permission"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 711 | |
Ryan Mitchell | 28afe68 | 2018-09-07 14:33:14 -0700 | [diff] [blame] | 712 | manifest_action["package"] = manifest_action; |
| 713 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 714 | return true; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 715 | } |
| 716 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 717 | static void FullyQualifyClassName(StringPiece package, StringPiece attr_ns, StringPiece attr_name, |
| 718 | xml::Element* el) { |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 719 | xml::Attribute* attr = el->FindAttribute(attr_ns, attr_name); |
| 720 | if (attr != nullptr) { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 721 | if (std::optional<std::string> new_value = |
| 722 | util::GetFullyQualifiedClassName(package, attr->value)) { |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 723 | attr->value = std::move(new_value.value()); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 724 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 725 | } |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 726 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 727 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 728 | static bool RenameManifestPackage(StringPiece package_override, xml::Element* manifest_el) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 729 | xml::Attribute* attr = manifest_el->FindAttribute({}, "package"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 730 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 731 | // We've already verified that the manifest element is present, with a package |
| 732 | // name specified. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 733 | CHECK(attr != nullptr); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 734 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 735 | std::string original_package = std::move(attr->value); |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 736 | attr->value.assign(package_override); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 737 | |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 738 | xml::Element* application_el = manifest_el->FindChild({}, "application"); |
| 739 | if (application_el != nullptr) { |
| 740 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", application_el); |
| 741 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "backupAgent", application_el); |
| 742 | |
| 743 | for (xml::Element* child_el : application_el->GetChildElements()) { |
| 744 | if (child_el->namespace_uri.empty()) { |
| 745 | if (child_el->name == "activity" || child_el->name == "activity-alias" || |
| 746 | child_el->name == "provider" || child_el->name == "receiver" || |
| 747 | child_el->name == "service") { |
| 748 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", child_el); |
Makoto Onuki | 808fd19 | 2021-12-10 09:40:54 -0800 | [diff] [blame] | 749 | continue; |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | if (child_el->name == "activity-alias") { |
| 753 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "targetActivity", child_el); |
Makoto Onuki | 808fd19 | 2021-12-10 09:40:54 -0800 | [diff] [blame] | 754 | continue; |
| 755 | } |
| 756 | |
| 757 | if (child_el->name == "processes") { |
| 758 | for (xml::Element* grand_child_el : child_el->GetChildElements()) { |
| 759 | if (grand_child_el->name == "process") { |
| 760 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", grand_child_el); |
| 761 | } |
| 762 | } |
| 763 | continue; |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | } |
| 767 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 768 | return true; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 771 | bool ManifestFixer::Consume(IAaptContext* context, xml::XmlResource* doc) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 772 | TRACE_CALL(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 773 | xml::Element* root = xml::FindRootElement(doc->root.get()); |
| 774 | if (!root || !root->namespace_uri.empty() || root->name != "manifest") { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 775 | context->GetDiagnostics()->Error(android::DiagMessage(doc->file.source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 776 | << "root tag must be <manifest>"); |
| 777 | return false; |
| 778 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 779 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 780 | if ((options_.min_sdk_version_default || options_.target_sdk_version_default) && |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 781 | root->FindChild({}, "uses-sdk") == nullptr) { |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 782 | // Auto insert a <uses-sdk> element. This must be inserted before the |
| 783 | // <application> tag. The device runtime PackageParser will make SDK version |
| 784 | // decisions while parsing <application>. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 785 | std::unique_ptr<xml::Element> uses_sdk = util::make_unique<xml::Element>(); |
| 786 | uses_sdk->name = "uses-sdk"; |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 787 | root->InsertChild(0, std::move(uses_sdk)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 788 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 789 | |
Alan Viverette | 07287be | 2023-05-02 16:34:27 -0400 | [diff] [blame] | 790 | if (!options_.no_compile_sdk_metadata && options_.compile_sdk_version) { |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 791 | xml::Attribute* attr = root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersion"); |
| 792 | |
| 793 | // Make sure we un-compile the value if it was set to something else. |
| 794 | attr->compiled_value = {}; |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 795 | attr->value = options_.compile_sdk_version.value(); |
Ryan Mitchell | aada89c | 2019-02-12 08:06:26 -0800 | [diff] [blame] | 796 | |
| 797 | attr = root->FindOrCreateAttribute("", "platformBuildVersionCode"); |
| 798 | |
| 799 | // Make sure we un-compile the value if it was set to something else. |
| 800 | attr->compiled_value = {}; |
| 801 | attr->value = options_.compile_sdk_version.value(); |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Alan Viverette | 07287be | 2023-05-02 16:34:27 -0400 | [diff] [blame] | 804 | if (!options_.no_compile_sdk_metadata && options_.compile_sdk_version_codename) { |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 805 | xml::Attribute* attr = |
| 806 | root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename"); |
| 807 | |
| 808 | // Make sure we un-compile the value if it was set to something else. |
| 809 | attr->compiled_value = {}; |
Ryan Mitchell | aada89c | 2019-02-12 08:06:26 -0800 | [diff] [blame] | 810 | attr->value = options_.compile_sdk_version_codename.value(); |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 811 | |
Ryan Mitchell | aada89c | 2019-02-12 08:06:26 -0800 | [diff] [blame] | 812 | attr = root->FindOrCreateAttribute("", "platformBuildVersionName"); |
| 813 | |
| 814 | // Make sure we un-compile the value if it was set to something else. |
| 815 | attr->compiled_value = {}; |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 816 | attr->value = options_.compile_sdk_version_codename.value(); |
| 817 | } |
| 818 | |
Andrei Onea | 7109b70 | 2023-02-23 17:43:25 +0000 | [diff] [blame] | 819 | if (!options_.fingerprint_prefixes.empty()) { |
| 820 | xml::Element* install_constraints_el = root->FindChild({}, "install-constraints"); |
| 821 | if (install_constraints_el == nullptr) { |
| 822 | std::unique_ptr<xml::Element> install_constraints = std::make_unique<xml::Element>(); |
| 823 | install_constraints->name = "install-constraints"; |
| 824 | install_constraints_el = install_constraints.get(); |
| 825 | root->AppendChild(std::move(install_constraints)); |
| 826 | } |
| 827 | for (const std::string& prefix : options_.fingerprint_prefixes) { |
| 828 | std::unique_ptr<xml::Element> prefix_el = std::make_unique<xml::Element>(); |
| 829 | prefix_el->name = "fingerprint-prefix"; |
| 830 | xml::Attribute* attr = prefix_el->FindOrCreateAttribute(xml::kSchemaAndroid, "value"); |
| 831 | attr->value = prefix; |
| 832 | install_constraints_el->AppendChild(std::move(prefix_el)); |
| 833 | } |
| 834 | } |
| 835 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 836 | xml::XmlActionExecutor executor; |
Brandon Liu | 7de701f | 2024-06-11 18:51:39 +0000 | [diff] [blame] | 837 | if (!BuildRules(&executor, context)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 838 | return false; |
| 839 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 840 | |
Izabela Orlowska | ad9e132 | 2017-12-19 16:22:42 +0000 | [diff] [blame] | 841 | xml::XmlActionExecutorPolicy policy = options_.warn_validation |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 842 | ? xml::XmlActionExecutorPolicy::kAllowListWarning |
| 843 | : xml::XmlActionExecutorPolicy::kAllowList; |
Izabela Orlowska | ad9e132 | 2017-12-19 16:22:42 +0000 | [diff] [blame] | 844 | if (!executor.Execute(policy, context->GetDiagnostics(), doc)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 845 | return false; |
| 846 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 847 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 848 | if (options_.rename_manifest_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 849 | // Rename manifest package outside of the XmlActionExecutor. |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 850 | // We need to extract the old package name and FullyQualify all class |
| 851 | // names. |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 852 | if (!RenameManifestPackage(options_.rename_manifest_package.value(), root)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 853 | return false; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 854 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 855 | } |
| 856 | return true; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 857 | } |
| 858 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 859 | } // namespace aapt |