blob: 7739171b347f0445657be84d4d4884152b92389f [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 Meyer94a53222024-07-16 12:27:47 -0700140
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000141 std::vector<std::string> name_parts = util::Split(flag_name, ':');
142 if (name_parts.size() > 2) {
143 diag->Error(android::DiagMessage()
144 << "Invalid feature flag and optional value '" << flag_and_value
Jeremy Meyer94a53222024-07-16 12:27:47 -0700145 << "'. Must be in the format 'flag_name[:READ_ONLY|READ_WRITE][=true|false]");
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000146 return false;
147 }
148 flag_name = name_parts[0];
149 bool read_only = false;
150 if (name_parts.size() == 2) {
Jeremy Meyer94a53222024-07-16 12:27:47 -0700151 if (name_parts[1] == "ro" || name_parts[1] == "READ_ONLY") {
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000152 read_only = true;
Jeremy Meyer94a53222024-07-16 12:27:47 -0700153 } else if (name_parts[1] == "READ_WRITE") {
154 read_only = false;
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000155 } else {
156 diag->Error(android::DiagMessage()
157 << "Invalid feature flag and optional value '" << flag_and_value
Jeremy Meyer94a53222024-07-16 12:27:47 -0700158 << "'. Must be in the format 'flag_name[:READ_ONLY|READ_WRITE][=true|false]");
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000159 return false;
160 }
161 }
Mark Punzalan5579cad2023-10-30 13:47:51 -0700162
163 std::optional<bool> flag_value = {};
164 if (parts.size() == 2) {
165 StringPiece str_flag_value = util::TrimWhitespace(parts[1]);
166 if (!str_flag_value.empty()) {
167 flag_value = ResourceUtils::ParseBool(parts[1]);
168 if (!flag_value.has_value()) {
169 diag->Error(android::DiagMessage() << "Invalid value for feature flag '" << flag_and_value
170 << "'. Value must be 'true' or 'false'");
171 return false;
172 }
173 }
174 }
175
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000176 auto ffp = FeatureFlagProperties{read_only, flag_value};
177 if (auto [it, inserted] = out_feature_flag_values->try_emplace(std::string(flag_name), ffp);
Mark Punzalan5579cad2023-10-30 13:47:51 -0700178 !inserted) {
179 // We are allowing the same flag to appear multiple times, last value wins.
180 diag->Note(android::DiagMessage()
181 << "Value for feature flag '" << flag_name << "' was given more than once");
Jeremy Meyerfc7aba62024-07-16 20:25:38 +0000182 it->second = ffp;
Mark Punzalan5579cad2023-10-30 13:47:51 -0700183 }
184 }
185 return true;
186}
187
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700188// Adjust the SplitConstraints so that their SDK version is stripped if it
189// is less than or equal to the minSdk. Otherwise the resources that have had
190// their SDK version stripped due to minSdk won't ever match.
191std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
192 int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
193 std::vector<SplitConstraints> adjusted_constraints;
194 adjusted_constraints.reserve(split_constraints.size());
195 for (const SplitConstraints& constraints : split_constraints) {
196 SplitConstraints constraint;
197 for (const ConfigDescription& config : constraints.configs) {
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700198 const ConfigDescription &configToInsert = (config.sdkVersion <= min_sdk)
199 ? config.CopyWithoutSdkVersion()
200 : config;
201 // only add the config if it actually selects something
202 if (configToInsert != ConfigDescription::DefaultConfig()) {
203 constraint.configs.insert(configToInsert);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700204 }
205 }
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700206 constraint.name = constraints.name;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700207 adjusted_constraints.push_back(std::move(constraint));
208 }
209 return adjusted_constraints;
210}
211
212static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700213 return xml::AaptAttribute(Attribute(), id);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700214}
215
Adam Lesinski6b372992017-08-09 10:54:23 -0700216static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
217 xml::NamespaceDecl decl;
218 decl.prefix = "android";
219 decl.uri = xml::kSchemaAndroid;
220 return decl;
221}
222
Donald Chai414e48a2017-11-09 21:06:52 -0800223// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
224// replacing nonconforming characters with underscores.
225//
226// See frameworks/base/core/java/android/content/pm/PackageParser.java which
227// checks this at runtime.
Izabela Orlowska10560192018-04-13 11:56:35 +0100228std::string MakePackageSafeName(const std::string &name) {
Donald Chaib8f078c2017-10-18 23:51:18 -0700229 std::string result(name);
Donald Chai414e48a2017-11-09 21:06:52 -0800230 bool first = true;
Donald Chaib8f078c2017-10-18 23:51:18 -0700231 for (char &c : result) {
Donald Chai414e48a2017-11-09 21:06:52 -0800232 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
233 first = false;
234 continue;
Donald Chaib8f078c2017-10-18 23:51:18 -0700235 }
Donald Chai414e48a2017-11-09 21:06:52 -0800236 if (!first) {
237 if (c >= '0' && c <= '9') {
238 continue;
239 }
240 }
241
242 c = '_';
243 first = false;
Donald Chaib8f078c2017-10-18 23:51:18 -0700244 }
245 return result;
246}
247
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700248std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
249 const SplitConstraints& constraints) {
250 const ResourceId kVersionCode(0x0101021b);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700251 const ResourceId kVersionCodeMajor(0x01010576);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700252 const ResourceId kRevisionCode(0x010104d5);
253 const ResourceId kHasCode(0x0101000c);
254
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700255 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700256 manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700257 manifest_el->name = "manifest";
258 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
259
260 if (app_info.version_code) {
261 const uint32_t version_code = app_info.version_code.value();
262 manifest_el->attributes.push_back(xml::Attribute{
263 xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
264 CreateAttributeWithId(kVersionCode),
265 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
266 }
267
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700268 if (app_info.version_code_major) {
269 const uint32_t version_code_major = app_info.version_code_major.value();
270 manifest_el->attributes.push_back(xml::Attribute{
271 xml::kSchemaAndroid, "versionCodeMajor", std::to_string(version_code_major),
272 CreateAttributeWithId(kVersionCodeMajor),
273 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code_major)});
274 }
275
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700276 if (app_info.revision_code) {
277 const uint32_t revision_code = app_info.revision_code.value();
278 manifest_el->attributes.push_back(xml::Attribute{
279 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
280 CreateAttributeWithId(kRevisionCode),
281 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
282 }
283
284 std::stringstream split_name;
285 if (app_info.split_name) {
286 split_name << app_info.split_name.value() << ".";
287 }
Donald Chaib8f078c2017-10-18 23:51:18 -0700288 std::vector<std::string> sanitized_config_names;
289 for (const auto &config : constraints.configs) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000290 sanitized_config_names.push_back(MakePackageSafeName(config.toString().c_str()));
Donald Chaib8f078c2017-10-18 23:51:18 -0700291 }
292 split_name << "config." << util::Joiner(sanitized_config_names, "_");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700293
294 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
295
296 if (app_info.split_name) {
297 manifest_el->attributes.push_back(
298 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
299 }
300
Adam Lesinski6b372992017-08-09 10:54:23 -0700301 // Splits may contain more configurations than originally desired (fall-back densities, etc.).
302 // This makes programmatic discovery of split targeting difficult. Encode the original
Adam Lesinski3d632392017-07-24 17:08:32 -0700303 // split constraints intended for this split.
304 std::stringstream target_config_str;
305 target_config_str << util::Joiner(constraints.configs, ",");
306 manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
307
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700308 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
309 application_el->name = "application";
310 application_el->attributes.push_back(
311 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
312 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
313
314 manifest_el->AppendChild(std::move(application_el));
Adam Lesinski6b372992017-08-09 10:54:23 -0700315
316 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
317 doc->root = std::move(manifest_el);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700318 return doc;
319}
320
Ryan Mitchell4382e442021-07-14 12:53:01 -0700321static std::optional<std::string> ExtractCompiledString(const xml::Attribute& attr,
322 std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700323 if (attr.compiled_value != nullptr) {
324 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700325 if (compiled_str != nullptr) {
326 if (!compiled_str->value->empty()) {
327 return *compiled_str->value;
328 } else {
329 *out_error = "compiled value is an empty string";
330 return {};
331 }
332 }
333 *out_error = "compiled value is not a string";
334 return {};
335 }
336
337 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700338 if (!attr.value.empty()) {
339 return attr.value;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700340 }
341 *out_error = "value is an empty string";
342 return {};
343}
344
Ryan Mitchell4382e442021-07-14 12:53:01 -0700345static std::optional<uint32_t> ExtractCompiledInt(const xml::Attribute& attr,
346 std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700347 if (attr.compiled_value != nullptr) {
348 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700349 if (compiled_prim != nullptr) {
350 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
351 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
352 return compiled_prim->value.data;
353 }
354 }
355 *out_error = "compiled value is not an integer";
356 return {};
357 }
358
359 // Fallback to the plain text value if there is one.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700360 std::optional<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700361 if (integer) {
362 return integer;
363 }
364 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700365 error_msg << "'" << attr.value << "' is not a valid integer";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700366 *out_error = error_msg.str();
367 return {};
368}
369
Ryan Mitchell4382e442021-07-14 12:53:01 -0700370static std::optional<int> ExtractSdkVersion(const xml::Attribute& attr, 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 *out_error = "compiled value is not an integer or string";
379 return {};
380 }
381
Adam Lesinski8780eb62017-10-31 17:44:39 -0700382 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700383 if (compiled_str != nullptr) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700384 std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700385 if (sdk_version) {
386 return sdk_version;
387 }
388
389 *out_error = "compiled string value is not a valid SDK version";
390 return {};
391 }
392 *out_error = "compiled value is not an integer or string";
393 return {};
394 }
395
396 // Fallback to the plain text value if there is one.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700397 std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700398 if (sdk_version) {
399 return sdk_version;
400 }
401 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700402 error_msg << "'" << attr.value << "' is not a valid SDK version";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700403 *out_error = error_msg.str();
404 return {};
405}
406
Ryan Mitchell4382e442021-07-14 12:53:01 -0700407std::optional<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000408 android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700409 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700410 const xml::Element* manifest_el = xml_res.root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700411 if (manifest_el == nullptr) {
412 return {};
413 }
414
415 AppInfo app_info;
416
417 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000418 diag->Error(android::DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700419 return {};
420 }
421
Adam Lesinski8780eb62017-10-31 17:44:39 -0700422 const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700423 if (!package_attr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000424 diag->Error(android::DiagMessage(xml_res.file.source)
425 << "<manifest> must have a 'package' attribute");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700426 return {};
427 }
428
429 std::string error_msg;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700430 std::optional<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700431 if (!maybe_package) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000432 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700433 << "invalid package name: " << error_msg);
434 return {};
435 }
436 app_info.package = maybe_package.value();
437
Adam Lesinski8780eb62017-10-31 17:44:39 -0700438 if (const xml::Attribute* version_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700439 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700440 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700441 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000442 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700443 << "invalid android:versionCode: " << error_msg);
444 return {};
445 }
446 app_info.version_code = maybe_code.value();
447 }
448
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700449 if (const xml::Attribute* version_code_major_attr =
450 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700451 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_major_attr, &error_msg);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700452 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000453 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
454 << "invalid android:versionCodeMajor: " << error_msg);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700455 return {};
456 }
457 app_info.version_code_major = maybe_code.value();
458 }
459
Adam Lesinski8780eb62017-10-31 17:44:39 -0700460 if (const xml::Attribute* revision_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700461 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700462 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700463 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000464 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700465 << "invalid android:revisionCode: " << error_msg);
466 return {};
467 }
468 app_info.revision_code = maybe_code.value();
469 }
470
Adam Lesinski8780eb62017-10-31 17:44:39 -0700471 if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700472 std::optional<std::string> maybe_split_name =
473 ExtractCompiledString(*split_name_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700474 if (!maybe_split_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000475 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700476 << "invalid split name: " << error_msg);
477 return {};
478 }
479 app_info.split_name = maybe_split_name.value();
480 }
481
Adam Lesinski8780eb62017-10-31 17:44:39 -0700482 if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
483 if (const xml::Attribute* min_sdk =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700484 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700485 std::optional<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700486 if (!maybe_sdk) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000487 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700488 << "invalid android:minSdkVersion: " << error_msg);
489 return {};
490 }
491 app_info.min_sdk_version = maybe_sdk.value();
492 }
493 }
494 return app_info;
495}
496
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700497void SetLongVersionCode(xml::Element* manifest, uint64_t version) {
498 // Write the low bits of the version code to android:versionCode
499 auto version_code = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCode");
500 version_code->value = StringPrintf("0x%08x", (uint32_t) (version & 0xffffffff));
501 version_code->compiled_value = ResourceUtils::TryParseInt(version_code->value);
502
503 auto version_high = (uint32_t) (version >> 32);
504 if (version_high != 0) {
505 // Write the high bits of the version code to android:versionCodeMajor
506 auto version_major = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCodeMajor");
507 version_major->value = StringPrintf("0x%08x", version_high);
508 version_major->compiled_value = ResourceUtils::TryParseInt(version_major->value);
509 } else {
510 manifest->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
511 }
512}
513
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100514std::regex GetRegularExpression(const std::string &input) {
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000515 // Standard ECMAScript grammar.
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100516 std::regex case_insensitive(
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000517 input, std::regex_constants::ECMAScript);
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100518 return case_insensitive;
519}
520
Iurii Makhno054e4332022-10-12 16:03:03 +0000521bool ParseResourceConfig(const std::string& content, IAaptContext* context,
522 std::unordered_set<ResourceName>& out_resource_exclude_list,
branliuf1ed5232022-12-16 19:02:29 +0800523 std::set<ResourceName>& out_name_collapse_exemptions,
524 std::set<ResourceName>& out_path_shorten_exemptions) {
Iurii Makhno054e4332022-10-12 16:03:03 +0000525 for (StringPiece line : util::Tokenize(content, '\n')) {
526 line = util::TrimWhitespace(line);
527 if (line.empty()) {
528 continue;
529 }
530
531 auto split_line = util::Split(line, '#');
532 if (split_line.size() < 2) {
533 context->GetDiagnostics()->Error(android::DiagMessage(line) << "No # found in line");
534 return false;
535 }
536 StringPiece resource_string = split_line[0];
537 StringPiece directives = split_line[1];
538 ResourceNameRef resource_name;
539 if (!ResourceUtils::ParseResourceName(resource_string, &resource_name)) {
540 context->GetDiagnostics()->Error(android::DiagMessage(line) << "Malformed resource name");
541 return false;
542 }
543 if (!resource_name.package.empty()) {
544 context->GetDiagnostics()->Error(android::DiagMessage(line)
545 << "Package set for resource. Only use type/name");
546 return false;
547 }
548 for (StringPiece directive : util::Tokenize(directives, ',')) {
549 if (directive == "remove") {
550 out_resource_exclude_list.insert(resource_name.ToResourceName());
551 } else if (directive == "no_collapse" || directive == "no_obfuscate") {
552 out_name_collapse_exemptions.insert(resource_name.ToResourceName());
branliuf1ed5232022-12-16 19:02:29 +0800553 } else if (directive == "no_path_shorten") {
554 out_path_shorten_exemptions.insert(resource_name.ToResourceName());
Iurii Makhno054e4332022-10-12 16:03:03 +0000555 }
556 }
557 }
558 return true;
559}
560
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700561} // namespace aapt