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 | |
| 17 | #include "ResourceUtils.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 18 | #include "link/ManifestFixer.h" |
| 19 | #include "util/Util.h" |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 20 | #include "xml/XmlActionExecutor.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 21 | #include "xml/XmlDom.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 22 | |
Adam Lesinski | 71965e8 | 2016-07-07 17:12:12 -0700 | [diff] [blame] | 23 | #include <unordered_set> |
| 24 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 25 | namespace aapt { |
| 26 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 27 | /** |
| 28 | * This is how PackageManager builds class names from AndroidManifest.xml entries. |
| 29 | */ |
| 30 | static bool nameIsJavaClassName(xml::Element* el, xml::Attribute* attr, |
| 31 | SourcePathDiagnostics* diag) { |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 32 | // We allow unqualified class names (ie: .HelloActivity) |
| 33 | // Since we don't know the package name, we can just make a fake one here and |
| 34 | // the test will be identical as long as the real package name is valid too. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 35 | Maybe<std::string> fullyQualifiedClassName = |
| 36 | util::getFullyQualifiedClassName("a", attr->value); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 37 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 38 | StringPiece qualifiedClassName = fullyQualifiedClassName |
Adam Lesinski | 71965e8 | 2016-07-07 17:12:12 -0700 | [diff] [blame] | 39 | ? fullyQualifiedClassName.value() : attr->value; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 41 | if (!util::isJavaClassName(qualifiedClassName)) { |
| 42 | diag->error(DiagMessage(el->lineNumber) |
| 43 | << "attribute 'android:name' in <" |
| 44 | << el->name << "> tag must be a valid Java class name"); |
| 45 | return false; |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | static bool optionalNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 51 | if (xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, "name")) { |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 52 | return nameIsJavaClassName(el, attr, diag); |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | static bool requiredNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 58 | if (xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, "name")) { |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 59 | return nameIsJavaClassName(el, attr, diag); |
| 60 | } |
| 61 | diag->error(DiagMessage(el->lineNumber) |
| 62 | << "<" << el->name << "> is missing attribute 'android:name'"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 63 | return false; |
| 64 | } |
| 65 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 66 | static bool verifyManifest(xml::Element* el, SourcePathDiagnostics* diag) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 67 | xml::Attribute* attr = el->findAttribute({}, "package"); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 68 | if (!attr) { |
| 69 | diag->error(DiagMessage(el->lineNumber) << "<manifest> tag is missing 'package' attribute"); |
| 70 | return false; |
| 71 | } else if (ResourceUtils::isReference(attr->value)) { |
| 72 | diag->error(DiagMessage(el->lineNumber) |
| 73 | << "attribute 'package' in <manifest> tag must not be a reference"); |
| 74 | return false; |
| 75 | } else if (!util::isJavaPackageName(attr->value)) { |
| 76 | diag->error(DiagMessage(el->lineNumber) |
| 77 | << "attribute 'package' in <manifest> tag is not a valid Java package name: '" |
| 78 | << attr->value << "'"); |
| 79 | return false; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 80 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 84 | /** |
| 85 | * The coreApp attribute in <manifest> is not a regular AAPT attribute, so type checking on it |
| 86 | * is manual. |
| 87 | */ |
| 88 | static bool fixCoreAppAttribute(xml::Element* el, SourcePathDiagnostics* diag) { |
| 89 | if (xml::Attribute* attr = el->findAttribute("", "coreApp")) { |
| 90 | std::unique_ptr<BinaryPrimitive> result = ResourceUtils::tryParseBool(attr->value); |
| 91 | if (!result) { |
| 92 | diag->error(DiagMessage(el->lineNumber) << "attribute coreApp must be a boolean"); |
| 93 | return false; |
| 94 | } |
| 95 | attr->compiledValue = std::move(result); |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 100 | bool ManifestFixer::buildRules(xml::XmlActionExecutor* executor, IDiagnostics* diag) { |
| 101 | // First verify some options. |
| 102 | if (mOptions.renameManifestPackage) { |
| 103 | if (!util::isJavaPackageName(mOptions.renameManifestPackage.value())) { |
| 104 | diag->error(DiagMessage() << "invalid manifest package override '" |
| 105 | << mOptions.renameManifestPackage.value() << "'"); |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (mOptions.renameInstrumentationTargetPackage) { |
| 111 | if (!util::isJavaPackageName(mOptions.renameInstrumentationTargetPackage.value())) { |
| 112 | diag->error(DiagMessage() << "invalid instrumentation target package override '" |
| 113 | << mOptions.renameInstrumentationTargetPackage.value() << "'"); |
| 114 | return false; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Common intent-filter actions. |
| 119 | xml::XmlNodeAction intentFilterAction; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 120 | intentFilterAction["action"]; |
| 121 | intentFilterAction["category"]; |
| 122 | intentFilterAction["data"]; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 123 | |
| 124 | // Common meta-data actions. |
| 125 | xml::XmlNodeAction metaDataAction; |
| 126 | |
| 127 | // Manifest actions. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 128 | xml::XmlNodeAction& manifestAction = (*executor)["manifest"]; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 129 | manifestAction.action(verifyManifest); |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 130 | manifestAction.action(fixCoreAppAttribute); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 131 | manifestAction.action([&](xml::Element* el) -> bool { |
| 132 | if (mOptions.versionNameDefault) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 133 | if (el->findAttribute(xml::kSchemaAndroid, "versionName") == nullptr) { |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 134 | el->attributes.push_back(xml::Attribute{ |
| 135 | xml::kSchemaAndroid, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 136 | "versionName", |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 137 | mOptions.versionNameDefault.value() }); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (mOptions.versionCodeDefault) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 142 | if (el->findAttribute(xml::kSchemaAndroid, "versionCode") == nullptr) { |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 143 | el->attributes.push_back(xml::Attribute{ |
| 144 | xml::kSchemaAndroid, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 145 | "versionCode", |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 146 | mOptions.versionCodeDefault.value() }); |
| 147 | } |
| 148 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 149 | return true; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 150 | }); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 151 | |
Adam Lesinski | 5ff3ad6 | 2016-04-13 20:30:45 -0700 | [diff] [blame] | 152 | // Meta tags. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 153 | manifestAction["eat-comment"]; |
Adam Lesinski | 5ff3ad6 | 2016-04-13 20:30:45 -0700 | [diff] [blame] | 154 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 155 | // Uses-sdk actions. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 156 | manifestAction["uses-sdk"].action([&](xml::Element* el) -> bool { |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 157 | if (mOptions.minSdkVersionDefault && |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 158 | el->findAttribute(xml::kSchemaAndroid, "minSdkVersion") == nullptr) { |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 159 | // There was no minSdkVersion defined and we have a default to assign. |
| 160 | el->attributes.push_back(xml::Attribute{ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 161 | xml::kSchemaAndroid, "minSdkVersion", |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 162 | mOptions.minSdkVersionDefault.value() }); |
| 163 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 164 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 165 | if (mOptions.targetSdkVersionDefault && |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 166 | el->findAttribute(xml::kSchemaAndroid, "targetSdkVersion") == nullptr) { |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 167 | // There was no targetSdkVersion defined and we have a default to assign. |
| 168 | el->attributes.push_back(xml::Attribute{ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 169 | xml::kSchemaAndroid, "targetSdkVersion", |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 170 | mOptions.targetSdkVersionDefault.value() }); |
| 171 | } |
| 172 | return true; |
| 173 | }); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 174 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 175 | // Instrumentation actions. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 176 | manifestAction["instrumentation"].action([&](xml::Element* el) -> bool { |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 177 | if (!mOptions.renameInstrumentationTargetPackage) { |
| 178 | return true; |
| 179 | } |
| 180 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 181 | if (xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, "targetPackage")) { |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 182 | attr->value = mOptions.renameInstrumentationTargetPackage.value(); |
| 183 | } |
| 184 | return true; |
| 185 | }); |
| 186 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 187 | manifestAction["original-package"]; |
| 188 | manifestAction["protected-broadcast"]; |
| 189 | manifestAction["uses-permission"]; |
| 190 | manifestAction["permission"]; |
| 191 | manifestAction["permission-tree"]; |
| 192 | manifestAction["permission-group"]; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 193 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 194 | manifestAction["uses-configuration"]; |
| 195 | manifestAction["uses-feature"]; |
| 196 | manifestAction["supports-screens"]; |
Adam Lesinski | a0b929d | 2016-09-19 09:50:45 -0700 | [diff] [blame^] | 197 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 198 | manifestAction["compatible-screens"]; |
Adam Lesinski | a0b929d | 2016-09-19 09:50:45 -0700 | [diff] [blame^] | 199 | manifestAction["compatible-screens"]["screen"]; |
| 200 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 201 | manifestAction["supports-gl-texture"]; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 202 | |
| 203 | // Application actions. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 204 | xml::XmlNodeAction& applicationAction = manifestAction["application"]; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 205 | applicationAction.action(optionalNameIsJavaClassName); |
| 206 | |
Adam Lesinski | fee32d4 | 2016-05-31 15:07:20 -0700 | [diff] [blame] | 207 | // Uses library actions. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 208 | applicationAction["uses-library"]; |
Adam Lesinski | fee32d4 | 2016-05-31 15:07:20 -0700 | [diff] [blame] | 209 | |
Adam Lesinski | 5d84ad5 | 2016-06-23 13:18:16 -0700 | [diff] [blame] | 210 | // Meta-data. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 211 | applicationAction["meta-data"] = metaDataAction; |
Adam Lesinski | 5d84ad5 | 2016-06-23 13:18:16 -0700 | [diff] [blame] | 212 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 213 | // Activity actions. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 214 | applicationAction["activity"].action(requiredNameIsJavaClassName); |
| 215 | applicationAction["activity"]["intent-filter"] = intentFilterAction; |
| 216 | applicationAction["activity"]["meta-data"] = metaDataAction; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 217 | |
| 218 | // Activity alias actions. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 219 | applicationAction["activity-alias"]["intent-filter"] = intentFilterAction; |
| 220 | applicationAction["activity-alias"]["meta-data"] = metaDataAction; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 221 | |
| 222 | // Service actions. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 223 | applicationAction["service"].action(requiredNameIsJavaClassName); |
| 224 | applicationAction["service"]["intent-filter"] = intentFilterAction; |
| 225 | applicationAction["service"]["meta-data"] = metaDataAction; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 226 | |
| 227 | // Receiver actions. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 228 | applicationAction["receiver"].action(requiredNameIsJavaClassName); |
| 229 | applicationAction["receiver"]["intent-filter"] = intentFilterAction; |
| 230 | applicationAction["receiver"]["meta-data"] = metaDataAction; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 231 | |
| 232 | // Provider actions. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 233 | applicationAction["provider"].action(requiredNameIsJavaClassName); |
Adam Lesinski | bb5a390 | 2016-08-01 15:01:08 -0700 | [diff] [blame] | 234 | applicationAction["provider"]["intent-filter"] = intentFilterAction; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 235 | applicationAction["provider"]["meta-data"] = metaDataAction; |
Adam Lesinski | bb5a390 | 2016-08-01 15:01:08 -0700 | [diff] [blame] | 236 | applicationAction["provider"]["grant-uri-permissions"]; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 237 | applicationAction["provider"]["path-permissions"]; |
Adam Lesinski | bb5a390 | 2016-08-01 15:01:08 -0700 | [diff] [blame] | 238 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 239 | return true; |
| 240 | } |
| 241 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 242 | class FullyQualifiedClassNameVisitor : public xml::Visitor { |
| 243 | public: |
| 244 | using xml::Visitor::visit; |
| 245 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 246 | explicit FullyQualifiedClassNameVisitor(const StringPiece& package) : mPackage(package) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void visit(xml::Element* el) override { |
| 250 | for (xml::Attribute& attr : el->attributes) { |
Adam Lesinski | 71965e8 | 2016-07-07 17:12:12 -0700 | [diff] [blame] | 251 | if (attr.namespaceUri == xml::kSchemaAndroid |
| 252 | && mClassAttributes.find(attr.name) != mClassAttributes.end()) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 253 | if (Maybe<std::string> newValue = |
Adam Lesinski | 71965e8 | 2016-07-07 17:12:12 -0700 | [diff] [blame] | 254 | util::getFullyQualifiedClassName(mPackage, attr.value)) { |
| 255 | attr.value = std::move(newValue.value()); |
| 256 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | // Super implementation to iterate over the children. |
| 261 | xml::Visitor::visit(el); |
| 262 | } |
| 263 | |
| 264 | private: |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 265 | StringPiece mPackage; |
| 266 | std::unordered_set<StringPiece> mClassAttributes = { "name" }; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 267 | }; |
| 268 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 269 | static bool renameManifestPackage(const StringPiece& packageOverride, xml::Element* manifestEl) { |
| 270 | xml::Attribute* attr = manifestEl->findAttribute({}, "package"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 271 | |
| 272 | // We've already verified that the manifest element is present, with a package name specified. |
| 273 | assert(attr); |
| 274 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 275 | std::string originalPackage = std::move(attr->value); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 276 | attr->value = packageOverride.toString(); |
| 277 | |
| 278 | FullyQualifiedClassNameVisitor visitor(originalPackage); |
| 279 | manifestEl->accept(&visitor); |
| 280 | return true; |
| 281 | } |
| 282 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 283 | bool ManifestFixer::consume(IAaptContext* context, xml::XmlResource* doc) { |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 284 | xml::Element* root = xml::findRootElement(doc->root.get()); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 285 | if (!root || !root->namespaceUri.empty() || root->name != "manifest") { |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 286 | context->getDiagnostics()->error(DiagMessage(doc->file.source) |
| 287 | << "root tag must be <manifest>"); |
| 288 | return false; |
| 289 | } |
| 290 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 291 | if ((mOptions.minSdkVersionDefault || mOptions.targetSdkVersionDefault) |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 292 | && root->findChild({}, "uses-sdk") == nullptr) { |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 293 | // Auto insert a <uses-sdk> element. |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 294 | std::unique_ptr<xml::Element> usesSdk = util::make_unique<xml::Element>(); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 295 | usesSdk->name = "uses-sdk"; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 296 | root->addChild(std::move(usesSdk)); |
| 297 | } |
| 298 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 299 | xml::XmlActionExecutor executor; |
| 300 | if (!buildRules(&executor, context->getDiagnostics())) { |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | if (!executor.execute(xml::XmlActionExecutorPolicy::Whitelist, context->getDiagnostics(), |
| 305 | doc)) { |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | if (mOptions.renameManifestPackage) { |
| 310 | // Rename manifest package outside of the XmlActionExecutor. |
| 311 | // We need to extract the old package name and FullyQualify all class names. |
| 312 | if (!renameManifestPackage(mOptions.renameManifestPackage.value(), root)) { |
| 313 | return false; |
| 314 | } |
| 315 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 316 | return true; |
| 317 | } |
| 318 | |
| 319 | } // namespace aapt |