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 | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 32 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 33 | * This is how PackageManager builds class names from AndroidManifest.xml |
| 34 | * entries. |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 35 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | static bool NameIsJavaClassName(xml::Element* el, xml::Attribute* attr, |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 37 | SourcePathDiagnostics* diag) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | // We allow unqualified class names (ie: .HelloActivity) |
| 39 | // Since we don't know the package name, we can just make a fake one here and |
| 40 | // 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] | 41 | Maybe<std::string> fully_qualified_class_name = |
| 42 | util::GetFullyQualifiedClassName("a", attr->value); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 43 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | StringPiece qualified_class_name = fully_qualified_class_name |
| 45 | ? fully_qualified_class_name.value() |
| 46 | : attr->value; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | if (!util::IsJavaClassName(qualified_class_name)) { |
| 49 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | << "attribute 'android:name' in <" << el->name |
| 51 | << "> tag must be a valid Java class name"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 52 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 57 | static bool OptionalNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) { |
| 59 | return NameIsJavaClassName(el, attr, diag); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 64 | static bool RequiredNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) { |
| 66 | return NameIsJavaClassName(el, attr, diag); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | << "<" << el->name << "> is missing attribute 'android:name'"); |
| 70 | return false; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 73 | static bool RequiredNameIsJavaPackage(xml::Element* el, SourcePathDiagnostics* diag) { |
| 74 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) { |
| 75 | return util::IsJavaPackageName(attr->value); |
| 76 | } |
| 77 | diag->Error(DiagMessage(el->line_number) |
| 78 | << "<" << el->name << "> is missing attribute 'android:name'"); |
| 79 | return false; |
| 80 | } |
| 81 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | static bool VerifyManifest(xml::Element* el, SourcePathDiagnostics* diag) { |
| 83 | xml::Attribute* attr = el->FindAttribute({}, "package"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | if (!attr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | << "<manifest> tag is missing 'package' attribute"); |
| 87 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | } else if (ResourceUtils::IsReference(attr->value)) { |
| 89 | diag->Error( |
| 90 | DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | << "attribute 'package' in <manifest> tag must not be a reference"); |
| 92 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | } else if (!util::IsJavaPackageName(attr->value)) { |
| 94 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | << "attribute 'package' in <manifest> tag is not a valid Java " |
| 96 | "package name: '" |
| 97 | << attr->value << "'"); |
| 98 | return false; |
| 99 | } |
| 100 | return true; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 103 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | * The coreApp attribute in <manifest> is not a regular AAPT attribute, so type |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 105 | * checking on it is manual. |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 106 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | static bool FixCoreAppAttribute(xml::Element* el, SourcePathDiagnostics* diag) { |
| 108 | if (xml::Attribute* attr = el->FindAttribute("", "coreApp")) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | std::unique_ptr<BinaryPrimitive> result = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | ResourceUtils::TryParseBool(attr->value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | if (!result) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | << "attribute coreApp must be a boolean"); |
| 114 | return false; |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 115 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | attr->compiled_value = std::move(result); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | } |
| 118 | return true; |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 121 | // Checks that <uses-feature> has android:glEsVersion or android:name, not both (or neither). |
| 122 | static bool VerifyUsesFeature(xml::Element* el, SourcePathDiagnostics* diag) { |
| 123 | bool has_name = false; |
| 124 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) { |
| 125 | if (attr->value.empty()) { |
| 126 | diag->Error(DiagMessage(el->line_number) |
| 127 | << "android:name in <uses-feature> must not be empty"); |
| 128 | return false; |
| 129 | } |
| 130 | has_name = true; |
| 131 | } |
| 132 | |
| 133 | bool has_gl_es_version = false; |
| 134 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "glEsVersion")) { |
| 135 | if (has_name) { |
| 136 | diag->Error(DiagMessage(el->line_number) |
| 137 | << "cannot define both android:name and android:glEsVersion in <uses-feature>"); |
| 138 | return false; |
| 139 | } |
| 140 | has_gl_es_version = true; |
| 141 | } |
| 142 | |
| 143 | if (!has_name && !has_gl_es_version) { |
| 144 | diag->Error(DiagMessage(el->line_number) |
| 145 | << "<uses-feature> must have either android:name or android:glEsVersion attribute"); |
| 146 | return false; |
| 147 | } |
| 148 | return true; |
| 149 | } |
| 150 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 151 | bool ManifestFixer::BuildRules(xml::XmlActionExecutor* executor, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | IDiagnostics* diag) { |
| 153 | // First verify some options. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | if (options_.rename_manifest_package) { |
| 155 | if (!util::IsJavaPackageName(options_.rename_manifest_package.value())) { |
| 156 | diag->Error(DiagMessage() << "invalid manifest package override '" |
| 157 | << options_.rename_manifest_package.value() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | << "'"); |
| 159 | return false; |
| 160 | } |
| 161 | } |
| 162 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 163 | if (options_.rename_instrumentation_target_package) { |
| 164 | if (!util::IsJavaPackageName( |
| 165 | options_.rename_instrumentation_target_package.value())) { |
| 166 | diag->Error(DiagMessage() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 167 | << "invalid instrumentation target package override '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | << options_.rename_instrumentation_target_package.value() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 169 | << "'"); |
| 170 | return false; |
| 171 | } |
| 172 | } |
| 173 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 174 | // Common <intent-filter> actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 175 | xml::XmlNodeAction intent_filter_action; |
| 176 | intent_filter_action["action"]; |
| 177 | intent_filter_action["category"]; |
| 178 | intent_filter_action["data"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 180 | // Common <meta-data> actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 181 | xml::XmlNodeAction meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 183 | // Common <uses-feature> actions. |
| 184 | xml::XmlNodeAction uses_feature_action; |
| 185 | uses_feature_action.Action(VerifyUsesFeature); |
| 186 | |
| 187 | // Common component actions. |
| 188 | xml::XmlNodeAction component_action; |
| 189 | component_action.Action(RequiredNameIsJavaClassName); |
| 190 | component_action["intent-filter"] = intent_filter_action; |
| 191 | component_action["meta-data"] = meta_data_action; |
| 192 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 193 | // Manifest actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 194 | xml::XmlNodeAction& manifest_action = (*executor)["manifest"]; |
| 195 | manifest_action.Action(VerifyManifest); |
| 196 | manifest_action.Action(FixCoreAppAttribute); |
| 197 | manifest_action.Action([&](xml::Element* el) -> bool { |
| 198 | if (options_.version_name_default) { |
| 199 | if (el->FindAttribute(xml::kSchemaAndroid, "versionName") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 200 | el->attributes.push_back( |
| 201 | xml::Attribute{xml::kSchemaAndroid, "versionName", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 202 | options_.version_name_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 206 | if (options_.version_code_default) { |
| 207 | if (el->FindAttribute(xml::kSchemaAndroid, "versionCode") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 208 | el->attributes.push_back( |
| 209 | xml::Attribute{xml::kSchemaAndroid, "versionCode", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | options_.version_code_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 211 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 212 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 213 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 214 | }); |
| 215 | |
| 216 | // Meta tags. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 217 | manifest_action["eat-comment"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | |
| 219 | // Uses-sdk actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 220 | manifest_action["uses-sdk"].Action([&](xml::Element* el) -> bool { |
| 221 | if (options_.min_sdk_version_default && |
| 222 | el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 223 | // There was no minSdkVersion defined and we have a default to assign. |
| 224 | el->attributes.push_back( |
| 225 | xml::Attribute{xml::kSchemaAndroid, "minSdkVersion", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 226 | options_.min_sdk_version_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 229 | if (options_.target_sdk_version_default && |
| 230 | el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 231 | // There was no targetSdkVersion defined and we have a default to assign. |
| 232 | el->attributes.push_back( |
| 233 | xml::Attribute{xml::kSchemaAndroid, "targetSdkVersion", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 234 | options_.target_sdk_version_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 235 | } |
| 236 | return true; |
| 237 | }); |
| 238 | |
| 239 | // Instrumentation actions. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 240 | manifest_action["instrumentation"].Action(RequiredNameIsJavaClassName); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 241 | manifest_action["instrumentation"].Action([&](xml::Element* el) -> bool { |
| 242 | if (!options_.rename_instrumentation_target_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 243 | return true; |
| 244 | } |
| 245 | |
| 246 | if (xml::Attribute* attr = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) { |
| 248 | attr->value = options_.rename_instrumentation_target_package.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 249 | } |
| 250 | return true; |
| 251 | }); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 252 | manifest_action["instrumentation"]["meta-data"] = meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 253 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 254 | manifest_action["original-package"]; |
| 255 | manifest_action["protected-broadcast"]; |
| 256 | manifest_action["uses-permission"]; |
| 257 | manifest_action["permission"]; |
| 258 | manifest_action["permission-tree"]; |
| 259 | manifest_action["permission-group"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | manifest_action["uses-configuration"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 261 | manifest_action["supports-screens"]; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 262 | manifest_action["uses-feature"] = uses_feature_action; |
| 263 | manifest_action["feature-group"]["uses-feature"] = uses_feature_action; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 264 | manifest_action["compatible-screens"]; |
| 265 | manifest_action["compatible-screens"]["screen"]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | manifest_action["supports-gl-texture"]; |
Adam Lesinski | 5119e51 | 2016-12-05 19:48:20 -0800 | [diff] [blame] | 267 | manifest_action["meta-data"] = meta_data_action; |
| 268 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 269 | // Application actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 270 | xml::XmlNodeAction& application_action = manifest_action["application"]; |
| 271 | application_action.Action(OptionalNameIsJavaClassName); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 272 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 273 | application_action["uses-library"].Action(RequiredNameIsJavaPackage); |
| 274 | application_action["library"].Action(RequiredNameIsJavaPackage); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 275 | application_action["meta-data"] = meta_data_action; |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 276 | application_action["activity"] = component_action; |
| 277 | application_action["activity-alias"] = component_action; |
| 278 | application_action["service"] = component_action; |
| 279 | application_action["receiver"] = component_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 280 | |
| 281 | // Provider actions. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 282 | application_action["provider"] = component_action; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 283 | application_action["provider"]["grant-uri-permissions"]; |
| 284 | application_action["provider"]["path-permissions"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 285 | |
| 286 | return true; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 289 | class FullyQualifiedClassNameVisitor : public xml::Visitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 290 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 291 | using xml::Visitor::Visit; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 292 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 293 | explicit FullyQualifiedClassNameVisitor(const StringPiece& package) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 294 | : package_(package) {} |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 295 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 296 | void Visit(xml::Element* el) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 297 | for (xml::Attribute& attr : el->attributes) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | if (attr.namespace_uri == xml::kSchemaAndroid && |
| 299 | class_attributes_.find(attr.name) != class_attributes_.end()) { |
| 300 | if (Maybe<std::string> new_value = |
| 301 | util::GetFullyQualifiedClassName(package_, attr.value)) { |
| 302 | attr.value = std::move(new_value.value()); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 303 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 304 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 307 | // Super implementation to iterate over the children. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 308 | xml::Visitor::Visit(el); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 312 | StringPiece package_; |
| 313 | std::unordered_set<StringPiece> class_attributes_ = {"name"}; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 314 | }; |
| 315 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 316 | static bool RenameManifestPackage(const StringPiece& package_override, |
| 317 | xml::Element* manifest_el) { |
| 318 | xml::Attribute* attr = manifest_el->FindAttribute({}, "package"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 319 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 320 | // We've already verified that the manifest element is present, with a package |
| 321 | // name specified. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 322 | CHECK(attr != nullptr); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 323 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 324 | std::string original_package = std::move(attr->value); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 325 | attr->value = package_override.to_string(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 326 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 327 | FullyQualifiedClassNameVisitor visitor(original_package); |
| 328 | manifest_el->Accept(&visitor); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 329 | return true; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | bool ManifestFixer::Consume(IAaptContext* context, xml::XmlResource* doc) { |
| 333 | xml::Element* root = xml::FindRootElement(doc->root.get()); |
| 334 | if (!root || !root->namespace_uri.empty() || root->name != "manifest") { |
| 335 | context->GetDiagnostics()->Error(DiagMessage(doc->file.source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 336 | << "root tag must be <manifest>"); |
| 337 | return false; |
| 338 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 339 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 340 | if ((options_.min_sdk_version_default || |
| 341 | options_.target_sdk_version_default) && |
| 342 | root->FindChild({}, "uses-sdk") == nullptr) { |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 343 | // Auto insert a <uses-sdk> element. This must be inserted before the |
| 344 | // <application> tag. The device runtime PackageParser will make SDK version |
| 345 | // decisions while parsing <application>. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 346 | std::unique_ptr<xml::Element> uses_sdk = util::make_unique<xml::Element>(); |
| 347 | uses_sdk->name = "uses-sdk"; |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 348 | root->InsertChild(0, std::move(uses_sdk)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 349 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 350 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 351 | xml::XmlActionExecutor executor; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 352 | if (!BuildRules(&executor, context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 353 | return false; |
| 354 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 355 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 356 | if (!executor.Execute(xml::XmlActionExecutorPolicy::kWhitelist, |
| 357 | context->GetDiagnostics(), doc)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 358 | return false; |
| 359 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 360 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 361 | if (options_.rename_manifest_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 362 | // Rename manifest package outside of the XmlActionExecutor. |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 363 | // We need to extract the old package name and FullyQualify all class |
| 364 | // names. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 365 | if (!RenameManifestPackage(options_.rename_manifest_package.value(), |
| 366 | root)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 367 | return false; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 368 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 369 | } |
| 370 | return true; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 373 | } // namespace aapt |