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