blob: e839fc1ceb0fea86adff0f37210c1e14547b8895 [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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070037std::optional<uint16_t> ParseTargetDensityParameter(StringPiece arg, android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070038 ConfigDescription preferred_density_config;
39 if (!ConfigDescription::Parse(arg, &preferred_density_config)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000040 diag->Error(android::DiagMessage()
41 << "invalid density '" << arg << "' for --preferred-density option");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070042 return {};
43 }
44
45 // Clear the version that can be automatically added.
46 preferred_density_config.sdkVersion = 0;
47
48 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
49 ConfigDescription::CONFIG_DENSITY) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000050 diag->Error(android::DiagMessage() << "invalid preferred density '" << arg << "'. "
51 << "Preferred density must only be a density value");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070052 return {};
53 }
54 return preferred_density_config.density;
55}
56
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070057bool ParseSplitParameter(StringPiece arg, android::IDiagnostics* diag, std::string* out_path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070058 SplitConstraints* out_split) {
59 CHECK(diag != nullptr);
60 CHECK(out_path != nullptr);
61 CHECK(out_split != nullptr);
62
Adam Lesinskidb091572017-04-13 12:48:56 -070063#ifdef _WIN32
64 const char sSeparator = ';';
65#else
66 const char sSeparator = ':';
67#endif
68
69 std::vector<std::string> parts = util::Split(arg, sSeparator);
Adam Lesinskid0f492d2017-04-03 18:12:45 -070070 if (parts.size() != 2) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000071 diag->Error(android::DiagMessage() << "invalid split parameter '" << arg << "'");
72 diag->Note(android::DiagMessage() << "should be --split path/to/output.apk" << sSeparator
73 << "<config>[,<config>...].");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070074 return false;
75 }
76
77 *out_path = parts[0];
Todd Kennedy9fbdf892018-08-28 16:31:15 -070078 out_split->name = parts[1];
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070079 for (StringPiece config_str : util::Tokenize(parts[1], ',')) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070080 ConfigDescription config;
81 if (!ConfigDescription::Parse(config_str, &config)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000082 diag->Error(android::DiagMessage()
83 << "invalid config '" << config_str << "' in split parameter '" << arg << "'");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070084 return false;
85 }
86 out_split->configs.insert(config);
87 }
88 return true;
89}
90
91std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000092 android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070093 std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>();
94 for (const std::string& config_arg : args) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070095 for (StringPiece config_str : util::Tokenize(config_arg, ',')) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070096 ConfigDescription config;
97 LocaleValue lv;
98 if (lv.InitFromFilterString(config_str)) {
99 lv.WriteTo(&config);
100 } else if (!ConfigDescription::Parse(config_str, &config)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000101 diag->Error(android::DiagMessage()
102 << "invalid config '" << config_str << "' for -c option");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700103 return {};
104 }
105
106 if (config.density != 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000107 diag->Warn(android::DiagMessage() << "ignoring density '" << config << "' for -c option");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700108 } else {
109 filter->AddConfig(config);
110 }
111 }
112 }
113 return std::move(filter);
114}
115
Mark Punzalan5579cad2023-10-30 13:47:51 -0700116bool ParseFeatureFlagsParameter(StringPiece arg, android::IDiagnostics* diag,
117 FeatureFlagValues* out_feature_flag_values) {
118 if (arg.empty()) {
119 return true;
120 }
121
122 for (StringPiece flag_and_value : util::Tokenize(arg, ',')) {
123 std::vector<std::string> parts = util::Split(flag_and_value, '=');
124 if (parts.empty()) {
125 continue;
126 }
127
128 if (parts.size() > 2) {
129 diag->Error(android::DiagMessage()
130 << "Invalid feature flag and optional value '" << flag_and_value
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000131 << "'. Must be in the format 'flag_name[:ro][=true|false]");
Mark Punzalan5579cad2023-10-30 13:47:51 -0700132 return false;
133 }
134
135 StringPiece flag_name = util::TrimWhitespace(parts[0]);
136 if (flag_name.empty()) {
137 diag->Error(android::DiagMessage() << "No name given for one or more flags in: " << arg);
138 return false;
139 }
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000140 std::vector<std::string> name_parts = util::Split(flag_name, ':');
141 if (name_parts.size() > 2) {
142 diag->Error(android::DiagMessage()
143 << "Invalid feature flag and optional value '" << flag_and_value
144 << "'. Must be in the format 'flag_name[:ro][=true|false]");
145 return false;
146 }
147 flag_name = name_parts[0];
148 bool read_only = false;
149 if (name_parts.size() == 2) {
150 if (name_parts[1] == "ro") {
151 read_only = true;
152 } else {
153 diag->Error(android::DiagMessage()
154 << "Invalid feature flag and optional value '" << flag_and_value
155 << "'. Must be in the format 'flag_name[:ro][=true|false]");
156 return false;
157 }
158 }
Mark Punzalan5579cad2023-10-30 13:47:51 -0700159
160 std::optional<bool> flag_value = {};
161 if (parts.size() == 2) {
162 StringPiece str_flag_value = util::TrimWhitespace(parts[1]);
163 if (!str_flag_value.empty()) {
164 flag_value = ResourceUtils::ParseBool(parts[1]);
165 if (!flag_value.has_value()) {
166 diag->Error(android::DiagMessage() << "Invalid value for feature flag '" << flag_and_value
167 << "'. Value must be 'true' or 'false'");
168 return false;
169 }
170 }
171 }
172
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000173 auto ffp = FeatureFlagProperties{read_only, flag_value};
174 if (auto [it, inserted] = out_feature_flag_values->try_emplace(std::string(flag_name), ffp);
Mark Punzalan5579cad2023-10-30 13:47:51 -0700175 !inserted) {
176 // We are allowing the same flag to appear multiple times, last value wins.
177 diag->Note(android::DiagMessage()
178 << "Value for feature flag '" << flag_name << "' was given more than once");
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000179 it->second = ffp;
Mark Punzalan5579cad2023-10-30 13:47:51 -0700180 }
181 }
182 return true;
183}
184
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700185// Adjust the SplitConstraints so that their SDK version is stripped if it
186// is less than or equal to the minSdk. Otherwise the resources that have had
187// their SDK version stripped due to minSdk won't ever match.
188std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
189 int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
190 std::vector<SplitConstraints> adjusted_constraints;
191 adjusted_constraints.reserve(split_constraints.size());
192 for (const SplitConstraints& constraints : split_constraints) {
193 SplitConstraints constraint;
194 for (const ConfigDescription& config : constraints.configs) {
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700195 const ConfigDescription &configToInsert = (config.sdkVersion <= min_sdk)
196 ? config.CopyWithoutSdkVersion()
197 : config;
198 // only add the config if it actually selects something
199 if (configToInsert != ConfigDescription::DefaultConfig()) {
200 constraint.configs.insert(configToInsert);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700201 }
202 }
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700203 constraint.name = constraints.name;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700204 adjusted_constraints.push_back(std::move(constraint));
205 }
206 return adjusted_constraints;
207}
208
209static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700210 return xml::AaptAttribute(Attribute(), id);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700211}
212
Adam Lesinski6b372992017-08-09 10:54:23 -0700213static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
214 xml::NamespaceDecl decl;
215 decl.prefix = "android";
216 decl.uri = xml::kSchemaAndroid;
217 return decl;
218}
219
Donald Chai414e48a2017-11-09 21:06:52 -0800220// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
221// replacing nonconforming characters with underscores.
222//
223// See frameworks/base/core/java/android/content/pm/PackageParser.java which
224// checks this at runtime.
Izabela Orlowska10560192018-04-13 11:56:35 +0100225std::string MakePackageSafeName(const std::string &name) {
Donald Chaib8f078c2017-10-18 23:51:18 -0700226 std::string result(name);
Donald Chai414e48a2017-11-09 21:06:52 -0800227 bool first = true;
Donald Chaib8f078c2017-10-18 23:51:18 -0700228 for (char &c : result) {
Donald Chai414e48a2017-11-09 21:06:52 -0800229 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
230 first = false;
231 continue;
Donald Chaib8f078c2017-10-18 23:51:18 -0700232 }
Donald Chai414e48a2017-11-09 21:06:52 -0800233 if (!first) {
234 if (c >= '0' && c <= '9') {
235 continue;
236 }
237 }
238
239 c = '_';
240 first = false;
Donald Chaib8f078c2017-10-18 23:51:18 -0700241 }
242 return result;
243}
244
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700245std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
246 const SplitConstraints& constraints) {
247 const ResourceId kVersionCode(0x0101021b);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700248 const ResourceId kVersionCodeMajor(0x01010576);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700249 const ResourceId kRevisionCode(0x010104d5);
250 const ResourceId kHasCode(0x0101000c);
251
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700252 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700253 manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700254 manifest_el->name = "manifest";
255 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
256
257 if (app_info.version_code) {
258 const uint32_t version_code = app_info.version_code.value();
259 manifest_el->attributes.push_back(xml::Attribute{
260 xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
261 CreateAttributeWithId(kVersionCode),
262 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
263 }
264
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700265 if (app_info.version_code_major) {
266 const uint32_t version_code_major = app_info.version_code_major.value();
267 manifest_el->attributes.push_back(xml::Attribute{
268 xml::kSchemaAndroid, "versionCodeMajor", std::to_string(version_code_major),
269 CreateAttributeWithId(kVersionCodeMajor),
270 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code_major)});
271 }
272
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700273 if (app_info.revision_code) {
274 const uint32_t revision_code = app_info.revision_code.value();
275 manifest_el->attributes.push_back(xml::Attribute{
276 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
277 CreateAttributeWithId(kRevisionCode),
278 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
279 }
280
281 std::stringstream split_name;
282 if (app_info.split_name) {
283 split_name << app_info.split_name.value() << ".";
284 }
Donald Chaib8f078c2017-10-18 23:51:18 -0700285 std::vector<std::string> sanitized_config_names;
286 for (const auto &config : constraints.configs) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000287 sanitized_config_names.push_back(MakePackageSafeName(config.toString().c_str()));
Donald Chaib8f078c2017-10-18 23:51:18 -0700288 }
289 split_name << "config." << util::Joiner(sanitized_config_names, "_");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700290
291 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
292
293 if (app_info.split_name) {
294 manifest_el->attributes.push_back(
295 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
296 }
297
Adam Lesinski6b372992017-08-09 10:54:23 -0700298 // Splits may contain more configurations than originally desired (fall-back densities, etc.).
299 // This makes programmatic discovery of split targeting difficult. Encode the original
Adam Lesinski3d632392017-07-24 17:08:32 -0700300 // split constraints intended for this split.
301 std::stringstream target_config_str;
302 target_config_str << util::Joiner(constraints.configs, ",");
303 manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
304
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700305 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
306 application_el->name = "application";
307 application_el->attributes.push_back(
308 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
309 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
310
311 manifest_el->AppendChild(std::move(application_el));
Adam Lesinski6b372992017-08-09 10:54:23 -0700312
313 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
314 doc->root = std::move(manifest_el);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700315 return doc;
316}
317
Ryan Mitchell4382e442021-07-14 12:53:01 -0700318static std::optional<std::string> ExtractCompiledString(const xml::Attribute& attr,
319 std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700320 if (attr.compiled_value != nullptr) {
321 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700322 if (compiled_str != nullptr) {
323 if (!compiled_str->value->empty()) {
324 return *compiled_str->value;
325 } else {
326 *out_error = "compiled value is an empty string";
327 return {};
328 }
329 }
330 *out_error = "compiled value is not a string";
331 return {};
332 }
333
334 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700335 if (!attr.value.empty()) {
336 return attr.value;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700337 }
338 *out_error = "value is an empty string";
339 return {};
340}
341
Ryan Mitchell4382e442021-07-14 12:53:01 -0700342static std::optional<uint32_t> ExtractCompiledInt(const xml::Attribute& attr,
343 std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700344 if (attr.compiled_value != nullptr) {
345 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700346 if (compiled_prim != nullptr) {
347 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
348 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
349 return compiled_prim->value.data;
350 }
351 }
352 *out_error = "compiled value is not an integer";
353 return {};
354 }
355
356 // Fallback to the plain text value if there is one.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700357 std::optional<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700358 if (integer) {
359 return integer;
360 }
361 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700362 error_msg << "'" << attr.value << "' is not a valid integer";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700363 *out_error = error_msg.str();
364 return {};
365}
366
Ryan Mitchell4382e442021-07-14 12:53:01 -0700367static std::optional<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700368 if (attr.compiled_value != nullptr) {
369 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700370 if (compiled_prim != nullptr) {
371 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
372 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
373 return compiled_prim->value.data;
374 }
375 *out_error = "compiled value is not an integer or string";
376 return {};
377 }
378
Adam Lesinski8780eb62017-10-31 17:44:39 -0700379 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700380 if (compiled_str != nullptr) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700381 std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700382 if (sdk_version) {
383 return sdk_version;
384 }
385
386 *out_error = "compiled string value is not a valid SDK version";
387 return {};
388 }
389 *out_error = "compiled value is not an integer or string";
390 return {};
391 }
392
393 // Fallback to the plain text value if there is one.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700394 std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700395 if (sdk_version) {
396 return sdk_version;
397 }
398 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700399 error_msg << "'" << attr.value << "' is not a valid SDK version";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700400 *out_error = error_msg.str();
401 return {};
402}
403
Ryan Mitchell4382e442021-07-14 12:53:01 -0700404std::optional<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000405 android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700406 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700407 const xml::Element* manifest_el = xml_res.root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700408 if (manifest_el == nullptr) {
409 return {};
410 }
411
412 AppInfo app_info;
413
414 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000415 diag->Error(android::DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700416 return {};
417 }
418
Adam Lesinski8780eb62017-10-31 17:44:39 -0700419 const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700420 if (!package_attr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000421 diag->Error(android::DiagMessage(xml_res.file.source)
422 << "<manifest> must have a 'package' attribute");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700423 return {};
424 }
425
426 std::string error_msg;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700427 std::optional<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700428 if (!maybe_package) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000429 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700430 << "invalid package name: " << error_msg);
431 return {};
432 }
433 app_info.package = maybe_package.value();
434
Adam Lesinski8780eb62017-10-31 17:44:39 -0700435 if (const xml::Attribute* version_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700436 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700437 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700438 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000439 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700440 << "invalid android:versionCode: " << error_msg);
441 return {};
442 }
443 app_info.version_code = maybe_code.value();
444 }
445
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700446 if (const xml::Attribute* version_code_major_attr =
447 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700448 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_major_attr, &error_msg);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700449 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000450 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
451 << "invalid android:versionCodeMajor: " << error_msg);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700452 return {};
453 }
454 app_info.version_code_major = maybe_code.value();
455 }
456
Adam Lesinski8780eb62017-10-31 17:44:39 -0700457 if (const xml::Attribute* revision_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700458 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700459 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700460 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000461 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700462 << "invalid android:revisionCode: " << error_msg);
463 return {};
464 }
465 app_info.revision_code = maybe_code.value();
466 }
467
Adam Lesinski8780eb62017-10-31 17:44:39 -0700468 if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700469 std::optional<std::string> maybe_split_name =
470 ExtractCompiledString(*split_name_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700471 if (!maybe_split_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000472 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700473 << "invalid split name: " << error_msg);
474 return {};
475 }
476 app_info.split_name = maybe_split_name.value();
477 }
478
Adam Lesinski8780eb62017-10-31 17:44:39 -0700479 if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
480 if (const xml::Attribute* min_sdk =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700481 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700482 std::optional<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700483 if (!maybe_sdk) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000484 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700485 << "invalid android:minSdkVersion: " << error_msg);
486 return {};
487 }
488 app_info.min_sdk_version = maybe_sdk.value();
489 }
490 }
491 return app_info;
492}
493
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700494void SetLongVersionCode(xml::Element* manifest, uint64_t version) {
495 // Write the low bits of the version code to android:versionCode
496 auto version_code = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCode");
497 version_code->value = StringPrintf("0x%08x", (uint32_t) (version & 0xffffffff));
498 version_code->compiled_value = ResourceUtils::TryParseInt(version_code->value);
499
500 auto version_high = (uint32_t) (version >> 32);
501 if (version_high != 0) {
502 // Write the high bits of the version code to android:versionCodeMajor
503 auto version_major = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCodeMajor");
504 version_major->value = StringPrintf("0x%08x", version_high);
505 version_major->compiled_value = ResourceUtils::TryParseInt(version_major->value);
506 } else {
507 manifest->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
508 }
509}
510
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100511std::regex GetRegularExpression(const std::string &input) {
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000512 // Standard ECMAScript grammar.
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100513 std::regex case_insensitive(
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000514 input, std::regex_constants::ECMAScript);
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100515 return case_insensitive;
516}
517
Iurii Makhno054e4332022-10-12 16:03:03 +0000518bool ParseResourceConfig(const std::string& content, IAaptContext* context,
519 std::unordered_set<ResourceName>& out_resource_exclude_list,
branliuf1ed5232022-12-16 19:02:29 +0800520 std::set<ResourceName>& out_name_collapse_exemptions,
521 std::set<ResourceName>& out_path_shorten_exemptions) {
Iurii Makhno054e4332022-10-12 16:03:03 +0000522 for (StringPiece line : util::Tokenize(content, '\n')) {
523 line = util::TrimWhitespace(line);
524 if (line.empty()) {
525 continue;
526 }
527
528 auto split_line = util::Split(line, '#');
529 if (split_line.size() < 2) {
530 context->GetDiagnostics()->Error(android::DiagMessage(line) << "No # found in line");
531 return false;
532 }
533 StringPiece resource_string = split_line[0];
534 StringPiece directives = split_line[1];
535 ResourceNameRef resource_name;
536 if (!ResourceUtils::ParseResourceName(resource_string, &resource_name)) {
537 context->GetDiagnostics()->Error(android::DiagMessage(line) << "Malformed resource name");
538 return false;
539 }
540 if (!resource_name.package.empty()) {
541 context->GetDiagnostics()->Error(android::DiagMessage(line)
542 << "Package set for resource. Only use type/name");
543 return false;
544 }
545 for (StringPiece directive : util::Tokenize(directives, ',')) {
546 if (directive == "remove") {
547 out_resource_exclude_list.insert(resource_name.ToResourceName());
548 } else if (directive == "no_collapse" || directive == "no_obfuscate") {
549 out_name_collapse_exemptions.insert(resource_name.ToResourceName());
branliuf1ed5232022-12-16 19:02:29 +0800550 } else if (directive == "no_path_shorten") {
551 out_path_shorten_exemptions.insert(resource_name.ToResourceName());
Iurii Makhno054e4332022-10-12 16:03:03 +0000552 }
553 }
554 }
555 return true;
556}
557
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700558} // namespace aapt