blob: 56e2f5243e96ee2ae73f3e993b51b602cd4b6f90 [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 Meyer56f36e82022-05-20 20:35:42 +000037std::optional<uint16_t> ParseTargetDensityParameter(const StringPiece& arg,
38 android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070039 ConfigDescription preferred_density_config;
40 if (!ConfigDescription::Parse(arg, &preferred_density_config)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000041 diag->Error(android::DiagMessage()
42 << "invalid density '" << arg << "' for --preferred-density option");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070043 return {};
44 }
45
46 // Clear the version that can be automatically added.
47 preferred_density_config.sdkVersion = 0;
48
49 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
50 ConfigDescription::CONFIG_DENSITY) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000051 diag->Error(android::DiagMessage() << "invalid preferred density '" << arg << "'. "
52 << "Preferred density must only be a density value");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070053 return {};
54 }
55 return preferred_density_config.density;
56}
57
Jeremy Meyer56f36e82022-05-20 20:35:42 +000058bool ParseSplitParameter(const StringPiece& arg, android::IDiagnostics* diag, std::string* out_path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070059 SplitConstraints* out_split) {
60 CHECK(diag != nullptr);
61 CHECK(out_path != nullptr);
62 CHECK(out_split != nullptr);
63
Adam Lesinskidb091572017-04-13 12:48:56 -070064#ifdef _WIN32
65 const char sSeparator = ';';
66#else
67 const char sSeparator = ':';
68#endif
69
70 std::vector<std::string> parts = util::Split(arg, sSeparator);
Adam Lesinskid0f492d2017-04-03 18:12:45 -070071 if (parts.size() != 2) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000072 diag->Error(android::DiagMessage() << "invalid split parameter '" << arg << "'");
73 diag->Note(android::DiagMessage() << "should be --split path/to/output.apk" << sSeparator
74 << "<config>[,<config>...].");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070075 return false;
76 }
77
78 *out_path = parts[0];
Todd Kennedy9fbdf892018-08-28 16:31:15 -070079 out_split->name = parts[1];
Adam Lesinskid0f492d2017-04-03 18:12:45 -070080 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
81 ConfigDescription config;
82 if (!ConfigDescription::Parse(config_str, &config)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000083 diag->Error(android::DiagMessage()
84 << "invalid config '" << config_str << "' in split parameter '" << arg << "'");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070085 return false;
86 }
87 out_split->configs.insert(config);
88 }
89 return true;
90}
91
92std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000093 android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070094 std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>();
95 for (const std::string& config_arg : args) {
96 for (const StringPiece& config_str : util::Tokenize(config_arg, ',')) {
97 ConfigDescription config;
98 LocaleValue lv;
99 if (lv.InitFromFilterString(config_str)) {
100 lv.WriteTo(&config);
101 } else if (!ConfigDescription::Parse(config_str, &config)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000102 diag->Error(android::DiagMessage()
103 << "invalid config '" << config_str << "' for -c option");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700104 return {};
105 }
106
107 if (config.density != 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000108 diag->Warn(android::DiagMessage() << "ignoring density '" << config << "' for -c option");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700109 } else {
110 filter->AddConfig(config);
111 }
112 }
113 }
114 return std::move(filter);
115}
116
117// Adjust the SplitConstraints so that their SDK version is stripped if it
118// is less than or equal to the minSdk. Otherwise the resources that have had
119// their SDK version stripped due to minSdk won't ever match.
120std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
121 int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
122 std::vector<SplitConstraints> adjusted_constraints;
123 adjusted_constraints.reserve(split_constraints.size());
124 for (const SplitConstraints& constraints : split_constraints) {
125 SplitConstraints constraint;
126 for (const ConfigDescription& config : constraints.configs) {
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700127 const ConfigDescription &configToInsert = (config.sdkVersion <= min_sdk)
128 ? config.CopyWithoutSdkVersion()
129 : config;
130 // only add the config if it actually selects something
131 if (configToInsert != ConfigDescription::DefaultConfig()) {
132 constraint.configs.insert(configToInsert);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700133 }
134 }
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700135 constraint.name = constraints.name;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700136 adjusted_constraints.push_back(std::move(constraint));
137 }
138 return adjusted_constraints;
139}
140
141static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700142 return xml::AaptAttribute(Attribute(), id);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700143}
144
Adam Lesinski6b372992017-08-09 10:54:23 -0700145static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
146 xml::NamespaceDecl decl;
147 decl.prefix = "android";
148 decl.uri = xml::kSchemaAndroid;
149 return decl;
150}
151
Donald Chai414e48a2017-11-09 21:06:52 -0800152// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
153// replacing nonconforming characters with underscores.
154//
155// See frameworks/base/core/java/android/content/pm/PackageParser.java which
156// checks this at runtime.
Izabela Orlowska10560192018-04-13 11:56:35 +0100157std::string MakePackageSafeName(const std::string &name) {
Donald Chaib8f078c2017-10-18 23:51:18 -0700158 std::string result(name);
Donald Chai414e48a2017-11-09 21:06:52 -0800159 bool first = true;
Donald Chaib8f078c2017-10-18 23:51:18 -0700160 for (char &c : result) {
Donald Chai414e48a2017-11-09 21:06:52 -0800161 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
162 first = false;
163 continue;
Donald Chaib8f078c2017-10-18 23:51:18 -0700164 }
Donald Chai414e48a2017-11-09 21:06:52 -0800165 if (!first) {
166 if (c >= '0' && c <= '9') {
167 continue;
168 }
169 }
170
171 c = '_';
172 first = false;
Donald Chaib8f078c2017-10-18 23:51:18 -0700173 }
174 return result;
175}
176
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700177std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
178 const SplitConstraints& constraints) {
179 const ResourceId kVersionCode(0x0101021b);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700180 const ResourceId kVersionCodeMajor(0x01010576);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700181 const ResourceId kRevisionCode(0x010104d5);
182 const ResourceId kHasCode(0x0101000c);
183
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700184 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700185 manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700186 manifest_el->name = "manifest";
187 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
188
189 if (app_info.version_code) {
190 const uint32_t version_code = app_info.version_code.value();
191 manifest_el->attributes.push_back(xml::Attribute{
192 xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
193 CreateAttributeWithId(kVersionCode),
194 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
195 }
196
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700197 if (app_info.version_code_major) {
198 const uint32_t version_code_major = app_info.version_code_major.value();
199 manifest_el->attributes.push_back(xml::Attribute{
200 xml::kSchemaAndroid, "versionCodeMajor", std::to_string(version_code_major),
201 CreateAttributeWithId(kVersionCodeMajor),
202 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code_major)});
203 }
204
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700205 if (app_info.revision_code) {
206 const uint32_t revision_code = app_info.revision_code.value();
207 manifest_el->attributes.push_back(xml::Attribute{
208 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
209 CreateAttributeWithId(kRevisionCode),
210 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
211 }
212
213 std::stringstream split_name;
214 if (app_info.split_name) {
215 split_name << app_info.split_name.value() << ".";
216 }
Donald Chaib8f078c2017-10-18 23:51:18 -0700217 std::vector<std::string> sanitized_config_names;
218 for (const auto &config : constraints.configs) {
219 sanitized_config_names.push_back(MakePackageSafeName(config.toString().string()));
220 }
221 split_name << "config." << util::Joiner(sanitized_config_names, "_");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700222
223 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
224
225 if (app_info.split_name) {
226 manifest_el->attributes.push_back(
227 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
228 }
229
Adam Lesinski6b372992017-08-09 10:54:23 -0700230 // Splits may contain more configurations than originally desired (fall-back densities, etc.).
231 // This makes programmatic discovery of split targeting difficult. Encode the original
Adam Lesinski3d632392017-07-24 17:08:32 -0700232 // split constraints intended for this split.
233 std::stringstream target_config_str;
234 target_config_str << util::Joiner(constraints.configs, ",");
235 manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
236
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700237 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
238 application_el->name = "application";
239 application_el->attributes.push_back(
240 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
241 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
242
243 manifest_el->AppendChild(std::move(application_el));
Adam Lesinski6b372992017-08-09 10:54:23 -0700244
245 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
246 doc->root = std::move(manifest_el);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700247 return doc;
248}
249
Ryan Mitchell4382e442021-07-14 12:53:01 -0700250static std::optional<std::string> ExtractCompiledString(const xml::Attribute& attr,
251 std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700252 if (attr.compiled_value != nullptr) {
253 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700254 if (compiled_str != nullptr) {
255 if (!compiled_str->value->empty()) {
256 return *compiled_str->value;
257 } else {
258 *out_error = "compiled value is an empty string";
259 return {};
260 }
261 }
262 *out_error = "compiled value is not a string";
263 return {};
264 }
265
266 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700267 if (!attr.value.empty()) {
268 return attr.value;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700269 }
270 *out_error = "value is an empty string";
271 return {};
272}
273
Ryan Mitchell4382e442021-07-14 12:53:01 -0700274static std::optional<uint32_t> ExtractCompiledInt(const xml::Attribute& attr,
275 std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700276 if (attr.compiled_value != nullptr) {
277 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700278 if (compiled_prim != nullptr) {
279 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
280 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
281 return compiled_prim->value.data;
282 }
283 }
284 *out_error = "compiled value is not an integer";
285 return {};
286 }
287
288 // Fallback to the plain text value if there is one.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700289 std::optional<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700290 if (integer) {
291 return integer;
292 }
293 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700294 error_msg << "'" << attr.value << "' is not a valid integer";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700295 *out_error = error_msg.str();
296 return {};
297}
298
Ryan Mitchell4382e442021-07-14 12:53:01 -0700299static std::optional<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700300 if (attr.compiled_value != nullptr) {
301 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700302 if (compiled_prim != nullptr) {
303 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
304 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
305 return compiled_prim->value.data;
306 }
307 *out_error = "compiled value is not an integer or string";
308 return {};
309 }
310
Adam Lesinski8780eb62017-10-31 17:44:39 -0700311 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700312 if (compiled_str != nullptr) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700313 std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700314 if (sdk_version) {
315 return sdk_version;
316 }
317
318 *out_error = "compiled string value is not a valid SDK version";
319 return {};
320 }
321 *out_error = "compiled value is not an integer or string";
322 return {};
323 }
324
325 // Fallback to the plain text value if there is one.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700326 std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700327 if (sdk_version) {
328 return sdk_version;
329 }
330 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700331 error_msg << "'" << attr.value << "' is not a valid SDK version";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700332 *out_error = error_msg.str();
333 return {};
334}
335
Ryan Mitchell4382e442021-07-14 12:53:01 -0700336std::optional<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000337 android::IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700338 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700339 const xml::Element* manifest_el = xml_res.root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700340 if (manifest_el == nullptr) {
341 return {};
342 }
343
344 AppInfo app_info;
345
346 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000347 diag->Error(android::DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700348 return {};
349 }
350
Adam Lesinski8780eb62017-10-31 17:44:39 -0700351 const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700352 if (!package_attr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000353 diag->Error(android::DiagMessage(xml_res.file.source)
354 << "<manifest> must have a 'package' attribute");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700355 return {};
356 }
357
358 std::string error_msg;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700359 std::optional<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700360 if (!maybe_package) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000361 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700362 << "invalid package name: " << error_msg);
363 return {};
364 }
365 app_info.package = maybe_package.value();
366
Adam Lesinski8780eb62017-10-31 17:44:39 -0700367 if (const xml::Attribute* version_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700368 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700369 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700370 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000371 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700372 << "invalid android:versionCode: " << error_msg);
373 return {};
374 }
375 app_info.version_code = maybe_code.value();
376 }
377
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700378 if (const xml::Attribute* version_code_major_attr =
379 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700380 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_major_attr, &error_msg);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700381 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000382 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
383 << "invalid android:versionCodeMajor: " << error_msg);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700384 return {};
385 }
386 app_info.version_code_major = maybe_code.value();
387 }
388
Adam Lesinski8780eb62017-10-31 17:44:39 -0700389 if (const xml::Attribute* revision_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700390 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700391 std::optional<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700392 if (!maybe_code) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000393 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700394 << "invalid android:revisionCode: " << error_msg);
395 return {};
396 }
397 app_info.revision_code = maybe_code.value();
398 }
399
Adam Lesinski8780eb62017-10-31 17:44:39 -0700400 if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700401 std::optional<std::string> maybe_split_name =
402 ExtractCompiledString(*split_name_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700403 if (!maybe_split_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000404 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700405 << "invalid split name: " << error_msg);
406 return {};
407 }
408 app_info.split_name = maybe_split_name.value();
409 }
410
Adam Lesinski8780eb62017-10-31 17:44:39 -0700411 if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
412 if (const xml::Attribute* min_sdk =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700413 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700414 std::optional<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700415 if (!maybe_sdk) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000416 diag->Error(android::DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700417 << "invalid android:minSdkVersion: " << error_msg);
418 return {};
419 }
420 app_info.min_sdk_version = maybe_sdk.value();
421 }
422 }
423 return app_info;
424}
425
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700426void SetLongVersionCode(xml::Element* manifest, uint64_t version) {
427 // Write the low bits of the version code to android:versionCode
428 auto version_code = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCode");
429 version_code->value = StringPrintf("0x%08x", (uint32_t) (version & 0xffffffff));
430 version_code->compiled_value = ResourceUtils::TryParseInt(version_code->value);
431
432 auto version_high = (uint32_t) (version >> 32);
433 if (version_high != 0) {
434 // Write the high bits of the version code to android:versionCodeMajor
435 auto version_major = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCodeMajor");
436 version_major->value = StringPrintf("0x%08x", version_high);
437 version_major->compiled_value = ResourceUtils::TryParseInt(version_major->value);
438 } else {
439 manifest->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
440 }
441}
442
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100443std::regex GetRegularExpression(const std::string &input) {
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000444 // Standard ECMAScript grammar.
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100445 std::regex case_insensitive(
Izabela Orlowska5b89b2d2019-11-27 18:37:09 +0000446 input, std::regex_constants::ECMAScript);
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100447 return case_insensitive;
448}
449
Iurii Makhno054e4332022-10-12 16:03:03 +0000450bool ParseResourceConfig(const std::string& content, IAaptContext* context,
451 std::unordered_set<ResourceName>& out_resource_exclude_list,
452 std::set<ResourceName>& out_name_collapse_exemptions) {
453 for (StringPiece line : util::Tokenize(content, '\n')) {
454 line = util::TrimWhitespace(line);
455 if (line.empty()) {
456 continue;
457 }
458
459 auto split_line = util::Split(line, '#');
460 if (split_line.size() < 2) {
461 context->GetDiagnostics()->Error(android::DiagMessage(line) << "No # found in line");
462 return false;
463 }
464 StringPiece resource_string = split_line[0];
465 StringPiece directives = split_line[1];
466 ResourceNameRef resource_name;
467 if (!ResourceUtils::ParseResourceName(resource_string, &resource_name)) {
468 context->GetDiagnostics()->Error(android::DiagMessage(line) << "Malformed resource name");
469 return false;
470 }
471 if (!resource_name.package.empty()) {
472 context->GetDiagnostics()->Error(android::DiagMessage(line)
473 << "Package set for resource. Only use type/name");
474 return false;
475 }
476 for (StringPiece directive : util::Tokenize(directives, ',')) {
477 if (directive == "remove") {
478 out_resource_exclude_list.insert(resource_name.ToResourceName());
479 } else if (directive == "no_collapse" || directive == "no_obfuscate") {
480 out_name_collapse_exemptions.insert(resource_name.ToResourceName());
481 }
482 }
483 }
484 return true;
485}
486
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700487} // namespace aapt