Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "cmd/Util.h" |
| 18 | |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "android-base/logging.h" |
| 22 | |
| 23 | #include "ConfigDescription.h" |
| 24 | #include "Locale.h" |
| 25 | #include "ResourceUtils.h" |
| 26 | #include "ValueVisitor.h" |
| 27 | #include "split/TableSplitter.h" |
| 28 | #include "util/Maybe.h" |
| 29 | #include "util/Util.h" |
| 30 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 31 | using ::android::StringPiece; |
Ryan Mitchell | 5fa2bb1 | 2018-07-12 11:24:51 -0700 | [diff] [blame^] | 32 | using ::android::base::StringPrintf; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 33 | |
| 34 | namespace aapt { |
| 35 | |
| 36 | Maybe<uint16_t> ParseTargetDensityParameter(const StringPiece& arg, IDiagnostics* diag) { |
| 37 | ConfigDescription preferred_density_config; |
| 38 | if (!ConfigDescription::Parse(arg, &preferred_density_config)) { |
| 39 | diag->Error(DiagMessage() << "invalid density '" << arg << "' for --preferred-density option"); |
| 40 | return {}; |
| 41 | } |
| 42 | |
| 43 | // Clear the version that can be automatically added. |
| 44 | preferred_density_config.sdkVersion = 0; |
| 45 | |
| 46 | if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) != |
| 47 | ConfigDescription::CONFIG_DENSITY) { |
| 48 | diag->Error(DiagMessage() << "invalid preferred density '" << arg << "'. " |
| 49 | << "Preferred density must only be a density value"); |
| 50 | return {}; |
| 51 | } |
| 52 | return preferred_density_config.density; |
| 53 | } |
| 54 | |
| 55 | bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag, std::string* out_path, |
| 56 | SplitConstraints* out_split) { |
| 57 | CHECK(diag != nullptr); |
| 58 | CHECK(out_path != nullptr); |
| 59 | CHECK(out_split != nullptr); |
| 60 | |
Adam Lesinski | db09157 | 2017-04-13 12:48:56 -0700 | [diff] [blame] | 61 | #ifdef _WIN32 |
| 62 | const char sSeparator = ';'; |
| 63 | #else |
| 64 | const char sSeparator = ':'; |
| 65 | #endif |
| 66 | |
| 67 | std::vector<std::string> parts = util::Split(arg, sSeparator); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 68 | if (parts.size() != 2) { |
| 69 | diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'"); |
Adam Lesinski | db09157 | 2017-04-13 12:48:56 -0700 | [diff] [blame] | 70 | diag->Note(DiagMessage() << "should be --split path/to/output.apk" << sSeparator |
| 71 | << "<config>[,<config>...]."); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | |
| 75 | *out_path = parts[0]; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 76 | for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) { |
| 77 | ConfigDescription config; |
| 78 | if (!ConfigDescription::Parse(config_str, &config)) { |
| 79 | diag->Error(DiagMessage() << "invalid config '" << config_str << "' in split parameter '" |
| 80 | << arg << "'"); |
| 81 | return false; |
| 82 | } |
| 83 | out_split->configs.insert(config); |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args, |
| 89 | IDiagnostics* diag) { |
| 90 | std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>(); |
| 91 | for (const std::string& config_arg : args) { |
| 92 | for (const StringPiece& config_str : util::Tokenize(config_arg, ',')) { |
| 93 | ConfigDescription config; |
| 94 | LocaleValue lv; |
| 95 | if (lv.InitFromFilterString(config_str)) { |
| 96 | lv.WriteTo(&config); |
| 97 | } else if (!ConfigDescription::Parse(config_str, &config)) { |
| 98 | diag->Error(DiagMessage() << "invalid config '" << config_str << "' for -c option"); |
| 99 | return {}; |
| 100 | } |
| 101 | |
| 102 | if (config.density != 0) { |
| 103 | diag->Warn(DiagMessage() << "ignoring density '" << config << "' for -c option"); |
| 104 | } else { |
| 105 | filter->AddConfig(config); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | return std::move(filter); |
| 110 | } |
| 111 | |
| 112 | // Adjust the SplitConstraints so that their SDK version is stripped if it |
| 113 | // is less than or equal to the minSdk. Otherwise the resources that have had |
| 114 | // their SDK version stripped due to minSdk won't ever match. |
| 115 | std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk( |
| 116 | int min_sdk, const std::vector<SplitConstraints>& split_constraints) { |
| 117 | std::vector<SplitConstraints> adjusted_constraints; |
| 118 | adjusted_constraints.reserve(split_constraints.size()); |
| 119 | for (const SplitConstraints& constraints : split_constraints) { |
| 120 | SplitConstraints constraint; |
| 121 | for (const ConfigDescription& config : constraints.configs) { |
| 122 | if (config.sdkVersion <= min_sdk) { |
| 123 | constraint.configs.insert(config.CopyWithoutSdkVersion()); |
| 124 | } else { |
| 125 | constraint.configs.insert(config); |
| 126 | } |
| 127 | } |
| 128 | adjusted_constraints.push_back(std::move(constraint)); |
| 129 | } |
| 130 | return adjusted_constraints; |
| 131 | } |
| 132 | |
| 133 | static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 134 | return xml::AaptAttribute(Attribute(), id); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 137 | static xml::NamespaceDecl CreateAndroidNamespaceDecl() { |
| 138 | xml::NamespaceDecl decl; |
| 139 | decl.prefix = "android"; |
| 140 | decl.uri = xml::kSchemaAndroid; |
| 141 | return decl; |
| 142 | } |
| 143 | |
Donald Chai | 414e48a | 2017-11-09 21:06:52 -0800 | [diff] [blame] | 144 | // Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by |
| 145 | // replacing nonconforming characters with underscores. |
| 146 | // |
| 147 | // See frameworks/base/core/java/android/content/pm/PackageParser.java which |
| 148 | // checks this at runtime. |
Izabela Orlowska | 1056019 | 2018-04-13 11:56:35 +0100 | [diff] [blame] | 149 | std::string MakePackageSafeName(const std::string &name) { |
Donald Chai | b8f078c | 2017-10-18 23:51:18 -0700 | [diff] [blame] | 150 | std::string result(name); |
Donald Chai | 414e48a | 2017-11-09 21:06:52 -0800 | [diff] [blame] | 151 | bool first = true; |
Donald Chai | b8f078c | 2017-10-18 23:51:18 -0700 | [diff] [blame] | 152 | for (char &c : result) { |
Donald Chai | 414e48a | 2017-11-09 21:06:52 -0800 | [diff] [blame] | 153 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { |
| 154 | first = false; |
| 155 | continue; |
Donald Chai | b8f078c | 2017-10-18 23:51:18 -0700 | [diff] [blame] | 156 | } |
Donald Chai | 414e48a | 2017-11-09 21:06:52 -0800 | [diff] [blame] | 157 | if (!first) { |
| 158 | if (c >= '0' && c <= '9') { |
| 159 | continue; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | c = '_'; |
| 164 | first = false; |
Donald Chai | b8f078c | 2017-10-18 23:51:18 -0700 | [diff] [blame] | 165 | } |
| 166 | return result; |
| 167 | } |
| 168 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 169 | std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info, |
| 170 | const SplitConstraints& constraints) { |
| 171 | const ResourceId kVersionCode(0x0101021b); |
Ryan Mitchell | 5fa2bb1 | 2018-07-12 11:24:51 -0700 | [diff] [blame^] | 172 | const ResourceId kVersionCodeMajor(0x01010576); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 173 | const ResourceId kRevisionCode(0x010104d5); |
| 174 | const ResourceId kHasCode(0x0101000c); |
| 175 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 176 | std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>(); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 177 | manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 178 | manifest_el->name = "manifest"; |
| 179 | manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package}); |
| 180 | |
| 181 | if (app_info.version_code) { |
| 182 | const uint32_t version_code = app_info.version_code.value(); |
| 183 | manifest_el->attributes.push_back(xml::Attribute{ |
| 184 | xml::kSchemaAndroid, "versionCode", std::to_string(version_code), |
| 185 | CreateAttributeWithId(kVersionCode), |
| 186 | util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)}); |
| 187 | } |
| 188 | |
Ryan Mitchell | 5fa2bb1 | 2018-07-12 11:24:51 -0700 | [diff] [blame^] | 189 | if (app_info.version_code_major) { |
| 190 | const uint32_t version_code_major = app_info.version_code_major.value(); |
| 191 | manifest_el->attributes.push_back(xml::Attribute{ |
| 192 | xml::kSchemaAndroid, "versionCodeMajor", std::to_string(version_code_major), |
| 193 | CreateAttributeWithId(kVersionCodeMajor), |
| 194 | util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code_major)}); |
| 195 | } |
| 196 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 197 | if (app_info.revision_code) { |
| 198 | const uint32_t revision_code = app_info.revision_code.value(); |
| 199 | manifest_el->attributes.push_back(xml::Attribute{ |
| 200 | xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code), |
| 201 | CreateAttributeWithId(kRevisionCode), |
| 202 | util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)}); |
| 203 | } |
| 204 | |
| 205 | std::stringstream split_name; |
| 206 | if (app_info.split_name) { |
| 207 | split_name << app_info.split_name.value() << "."; |
| 208 | } |
Donald Chai | b8f078c | 2017-10-18 23:51:18 -0700 | [diff] [blame] | 209 | std::vector<std::string> sanitized_config_names; |
| 210 | for (const auto &config : constraints.configs) { |
| 211 | sanitized_config_names.push_back(MakePackageSafeName(config.toString().string())); |
| 212 | } |
| 213 | split_name << "config." << util::Joiner(sanitized_config_names, "_"); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 214 | |
| 215 | manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()}); |
| 216 | |
| 217 | if (app_info.split_name) { |
| 218 | manifest_el->attributes.push_back( |
| 219 | xml::Attribute{"", "configForSplit", app_info.split_name.value()}); |
| 220 | } |
| 221 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 222 | // Splits may contain more configurations than originally desired (fall-back densities, etc.). |
| 223 | // This makes programmatic discovery of split targeting difficult. Encode the original |
Adam Lesinski | 3d63239 | 2017-07-24 17:08:32 -0700 | [diff] [blame] | 224 | // split constraints intended for this split. |
| 225 | std::stringstream target_config_str; |
| 226 | target_config_str << util::Joiner(constraints.configs, ","); |
| 227 | manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()}); |
| 228 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 229 | std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>(); |
| 230 | application_el->name = "application"; |
| 231 | application_el->attributes.push_back( |
| 232 | xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode), |
| 233 | util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)}); |
| 234 | |
| 235 | manifest_el->AppendChild(std::move(application_el)); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 236 | |
| 237 | std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>(); |
| 238 | doc->root = std::move(manifest_el); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 239 | return doc; |
| 240 | } |
| 241 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 242 | static Maybe<std::string> ExtractCompiledString(const xml::Attribute& attr, |
| 243 | std::string* out_error) { |
| 244 | if (attr.compiled_value != nullptr) { |
| 245 | const String* compiled_str = ValueCast<String>(attr.compiled_value.get()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 246 | if (compiled_str != nullptr) { |
| 247 | if (!compiled_str->value->empty()) { |
| 248 | return *compiled_str->value; |
| 249 | } else { |
| 250 | *out_error = "compiled value is an empty string"; |
| 251 | return {}; |
| 252 | } |
| 253 | } |
| 254 | *out_error = "compiled value is not a string"; |
| 255 | return {}; |
| 256 | } |
| 257 | |
| 258 | // Fallback to the plain text value if there is one. |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 259 | if (!attr.value.empty()) { |
| 260 | return attr.value; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 261 | } |
| 262 | *out_error = "value is an empty string"; |
| 263 | return {}; |
| 264 | } |
| 265 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 266 | static Maybe<uint32_t> ExtractCompiledInt(const xml::Attribute& attr, std::string* out_error) { |
| 267 | if (attr.compiled_value != nullptr) { |
| 268 | const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 269 | if (compiled_prim != nullptr) { |
| 270 | if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT && |
| 271 | compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) { |
| 272 | return compiled_prim->value.data; |
| 273 | } |
| 274 | } |
| 275 | *out_error = "compiled value is not an integer"; |
| 276 | return {}; |
| 277 | } |
| 278 | |
| 279 | // Fallback to the plain text value if there is one. |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 280 | Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr.value); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 281 | if (integer) { |
| 282 | return integer; |
| 283 | } |
| 284 | std::stringstream error_msg; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 285 | error_msg << "'" << attr.value << "' is not a valid integer"; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 286 | *out_error = error_msg.str(); |
| 287 | return {}; |
| 288 | } |
| 289 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 290 | static Maybe<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) { |
| 291 | if (attr.compiled_value != nullptr) { |
| 292 | const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 293 | if (compiled_prim != nullptr) { |
| 294 | if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT && |
| 295 | compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) { |
| 296 | return compiled_prim->value.data; |
| 297 | } |
| 298 | *out_error = "compiled value is not an integer or string"; |
| 299 | return {}; |
| 300 | } |
| 301 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 302 | const String* compiled_str = ValueCast<String>(attr.compiled_value.get()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 303 | if (compiled_str != nullptr) { |
| 304 | Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value); |
| 305 | if (sdk_version) { |
| 306 | return sdk_version; |
| 307 | } |
| 308 | |
| 309 | *out_error = "compiled string value is not a valid SDK version"; |
| 310 | return {}; |
| 311 | } |
| 312 | *out_error = "compiled value is not an integer or string"; |
| 313 | return {}; |
| 314 | } |
| 315 | |
| 316 | // Fallback to the plain text value if there is one. |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 317 | Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 318 | if (sdk_version) { |
| 319 | return sdk_version; |
| 320 | } |
| 321 | std::stringstream error_msg; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 322 | error_msg << "'" << attr.value << "' is not a valid SDK version"; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 323 | *out_error = error_msg.str(); |
| 324 | return {}; |
| 325 | } |
| 326 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 327 | Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res, |
| 328 | IDiagnostics* diag) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 329 | // Make sure the first element is <manifest> with package attribute. |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 330 | const xml::Element* manifest_el = xml_res.root.get(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 331 | if (manifest_el == nullptr) { |
| 332 | return {}; |
| 333 | } |
| 334 | |
| 335 | AppInfo app_info; |
| 336 | |
| 337 | if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 338 | diag->Error(DiagMessage(xml_res.file.source) << "root tag must be <manifest>"); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 339 | return {}; |
| 340 | } |
| 341 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 342 | const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package"); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 343 | if (!package_attr) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 344 | diag->Error(DiagMessage(xml_res.file.source) << "<manifest> must have a 'package' attribute"); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 345 | return {}; |
| 346 | } |
| 347 | |
| 348 | std::string error_msg; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 349 | Maybe<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 350 | if (!maybe_package) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 351 | diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number)) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 352 | << "invalid package name: " << error_msg); |
| 353 | return {}; |
| 354 | } |
| 355 | app_info.package = maybe_package.value(); |
| 356 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 357 | if (const xml::Attribute* version_code_attr = |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 358 | manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 359 | Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 360 | if (!maybe_code) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 361 | diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number)) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 362 | << "invalid android:versionCode: " << error_msg); |
| 363 | return {}; |
| 364 | } |
| 365 | app_info.version_code = maybe_code.value(); |
| 366 | } |
| 367 | |
Ryan Mitchell | 5fa2bb1 | 2018-07-12 11:24:51 -0700 | [diff] [blame^] | 368 | if (const xml::Attribute* version_code_major_attr = |
| 369 | manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) { |
| 370 | Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_major_attr, &error_msg); |
| 371 | if (!maybe_code) { |
| 372 | diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number)) |
| 373 | << "invalid android:versionCodeMajor: " << error_msg); |
| 374 | return {}; |
| 375 | } |
| 376 | app_info.version_code_major = maybe_code.value(); |
| 377 | } |
| 378 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 379 | if (const xml::Attribute* revision_code_attr = |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 380 | manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 381 | Maybe<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 382 | if (!maybe_code) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 383 | diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number)) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 384 | << "invalid android:revisionCode: " << error_msg); |
| 385 | return {}; |
| 386 | } |
| 387 | app_info.revision_code = maybe_code.value(); |
| 388 | } |
| 389 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 390 | if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) { |
| 391 | Maybe<std::string> maybe_split_name = ExtractCompiledString(*split_name_attr, &error_msg); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 392 | if (!maybe_split_name) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 393 | diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number)) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 394 | << "invalid split name: " << error_msg); |
| 395 | return {}; |
| 396 | } |
| 397 | app_info.split_name = maybe_split_name.value(); |
| 398 | } |
| 399 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 400 | if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) { |
| 401 | if (const xml::Attribute* min_sdk = |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 402 | uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 403 | Maybe<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 404 | if (!maybe_sdk) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 405 | diag->Error(DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number)) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 406 | << "invalid android:minSdkVersion: " << error_msg); |
| 407 | return {}; |
| 408 | } |
| 409 | app_info.min_sdk_version = maybe_sdk.value(); |
| 410 | } |
| 411 | } |
| 412 | return app_info; |
| 413 | } |
| 414 | |
Ryan Mitchell | 5fa2bb1 | 2018-07-12 11:24:51 -0700 | [diff] [blame^] | 415 | void SetLongVersionCode(xml::Element* manifest, uint64_t version) { |
| 416 | // Write the low bits of the version code to android:versionCode |
| 417 | auto version_code = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCode"); |
| 418 | version_code->value = StringPrintf("0x%08x", (uint32_t) (version & 0xffffffff)); |
| 419 | version_code->compiled_value = ResourceUtils::TryParseInt(version_code->value); |
| 420 | |
| 421 | auto version_high = (uint32_t) (version >> 32); |
| 422 | if (version_high != 0) { |
| 423 | // Write the high bits of the version code to android:versionCodeMajor |
| 424 | auto version_major = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCodeMajor"); |
| 425 | version_major->value = StringPrintf("0x%08x", version_high); |
| 426 | version_major->compiled_value = ResourceUtils::TryParseInt(version_major->value); |
| 427 | } else { |
| 428 | manifest->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor"); |
| 429 | } |
| 430 | } |
| 431 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 432 | } // namespace aapt |