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. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | Maybe<std::string> fully_qualified_class_name = |
| 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) { |
| 149 | if (!ResourceUtils::ParseBool(attr->value).value_or_default(false)) { |
| 150 | // The isFeatureSplit attribute is false, which conflicts with the use |
| 151 | // of "featureSplit". |
| 152 | diag->Error(DiagMessage(el->line_number) |
| 153 | << "attribute 'featureSplit' used in <manifest> but 'android:isFeatureSplit' " |
| 154 | "is not 'true'"); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | // The attribute is already there and set to true, nothing to do. |
| 159 | } else { |
| 160 | el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, kIsFeatureSplit, "true"}); |
| 161 | } |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 166 | static bool VerifyManifest(xml::Element* el, xml::XmlActionExecutorPolicy policy, |
| 167 | SourcePathDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | xml::Attribute* attr = el->FindAttribute({}, "package"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 169 | if (!attr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 170 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | << "<manifest> tag is missing 'package' attribute"); |
| 172 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 173 | } else if (ResourceUtils::IsReference(attr->value)) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 174 | diag->Error(DiagMessage(el->line_number) |
| 175 | << "attribute 'package' in <manifest> tag must not be a reference"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | return false; |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 177 | } else if (!util::IsAndroidPackageName(attr->value)) { |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 178 | DiagMessage error_msg(el->line_number); |
| 179 | error_msg << "attribute 'package' in <manifest> tag is not a valid Android package name: '" |
| 180 | << attr->value << "'"; |
| 181 | if (policy == xml::XmlActionExecutorPolicy::kAllowListWarning) { |
| 182 | // Treat the error only as a warning. |
| 183 | diag->Warn(error_msg); |
| 184 | } else { |
| 185 | diag->Error(error_msg); |
| 186 | return false; |
| 187 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 188 | } |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 189 | |
| 190 | attr = el->FindAttribute({}, "split"); |
| 191 | if (attr) { |
| 192 | if (!util::IsJavaPackageName(attr->value)) { |
| 193 | diag->Error(DiagMessage(el->line_number) << "attribute 'split' in <manifest> tag is not a " |
| 194 | "valid split name"); |
| 195 | return false; |
| 196 | } |
| 197 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 198 | return true; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 201 | // The coreApp attribute in <manifest> is not a regular AAPT attribute, so type |
| 202 | // checking on it is manual. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 203 | static bool FixCoreAppAttribute(xml::Element* el, SourcePathDiagnostics* diag) { |
| 204 | if (xml::Attribute* attr = el->FindAttribute("", "coreApp")) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 205 | std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseBool(attr->value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 206 | if (!result) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 207 | diag->Error(DiagMessage(el->line_number) << "attribute coreApp must be a boolean"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 208 | return false; |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 209 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | attr->compiled_value = std::move(result); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 211 | } |
| 212 | return true; |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 215 | // Checks that <uses-feature> has android:glEsVersion or android:name, not both (or neither). |
| 216 | static bool VerifyUsesFeature(xml::Element* el, SourcePathDiagnostics* diag) { |
| 217 | bool has_name = false; |
| 218 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) { |
| 219 | if (attr->value.empty()) { |
| 220 | diag->Error(DiagMessage(el->line_number) |
| 221 | << "android:name in <uses-feature> must not be empty"); |
| 222 | return false; |
| 223 | } |
| 224 | has_name = true; |
| 225 | } |
| 226 | |
| 227 | bool has_gl_es_version = false; |
| 228 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "glEsVersion")) { |
| 229 | if (has_name) { |
| 230 | diag->Error(DiagMessage(el->line_number) |
| 231 | << "cannot define both android:name and android:glEsVersion in <uses-feature>"); |
| 232 | return false; |
| 233 | } |
| 234 | has_gl_es_version = true; |
| 235 | } |
| 236 | |
| 237 | if (!has_name && !has_gl_es_version) { |
| 238 | diag->Error(DiagMessage(el->line_number) |
| 239 | << "<uses-feature> must have either android:name or android:glEsVersion attribute"); |
| 240 | return false; |
| 241 | } |
| 242 | return true; |
| 243 | } |
| 244 | |
Donald Chai | 6e49735 | 2019-05-19 21:07:50 -0700 | [diff] [blame] | 245 | // Ensure that 'ns_decls' contains a declaration for 'uri', using 'prefix' as |
| 246 | // the xmlns prefix if possible. |
| 247 | static void EnsureNamespaceIsDeclared(const std::string& prefix, const std::string& uri, |
| 248 | std::vector<xml::NamespaceDecl>* ns_decls) { |
| 249 | if (std::find_if(ns_decls->begin(), ns_decls->end(), [&](const xml::NamespaceDecl& ns_decl) { |
| 250 | return ns_decl.uri == uri; |
| 251 | }) != ns_decls->end()) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | std::set<std::string> used_prefixes; |
| 256 | for (const auto& ns_decl : *ns_decls) { |
| 257 | used_prefixes.insert(ns_decl.prefix); |
| 258 | } |
| 259 | |
| 260 | // Make multiple attempts in the unlikely event that 'prefix' is already taken. |
| 261 | std::string disambiguator; |
| 262 | for (int i = 0; i < used_prefixes.size() + 1; i++) { |
| 263 | std::string attempted_prefix = prefix + disambiguator; |
| 264 | if (used_prefixes.find(attempted_prefix) == used_prefixes.end()) { |
| 265 | ns_decls->push_back(xml::NamespaceDecl{attempted_prefix, uri}); |
| 266 | return; |
| 267 | } |
| 268 | disambiguator = std::to_string(i); |
| 269 | } |
| 270 | } |
| 271 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | bool ManifestFixer::BuildRules(xml::XmlActionExecutor* executor, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | IDiagnostics* diag) { |
| 274 | // First verify some options. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 275 | if (options_.rename_manifest_package) { |
| 276 | if (!util::IsJavaPackageName(options_.rename_manifest_package.value())) { |
| 277 | diag->Error(DiagMessage() << "invalid manifest package override '" |
| 278 | << options_.rename_manifest_package.value() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 279 | << "'"); |
| 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 284 | if (options_.rename_instrumentation_target_package) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 285 | if (!util::IsJavaPackageName(options_.rename_instrumentation_target_package.value())) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 286 | diag->Error(DiagMessage() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 287 | << "invalid instrumentation target package override '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 288 | << options_.rename_instrumentation_target_package.value() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 289 | << "'"); |
| 290 | return false; |
| 291 | } |
| 292 | } |
| 293 | |
Roshan Pius | ae12ce4 | 2020-04-25 16:08:55 -0700 | [diff] [blame] | 294 | if (options_.rename_overlay_target_package) { |
| 295 | if (!util::IsJavaPackageName(options_.rename_overlay_target_package.value())) { |
| 296 | diag->Error(DiagMessage() |
| 297 | << "invalid overlay target package override '" |
| 298 | << options_.rename_overlay_target_package.value() |
| 299 | << "'"); |
| 300 | return false; |
| 301 | } |
| 302 | } |
| 303 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 304 | // Common <intent-filter> actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 305 | xml::XmlNodeAction intent_filter_action; |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 306 | intent_filter_action["action"].Action(RequiredNameIsNotEmpty); |
| 307 | intent_filter_action["category"].Action(RequiredNameIsNotEmpty); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 308 | intent_filter_action["data"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 309 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 310 | // Common <meta-data> actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 311 | xml::XmlNodeAction meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 312 | |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 313 | // Common <property> actions. |
| 314 | xml::XmlNodeAction property_action; |
| 315 | property_action.Action(RequiredOneAndroidAttribute("resource", "value")); |
| 316 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 317 | // Common <uses-feature> actions. |
| 318 | xml::XmlNodeAction uses_feature_action; |
| 319 | uses_feature_action.Action(VerifyUsesFeature); |
| 320 | |
| 321 | // Common component actions. |
| 322 | xml::XmlNodeAction component_action; |
| 323 | component_action.Action(RequiredNameIsJavaClassName); |
| 324 | component_action["intent-filter"] = intent_filter_action; |
Ryan Mitchell | 28afe68 | 2018-09-07 14:33:14 -0700 | [diff] [blame] | 325 | component_action["preferred"] = intent_filter_action; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 326 | component_action["meta-data"] = meta_data_action; |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 327 | component_action["property"] = property_action; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 328 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 329 | // Manifest actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 330 | xml::XmlNodeAction& manifest_action = (*executor)["manifest"]; |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 331 | manifest_action.Action(AutoGenerateIsFeatureSplit); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | manifest_action.Action(VerifyManifest); |
| 333 | manifest_action.Action(FixCoreAppAttribute); |
| 334 | manifest_action.Action([&](xml::Element* el) -> bool { |
Donald Chai | 6e49735 | 2019-05-19 21:07:50 -0700 | [diff] [blame] | 335 | EnsureNamespaceIsDeclared("android", xml::kSchemaAndroid, &el->namespace_decls); |
| 336 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 337 | if (options_.version_name_default) { |
Colin Cross | dcd58c4 | 2018-05-25 22:46:35 -0700 | [diff] [blame] | 338 | if (options_.replace_version) { |
| 339 | el->RemoveAttribute(xml::kSchemaAndroid, "versionName"); |
| 340 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 341 | if (el->FindAttribute(xml::kSchemaAndroid, "versionName") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 342 | el->attributes.push_back( |
| 343 | xml::Attribute{xml::kSchemaAndroid, "versionName", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 344 | options_.version_name_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 345 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 348 | if (options_.version_code_default) { |
Colin Cross | dcd58c4 | 2018-05-25 22:46:35 -0700 | [diff] [blame] | 349 | if (options_.replace_version) { |
| 350 | el->RemoveAttribute(xml::kSchemaAndroid, "versionCode"); |
| 351 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 352 | if (el->FindAttribute(xml::kSchemaAndroid, "versionCode") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 353 | el->attributes.push_back( |
| 354 | xml::Attribute{xml::kSchemaAndroid, "versionCode", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 355 | options_.version_code_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 356 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 357 | } |
Ryan Mitchell | 7cb82a8 | 2018-05-10 15:35:31 -0700 | [diff] [blame] | 358 | |
Ryan Mitchell | 704090e | 2018-07-31 14:59:25 -0700 | [diff] [blame] | 359 | if (options_.version_code_major_default) { |
| 360 | if (options_.replace_version) { |
| 361 | el->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor"); |
| 362 | } |
| 363 | if (el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor") == nullptr) { |
| 364 | el->attributes.push_back( |
| 365 | xml::Attribute{xml::kSchemaAndroid, "versionCodeMajor", |
| 366 | options_.version_code_major_default.value()}); |
| 367 | } |
| 368 | } |
| 369 | |
Rhed Jao | b9ccb8a4 | 2020-11-30 21:42:16 +0800 | [diff] [blame] | 370 | if (options_.revision_code_default) { |
| 371 | if (options_.replace_version) { |
| 372 | el->RemoveAttribute(xml::kSchemaAndroid, "revisionCode"); |
| 373 | } |
| 374 | if (el->FindAttribute(xml::kSchemaAndroid, "revisionCode") == nullptr) { |
| 375 | el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "revisionCode", |
| 376 | options_.revision_code_default.value()}); |
| 377 | } |
| 378 | } |
| 379 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 380 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 381 | }); |
| 382 | |
| 383 | // Meta tags. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 384 | manifest_action["eat-comment"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 385 | |
| 386 | // Uses-sdk actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 387 | manifest_action["uses-sdk"].Action([&](xml::Element* el) -> bool { |
| 388 | if (options_.min_sdk_version_default && |
| 389 | el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 390 | // There was no minSdkVersion defined and we have a default to assign. |
| 391 | el->attributes.push_back( |
| 392 | xml::Attribute{xml::kSchemaAndroid, "minSdkVersion", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 393 | options_.min_sdk_version_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 396 | if (options_.target_sdk_version_default && |
| 397 | el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 398 | // There was no targetSdkVersion defined and we have a default to assign. |
| 399 | el->attributes.push_back( |
| 400 | xml::Attribute{xml::kSchemaAndroid, "targetSdkVersion", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 401 | options_.target_sdk_version_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 402 | } |
| 403 | return true; |
| 404 | }); |
Anton Hansson | b2f709d | 2020-01-09 10:25:23 +0000 | [diff] [blame] | 405 | manifest_action["uses-sdk"]["extension-sdk"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 406 | |
| 407 | // Instrumentation actions. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 408 | manifest_action["instrumentation"].Action(RequiredNameIsJavaClassName); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 409 | manifest_action["instrumentation"].Action([&](xml::Element* el) -> bool { |
| 410 | if (!options_.rename_instrumentation_target_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 411 | return true; |
| 412 | } |
| 413 | |
| 414 | if (xml::Attribute* attr = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 415 | el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) { |
| 416 | attr->value = options_.rename_instrumentation_target_package.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 417 | } |
| 418 | return true; |
| 419 | }); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 420 | manifest_action["instrumentation"]["meta-data"] = meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 421 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 422 | manifest_action["attribution"]; |
| 423 | manifest_action["attribution"]["inherit-from"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 424 | manifest_action["original-package"]; |
Roshan Pius | ae12ce4 | 2020-04-25 16:08:55 -0700 | [diff] [blame] | 425 | manifest_action["overlay"].Action([&](xml::Element* el) -> bool { |
| 426 | if (!options_.rename_overlay_target_package) { |
| 427 | return true; |
| 428 | } |
| 429 | |
| 430 | if (xml::Attribute* attr = |
| 431 | el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) { |
| 432 | attr->value = options_.rename_overlay_target_package.value(); |
| 433 | } |
| 434 | return true; |
| 435 | }); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 436 | manifest_action["protected-broadcast"]; |
Alan Viverette | cf5326f | 2018-01-05 16:03:50 -0500 | [diff] [blame] | 437 | manifest_action["adopt-permissions"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 438 | manifest_action["uses-permission"]; |
Sergey Nikolaienkov | 65f9099 | 2020-09-30 08:17:09 +0000 | [diff] [blame] | 439 | manifest_action["uses-permission"]["required-feature"].Action(RequiredNameIsNotEmpty); |
| 440 | manifest_action["uses-permission"]["required-not-feature"].Action(RequiredNameIsNotEmpty); |
Adam Lesinski | 4b585db | 2017-05-12 15:25:50 -0700 | [diff] [blame] | 441 | manifest_action["uses-permission-sdk-23"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 442 | manifest_action["permission"]; |
Ryan Mitchell | 66f6cfb | 2018-07-25 16:15:17 -0700 | [diff] [blame] | 443 | manifest_action["permission"]["meta-data"] = meta_data_action; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 444 | manifest_action["permission-tree"]; |
| 445 | manifest_action["permission-group"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 446 | manifest_action["uses-configuration"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 447 | manifest_action["supports-screens"]; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 448 | manifest_action["uses-feature"] = uses_feature_action; |
| 449 | manifest_action["feature-group"]["uses-feature"] = uses_feature_action; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 450 | manifest_action["compatible-screens"]; |
| 451 | manifest_action["compatible-screens"]["screen"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 452 | manifest_action["supports-gl-texture"]; |
Ryan Mitchell | 66f6cfb | 2018-07-25 16:15:17 -0700 | [diff] [blame] | 453 | manifest_action["restrict-update"]; |
| 454 | manifest_action["package-verifier"]; |
Adam Lesinski | 5119e51 | 2016-12-05 19:48:20 -0800 | [diff] [blame] | 455 | manifest_action["meta-data"] = meta_data_action; |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 456 | manifest_action["uses-split"].Action(RequiredNameIsJavaPackage); |
Patrick Baumann | a4ffb45 | 2019-06-24 15:01:49 -0700 | [diff] [blame] | 457 | manifest_action["queries"]["package"].Action(RequiredNameIsJavaPackage); |
| 458 | manifest_action["queries"]["intent"] = intent_filter_action; |
Patrick Baumann | 9918123 | 2020-01-28 10:55:25 -0800 | [diff] [blame] | 459 | manifest_action["queries"]["provider"].Action(RequiredAndroidAttribute("authorities")); |
Patrick Baumann | a4ffb45 | 2019-06-24 15:01:49 -0700 | [diff] [blame] | 460 | // TODO: more complicated component name tag |
Adam Lesinski | 5119e51 | 2016-12-05 19:48:20 -0800 | [diff] [blame] | 461 | |
Adam Lesinski | 87f1e0f | 2017-06-27 16:21:58 -0700 | [diff] [blame] | 462 | manifest_action["key-sets"]["key-set"]["public-key"]; |
| 463 | manifest_action["key-sets"]["upgrade-key-set"]; |
| 464 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 465 | // Application actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 466 | xml::XmlNodeAction& application_action = manifest_action["application"]; |
| 467 | application_action.Action(OptionalNameIsJavaClassName); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 468 | |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 469 | application_action["uses-library"].Action(RequiredNameIsNotEmpty); |
Jiyong Park | 6a5b8b1 | 2020-06-30 13:23:36 +0900 | [diff] [blame] | 470 | application_action["uses-native-library"].Action(RequiredNameIsNotEmpty); |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 471 | application_action["library"].Action(RequiredNameIsNotEmpty); |
Chris Craik | 335b565 | 2019-04-04 12:46:47 -0700 | [diff] [blame] | 472 | application_action["profileable"]; |
Todd Kennedy | ce3e129 | 2020-10-29 17:14:24 -0700 | [diff] [blame] | 473 | application_action["property"] = property_action; |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 474 | |
| 475 | xml::XmlNodeAction& static_library_action = application_action["static-library"]; |
| 476 | static_library_action.Action(RequiredNameIsJavaPackage); |
| 477 | static_library_action.Action(RequiredAndroidAttribute("version")); |
| 478 | |
| 479 | xml::XmlNodeAction& uses_static_library_action = application_action["uses-static-library"]; |
| 480 | uses_static_library_action.Action(RequiredNameIsJavaPackage); |
| 481 | uses_static_library_action.Action(RequiredAndroidAttribute("version")); |
| 482 | uses_static_library_action.Action(RequiredAndroidAttribute("certDigest")); |
Ryan Mitchell | 66f6cfb | 2018-07-25 16:15:17 -0700 | [diff] [blame] | 483 | uses_static_library_action["additional-certificate"]; |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 484 | |
Dianne Hackborn | 813d750 | 2018-10-02 16:59:46 -0700 | [diff] [blame] | 485 | xml::XmlNodeAction& uses_package_action = application_action["uses-package"]; |
| 486 | uses_package_action.Action(RequiredNameIsJavaPackage); |
| 487 | uses_package_action["additional-certificate"]; |
| 488 | |
Ryan Mitchell | e5b38a6 | 2018-03-23 13:35:00 -0700 | [diff] [blame] | 489 | if (options_.debug_mode) { |
| 490 | application_action.Action([&](xml::Element* el) -> bool { |
| 491 | xml::Attribute *attr = el->FindOrCreateAttribute(xml::kSchemaAndroid, "debuggable"); |
| 492 | attr->value = "true"; |
| 493 | return true; |
| 494 | }); |
| 495 | } |
| 496 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 497 | application_action["meta-data"] = meta_data_action; |
Adam Lesinski | 7a917a2 | 2017-06-02 12:55:24 -0700 | [diff] [blame] | 498 | |
Dianne Hackborn | fc0839a | 2020-01-31 11:02:28 -0800 | [diff] [blame] | 499 | application_action["processes"]; |
| 500 | application_action["processes"]["deny-permission"]; |
| 501 | application_action["processes"]["allow-permission"]; |
| 502 | application_action["processes"]["process"]["deny-permission"]; |
| 503 | application_action["processes"]["process"]["allow-permission"]; |
| 504 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 505 | application_action["activity"] = component_action; |
Adam Lesinski | 7a917a2 | 2017-06-02 12:55:24 -0700 | [diff] [blame] | 506 | application_action["activity"]["layout"]; |
| 507 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 508 | application_action["activity-alias"] = component_action; |
| 509 | application_action["service"] = component_action; |
| 510 | application_action["receiver"] = component_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 511 | |
| 512 | // Provider actions. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 513 | application_action["provider"] = component_action; |
Adam Lesinski | c10c0d0 | 2017-04-28 12:54:08 -0700 | [diff] [blame] | 514 | application_action["provider"]["grant-uri-permission"]; |
Adam Lesinski | 25783ca | 2017-04-24 13:33:47 -0700 | [diff] [blame] | 515 | application_action["provider"]["path-permission"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 516 | |
Ryan Mitchell | 28afe68 | 2018-09-07 14:33:14 -0700 | [diff] [blame] | 517 | manifest_action["package"] = manifest_action; |
| 518 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 519 | return true; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 522 | static void FullyQualifyClassName(const StringPiece& package, const StringPiece& attr_ns, |
| 523 | const StringPiece& attr_name, xml::Element* el) { |
| 524 | xml::Attribute* attr = el->FindAttribute(attr_ns, attr_name); |
| 525 | if (attr != nullptr) { |
| 526 | if (Maybe<std::string> new_value = util::GetFullyQualifiedClassName(package, attr->value)) { |
| 527 | attr->value = std::move(new_value.value()); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 528 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 529 | } |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 530 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 531 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 532 | static bool RenameManifestPackage(const StringPiece& package_override, xml::Element* manifest_el) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 533 | xml::Attribute* attr = manifest_el->FindAttribute({}, "package"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 534 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 535 | // We've already verified that the manifest element is present, with a package |
| 536 | // name specified. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 537 | CHECK(attr != nullptr); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 538 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 539 | std::string original_package = std::move(attr->value); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 540 | attr->value = package_override.to_string(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 541 | |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 542 | xml::Element* application_el = manifest_el->FindChild({}, "application"); |
| 543 | if (application_el != nullptr) { |
| 544 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", application_el); |
| 545 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "backupAgent", application_el); |
| 546 | |
| 547 | for (xml::Element* child_el : application_el->GetChildElements()) { |
| 548 | if (child_el->namespace_uri.empty()) { |
| 549 | if (child_el->name == "activity" || child_el->name == "activity-alias" || |
| 550 | child_el->name == "provider" || child_el->name == "receiver" || |
| 551 | child_el->name == "service") { |
| 552 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", child_el); |
| 553 | } |
| 554 | |
| 555 | if (child_el->name == "activity-alias") { |
| 556 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "targetActivity", child_el); |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 561 | return true; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 564 | bool ManifestFixer::Consume(IAaptContext* context, xml::XmlResource* doc) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 565 | TRACE_CALL(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 566 | xml::Element* root = xml::FindRootElement(doc->root.get()); |
| 567 | if (!root || !root->namespace_uri.empty() || root->name != "manifest") { |
| 568 | context->GetDiagnostics()->Error(DiagMessage(doc->file.source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 569 | << "root tag must be <manifest>"); |
| 570 | return false; |
| 571 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 572 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 573 | if ((options_.min_sdk_version_default || options_.target_sdk_version_default) && |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 574 | root->FindChild({}, "uses-sdk") == nullptr) { |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 575 | // Auto insert a <uses-sdk> element. This must be inserted before the |
| 576 | // <application> tag. The device runtime PackageParser will make SDK version |
| 577 | // decisions while parsing <application>. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 578 | std::unique_ptr<xml::Element> uses_sdk = util::make_unique<xml::Element>(); |
| 579 | uses_sdk->name = "uses-sdk"; |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 580 | root->InsertChild(0, std::move(uses_sdk)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 581 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 582 | |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 583 | if (options_.compile_sdk_version) { |
| 584 | xml::Attribute* attr = root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersion"); |
| 585 | |
| 586 | // Make sure we un-compile the value if it was set to something else. |
| 587 | attr->compiled_value = {}; |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 588 | attr->value = options_.compile_sdk_version.value(); |
Ryan Mitchell | aada89c | 2019-02-12 08:06:26 -0800 | [diff] [blame] | 589 | |
| 590 | attr = root->FindOrCreateAttribute("", "platformBuildVersionCode"); |
| 591 | |
| 592 | // Make sure we un-compile the value if it was set to something else. |
| 593 | attr->compiled_value = {}; |
| 594 | attr->value = options_.compile_sdk_version.value(); |
| 595 | |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | if (options_.compile_sdk_version_codename) { |
| 599 | xml::Attribute* attr = |
| 600 | root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename"); |
| 601 | |
| 602 | // Make sure we un-compile the value if it was set to something else. |
| 603 | attr->compiled_value = {}; |
Ryan Mitchell | aada89c | 2019-02-12 08:06:26 -0800 | [diff] [blame] | 604 | attr->value = options_.compile_sdk_version_codename.value(); |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 605 | |
Ryan Mitchell | aada89c | 2019-02-12 08:06:26 -0800 | [diff] [blame] | 606 | attr = root->FindOrCreateAttribute("", "platformBuildVersionName"); |
| 607 | |
| 608 | // Make sure we un-compile the value if it was set to something else. |
| 609 | attr->compiled_value = {}; |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 610 | attr->value = options_.compile_sdk_version_codename.value(); |
| 611 | } |
| 612 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 613 | xml::XmlActionExecutor executor; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 614 | if (!BuildRules(&executor, context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 615 | return false; |
| 616 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 617 | |
Izabela Orlowska | ad9e132 | 2017-12-19 16:22:42 +0000 | [diff] [blame] | 618 | xml::XmlActionExecutorPolicy policy = options_.warn_validation |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 619 | ? xml::XmlActionExecutorPolicy::kAllowListWarning |
| 620 | : xml::XmlActionExecutorPolicy::kAllowList; |
Izabela Orlowska | ad9e132 | 2017-12-19 16:22:42 +0000 | [diff] [blame] | 621 | if (!executor.Execute(policy, context->GetDiagnostics(), doc)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 622 | return false; |
| 623 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 624 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 625 | if (options_.rename_manifest_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 626 | // Rename manifest package outside of the XmlActionExecutor. |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 627 | // We need to extract the old package name and FullyQualify all class |
| 628 | // names. |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 629 | if (!RenameManifestPackage(options_.rename_manifest_package.value(), root)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 630 | return false; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 631 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 632 | } |
| 633 | return true; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 636 | } // namespace aapt |