blob: 3244fb83fa4bc5e07e2b2acbe3a6d8cff3735863 [file] [log] [blame]
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001/*
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"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020022#include "androidfw/ConfigDescription.h"
23#include "androidfw/Locale.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070024#include "ResourceUtils.h"
25#include "ValueVisitor.h"
26#include "split/TableSplitter.h"
Ryan Mitchell4382e442021-07-14 12:53:01 -070027
Adam Lesinskid0f492d2017-04-03 18:12:45 -070028#include "util/Util.h"
29
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020030using ::android::ConfigDescription;
31using ::android::LocaleValue;
Adam Lesinski6b372992017-08-09 10:54:23 -070032using ::android::StringPiece;
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -070033using ::android::base::StringPrintf;
Adam Lesinskid0f492d2017-04-03 18:12:45 -070034
35namespace aapt {
36
Ryan Mitchell4382e442021-07-14 12:53:01 -070037std::optional<uint16_t> ParseTargetDensityParameter(const StringPiece& arg, IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070038 ConfigDescription preferred_density_config;
39 if (!ConfigDescription::Parse(arg, &preferred_density_config)) {
40 diag->Error(DiagMessage() << "invalid density '" << arg << "' for --preferred-density option");
41 return {};
42 }
43
44 // Clear the version that can be automatically added.
45 preferred_density_config.sdkVersion = 0;
46
47 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
48 ConfigDescription::CONFIG_DENSITY) {
49 diag->Error(DiagMessage() << "invalid preferred density '" << arg << "'. "
50 << "Preferred density must only be a density value");
51 return {};
52 }
53 return preferred_density_config.density;
54}
55
56bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag, std::string* out_path,
57 SplitConstraints* out_split) {
58 CHECK(diag != nullptr);
59 CHECK(out_path != nullptr);
60 CHECK(out_split != nullptr);
61
Adam Lesinskidb091572017-04-13 12:48:56 -070062#ifdef _WIN32
63 const char sSeparator = ';';
64#else
65 const char sSeparator = ':';
66#endif
67
68 std::vector<std::string> parts = util::Split(arg, sSeparator);
Adam Lesinskid0f492d2017-04-03 18:12:45 -070069 if (parts.size() != 2) {
70 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
Adam Lesinskidb091572017-04-13 12:48:56 -070071 diag->Note(DiagMessage() << "should be --split path/to/output.apk" << sSeparator
72 << "<config>[,<config>...].");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070073 return false;
74 }
75
76 *out_path = parts[0];
Todd Kennedy9fbdf892018-08-28 16:31:15 -070077 out_split->name = parts[1];
Adam Lesinskid0f492d2017-04-03 18:12:45 -070078 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
79 ConfigDescription config;
80 if (!ConfigDescription::Parse(config_str, &config)) {
81 diag->Error(DiagMessage() << "invalid config '" << config_str << "' in split parameter '"
82 << arg << "'");
83 return false;
84 }
85 out_split->configs.insert(config);
86 }
87 return true;
88}
89
90std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
91 IDiagnostics* diag) {
92 std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>();
93 for (const std::string& config_arg : args) {
94 for (const StringPiece& config_str : util::Tokenize(config_arg, ',')) {
95 ConfigDescription config;
96 LocaleValue lv;
97 if (lv.InitFromFilterString(config_str)) {
98 lv.WriteTo(&config);
99 } else if (!ConfigDescription::Parse(config_str, &config)) {
100 diag->Error(DiagMessage() << "invalid config '" << config_str << "' for -c option");
101 return {};
102 }
103
104 if (config.density != 0) {
105 diag->Warn(DiagMessage() << "ignoring density '" << config << "' for -c option");
106 } else {
107 filter->AddConfig(config);
108 }
109 }
110 }
111 return std::move(filter);
112}
113
114// Adjust the SplitConstraints so that their SDK version is stripped if it
115// is less than or equal to the minSdk. Otherwise the resources that have had
116// their SDK version stripped due to minSdk won't ever match.
117std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
118 int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
119 std::vector<SplitConstraints> adjusted_constraints;
120 adjusted_constraints.reserve(split_constraints.size());
121 for (const SplitConstraints& constraints : split_constraints) {
122 SplitConstraints constraint;
123 for (const ConfigDescription& config : constraints.configs) {
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700124 const ConfigDescription &configToInsert = (config.sdkVersion <= min_sdk)
125 ? config.CopyWithoutSdkVersion()
126 : config;
127 // only add the config if it actually selects something
128 if (configToInsert != ConfigDescription::DefaultConfig()) {
129 constraint.configs.insert(configToInsert);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700130 }
131 }
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700132 constraint.name = constraints.name;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700133 adjusted_constraints.push_back(std::move(constraint));
134 }
135 return adjusted_constraints;
136}
137
138static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700139 return xml::AaptAttribute(Attribute(), id);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700140}
141
Adam Lesinski6b372992017-08-09 10:54:23 -0700142static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
143 xml::NamespaceDecl decl;
144 decl.prefix = "android";
145 decl.uri = xml::kSchemaAndroid;
146 return decl;
147}
148
Donald Chai414e48a2017-11-09 21:06:52 -0800149// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
150// replacing nonconforming characters with underscores.
151//
152// See frameworks/base/core/java/android/content/pm/PackageParser.java which
153// checks this at runtime.
Izabela Orlowska10560192018-04-13 11:56:35 +0100154std::string MakePackageSafeName(const std::string &name) {
Donald Chaib8f078c2017-10-18 23:51:18 -0700155 std::string result(name);
Donald Chai414e48a2017-11-09 21:06:52 -0800156 bool first = true;
Donald Chaib8f078c2017-10-18 23:51:18 -0700157 for (char &c : result) {
Donald Chai414e48a2017-11-09 21:06:52 -0800158 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
159 first = false;
160 continue;
Donald Chaib8f078c2017-10-18 23:51:18 -0700161 }
Donald Chai414e48a2017-11-09 21:06:52 -0800162 if (!first) {
163 if (c >= '0' && c <= '9') {
164 continue;
165 }
166 }
167
168 c = '_';
169 first = false;
Donald Chaib8f078c2017-10-18 23:51:18 -0700170 }
171 return result;
172}
173
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700174std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
175 const SplitConstraints& constraints) {
176 const ResourceId kVersionCode(0x0101021b);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700177 const ResourceId kVersionCodeMajor(0x01010576);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700178 const ResourceId kRevisionCode(0x010104d5);
179 const ResourceId kHasCode(0x0101000c);
180
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700181 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700182 manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700183 manifest_el->name = "manifest";
184 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
185
186 if (app_info.version_code) {
187 const uint32_t version_code = app_info.version_code.value();
188 manifest_el->attributes.push_back(xml::Attribute{
189 xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
190 CreateAttributeWithId(kVersionCode),
191 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
192 }
193
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700194 if (app_info.version_code_major) {
195 const uint32_t version_code_major = app_info.version_code_major.value();
196 manifest_el->attributes.push_back(xml::Attribute{
197 xml::kSchemaAndroid, "versionCodeMajor", std::to_string(version_code_major),
198 CreateAttributeWithId(kVersionCodeMajor),
199 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code_major)});
200 }
201
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700202 if (app_info.revision_code) {
203 const uint32_t revision_code = app_info.revision_code.value();
204 manifest_el->attributes.push_back(xml::Attribute{
205 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
206 CreateAttributeWithId(kRevisionCode),
207 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
208 }
209
210 std::stringstream split_name;
211 if (app_info.split_name) {
212 split_name << app_info.split_name.value() << ".";
213 }
Donald Chaib8f078c2017-10-18 23:51:18 -0700214 std::vector<std::string> sanitized_config_names;
215 for (const auto &config : constraints.configs) {
216 sanitized_config_names.push_back(MakePackageSafeName(config.toString().string()));
217 }
218 split_name << "config." << util::Joiner(sanitized_config_names, "_");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700219
220 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
221
222 if (app_info.split_name) {
223 manifest_el->attributes.push_back(
224 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
225 }
226
Adam Lesinski6b372992017-08-09 10:54:23 -0700227 // Splits may contain more configurations than originally desired (fall-back densities, etc.).
228 // This makes programmatic discovery of split targeting difficult. Encode the original
Adam Lesinski3d632392017-07-24 17:08:32 -0700229 // split constraints intended for this split.
230 std::stringstream target_config_str;
231 target_config_str << util::Joiner(constraints.configs, ",");
232 manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
233
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700234 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
235 application_el->name = "application";
236 application_el->attributes.push_back(
237 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
238 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
239
240 manifest_el->AppendChild(std::move(application_el));
Adam Lesinski6b372992017-08-09 10:54:23 -0700241
242 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
243 doc->root = std::move(manifest_el);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700244 return doc;
245}
246
Ryan Mitchell4382e442021-07-14 12:53:01 -0700247static std::optional<std::string> ExtractCompiledString(const xml::Attribute& attr,
248 std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700249 if (attr.compiled_value != nullptr) {
250 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700251 if (compiled_str != nullptr) {
252 if (!compiled_str->value->empty()) {
253 return *compiled_str->value;
254 } else {
255 *out_error = "compiled value is an empty string";
256 return {};
257 }
258 }
259 *out_error = "compiled value is not a string";
260 return {};
261 }
262
263 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700264 if (!attr.value.empty()) {
265 return attr.value;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700266 }
267 *out_error = "value is an empty string";
268 return {};
269}
270
Ryan Mitchell4382e442021-07-14 12:53:01 -0700271static std::optional<uint32_t> ExtractCompiledInt(const xml::Attribute& attr,
272 std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700273 if (attr.compiled_value != nullptr) {
274 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700275 if (compiled_prim != nullptr) {
276 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
277 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
278 return compiled_prim->value.data;
279 }
280 }
281 *out_error = "compiled value is not an integer";
282 return {};
283 }
284
285 // Fallback to the plain text value if there is one.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700286 std::optional<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700287 if (integer) {
288 return integer;
289 }
290 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700291 error_msg << "'" << attr.value << "' is not a valid integer";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700292 *out_error = error_msg.str();
293 return {};
294}
295
Ryan Mitchell4382e442021-07-14 12:53:01 -0700296static std::optional<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700297 if (attr.compiled_value != nullptr) {
298 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700299 if (compiled_prim != nullptr) {
300 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
301 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
302 return compiled_prim->value.data;
303 }
304 *out_error = "compiled value is not an integer or string";
305 return {};
306 }
307
Adam Lesinski8780eb62017-10-31 17:44:39 -0700308 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700309 if (compiled_str != nullptr) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700310 std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700311 if (sdk_version) {
312 return sdk_version;
313 }
314
315 *out_error = "compiled string value is not a valid SDK version";
316 return {};
317 }
318 *out_error = "compiled value is not an integer or string";
319 return {};
320 }
321
322 // Fallback to the plain text value if there is one.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700323 std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700324 if (sdk_version) {
325 return sdk_version;
326 }
327 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700328 error_msg << "'" << attr.value << "' is not a valid SDK version";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700329 *out_error = error_msg.str();
330 return {};
331}
332
Ryan Mitchell4382e442021-07-14 12:53:01 -0700333std::optional<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
334 IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700335 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700336 const xml::Element* manifest_el = xml_res.root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700337 if (manifest_el == nullptr) {
338 return {};
339 }
340
341 AppInfo app_info;
342
343 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700344 diag->Error(DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700345 return {};
346 }
347
Adam Lesinski8780eb62017-10-31 17:44:39 -0700348 const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700349 if (!package_attr) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700350 diag->Error(DiagMessage(xml_res.file.source) << "<manifest> must have a 'package' attribute");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700351 return {};
352 }
353
354 std::string error_msg;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700355 std::optional<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700356 if (!maybe_package) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700357 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700358 << "invalid package name: " << error_msg);
359 return {};
360 }
361 app_info.package = maybe_package.value();
362
Adam Lesinski8780eb62017-10-31 17:44:39 -0700363 if (const xml::Attribute* version_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700364 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700365 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700366 if (!maybe_code) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700367 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700368 << "invalid android:versionCode: " << error_msg);
369 return {};
370 }
371 app_info.version_code = maybe_code.value();
372 }
373
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700374 if (const xml::Attribute* version_code_major_attr =
375 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700376 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_major_attr, &error_msg);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700377 if (!maybe_code) {
378 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
379 << "invalid android:versionCodeMajor: " << error_msg);
380 return {};
381 }
382 app_info.version_code_major = maybe_code.value();
383 }
384
Adam Lesinski8780eb62017-10-31 17:44:39 -0700385 if (const xml::Attribute* revision_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700386 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700387 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700388 if (!maybe_code) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700389 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700390 << "invalid android:revisionCode: " << error_msg);
391 return {};
392 }
393 app_info.revision_code = maybe_code.value();
394 }
395
Adam Lesinski8780eb62017-10-31 17:44:39 -0700396 if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700397 std::optional<std::string> maybe_split_name =
398 ExtractCompiledString(*split_name_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700399 if (!maybe_split_name) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700400 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700401 << "invalid split name: " << error_msg);
402 return {};
403 }
404 app_info.split_name = maybe_split_name.value();
405 }
406
Adam Lesinski8780eb62017-10-31 17:44:39 -0700407 if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
408 if (const xml::Attribute* min_sdk =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700409 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700410 std::optional<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700411 if (!maybe_sdk) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700412 diag->Error(DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700413 << "invalid android:minSdkVersion: " << error_msg);
414 return {};
415 }
416 app_info.min_sdk_version = maybe_sdk.value();
417 }
418 }
419 return app_info;
420}
421
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700422void SetLongVersionCode(xml::Element* manifest, uint64_t version) {
423 // Write the low bits of the version code to android:versionCode
424 auto version_code = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCode");
425 version_code->value = StringPrintf("0x%08x", (uint32_t) (version & 0xffffffff));
426 version_code->compiled_value = ResourceUtils::TryParseInt(version_code->value);
427
428 auto version_high = (uint32_t) (version >> 32);
429 if (version_high != 0) {
430 // Write the high bits of the version code to android:versionCodeMajor
431 auto version_major = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCodeMajor");
432 version_major->value = StringPrintf("0x%08x", version_high);
433 version_major->compiled_value = ResourceUtils::TryParseInt(version_major->value);
434 } else {
435 manifest->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
436 }
437}
438
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100439std::regex GetRegularExpression(const std::string &input) {
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000440 // Standard ECMAScript grammar.
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100441 std::regex case_insensitive(
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000442 input, std::regex_constants::ECMAScript);
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100443 return case_insensitive;
444}
445
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700446} // namespace aapt