blob: 08f8f0d85807f439b9417c4f5e69c42b0df4a9da [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
Jeremy Meyere08e0372024-09-17 12:45:26 -070037std::optional<FeatureFlagAttribute> ParseFlag(std::optional<std::string_view> flag_text) {
38 if (!flag_text || flag_text->empty()) {
39 return {};
40 }
41 FeatureFlagAttribute flag;
42 if (flag_text->starts_with('!')) {
43 flag.negated = true;
44 flag.name = flag_text->substr(1);
45 } else {
46 flag.name = flag_text.value();
47 }
48 return flag;
49}
50
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -070051std::optional<FlagStatus> GetFlagStatus(const std::optional<FeatureFlagAttribute>& flag,
52 const FeatureFlagValues& feature_flag_values,
53 std::string* out_err) {
54 if (!flag) {
55 return FlagStatus::NoFlag;
56 }
57 auto flag_it = feature_flag_values.find(flag->name);
58 if (flag_it == feature_flag_values.end()) {
59 *out_err = "Resource flag value undefined: " + flag->name;
60 return {};
61 }
62 const auto& flag_properties = flag_it->second;
63 if (!flag_properties.read_only) {
64 *out_err = "Only read only flags may be used with resources: " + flag->name;
65 return {};
66 }
67 if (!flag_properties.enabled.has_value()) {
68 *out_err = "Only flags with a value may be used with resources: " + flag->name;
69 return {};
70 }
71 return (flag_properties.enabled.value() != flag->negated) ? FlagStatus::Enabled
72 : FlagStatus::Disabled;
73}
74
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070075std::optional<uint16_t> ParseTargetDensityParameter(StringPiece arg, android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070076 ConfigDescription preferred_density_config;
77 if (!ConfigDescription::Parse(arg, &preferred_density_config)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000078 diag->Error(android::DiagMessage()
79 << "invalid density '" << arg << "' for --preferred-density option");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070080 return {};
81 }
82
83 // Clear the version that can be automatically added.
84 preferred_density_config.sdkVersion = 0;
85
86 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
87 ConfigDescription::CONFIG_DENSITY) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000088 diag->Error(android::DiagMessage() << "invalid preferred density '" << arg << "'. "
89 << "Preferred density must only be a density value");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070090 return {};
91 }
92 return preferred_density_config.density;
93}
94
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070095bool ParseSplitParameter(StringPiece arg, android::IDiagnostics* diag, std::string* out_path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070096 SplitConstraints* out_split) {
97 CHECK(diag != nullptr);
98 CHECK(out_path != nullptr);
99 CHECK(out_split != nullptr);
100
Adam Lesinskidb091572017-04-13 12:48:56 -0700101#ifdef _WIN32
102 const char sSeparator = ';';
103#else
104 const char sSeparator = ':';
105#endif
106
107 std::vector<std::string> parts = util::Split(arg, sSeparator);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700108 if (parts.size() != 2) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000109 diag->Error(android::DiagMessage() << "invalid split parameter '" << arg << "'");
110 diag->Note(android::DiagMessage() << "should be --split path/to/output.apk" << sSeparator
111 << "<config>[,<config>...].");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700112 return false;
113 }
114
115 *out_path = parts[0];
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700116 out_split->name = parts[1];
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700117 for (StringPiece config_str : util::Tokenize(parts[1], ',')) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700118 ConfigDescription config;
119 if (!ConfigDescription::Parse(config_str, &config)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000120 diag->Error(android::DiagMessage()
121 << "invalid config '" << config_str << "' in split parameter '" << arg << "'");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700122 return false;
123 }
124 out_split->configs.insert(config);
125 }
126 return true;
127}
128
129std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000130 android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700131 std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>();
132 for (const std::string& config_arg : args) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700133 for (StringPiece config_str : util::Tokenize(config_arg, ',')) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700134 ConfigDescription config;
135 LocaleValue lv;
136 if (lv.InitFromFilterString(config_str)) {
137 lv.WriteTo(&config);
138 } else if (!ConfigDescription::Parse(config_str, &config)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000139 diag->Error(android::DiagMessage()
140 << "invalid config '" << config_str << "' for -c option");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700141 return {};
142 }
143
144 if (config.density != 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000145 diag->Warn(android::DiagMessage() << "ignoring density '" << config << "' for -c option");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700146 } else {
147 filter->AddConfig(config);
148 }
149 }
150 }
151 return std::move(filter);
152}
153
Mark Punzalan5579cad2023-10-30 13:47:51 -0700154bool ParseFeatureFlagsParameter(StringPiece arg, android::IDiagnostics* diag,
155 FeatureFlagValues* out_feature_flag_values) {
156 if (arg.empty()) {
157 return true;
158 }
159
160 for (StringPiece flag_and_value : util::Tokenize(arg, ',')) {
161 std::vector<std::string> parts = util::Split(flag_and_value, '=');
162 if (parts.empty()) {
163 continue;
164 }
165
166 if (parts.size() > 2) {
167 diag->Error(android::DiagMessage()
168 << "Invalid feature flag and optional value '" << flag_and_value
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000169 << "'. Must be in the format 'flag_name[:ro][=true|false]");
Mark Punzalan5579cad2023-10-30 13:47:51 -0700170 return false;
171 }
172
173 StringPiece flag_name = util::TrimWhitespace(parts[0]);
174 if (flag_name.empty()) {
175 diag->Error(android::DiagMessage() << "No name given for one or more flags in: " << arg);
176 return false;
177 }
Jeremy Meyer94a53222024-07-16 12:27:47 -0700178
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000179 std::vector<std::string> name_parts = util::Split(flag_name, ':');
180 if (name_parts.size() > 2) {
181 diag->Error(android::DiagMessage()
182 << "Invalid feature flag and optional value '" << flag_and_value
Jeremy Meyer94a53222024-07-16 12:27:47 -0700183 << "'. Must be in the format 'flag_name[:READ_ONLY|READ_WRITE][=true|false]");
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000184 return false;
185 }
186 flag_name = name_parts[0];
187 bool read_only = false;
188 if (name_parts.size() == 2) {
Jeremy Meyer94a53222024-07-16 12:27:47 -0700189 if (name_parts[1] == "ro" || name_parts[1] == "READ_ONLY") {
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000190 read_only = true;
Jeremy Meyer94a53222024-07-16 12:27:47 -0700191 } else if (name_parts[1] == "READ_WRITE") {
192 read_only = false;
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000193 } else {
194 diag->Error(android::DiagMessage()
195 << "Invalid feature flag and optional value '" << flag_and_value
Jeremy Meyer94a53222024-07-16 12:27:47 -0700196 << "'. Must be in the format 'flag_name[:READ_ONLY|READ_WRITE][=true|false]");
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000197 return false;
198 }
199 }
Mark Punzalan5579cad2023-10-30 13:47:51 -0700200
201 std::optional<bool> flag_value = {};
202 if (parts.size() == 2) {
203 StringPiece str_flag_value = util::TrimWhitespace(parts[1]);
204 if (!str_flag_value.empty()) {
205 flag_value = ResourceUtils::ParseBool(parts[1]);
206 if (!flag_value.has_value()) {
207 diag->Error(android::DiagMessage() << "Invalid value for feature flag '" << flag_and_value
208 << "'. Value must be 'true' or 'false'");
209 return false;
210 }
211 }
212 }
213
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000214 auto ffp = FeatureFlagProperties{read_only, flag_value};
215 if (auto [it, inserted] = out_feature_flag_values->try_emplace(std::string(flag_name), ffp);
Mark Punzalan5579cad2023-10-30 13:47:51 -0700216 !inserted) {
217 // We are allowing the same flag to appear multiple times, last value wins.
218 diag->Note(android::DiagMessage()
219 << "Value for feature flag '" << flag_name << "' was given more than once");
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000220 it->second = ffp;
Mark Punzalan5579cad2023-10-30 13:47:51 -0700221 }
222 }
223 return true;
224}
225
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700226// Adjust the SplitConstraints so that their SDK version is stripped if it
227// is less than or equal to the minSdk. Otherwise the resources that have had
228// their SDK version stripped due to minSdk won't ever match.
229std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
230 int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
231 std::vector<SplitConstraints> adjusted_constraints;
232 adjusted_constraints.reserve(split_constraints.size());
233 for (const SplitConstraints& constraints : split_constraints) {
234 SplitConstraints constraint;
235 for (const ConfigDescription& config : constraints.configs) {
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700236 const ConfigDescription &configToInsert = (config.sdkVersion <= min_sdk)
237 ? config.CopyWithoutSdkVersion()
238 : config;
239 // only add the config if it actually selects something
240 if (configToInsert != ConfigDescription::DefaultConfig()) {
241 constraint.configs.insert(configToInsert);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700242 }
243 }
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700244 constraint.name = constraints.name;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700245 adjusted_constraints.push_back(std::move(constraint));
246 }
247 return adjusted_constraints;
248}
249
250static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700251 return xml::AaptAttribute(Attribute(), id);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700252}
253
Adam Lesinski6b372992017-08-09 10:54:23 -0700254static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
255 xml::NamespaceDecl decl;
256 decl.prefix = "android";
257 decl.uri = xml::kSchemaAndroid;
258 return decl;
259}
260
Donald Chai414e48a2017-11-09 21:06:52 -0800261// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
262// replacing nonconforming characters with underscores.
263//
264// See frameworks/base/core/java/android/content/pm/PackageParser.java which
265// checks this at runtime.
Izabela Orlowska10560192018-04-13 11:56:35 +0100266std::string MakePackageSafeName(const std::string &name) {
Donald Chaib8f078c2017-10-18 23:51:18 -0700267 std::string result(name);
Donald Chai414e48a2017-11-09 21:06:52 -0800268 bool first = true;
Donald Chaib8f078c2017-10-18 23:51:18 -0700269 for (char &c : result) {
Donald Chai414e48a2017-11-09 21:06:52 -0800270 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
271 first = false;
272 continue;
Donald Chaib8f078c2017-10-18 23:51:18 -0700273 }
Donald Chai414e48a2017-11-09 21:06:52 -0800274 if (!first) {
275 if (c >= '0' && c <= '9') {
276 continue;
277 }
278 }
279
280 c = '_';
281 first = false;
Donald Chaib8f078c2017-10-18 23:51:18 -0700282 }
283 return result;
284}
285
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700286std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
287 const SplitConstraints& constraints) {
288 const ResourceId kVersionCode(0x0101021b);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700289 const ResourceId kVersionCodeMajor(0x01010576);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700290 const ResourceId kRevisionCode(0x010104d5);
291 const ResourceId kHasCode(0x0101000c);
292
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700293 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700294 manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700295 manifest_el->name = "manifest";
296 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
297
298 if (app_info.version_code) {
299 const uint32_t version_code = app_info.version_code.value();
300 manifest_el->attributes.push_back(xml::Attribute{
301 xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
302 CreateAttributeWithId(kVersionCode),
303 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
304 }
305
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700306 if (app_info.version_code_major) {
307 const uint32_t version_code_major = app_info.version_code_major.value();
308 manifest_el->attributes.push_back(xml::Attribute{
309 xml::kSchemaAndroid, "versionCodeMajor", std::to_string(version_code_major),
310 CreateAttributeWithId(kVersionCodeMajor),
311 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code_major)});
312 }
313
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700314 if (app_info.revision_code) {
315 const uint32_t revision_code = app_info.revision_code.value();
316 manifest_el->attributes.push_back(xml::Attribute{
317 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
318 CreateAttributeWithId(kRevisionCode),
319 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
320 }
321
322 std::stringstream split_name;
323 if (app_info.split_name) {
324 split_name << app_info.split_name.value() << ".";
325 }
Donald Chaib8f078c2017-10-18 23:51:18 -0700326 std::vector<std::string> sanitized_config_names;
327 for (const auto &config : constraints.configs) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000328 sanitized_config_names.push_back(MakePackageSafeName(config.toString().c_str()));
Donald Chaib8f078c2017-10-18 23:51:18 -0700329 }
330 split_name << "config." << util::Joiner(sanitized_config_names, "_");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700331
332 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
333
334 if (app_info.split_name) {
335 manifest_el->attributes.push_back(
336 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
337 }
338
Adam Lesinski6b372992017-08-09 10:54:23 -0700339 // Splits may contain more configurations than originally desired (fall-back densities, etc.).
340 // This makes programmatic discovery of split targeting difficult. Encode the original
Adam Lesinski3d632392017-07-24 17:08:32 -0700341 // split constraints intended for this split.
342 std::stringstream target_config_str;
343 target_config_str << util::Joiner(constraints.configs, ",");
344 manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
345
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700346 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
347 application_el->name = "application";
348 application_el->attributes.push_back(
349 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
350 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
351
352 manifest_el->AppendChild(std::move(application_el));
Adam Lesinski6b372992017-08-09 10:54:23 -0700353
354 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
355 doc->root = std::move(manifest_el);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700356 return doc;
357}
358
Ryan Mitchell4382e442021-07-14 12:53:01 -0700359static std::optional<std::string> ExtractCompiledString(const xml::Attribute& attr,
360 std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700361 if (attr.compiled_value != nullptr) {
362 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700363 if (compiled_str != nullptr) {
364 if (!compiled_str->value->empty()) {
365 return *compiled_str->value;
366 } else {
367 *out_error = "compiled value is an empty string";
368 return {};
369 }
370 }
371 *out_error = "compiled value is not a string";
372 return {};
373 }
374
375 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700376 if (!attr.value.empty()) {
377 return attr.value;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700378 }
379 *out_error = "value is an empty string";
380 return {};
381}
382
Ryan Mitchell4382e442021-07-14 12:53:01 -0700383static std::optional<uint32_t> ExtractCompiledInt(const xml::Attribute& attr,
384 std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700385 if (attr.compiled_value != nullptr) {
386 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700387 if (compiled_prim != nullptr) {
388 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
389 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
390 return compiled_prim->value.data;
391 }
392 }
393 *out_error = "compiled value is not an integer";
394 return {};
395 }
396
397 // Fallback to the plain text value if there is one.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700398 std::optional<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700399 if (integer) {
400 return integer;
401 }
402 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700403 error_msg << "'" << attr.value << "' is not a valid integer";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700404 *out_error = error_msg.str();
405 return {};
406}
407
Ryan Mitchell4382e442021-07-14 12:53:01 -0700408static std::optional<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700409 if (attr.compiled_value != nullptr) {
410 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700411 if (compiled_prim != nullptr) {
412 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
413 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
414 return compiled_prim->value.data;
415 }
416 *out_error = "compiled value is not an integer or string";
417 return {};
418 }
419
Adam Lesinski8780eb62017-10-31 17:44:39 -0700420 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700421 if (compiled_str != nullptr) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700422 std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700423 if (sdk_version) {
424 return sdk_version;
425 }
426
427 *out_error = "compiled string value is not a valid SDK version";
428 return {};
429 }
430 *out_error = "compiled value is not an integer or string";
431 return {};
432 }
433
434 // Fallback to the plain text value if there is one.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700435 std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700436 if (sdk_version) {
437 return sdk_version;
438 }
439 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700440 error_msg << "'" << attr.value << "' is not a valid SDK version";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700441 *out_error = error_msg.str();
442 return {};
443}
444
Ryan Mitchell4382e442021-07-14 12:53:01 -0700445std::optional<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000446 android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700447 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700448 const xml::Element* manifest_el = xml_res.root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700449 if (manifest_el == nullptr) {
450 return {};
451 }
452
453 AppInfo app_info;
454
455 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000456 diag->Error(android::DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700457 return {};
458 }
459
Adam Lesinski8780eb62017-10-31 17:44:39 -0700460 const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700461 if (!package_attr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000462 diag->Error(android::DiagMessage(xml_res.file.source)
463 << "<manifest> must have a 'package' attribute");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700464 return {};
465 }
466
467 std::string error_msg;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700468 std::optional<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700469 if (!maybe_package) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000470 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700471 << "invalid package name: " << error_msg);
472 return {};
473 }
474 app_info.package = maybe_package.value();
475
Adam Lesinski8780eb62017-10-31 17:44:39 -0700476 if (const xml::Attribute* version_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700477 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700478 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700479 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000480 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700481 << "invalid android:versionCode: " << error_msg);
482 return {};
483 }
484 app_info.version_code = maybe_code.value();
485 }
486
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700487 if (const xml::Attribute* version_code_major_attr =
488 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700489 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_major_attr, &error_msg);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700490 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000491 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
492 << "invalid android:versionCodeMajor: " << error_msg);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700493 return {};
494 }
495 app_info.version_code_major = maybe_code.value();
496 }
497
Adam Lesinski8780eb62017-10-31 17:44:39 -0700498 if (const xml::Attribute* revision_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700499 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700500 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700501 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000502 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700503 << "invalid android:revisionCode: " << error_msg);
504 return {};
505 }
506 app_info.revision_code = maybe_code.value();
507 }
508
Adam Lesinski8780eb62017-10-31 17:44:39 -0700509 if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700510 std::optional<std::string> maybe_split_name =
511 ExtractCompiledString(*split_name_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700512 if (!maybe_split_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000513 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700514 << "invalid split name: " << error_msg);
515 return {};
516 }
517 app_info.split_name = maybe_split_name.value();
518 }
519
Adam Lesinski8780eb62017-10-31 17:44:39 -0700520 if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
521 if (const xml::Attribute* min_sdk =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700522 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700523 std::optional<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700524 if (!maybe_sdk) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000525 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700526 << "invalid android:minSdkVersion: " << error_msg);
527 return {};
528 }
529 app_info.min_sdk_version = maybe_sdk.value();
530 }
531 }
532 return app_info;
533}
534
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700535void SetLongVersionCode(xml::Element* manifest, uint64_t version) {
536 // Write the low bits of the version code to android:versionCode
537 auto version_code = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCode");
538 version_code->value = StringPrintf("0x%08x", (uint32_t) (version & 0xffffffff));
539 version_code->compiled_value = ResourceUtils::TryParseInt(version_code->value);
540
541 auto version_high = (uint32_t) (version >> 32);
542 if (version_high != 0) {
543 // Write the high bits of the version code to android:versionCodeMajor
544 auto version_major = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCodeMajor");
545 version_major->value = StringPrintf("0x%08x", version_high);
546 version_major->compiled_value = ResourceUtils::TryParseInt(version_major->value);
547 } else {
548 manifest->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
549 }
550}
551
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100552std::regex GetRegularExpression(const std::string &input) {
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000553 // Standard ECMAScript grammar.
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100554 std::regex case_insensitive(
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000555 input, std::regex_constants::ECMAScript);
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100556 return case_insensitive;
557}
558
Iurii Makhno054e4332022-10-12 16:03:03 +0000559bool ParseResourceConfig(const std::string& content, IAaptContext* context,
560 std::unordered_set<ResourceName>& out_resource_exclude_list,
branliuf1ed5232022-12-16 19:02:29 +0800561 std::set<ResourceName>& out_name_collapse_exemptions,
562 std::set<ResourceName>& out_path_shorten_exemptions) {
Iurii Makhno054e4332022-10-12 16:03:03 +0000563 for (StringPiece line : util::Tokenize(content, '\n')) {
564 line = util::TrimWhitespace(line);
565 if (line.empty()) {
566 continue;
567 }
568
569 auto split_line = util::Split(line, '#');
570 if (split_line.size() < 2) {
571 context->GetDiagnostics()->Error(android::DiagMessage(line) << "No # found in line");
572 return false;
573 }
574 StringPiece resource_string = split_line[0];
575 StringPiece directives = split_line[1];
576 ResourceNameRef resource_name;
577 if (!ResourceUtils::ParseResourceName(resource_string, &resource_name)) {
578 context->GetDiagnostics()->Error(android::DiagMessage(line) << "Malformed resource name");
579 return false;
580 }
581 if (!resource_name.package.empty()) {
582 context->GetDiagnostics()->Error(android::DiagMessage(line)
583 << "Package set for resource. Only use type/name");
584 return false;
585 }
586 for (StringPiece directive : util::Tokenize(directives, ',')) {
587 if (directive == "remove") {
588 out_resource_exclude_list.insert(resource_name.ToResourceName());
589 } else if (directive == "no_collapse" || directive == "no_obfuscate") {
590 out_name_collapse_exemptions.insert(resource_name.ToResourceName());
branliuf1ed5232022-12-16 19:02:29 +0800591 } else if (directive == "no_path_shorten") {
592 out_path_shorten_exemptions.insert(resource_name.ToResourceName());
Iurii Makhno054e4332022-10-12 16:03:03 +0000593 }
594 }
595 }
596 return true;
597}
598
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700599} // namespace aapt