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" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 24 | #include "util/Util.h" |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 25 | #include "xml/XmlActionExecutor.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 26 | #include "xml/XmlDom.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 27 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 28 | using android::StringPiece; |
| 29 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 30 | namespace aapt { |
| 31 | |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 32 | static bool RequiredNameIsNotEmpty(xml::Element* el, SourcePathDiagnostics* diag) { |
| 33 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name"); |
| 34 | if (attr == nullptr) { |
| 35 | diag->Error(DiagMessage(el->line_number) |
| 36 | << "<" << el->name << "> is missing attribute 'android:name'"); |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | if (attr->value.empty()) { |
| 41 | diag->Error(DiagMessage(el->line_number) |
| 42 | << "attribute 'android:name' in <" << el->name << "> tag must not be empty"); |
| 43 | return false; |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 48 | // This is how PackageManager builds class names from AndroidManifest.xml entries. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | static bool NameIsJavaClassName(xml::Element* el, xml::Attribute* attr, |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 50 | SourcePathDiagnostics* diag) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | // We allow unqualified class names (ie: .HelloActivity) |
| 52 | // Since we don't know the package name, we can just make a fake one here and |
| 53 | // 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] | 54 | Maybe<std::string> fully_qualified_class_name = |
| 55 | util::GetFullyQualifiedClassName("a", attr->value); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 56 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | StringPiece qualified_class_name = fully_qualified_class_name |
| 58 | ? fully_qualified_class_name.value() |
| 59 | : attr->value; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | if (!util::IsJavaClassName(qualified_class_name)) { |
| 62 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | << "attribute 'android:name' in <" << el->name |
| 64 | << "> tag must be a valid Java class name"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 65 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 70 | static bool OptionalNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) { |
| 72 | return NameIsJavaClassName(el, attr, diag); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 77 | static bool RequiredNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) { |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 78 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name"); |
| 79 | if (attr == nullptr) { |
| 80 | diag->Error(DiagMessage(el->line_number) |
| 81 | << "<" << el->name << "> is missing attribute 'android:name'"); |
| 82 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | } |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 84 | return NameIsJavaClassName(el, attr, diag); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 87 | static bool RequiredNameIsJavaPackage(xml::Element* el, SourcePathDiagnostics* diag) { |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 88 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name"); |
| 89 | if (attr == nullptr) { |
| 90 | diag->Error(DiagMessage(el->line_number) |
| 91 | << "<" << el->name << "> is missing attribute 'android:name'"); |
| 92 | return false; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 93 | } |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 94 | |
| 95 | if (!util::IsJavaPackageName(attr->value)) { |
| 96 | diag->Error(DiagMessage(el->line_number) << "attribute 'android:name' in <" << el->name |
| 97 | << "> tag must be a valid Java package name"); |
| 98 | return false; |
| 99 | } |
| 100 | return true; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 103 | static xml::XmlNodeAction::ActionFuncWithDiag RequiredAndroidAttribute(const std::string& attr) { |
| 104 | return [=](xml::Element* el, SourcePathDiagnostics* diag) -> bool { |
| 105 | if (el->FindAttribute(xml::kSchemaAndroid, attr) == nullptr) { |
| 106 | diag->Error(DiagMessage(el->line_number) |
| 107 | << "<" << el->name << "> is missing required attribute 'android:" << attr << "'"); |
| 108 | return false; |
| 109 | } |
| 110 | return true; |
| 111 | }; |
| 112 | } |
| 113 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 114 | static bool AutoGenerateIsFeatureSplit(xml::Element* el, SourcePathDiagnostics* diag) { |
| 115 | constexpr const char* kFeatureSplit = "featureSplit"; |
| 116 | constexpr const char* kIsFeatureSplit = "isFeatureSplit"; |
| 117 | |
| 118 | xml::Attribute* attr = el->FindAttribute({}, kFeatureSplit); |
| 119 | if (attr != nullptr) { |
| 120 | // Rewrite the featureSplit attribute to be "split". This is what the |
| 121 | // platform recognizes. |
| 122 | attr->name = "split"; |
| 123 | |
| 124 | // Now inject the android:isFeatureSplit="true" attribute. |
| 125 | xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, kIsFeatureSplit); |
| 126 | if (attr != nullptr) { |
| 127 | if (!ResourceUtils::ParseBool(attr->value).value_or_default(false)) { |
| 128 | // The isFeatureSplit attribute is false, which conflicts with the use |
| 129 | // of "featureSplit". |
| 130 | diag->Error(DiagMessage(el->line_number) |
| 131 | << "attribute 'featureSplit' used in <manifest> but 'android:isFeatureSplit' " |
| 132 | "is not 'true'"); |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | // The attribute is already there and set to true, nothing to do. |
| 137 | } else { |
| 138 | el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, kIsFeatureSplit, "true"}); |
| 139 | } |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 144 | static bool VerifyManifest(xml::Element* el, SourcePathDiagnostics* diag) { |
| 145 | xml::Attribute* attr = el->FindAttribute({}, "package"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 146 | if (!attr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 147 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 148 | << "<manifest> tag is missing 'package' attribute"); |
| 149 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 150 | } else if (ResourceUtils::IsReference(attr->value)) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 151 | diag->Error(DiagMessage(el->line_number) |
| 152 | << "attribute 'package' in <manifest> tag must not be a reference"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | return false; |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 154 | } else if (!util::IsAndroidPackageName(attr->value)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 156 | << "attribute 'package' in <manifest> tag is not a valid Android package name: '" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 157 | << attr->value << "'"); |
| 158 | return false; |
| 159 | } |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 160 | |
| 161 | attr = el->FindAttribute({}, "split"); |
| 162 | if (attr) { |
| 163 | if (!util::IsJavaPackageName(attr->value)) { |
| 164 | diag->Error(DiagMessage(el->line_number) << "attribute 'split' in <manifest> tag is not a " |
| 165 | "valid split name"); |
| 166 | return false; |
| 167 | } |
| 168 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 169 | return true; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 172 | // The coreApp attribute in <manifest> is not a regular AAPT attribute, so type |
| 173 | // checking on it is manual. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 174 | static bool FixCoreAppAttribute(xml::Element* el, SourcePathDiagnostics* diag) { |
| 175 | if (xml::Attribute* attr = el->FindAttribute("", "coreApp")) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 176 | std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseBool(attr->value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 177 | if (!result) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 178 | diag->Error(DiagMessage(el->line_number) << "attribute coreApp must be a boolean"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | return false; |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 180 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 181 | attr->compiled_value = std::move(result); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | } |
| 183 | return true; |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 186 | // Checks that <uses-feature> has android:glEsVersion or android:name, not both (or neither). |
| 187 | static bool VerifyUsesFeature(xml::Element* el, SourcePathDiagnostics* diag) { |
| 188 | bool has_name = false; |
| 189 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) { |
| 190 | if (attr->value.empty()) { |
| 191 | diag->Error(DiagMessage(el->line_number) |
| 192 | << "android:name in <uses-feature> must not be empty"); |
| 193 | return false; |
| 194 | } |
| 195 | has_name = true; |
| 196 | } |
| 197 | |
| 198 | bool has_gl_es_version = false; |
| 199 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "glEsVersion")) { |
| 200 | if (has_name) { |
| 201 | diag->Error(DiagMessage(el->line_number) |
| 202 | << "cannot define both android:name and android:glEsVersion in <uses-feature>"); |
| 203 | return false; |
| 204 | } |
| 205 | has_gl_es_version = true; |
| 206 | } |
| 207 | |
| 208 | if (!has_name && !has_gl_es_version) { |
| 209 | diag->Error(DiagMessage(el->line_number) |
| 210 | << "<uses-feature> must have either android:name or android:glEsVersion attribute"); |
| 211 | return false; |
| 212 | } |
| 213 | return true; |
| 214 | } |
| 215 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 216 | bool ManifestFixer::BuildRules(xml::XmlActionExecutor* executor, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 217 | IDiagnostics* diag) { |
| 218 | // First verify some options. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 219 | if (options_.rename_manifest_package) { |
| 220 | if (!util::IsJavaPackageName(options_.rename_manifest_package.value())) { |
| 221 | diag->Error(DiagMessage() << "invalid manifest package override '" |
| 222 | << options_.rename_manifest_package.value() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 223 | << "'"); |
| 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 228 | if (options_.rename_instrumentation_target_package) { |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 229 | if (!util::IsJavaPackageName(options_.rename_instrumentation_target_package.value())) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 230 | diag->Error(DiagMessage() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 231 | << "invalid instrumentation target package override '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | << options_.rename_instrumentation_target_package.value() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 233 | << "'"); |
| 234 | return false; |
| 235 | } |
| 236 | } |
| 237 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 238 | // Common <intent-filter> actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 239 | xml::XmlNodeAction intent_filter_action; |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 240 | intent_filter_action["action"].Action(RequiredNameIsNotEmpty); |
| 241 | intent_filter_action["category"].Action(RequiredNameIsNotEmpty); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 242 | intent_filter_action["data"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 243 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 244 | // Common <meta-data> actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 245 | xml::XmlNodeAction meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 246 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 247 | // Common <uses-feature> actions. |
| 248 | xml::XmlNodeAction uses_feature_action; |
| 249 | uses_feature_action.Action(VerifyUsesFeature); |
| 250 | |
| 251 | // Common component actions. |
| 252 | xml::XmlNodeAction component_action; |
| 253 | component_action.Action(RequiredNameIsJavaClassName); |
| 254 | component_action["intent-filter"] = intent_filter_action; |
| 255 | component_action["meta-data"] = meta_data_action; |
| 256 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 257 | // Manifest actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 258 | xml::XmlNodeAction& manifest_action = (*executor)["manifest"]; |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 259 | manifest_action.Action(AutoGenerateIsFeatureSplit); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | manifest_action.Action(VerifyManifest); |
| 261 | manifest_action.Action(FixCoreAppAttribute); |
| 262 | manifest_action.Action([&](xml::Element* el) -> bool { |
| 263 | if (options_.version_name_default) { |
| 264 | if (el->FindAttribute(xml::kSchemaAndroid, "versionName") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 265 | el->attributes.push_back( |
| 266 | xml::Attribute{xml::kSchemaAndroid, "versionName", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 267 | options_.version_name_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 268 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 271 | if (options_.version_code_default) { |
| 272 | if (el->FindAttribute(xml::kSchemaAndroid, "versionCode") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | el->attributes.push_back( |
| 274 | xml::Attribute{xml::kSchemaAndroid, "versionCode", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 275 | options_.version_code_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 276 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 277 | } |
Ryan Mitchell | 7cb82a8 | 2018-05-10 15:35:31 -0700 | [diff] [blame] | 278 | |
| 279 | if (el->FindAttribute("", "platformBuildVersionCode") == nullptr) { |
| 280 | auto versionCode = el->FindAttribute(xml::kSchemaAndroid, "versionCode"); |
| 281 | if (versionCode != nullptr) { |
| 282 | el->attributes.push_back(xml::Attribute{"", "platformBuildVersionCode", |
| 283 | versionCode->value}); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | if (el->FindAttribute("", "platformBuildVersionName") == nullptr) { |
| 288 | auto versionName = el->FindAttribute(xml::kSchemaAndroid, "versionName"); |
| 289 | if (versionName != nullptr) { |
| 290 | el->attributes.push_back(xml::Attribute{"", "platformBuildVersionName", |
| 291 | versionName->value}); |
| 292 | } |
| 293 | } |
| 294 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 295 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 296 | }); |
| 297 | |
| 298 | // Meta tags. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 299 | manifest_action["eat-comment"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 300 | |
| 301 | // Uses-sdk actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 302 | manifest_action["uses-sdk"].Action([&](xml::Element* el) -> bool { |
| 303 | if (options_.min_sdk_version_default && |
| 304 | el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 305 | // There was no minSdkVersion defined and we have a default to assign. |
| 306 | el->attributes.push_back( |
| 307 | xml::Attribute{xml::kSchemaAndroid, "minSdkVersion", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 308 | options_.min_sdk_version_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 311 | if (options_.target_sdk_version_default && |
| 312 | el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 313 | // There was no targetSdkVersion defined and we have a default to assign. |
| 314 | el->attributes.push_back( |
| 315 | xml::Attribute{xml::kSchemaAndroid, "targetSdkVersion", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 316 | options_.target_sdk_version_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 317 | } |
| 318 | return true; |
| 319 | }); |
| 320 | |
| 321 | // Instrumentation actions. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 322 | manifest_action["instrumentation"].Action(RequiredNameIsJavaClassName); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 323 | manifest_action["instrumentation"].Action([&](xml::Element* el) -> bool { |
| 324 | if (!options_.rename_instrumentation_target_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 325 | return true; |
| 326 | } |
| 327 | |
| 328 | if (xml::Attribute* attr = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 329 | el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) { |
| 330 | attr->value = options_.rename_instrumentation_target_package.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 331 | } |
| 332 | return true; |
| 333 | }); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 334 | manifest_action["instrumentation"]["meta-data"] = meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 335 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 336 | manifest_action["original-package"]; |
MÃ¥rten Kongstad | c903d2e | 2016-12-09 00:23:41 +0100 | [diff] [blame] | 337 | manifest_action["overlay"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 338 | manifest_action["protected-broadcast"]; |
Alan Viverette | cf5326f | 2018-01-05 16:03:50 -0500 | [diff] [blame] | 339 | manifest_action["adopt-permissions"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 340 | manifest_action["uses-permission"]; |
Adam Lesinski | 4b585db | 2017-05-12 15:25:50 -0700 | [diff] [blame] | 341 | manifest_action["uses-permission-sdk-23"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 342 | manifest_action["permission"]; |
| 343 | manifest_action["permission-tree"]; |
| 344 | manifest_action["permission-group"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 345 | manifest_action["uses-configuration"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 346 | manifest_action["supports-screens"]; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 347 | manifest_action["uses-feature"] = uses_feature_action; |
| 348 | manifest_action["feature-group"]["uses-feature"] = uses_feature_action; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 349 | manifest_action["compatible-screens"]; |
| 350 | manifest_action["compatible-screens"]["screen"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 351 | manifest_action["supports-gl-texture"]; |
Adam Lesinski | 5119e51 | 2016-12-05 19:48:20 -0800 | [diff] [blame] | 352 | manifest_action["meta-data"] = meta_data_action; |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 353 | manifest_action["uses-split"].Action(RequiredNameIsJavaPackage); |
Adam Lesinski | 5119e51 | 2016-12-05 19:48:20 -0800 | [diff] [blame] | 354 | |
Adam Lesinski | 87f1e0f | 2017-06-27 16:21:58 -0700 | [diff] [blame] | 355 | manifest_action["key-sets"]["key-set"]["public-key"]; |
| 356 | manifest_action["key-sets"]["upgrade-key-set"]; |
| 357 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 358 | // Application actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 359 | xml::XmlNodeAction& application_action = manifest_action["application"]; |
| 360 | application_action.Action(OptionalNameIsJavaClassName); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 361 | |
Adam Lesinski | fca5e42 | 2017-12-20 15:03:36 -0800 | [diff] [blame] | 362 | application_action["uses-library"].Action(RequiredNameIsNotEmpty); |
| 363 | application_action["library"].Action(RequiredNameIsNotEmpty); |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 364 | |
| 365 | xml::XmlNodeAction& static_library_action = application_action["static-library"]; |
| 366 | static_library_action.Action(RequiredNameIsJavaPackage); |
| 367 | static_library_action.Action(RequiredAndroidAttribute("version")); |
| 368 | |
| 369 | xml::XmlNodeAction& uses_static_library_action = application_action["uses-static-library"]; |
| 370 | uses_static_library_action.Action(RequiredNameIsJavaPackage); |
| 371 | uses_static_library_action.Action(RequiredAndroidAttribute("version")); |
| 372 | uses_static_library_action.Action(RequiredAndroidAttribute("certDigest")); |
| 373 | |
Ryan Mitchell | e5b38a6 | 2018-03-23 13:35:00 -0700 | [diff] [blame] | 374 | if (options_.debug_mode) { |
| 375 | application_action.Action([&](xml::Element* el) -> bool { |
| 376 | xml::Attribute *attr = el->FindOrCreateAttribute(xml::kSchemaAndroid, "debuggable"); |
| 377 | attr->value = "true"; |
| 378 | return true; |
| 379 | }); |
| 380 | } |
| 381 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 382 | application_action["meta-data"] = meta_data_action; |
Adam Lesinski | 7a917a2 | 2017-06-02 12:55:24 -0700 | [diff] [blame] | 383 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 384 | application_action["activity"] = component_action; |
Adam Lesinski | 7a917a2 | 2017-06-02 12:55:24 -0700 | [diff] [blame] | 385 | application_action["activity"]["layout"]; |
| 386 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 387 | application_action["activity-alias"] = component_action; |
| 388 | application_action["service"] = component_action; |
| 389 | application_action["receiver"] = component_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 390 | |
| 391 | // Provider actions. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 392 | application_action["provider"] = component_action; |
Adam Lesinski | c10c0d0 | 2017-04-28 12:54:08 -0700 | [diff] [blame] | 393 | application_action["provider"]["grant-uri-permission"]; |
Adam Lesinski | 25783ca | 2017-04-24 13:33:47 -0700 | [diff] [blame] | 394 | application_action["provider"]["path-permission"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 395 | |
| 396 | return true; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 399 | static void FullyQualifyClassName(const StringPiece& package, const StringPiece& attr_ns, |
| 400 | const StringPiece& attr_name, xml::Element* el) { |
| 401 | xml::Attribute* attr = el->FindAttribute(attr_ns, attr_name); |
| 402 | if (attr != nullptr) { |
| 403 | if (Maybe<std::string> new_value = util::GetFullyQualifiedClassName(package, attr->value)) { |
| 404 | attr->value = std::move(new_value.value()); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 405 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 406 | } |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 407 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 408 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 409 | static bool RenameManifestPackage(const StringPiece& package_override, xml::Element* manifest_el) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 410 | xml::Attribute* attr = manifest_el->FindAttribute({}, "package"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 411 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 412 | // We've already verified that the manifest element is present, with a package |
| 413 | // name specified. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 414 | CHECK(attr != nullptr); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 415 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 416 | std::string original_package = std::move(attr->value); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 417 | attr->value = package_override.to_string(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 418 | |
Adam Lesinski | 23034b9 | 2017-11-29 16:27:44 -0800 | [diff] [blame] | 419 | xml::Element* application_el = manifest_el->FindChild({}, "application"); |
| 420 | if (application_el != nullptr) { |
| 421 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", application_el); |
| 422 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "backupAgent", application_el); |
| 423 | |
| 424 | for (xml::Element* child_el : application_el->GetChildElements()) { |
| 425 | if (child_el->namespace_uri.empty()) { |
| 426 | if (child_el->name == "activity" || child_el->name == "activity-alias" || |
| 427 | child_el->name == "provider" || child_el->name == "receiver" || |
| 428 | child_el->name == "service") { |
| 429 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "name", child_el); |
| 430 | } |
| 431 | |
| 432 | if (child_el->name == "activity-alias") { |
| 433 | FullyQualifyClassName(original_package, xml::kSchemaAndroid, "targetActivity", child_el); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 438 | return true; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 441 | bool ManifestFixer::Consume(IAaptContext* context, xml::XmlResource* doc) { |
| 442 | xml::Element* root = xml::FindRootElement(doc->root.get()); |
| 443 | if (!root || !root->namespace_uri.empty() || root->name != "manifest") { |
| 444 | context->GetDiagnostics()->Error(DiagMessage(doc->file.source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 445 | << "root tag must be <manifest>"); |
| 446 | return false; |
| 447 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 448 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 449 | if ((options_.min_sdk_version_default || options_.target_sdk_version_default) && |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 450 | root->FindChild({}, "uses-sdk") == nullptr) { |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 451 | // Auto insert a <uses-sdk> element. This must be inserted before the |
| 452 | // <application> tag. The device runtime PackageParser will make SDK version |
| 453 | // decisions while parsing <application>. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 454 | std::unique_ptr<xml::Element> uses_sdk = util::make_unique<xml::Element>(); |
| 455 | uses_sdk->name = "uses-sdk"; |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 456 | root->InsertChild(0, std::move(uses_sdk)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 457 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 458 | |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 459 | if (options_.compile_sdk_version) { |
| 460 | xml::Attribute* attr = root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersion"); |
| 461 | |
| 462 | // Make sure we un-compile the value if it was set to something else. |
| 463 | attr->compiled_value = {}; |
| 464 | |
| 465 | attr->value = options_.compile_sdk_version.value(); |
| 466 | } |
| 467 | |
| 468 | if (options_.compile_sdk_version_codename) { |
| 469 | xml::Attribute* attr = |
| 470 | root->FindOrCreateAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename"); |
| 471 | |
| 472 | // Make sure we un-compile the value if it was set to something else. |
| 473 | attr->compiled_value = {}; |
| 474 | |
| 475 | attr->value = options_.compile_sdk_version_codename.value(); |
| 476 | } |
| 477 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 478 | xml::XmlActionExecutor executor; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 479 | if (!BuildRules(&executor, context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 480 | return false; |
| 481 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 482 | |
Izabela Orlowska | ad9e132 | 2017-12-19 16:22:42 +0000 | [diff] [blame] | 483 | xml::XmlActionExecutorPolicy policy = options_.warn_validation |
| 484 | ? xml::XmlActionExecutorPolicy::kWhitelistWarning |
| 485 | : xml::XmlActionExecutorPolicy::kWhitelist; |
| 486 | if (!executor.Execute(policy, context->GetDiagnostics(), doc)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 487 | return false; |
| 488 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 489 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 490 | if (options_.rename_manifest_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 491 | // Rename manifest package outside of the XmlActionExecutor. |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 492 | // We need to extract the old package name and FullyQualify all class |
| 493 | // names. |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 494 | if (!RenameManifestPackage(options_.rename_manifest_package.value(), root)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 495 | return false; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 496 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 497 | } |
| 498 | return true; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 501 | } // namespace aapt |