blob: 2177c345260c5f986fae0c0028fd55861377b235 [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 Meyer3d8d4a12024-08-23 17:29:03 -070037std::optional<FlagStatus> GetFlagStatus(const std::optional<FeatureFlagAttribute>& flag,
38 const FeatureFlagValues& feature_flag_values,
39 std::string* out_err) {
40 if (!flag) {
41 return FlagStatus::NoFlag;
42 }
43 auto flag_it = feature_flag_values.find(flag->name);
44 if (flag_it == feature_flag_values.end()) {
45 *out_err = "Resource flag value undefined: " + flag->name;
46 return {};
47 }
48 const auto& flag_properties = flag_it->second;
49 if (!flag_properties.read_only) {
50 *out_err = "Only read only flags may be used with resources: " + flag->name;
51 return {};
52 }
53 if (!flag_properties.enabled.has_value()) {
54 *out_err = "Only flags with a value may be used with resources: " + flag->name;
55 return {};
56 }
57 return (flag_properties.enabled.value() != flag->negated) ? FlagStatus::Enabled
58 : FlagStatus::Disabled;
59}
60
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070061std::optional<uint16_t> ParseTargetDensityParameter(StringPiece arg, android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070062 ConfigDescription preferred_density_config;
63 if (!ConfigDescription::Parse(arg, &preferred_density_config)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000064 diag->Error(android::DiagMessage()
65 << "invalid density '" << arg << "' for --preferred-density option");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070066 return {};
67 }
68
69 // Clear the version that can be automatically added.
70 preferred_density_config.sdkVersion = 0;
71
72 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
73 ConfigDescription::CONFIG_DENSITY) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000074 diag->Error(android::DiagMessage() << "invalid preferred density '" << arg << "'. "
75 << "Preferred density must only be a density value");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070076 return {};
77 }
78 return preferred_density_config.density;
79}
80
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070081bool ParseSplitParameter(StringPiece arg, android::IDiagnostics* diag, std::string* out_path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070082 SplitConstraints* out_split) {
83 CHECK(diag != nullptr);
84 CHECK(out_path != nullptr);
85 CHECK(out_split != nullptr);
86
Adam Lesinskidb091572017-04-13 12:48:56 -070087#ifdef _WIN32
88 const char sSeparator = ';';
89#else
90 const char sSeparator = ':';
91#endif
92
93 std::vector<std::string> parts = util::Split(arg, sSeparator);
Adam Lesinskid0f492d2017-04-03 18:12:45 -070094 if (parts.size() != 2) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000095 diag->Error(android::DiagMessage() << "invalid split parameter '" << arg << "'");
96 diag->Note(android::DiagMessage() << "should be --split path/to/output.apk" << sSeparator
97 << "<config>[,<config>...].");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070098 return false;
99 }
100
101 *out_path = parts[0];
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700102 out_split->name = parts[1];
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700103 for (StringPiece config_str : util::Tokenize(parts[1], ',')) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700104 ConfigDescription config;
105 if (!ConfigDescription::Parse(config_str, &config)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000106 diag->Error(android::DiagMessage()
107 << "invalid config '" << config_str << "' in split parameter '" << arg << "'");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700108 return false;
109 }
110 out_split->configs.insert(config);
111 }
112 return true;
113}
114
115std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000116 android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700117 std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>();
118 for (const std::string& config_arg : args) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700119 for (StringPiece config_str : util::Tokenize(config_arg, ',')) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700120 ConfigDescription config;
121 LocaleValue lv;
122 if (lv.InitFromFilterString(config_str)) {
123 lv.WriteTo(&config);
124 } else if (!ConfigDescription::Parse(config_str, &config)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000125 diag->Error(android::DiagMessage()
126 << "invalid config '" << config_str << "' for -c option");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700127 return {};
128 }
129
130 if (config.density != 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000131 diag->Warn(android::DiagMessage() << "ignoring density '" << config << "' for -c option");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700132 } else {
133 filter->AddConfig(config);
134 }
135 }
136 }
137 return std::move(filter);
138}
139
Mark Punzalan5579cad2023-10-30 13:47:51 -0700140bool ParseFeatureFlagsParameter(StringPiece arg, android::IDiagnostics* diag,
141 FeatureFlagValues* out_feature_flag_values) {
142 if (arg.empty()) {
143 return true;
144 }
145
146 for (StringPiece flag_and_value : util::Tokenize(arg, ',')) {
147 std::vector<std::string> parts = util::Split(flag_and_value, '=');
148 if (parts.empty()) {
149 continue;
150 }
151
152 if (parts.size() > 2) {
153 diag->Error(android::DiagMessage()
154 << "Invalid feature flag and optional value '" << flag_and_value
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000155 << "'. Must be in the format 'flag_name[:ro][=true|false]");
Mark Punzalan5579cad2023-10-30 13:47:51 -0700156 return false;
157 }
158
159 StringPiece flag_name = util::TrimWhitespace(parts[0]);
160 if (flag_name.empty()) {
161 diag->Error(android::DiagMessage() << "No name given for one or more flags in: " << arg);
162 return false;
163 }
Jeremy Meyer94a53222024-07-16 12:27:47 -0700164
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000165 std::vector<std::string> name_parts = util::Split(flag_name, ':');
166 if (name_parts.size() > 2) {
167 diag->Error(android::DiagMessage()
168 << "Invalid feature flag and optional value '" << flag_and_value
Jeremy Meyer94a53222024-07-16 12:27:47 -0700169 << "'. Must be in the format 'flag_name[:READ_ONLY|READ_WRITE][=true|false]");
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000170 return false;
171 }
172 flag_name = name_parts[0];
173 bool read_only = false;
174 if (name_parts.size() == 2) {
Jeremy Meyer94a53222024-07-16 12:27:47 -0700175 if (name_parts[1] == "ro" || name_parts[1] == "READ_ONLY") {
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000176 read_only = true;
Jeremy Meyer94a53222024-07-16 12:27:47 -0700177 } else if (name_parts[1] == "READ_WRITE") {
178 read_only = false;
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000179 } else {
180 diag->Error(android::DiagMessage()
181 << "Invalid feature flag and optional value '" << flag_and_value
Jeremy Meyer94a53222024-07-16 12:27:47 -0700182 << "'. Must be in the format 'flag_name[:READ_ONLY|READ_WRITE][=true|false]");
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000183 return false;
184 }
185 }
Mark Punzalan5579cad2023-10-30 13:47:51 -0700186
187 std::optional<bool> flag_value = {};
188 if (parts.size() == 2) {
189 StringPiece str_flag_value = util::TrimWhitespace(parts[1]);
190 if (!str_flag_value.empty()) {
191 flag_value = ResourceUtils::ParseBool(parts[1]);
192 if (!flag_value.has_value()) {
193 diag->Error(android::DiagMessage() << "Invalid value for feature flag '" << flag_and_value
194 << "'. Value must be 'true' or 'false'");
195 return false;
196 }
197 }
198 }
199
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000200 auto ffp = FeatureFlagProperties{read_only, flag_value};
201 if (auto [it, inserted] = out_feature_flag_values->try_emplace(std::string(flag_name), ffp);
Mark Punzalan5579cad2023-10-30 13:47:51 -0700202 !inserted) {
203 // We are allowing the same flag to appear multiple times, last value wins.
204 diag->Note(android::DiagMessage()
205 << "Value for feature flag '" << flag_name << "' was given more than once");
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000206 it->second = ffp;
Mark Punzalan5579cad2023-10-30 13:47:51 -0700207 }
208 }
209 return true;
210}
211
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700212// Adjust the SplitConstraints so that their SDK version is stripped if it
213// is less than or equal to the minSdk. Otherwise the resources that have had
214// their SDK version stripped due to minSdk won't ever match.
215std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
216 int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
217 std::vector<SplitConstraints> adjusted_constraints;
218 adjusted_constraints.reserve(split_constraints.size());
219 for (const SplitConstraints& constraints : split_constraints) {
220 SplitConstraints constraint;
221 for (const ConfigDescription& config : constraints.configs) {
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700222 const ConfigDescription &configToInsert = (config.sdkVersion <= min_sdk)
223 ? config.CopyWithoutSdkVersion()
224 : config;
225 // only add the config if it actually selects something
226 if (configToInsert != ConfigDescription::DefaultConfig()) {
227 constraint.configs.insert(configToInsert);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700228 }
229 }
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700230 constraint.name = constraints.name;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700231 adjusted_constraints.push_back(std::move(constraint));
232 }
233 return adjusted_constraints;
234}
235
236static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700237 return xml::AaptAttribute(Attribute(), id);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700238}
239
Adam Lesinski6b372992017-08-09 10:54:23 -0700240static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
241 xml::NamespaceDecl decl;
242 decl.prefix = "android";
243 decl.uri = xml::kSchemaAndroid;
244 return decl;
245}
246
Donald Chai414e48a2017-11-09 21:06:52 -0800247// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
248// replacing nonconforming characters with underscores.
249//
250// See frameworks/base/core/java/android/content/pm/PackageParser.java which
251// checks this at runtime.
Izabela Orlowska10560192018-04-13 11:56:35 +0100252std::string MakePackageSafeName(const std::string &name) {
Donald Chaib8f078c2017-10-18 23:51:18 -0700253 std::string result(name);
Donald Chai414e48a2017-11-09 21:06:52 -0800254 bool first = true;
Donald Chaib8f078c2017-10-18 23:51:18 -0700255 for (char &c : result) {
Donald Chai414e48a2017-11-09 21:06:52 -0800256 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
257 first = false;
258 continue;
Donald Chaib8f078c2017-10-18 23:51:18 -0700259 }
Donald Chai414e48a2017-11-09 21:06:52 -0800260 if (!first) {
261 if (c >= '0' && c <= '9') {
262 continue;
263 }
264 }
265
266 c = '_';
267 first = false;
Donald Chaib8f078c2017-10-18 23:51:18 -0700268 }
269 return result;
270}
271
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700272std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
273 const SplitConstraints& constraints) {
274 const ResourceId kVersionCode(0x0101021b);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700275 const ResourceId kVersionCodeMajor(0x01010576);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700276 const ResourceId kRevisionCode(0x010104d5);
277 const ResourceId kHasCode(0x0101000c);
278
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700279 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700280 manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700281 manifest_el->name = "manifest";
282 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
283
284 if (app_info.version_code) {
285 const uint32_t version_code = app_info.version_code.value();
286 manifest_el->attributes.push_back(xml::Attribute{
287 xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
288 CreateAttributeWithId(kVersionCode),
289 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
290 }
291
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700292 if (app_info.version_code_major) {
293 const uint32_t version_code_major = app_info.version_code_major.value();
294 manifest_el->attributes.push_back(xml::Attribute{
295 xml::kSchemaAndroid, "versionCodeMajor", std::to_string(version_code_major),
296 CreateAttributeWithId(kVersionCodeMajor),
297 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code_major)});
298 }
299
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700300 if (app_info.revision_code) {
301 const uint32_t revision_code = app_info.revision_code.value();
302 manifest_el->attributes.push_back(xml::Attribute{
303 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
304 CreateAttributeWithId(kRevisionCode),
305 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
306 }
307
308 std::stringstream split_name;
309 if (app_info.split_name) {
310 split_name << app_info.split_name.value() << ".";
311 }
Donald Chaib8f078c2017-10-18 23:51:18 -0700312 std::vector<std::string> sanitized_config_names;
313 for (const auto &config : constraints.configs) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000314 sanitized_config_names.push_back(MakePackageSafeName(config.toString().c_str()));
Donald Chaib8f078c2017-10-18 23:51:18 -0700315 }
316 split_name << "config." << util::Joiner(sanitized_config_names, "_");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700317
318 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
319
320 if (app_info.split_name) {
321 manifest_el->attributes.push_back(
322 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
323 }
324
Adam Lesinski6b372992017-08-09 10:54:23 -0700325 // Splits may contain more configurations than originally desired (fall-back densities, etc.).
326 // This makes programmatic discovery of split targeting difficult. Encode the original
Adam Lesinski3d632392017-07-24 17:08:32 -0700327 // split constraints intended for this split.
328 std::stringstream target_config_str;
329 target_config_str << util::Joiner(constraints.configs, ",");
330 manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
331
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700332 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
333 application_el->name = "application";
334 application_el->attributes.push_back(
335 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
336 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
337
338 manifest_el->AppendChild(std::move(application_el));
Adam Lesinski6b372992017-08-09 10:54:23 -0700339
340 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
341 doc->root = std::move(manifest_el);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700342 return doc;
343}
344
Ryan Mitchell4382e442021-07-14 12:53:01 -0700345static std::optional<std::string> ExtractCompiledString(const xml::Attribute& attr,
346 std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700347 if (attr.compiled_value != nullptr) {
348 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700349 if (compiled_str != nullptr) {
350 if (!compiled_str->value->empty()) {
351 return *compiled_str->value;
352 } else {
353 *out_error = "compiled value is an empty string";
354 return {};
355 }
356 }
357 *out_error = "compiled value is not a string";
358 return {};
359 }
360
361 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700362 if (!attr.value.empty()) {
363 return attr.value;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700364 }
365 *out_error = "value is an empty string";
366 return {};
367}
368
Ryan Mitchell4382e442021-07-14 12:53:01 -0700369static std::optional<uint32_t> ExtractCompiledInt(const xml::Attribute& attr,
370 std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700371 if (attr.compiled_value != nullptr) {
372 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700373 if (compiled_prim != nullptr) {
374 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
375 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
376 return compiled_prim->value.data;
377 }
378 }
379 *out_error = "compiled value is not an integer";
380 return {};
381 }
382
383 // Fallback to the plain text value if there is one.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700384 std::optional<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700385 if (integer) {
386 return integer;
387 }
388 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700389 error_msg << "'" << attr.value << "' is not a valid integer";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700390 *out_error = error_msg.str();
391 return {};
392}
393
Ryan Mitchell4382e442021-07-14 12:53:01 -0700394static std::optional<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700395 if (attr.compiled_value != nullptr) {
396 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700397 if (compiled_prim != nullptr) {
398 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
399 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
400 return compiled_prim->value.data;
401 }
402 *out_error = "compiled value is not an integer or string";
403 return {};
404 }
405
Adam Lesinski8780eb62017-10-31 17:44:39 -0700406 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700407 if (compiled_str != nullptr) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700408 std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700409 if (sdk_version) {
410 return sdk_version;
411 }
412
413 *out_error = "compiled string value is not a valid SDK version";
414 return {};
415 }
416 *out_error = "compiled value is not an integer or string";
417 return {};
418 }
419
420 // Fallback to the plain text value if there is one.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700421 std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700422 if (sdk_version) {
423 return sdk_version;
424 }
425 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700426 error_msg << "'" << attr.value << "' is not a valid SDK version";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700427 *out_error = error_msg.str();
428 return {};
429}
430
Ryan Mitchell4382e442021-07-14 12:53:01 -0700431std::optional<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000432 android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700433 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700434 const xml::Element* manifest_el = xml_res.root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700435 if (manifest_el == nullptr) {
436 return {};
437 }
438
439 AppInfo app_info;
440
441 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000442 diag->Error(android::DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700443 return {};
444 }
445
Adam Lesinski8780eb62017-10-31 17:44:39 -0700446 const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700447 if (!package_attr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000448 diag->Error(android::DiagMessage(xml_res.file.source)
449 << "<manifest> must have a 'package' attribute");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700450 return {};
451 }
452
453 std::string error_msg;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700454 std::optional<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700455 if (!maybe_package) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000456 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700457 << "invalid package name: " << error_msg);
458 return {};
459 }
460 app_info.package = maybe_package.value();
461
Adam Lesinski8780eb62017-10-31 17:44:39 -0700462 if (const xml::Attribute* version_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700463 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700464 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700465 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000466 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700467 << "invalid android:versionCode: " << error_msg);
468 return {};
469 }
470 app_info.version_code = maybe_code.value();
471 }
472
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700473 if (const xml::Attribute* version_code_major_attr =
474 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700475 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_major_attr, &error_msg);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700476 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000477 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
478 << "invalid android:versionCodeMajor: " << error_msg);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700479 return {};
480 }
481 app_info.version_code_major = maybe_code.value();
482 }
483
Adam Lesinski8780eb62017-10-31 17:44:39 -0700484 if (const xml::Attribute* revision_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700485 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700486 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700487 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000488 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700489 << "invalid android:revisionCode: " << error_msg);
490 return {};
491 }
492 app_info.revision_code = maybe_code.value();
493 }
494
Adam Lesinski8780eb62017-10-31 17:44:39 -0700495 if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700496 std::optional<std::string> maybe_split_name =
497 ExtractCompiledString(*split_name_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700498 if (!maybe_split_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000499 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700500 << "invalid split name: " << error_msg);
501 return {};
502 }
503 app_info.split_name = maybe_split_name.value();
504 }
505
Adam Lesinski8780eb62017-10-31 17:44:39 -0700506 if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
507 if (const xml::Attribute* min_sdk =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700508 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700509 std::optional<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700510 if (!maybe_sdk) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000511 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700512 << "invalid android:minSdkVersion: " << error_msg);
513 return {};
514 }
515 app_info.min_sdk_version = maybe_sdk.value();
516 }
517 }
518 return app_info;
519}
520
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700521void SetLongVersionCode(xml::Element* manifest, uint64_t version) {
522 // Write the low bits of the version code to android:versionCode
523 auto version_code = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCode");
524 version_code->value = StringPrintf("0x%08x", (uint32_t) (version & 0xffffffff));
525 version_code->compiled_value = ResourceUtils::TryParseInt(version_code->value);
526
527 auto version_high = (uint32_t) (version >> 32);
528 if (version_high != 0) {
529 // Write the high bits of the version code to android:versionCodeMajor
530 auto version_major = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCodeMajor");
531 version_major->value = StringPrintf("0x%08x", version_high);
532 version_major->compiled_value = ResourceUtils::TryParseInt(version_major->value);
533 } else {
534 manifest->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
535 }
536}
537
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100538std::regex GetRegularExpression(const std::string &input) {
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000539 // Standard ECMAScript grammar.
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100540 std::regex case_insensitive(
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000541 input, std::regex_constants::ECMAScript);
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100542 return case_insensitive;
543}
544
Iurii Makhno054e4332022-10-12 16:03:03 +0000545bool ParseResourceConfig(const std::string& content, IAaptContext* context,
546 std::unordered_set<ResourceName>& out_resource_exclude_list,
branliuf1ed5232022-12-16 19:02:29 +0800547 std::set<ResourceName>& out_name_collapse_exemptions,
548 std::set<ResourceName>& out_path_shorten_exemptions) {
Iurii Makhno054e4332022-10-12 16:03:03 +0000549 for (StringPiece line : util::Tokenize(content, '\n')) {
550 line = util::TrimWhitespace(line);
551 if (line.empty()) {
552 continue;
553 }
554
555 auto split_line = util::Split(line, '#');
556 if (split_line.size() < 2) {
557 context->GetDiagnostics()->Error(android::DiagMessage(line) << "No # found in line");
558 return false;
559 }
560 StringPiece resource_string = split_line[0];
561 StringPiece directives = split_line[1];
562 ResourceNameRef resource_name;
563 if (!ResourceUtils::ParseResourceName(resource_string, &resource_name)) {
564 context->GetDiagnostics()->Error(android::DiagMessage(line) << "Malformed resource name");
565 return false;
566 }
567 if (!resource_name.package.empty()) {
568 context->GetDiagnostics()->Error(android::DiagMessage(line)
569 << "Package set for resource. Only use type/name");
570 return false;
571 }
572 for (StringPiece directive : util::Tokenize(directives, ',')) {
573 if (directive == "remove") {
574 out_resource_exclude_list.insert(resource_name.ToResourceName());
575 } else if (directive == "no_collapse" || directive == "no_obfuscate") {
576 out_name_collapse_exemptions.insert(resource_name.ToResourceName());
branliuf1ed5232022-12-16 19:02:29 +0800577 } else if (directive == "no_path_shorten") {
578 out_path_shorten_exemptions.insert(resource_name.ToResourceName());
Iurii Makhno054e4332022-10-12 16:03:03 +0000579 }
580 }
581 }
582 return true;
583}
584
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700585} // namespace aapt