blob: 5862d31e3f944407902a8f688fb1fa023fc71e3c [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"
22
23#include "ConfigDescription.h"
24#include "Locale.h"
25#include "ResourceUtils.h"
26#include "ValueVisitor.h"
27#include "split/TableSplitter.h"
28#include "util/Maybe.h"
29#include "util/Util.h"
30
Adam Lesinski6b372992017-08-09 10:54:23 -070031using ::android::StringPiece;
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -070032using ::android::base::StringPrintf;
Adam Lesinskid0f492d2017-04-03 18:12:45 -070033
34namespace aapt {
35
36Maybe<uint16_t> ParseTargetDensityParameter(const StringPiece& arg, IDiagnostics* diag) {
37 ConfigDescription preferred_density_config;
38 if (!ConfigDescription::Parse(arg, &preferred_density_config)) {
39 diag->Error(DiagMessage() << "invalid density '" << arg << "' for --preferred-density option");
40 return {};
41 }
42
43 // Clear the version that can be automatically added.
44 preferred_density_config.sdkVersion = 0;
45
46 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
47 ConfigDescription::CONFIG_DENSITY) {
48 diag->Error(DiagMessage() << "invalid preferred density '" << arg << "'. "
49 << "Preferred density must only be a density value");
50 return {};
51 }
52 return preferred_density_config.density;
53}
54
55bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag, std::string* out_path,
56 SplitConstraints* out_split) {
57 CHECK(diag != nullptr);
58 CHECK(out_path != nullptr);
59 CHECK(out_split != nullptr);
60
Adam Lesinskidb091572017-04-13 12:48:56 -070061#ifdef _WIN32
62 const char sSeparator = ';';
63#else
64 const char sSeparator = ':';
65#endif
66
67 std::vector<std::string> parts = util::Split(arg, sSeparator);
Adam Lesinskid0f492d2017-04-03 18:12:45 -070068 if (parts.size() != 2) {
69 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
Adam Lesinskidb091572017-04-13 12:48:56 -070070 diag->Note(DiagMessage() << "should be --split path/to/output.apk" << sSeparator
71 << "<config>[,<config>...].");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070072 return false;
73 }
74
75 *out_path = parts[0];
Todd Kennedy9fbdf892018-08-28 16:31:15 -070076 out_split->name = parts[1];
Adam Lesinskid0f492d2017-04-03 18:12:45 -070077 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
78 ConfigDescription config;
79 if (!ConfigDescription::Parse(config_str, &config)) {
80 diag->Error(DiagMessage() << "invalid config '" << config_str << "' in split parameter '"
81 << arg << "'");
82 return false;
83 }
84 out_split->configs.insert(config);
85 }
86 return true;
87}
88
89std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
90 IDiagnostics* diag) {
91 std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>();
92 for (const std::string& config_arg : args) {
93 for (const StringPiece& config_str : util::Tokenize(config_arg, ',')) {
94 ConfigDescription config;
95 LocaleValue lv;
96 if (lv.InitFromFilterString(config_str)) {
97 lv.WriteTo(&config);
98 } else if (!ConfigDescription::Parse(config_str, &config)) {
99 diag->Error(DiagMessage() << "invalid config '" << config_str << "' for -c option");
100 return {};
101 }
102
103 if (config.density != 0) {
104 diag->Warn(DiagMessage() << "ignoring density '" << config << "' for -c option");
105 } else {
106 filter->AddConfig(config);
107 }
108 }
109 }
110 return std::move(filter);
111}
112
113// Adjust the SplitConstraints so that their SDK version is stripped if it
114// is less than or equal to the minSdk. Otherwise the resources that have had
115// their SDK version stripped due to minSdk won't ever match.
116std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
117 int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
118 std::vector<SplitConstraints> adjusted_constraints;
119 adjusted_constraints.reserve(split_constraints.size());
120 for (const SplitConstraints& constraints : split_constraints) {
121 SplitConstraints constraint;
122 for (const ConfigDescription& config : constraints.configs) {
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700123 const ConfigDescription &configToInsert = (config.sdkVersion <= min_sdk)
124 ? config.CopyWithoutSdkVersion()
125 : config;
126 // only add the config if it actually selects something
127 if (configToInsert != ConfigDescription::DefaultConfig()) {
128 constraint.configs.insert(configToInsert);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700129 }
130 }
Todd Kennedy9fbdf892018-08-28 16:31:15 -0700131 constraint.name = constraints.name;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700132 adjusted_constraints.push_back(std::move(constraint));
133 }
134 return adjusted_constraints;
135}
136
137static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700138 return xml::AaptAttribute(Attribute(), id);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700139}
140
Adam Lesinski6b372992017-08-09 10:54:23 -0700141static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
142 xml::NamespaceDecl decl;
143 decl.prefix = "android";
144 decl.uri = xml::kSchemaAndroid;
145 return decl;
146}
147
Donald Chai414e48a2017-11-09 21:06:52 -0800148// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
149// replacing nonconforming characters with underscores.
150//
151// See frameworks/base/core/java/android/content/pm/PackageParser.java which
152// checks this at runtime.
Izabela Orlowska10560192018-04-13 11:56:35 +0100153std::string MakePackageSafeName(const std::string &name) {
Donald Chaib8f078c2017-10-18 23:51:18 -0700154 std::string result(name);
Donald Chai414e48a2017-11-09 21:06:52 -0800155 bool first = true;
Donald Chaib8f078c2017-10-18 23:51:18 -0700156 for (char &c : result) {
Donald Chai414e48a2017-11-09 21:06:52 -0800157 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
158 first = false;
159 continue;
Donald Chaib8f078c2017-10-18 23:51:18 -0700160 }
Donald Chai414e48a2017-11-09 21:06:52 -0800161 if (!first) {
162 if (c >= '0' && c <= '9') {
163 continue;
164 }
165 }
166
167 c = '_';
168 first = false;
Donald Chaib8f078c2017-10-18 23:51:18 -0700169 }
170 return result;
171}
172
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700173std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
174 const SplitConstraints& constraints) {
175 const ResourceId kVersionCode(0x0101021b);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700176 const ResourceId kVersionCodeMajor(0x01010576);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700177 const ResourceId kRevisionCode(0x010104d5);
178 const ResourceId kHasCode(0x0101000c);
179
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700180 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700181 manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700182 manifest_el->name = "manifest";
183 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
184
185 if (app_info.version_code) {
186 const uint32_t version_code = app_info.version_code.value();
187 manifest_el->attributes.push_back(xml::Attribute{
188 xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
189 CreateAttributeWithId(kVersionCode),
190 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
191 }
192
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700193 if (app_info.version_code_major) {
194 const uint32_t version_code_major = app_info.version_code_major.value();
195 manifest_el->attributes.push_back(xml::Attribute{
196 xml::kSchemaAndroid, "versionCodeMajor", std::to_string(version_code_major),
197 CreateAttributeWithId(kVersionCodeMajor),
198 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code_major)});
199 }
200
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700201 if (app_info.revision_code) {
202 const uint32_t revision_code = app_info.revision_code.value();
203 manifest_el->attributes.push_back(xml::Attribute{
204 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
205 CreateAttributeWithId(kRevisionCode),
206 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
207 }
208
209 std::stringstream split_name;
210 if (app_info.split_name) {
211 split_name << app_info.split_name.value() << ".";
212 }
Donald Chaib8f078c2017-10-18 23:51:18 -0700213 std::vector<std::string> sanitized_config_names;
214 for (const auto &config : constraints.configs) {
215 sanitized_config_names.push_back(MakePackageSafeName(config.toString().string()));
216 }
217 split_name << "config." << util::Joiner(sanitized_config_names, "_");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700218
219 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
220
221 if (app_info.split_name) {
222 manifest_el->attributes.push_back(
223 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
224 }
225
Adam Lesinski6b372992017-08-09 10:54:23 -0700226 // Splits may contain more configurations than originally desired (fall-back densities, etc.).
227 // This makes programmatic discovery of split targeting difficult. Encode the original
Adam Lesinski3d632392017-07-24 17:08:32 -0700228 // split constraints intended for this split.
229 std::stringstream target_config_str;
230 target_config_str << util::Joiner(constraints.configs, ",");
231 manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
232
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700233 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
234 application_el->name = "application";
235 application_el->attributes.push_back(
236 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
237 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
238
239 manifest_el->AppendChild(std::move(application_el));
Adam Lesinski6b372992017-08-09 10:54:23 -0700240
241 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
242 doc->root = std::move(manifest_el);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700243 return doc;
244}
245
Adam Lesinski8780eb62017-10-31 17:44:39 -0700246static Maybe<std::string> ExtractCompiledString(const xml::Attribute& attr,
247 std::string* out_error) {
248 if (attr.compiled_value != nullptr) {
249 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700250 if (compiled_str != nullptr) {
251 if (!compiled_str->value->empty()) {
252 return *compiled_str->value;
253 } else {
254 *out_error = "compiled value is an empty string";
255 return {};
256 }
257 }
258 *out_error = "compiled value is not a string";
259 return {};
260 }
261
262 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700263 if (!attr.value.empty()) {
264 return attr.value;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700265 }
266 *out_error = "value is an empty string";
267 return {};
268}
269
Adam Lesinski8780eb62017-10-31 17:44:39 -0700270static Maybe<uint32_t> ExtractCompiledInt(const xml::Attribute& attr, std::string* out_error) {
271 if (attr.compiled_value != nullptr) {
272 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700273 if (compiled_prim != nullptr) {
274 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
275 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
276 return compiled_prim->value.data;
277 }
278 }
279 *out_error = "compiled value is not an integer";
280 return {};
281 }
282
283 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700284 Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700285 if (integer) {
286 return integer;
287 }
288 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700289 error_msg << "'" << attr.value << "' is not a valid integer";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700290 *out_error = error_msg.str();
291 return {};
292}
293
Adam Lesinski8780eb62017-10-31 17:44:39 -0700294static Maybe<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
295 if (attr.compiled_value != nullptr) {
296 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700297 if (compiled_prim != nullptr) {
298 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
299 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
300 return compiled_prim->value.data;
301 }
302 *out_error = "compiled value is not an integer or string";
303 return {};
304 }
305
Adam Lesinski8780eb62017-10-31 17:44:39 -0700306 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700307 if (compiled_str != nullptr) {
308 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
309 if (sdk_version) {
310 return sdk_version;
311 }
312
313 *out_error = "compiled string value is not a valid SDK version";
314 return {};
315 }
316 *out_error = "compiled value is not an integer or string";
317 return {};
318 }
319
320 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700321 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700322 if (sdk_version) {
323 return sdk_version;
324 }
325 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700326 error_msg << "'" << attr.value << "' is not a valid SDK version";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700327 *out_error = error_msg.str();
328 return {};
329}
330
Adam Lesinski8780eb62017-10-31 17:44:39 -0700331Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
332 IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700333 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700334 const xml::Element* manifest_el = xml_res.root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700335 if (manifest_el == nullptr) {
336 return {};
337 }
338
339 AppInfo app_info;
340
341 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700342 diag->Error(DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700343 return {};
344 }
345
Adam Lesinski8780eb62017-10-31 17:44:39 -0700346 const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700347 if (!package_attr) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700348 diag->Error(DiagMessage(xml_res.file.source) << "<manifest> must have a 'package' attribute");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700349 return {};
350 }
351
352 std::string error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700353 Maybe<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700354 if (!maybe_package) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700355 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700356 << "invalid package name: " << error_msg);
357 return {};
358 }
359 app_info.package = maybe_package.value();
360
Adam Lesinski8780eb62017-10-31 17:44:39 -0700361 if (const xml::Attribute* version_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700362 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700363 Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700364 if (!maybe_code) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700365 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700366 << "invalid android:versionCode: " << error_msg);
367 return {};
368 }
369 app_info.version_code = maybe_code.value();
370 }
371
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700372 if (const xml::Attribute* version_code_major_attr =
373 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) {
374 Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_major_attr, &error_msg);
375 if (!maybe_code) {
376 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
377 << "invalid android:versionCodeMajor: " << error_msg);
378 return {};
379 }
380 app_info.version_code_major = maybe_code.value();
381 }
382
Adam Lesinski8780eb62017-10-31 17:44:39 -0700383 if (const xml::Attribute* revision_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700384 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700385 Maybe<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700386 if (!maybe_code) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700387 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700388 << "invalid android:revisionCode: " << error_msg);
389 return {};
390 }
391 app_info.revision_code = maybe_code.value();
392 }
393
Adam Lesinski8780eb62017-10-31 17:44:39 -0700394 if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
395 Maybe<std::string> maybe_split_name = ExtractCompiledString(*split_name_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700396 if (!maybe_split_name) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700397 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700398 << "invalid split name: " << error_msg);
399 return {};
400 }
401 app_info.split_name = maybe_split_name.value();
402 }
403
Adam Lesinski8780eb62017-10-31 17:44:39 -0700404 if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
405 if (const xml::Attribute* min_sdk =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700406 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700407 Maybe<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700408 if (!maybe_sdk) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700409 diag->Error(DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700410 << "invalid android:minSdkVersion: " << error_msg);
411 return {};
412 }
413 app_info.min_sdk_version = maybe_sdk.value();
414 }
415 }
416 return app_info;
417}
418
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700419void SetLongVersionCode(xml::Element* manifest, uint64_t version) {
420 // Write the low bits of the version code to android:versionCode
421 auto version_code = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCode");
422 version_code->value = StringPrintf("0x%08x", (uint32_t) (version & 0xffffffff));
423 version_code->compiled_value = ResourceUtils::TryParseInt(version_code->value);
424
425 auto version_high = (uint32_t) (version >> 32);
426 if (version_high != 0) {
427 // Write the high bits of the version code to android:versionCodeMajor
428 auto version_major = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCodeMajor");
429 version_major->value = StringPrintf("0x%08x", version_high);
430 version_major->compiled_value = ResourceUtils::TryParseInt(version_major->value);
431 } else {
432 manifest->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
433 }
434}
435
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700436} // namespace aapt