blob: c6c82b04ff2e579c735a9447a6ecb475691a8457 [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];
Adam Lesinskid0f492d2017-04-03 18:12:45 -070076 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
77 ConfigDescription config;
78 if (!ConfigDescription::Parse(config_str, &config)) {
79 diag->Error(DiagMessage() << "invalid config '" << config_str << "' in split parameter '"
80 << arg << "'");
81 return false;
82 }
83 out_split->configs.insert(config);
84 }
85 return true;
86}
87
88std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
89 IDiagnostics* diag) {
90 std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>();
91 for (const std::string& config_arg : args) {
92 for (const StringPiece& config_str : util::Tokenize(config_arg, ',')) {
93 ConfigDescription config;
94 LocaleValue lv;
95 if (lv.InitFromFilterString(config_str)) {
96 lv.WriteTo(&config);
97 } else if (!ConfigDescription::Parse(config_str, &config)) {
98 diag->Error(DiagMessage() << "invalid config '" << config_str << "' for -c option");
99 return {};
100 }
101
102 if (config.density != 0) {
103 diag->Warn(DiagMessage() << "ignoring density '" << config << "' for -c option");
104 } else {
105 filter->AddConfig(config);
106 }
107 }
108 }
109 return std::move(filter);
110}
111
112// Adjust the SplitConstraints so that their SDK version is stripped if it
113// is less than or equal to the minSdk. Otherwise the resources that have had
114// their SDK version stripped due to minSdk won't ever match.
115std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
116 int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
117 std::vector<SplitConstraints> adjusted_constraints;
118 adjusted_constraints.reserve(split_constraints.size());
119 for (const SplitConstraints& constraints : split_constraints) {
120 SplitConstraints constraint;
121 for (const ConfigDescription& config : constraints.configs) {
122 if (config.sdkVersion <= min_sdk) {
123 constraint.configs.insert(config.CopyWithoutSdkVersion());
124 } else {
125 constraint.configs.insert(config);
126 }
127 }
128 adjusted_constraints.push_back(std::move(constraint));
129 }
130 return adjusted_constraints;
131}
132
133static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700134 return xml::AaptAttribute(Attribute(), id);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700135}
136
Adam Lesinski6b372992017-08-09 10:54:23 -0700137static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
138 xml::NamespaceDecl decl;
139 decl.prefix = "android";
140 decl.uri = xml::kSchemaAndroid;
141 return decl;
142}
143
Donald Chai414e48a2017-11-09 21:06:52 -0800144// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
145// replacing nonconforming characters with underscores.
146//
147// See frameworks/base/core/java/android/content/pm/PackageParser.java which
148// checks this at runtime.
Izabela Orlowska10560192018-04-13 11:56:35 +0100149std::string MakePackageSafeName(const std::string &name) {
Donald Chaib8f078c2017-10-18 23:51:18 -0700150 std::string result(name);
Donald Chai414e48a2017-11-09 21:06:52 -0800151 bool first = true;
Donald Chaib8f078c2017-10-18 23:51:18 -0700152 for (char &c : result) {
Donald Chai414e48a2017-11-09 21:06:52 -0800153 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
154 first = false;
155 continue;
Donald Chaib8f078c2017-10-18 23:51:18 -0700156 }
Donald Chai414e48a2017-11-09 21:06:52 -0800157 if (!first) {
158 if (c >= '0' && c <= '9') {
159 continue;
160 }
161 }
162
163 c = '_';
164 first = false;
Donald Chaib8f078c2017-10-18 23:51:18 -0700165 }
166 return result;
167}
168
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700169std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
170 const SplitConstraints& constraints) {
171 const ResourceId kVersionCode(0x0101021b);
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700172 const ResourceId kVersionCodeMajor(0x01010576);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700173 const ResourceId kRevisionCode(0x010104d5);
174 const ResourceId kHasCode(0x0101000c);
175
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700176 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700177 manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700178 manifest_el->name = "manifest";
179 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
180
181 if (app_info.version_code) {
182 const uint32_t version_code = app_info.version_code.value();
183 manifest_el->attributes.push_back(xml::Attribute{
184 xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
185 CreateAttributeWithId(kVersionCode),
186 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
187 }
188
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700189 if (app_info.version_code_major) {
190 const uint32_t version_code_major = app_info.version_code_major.value();
191 manifest_el->attributes.push_back(xml::Attribute{
192 xml::kSchemaAndroid, "versionCodeMajor", std::to_string(version_code_major),
193 CreateAttributeWithId(kVersionCodeMajor),
194 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code_major)});
195 }
196
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700197 if (app_info.revision_code) {
198 const uint32_t revision_code = app_info.revision_code.value();
199 manifest_el->attributes.push_back(xml::Attribute{
200 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
201 CreateAttributeWithId(kRevisionCode),
202 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
203 }
204
205 std::stringstream split_name;
206 if (app_info.split_name) {
207 split_name << app_info.split_name.value() << ".";
208 }
Donald Chaib8f078c2017-10-18 23:51:18 -0700209 std::vector<std::string> sanitized_config_names;
210 for (const auto &config : constraints.configs) {
211 sanitized_config_names.push_back(MakePackageSafeName(config.toString().string()));
212 }
213 split_name << "config." << util::Joiner(sanitized_config_names, "_");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700214
215 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
216
217 if (app_info.split_name) {
218 manifest_el->attributes.push_back(
219 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
220 }
221
Adam Lesinski6b372992017-08-09 10:54:23 -0700222 // Splits may contain more configurations than originally desired (fall-back densities, etc.).
223 // This makes programmatic discovery of split targeting difficult. Encode the original
Adam Lesinski3d632392017-07-24 17:08:32 -0700224 // split constraints intended for this split.
225 std::stringstream target_config_str;
226 target_config_str << util::Joiner(constraints.configs, ",");
227 manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
228
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700229 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
230 application_el->name = "application";
231 application_el->attributes.push_back(
232 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
233 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
234
235 manifest_el->AppendChild(std::move(application_el));
Adam Lesinski6b372992017-08-09 10:54:23 -0700236
237 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
238 doc->root = std::move(manifest_el);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700239 return doc;
240}
241
Adam Lesinski8780eb62017-10-31 17:44:39 -0700242static Maybe<std::string> ExtractCompiledString(const xml::Attribute& attr,
243 std::string* out_error) {
244 if (attr.compiled_value != nullptr) {
245 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700246 if (compiled_str != nullptr) {
247 if (!compiled_str->value->empty()) {
248 return *compiled_str->value;
249 } else {
250 *out_error = "compiled value is an empty string";
251 return {};
252 }
253 }
254 *out_error = "compiled value is not a string";
255 return {};
256 }
257
258 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700259 if (!attr.value.empty()) {
260 return attr.value;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700261 }
262 *out_error = "value is an empty string";
263 return {};
264}
265
Adam Lesinski8780eb62017-10-31 17:44:39 -0700266static Maybe<uint32_t> ExtractCompiledInt(const xml::Attribute& attr, std::string* out_error) {
267 if (attr.compiled_value != nullptr) {
268 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700269 if (compiled_prim != nullptr) {
270 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
271 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
272 return compiled_prim->value.data;
273 }
274 }
275 *out_error = "compiled value is not an integer";
276 return {};
277 }
278
279 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700280 Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700281 if (integer) {
282 return integer;
283 }
284 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700285 error_msg << "'" << attr.value << "' is not a valid integer";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700286 *out_error = error_msg.str();
287 return {};
288}
289
Adam Lesinski8780eb62017-10-31 17:44:39 -0700290static Maybe<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
291 if (attr.compiled_value != nullptr) {
292 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700293 if (compiled_prim != nullptr) {
294 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
295 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
296 return compiled_prim->value.data;
297 }
298 *out_error = "compiled value is not an integer or string";
299 return {};
300 }
301
Adam Lesinski8780eb62017-10-31 17:44:39 -0700302 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700303 if (compiled_str != nullptr) {
304 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
305 if (sdk_version) {
306 return sdk_version;
307 }
308
309 *out_error = "compiled string value is not a valid SDK version";
310 return {};
311 }
312 *out_error = "compiled value is not an integer or string";
313 return {};
314 }
315
316 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700317 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700318 if (sdk_version) {
319 return sdk_version;
320 }
321 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700322 error_msg << "'" << attr.value << "' is not a valid SDK version";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700323 *out_error = error_msg.str();
324 return {};
325}
326
Adam Lesinski8780eb62017-10-31 17:44:39 -0700327Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
328 IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700329 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700330 const xml::Element* manifest_el = xml_res.root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700331 if (manifest_el == nullptr) {
332 return {};
333 }
334
335 AppInfo app_info;
336
337 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700338 diag->Error(DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700339 return {};
340 }
341
Adam Lesinski8780eb62017-10-31 17:44:39 -0700342 const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700343 if (!package_attr) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700344 diag->Error(DiagMessage(xml_res.file.source) << "<manifest> must have a 'package' attribute");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700345 return {};
346 }
347
348 std::string error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700349 Maybe<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700350 if (!maybe_package) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700351 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700352 << "invalid package name: " << error_msg);
353 return {};
354 }
355 app_info.package = maybe_package.value();
356
Adam Lesinski8780eb62017-10-31 17:44:39 -0700357 if (const xml::Attribute* version_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700358 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700359 Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700360 if (!maybe_code) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700361 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700362 << "invalid android:versionCode: " << error_msg);
363 return {};
364 }
365 app_info.version_code = maybe_code.value();
366 }
367
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700368 if (const xml::Attribute* version_code_major_attr =
369 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) {
370 Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_major_attr, &error_msg);
371 if (!maybe_code) {
372 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
373 << "invalid android:versionCodeMajor: " << error_msg);
374 return {};
375 }
376 app_info.version_code_major = maybe_code.value();
377 }
378
Adam Lesinski8780eb62017-10-31 17:44:39 -0700379 if (const xml::Attribute* revision_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700380 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700381 Maybe<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700382 if (!maybe_code) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700383 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700384 << "invalid android:revisionCode: " << error_msg);
385 return {};
386 }
387 app_info.revision_code = maybe_code.value();
388 }
389
Adam Lesinski8780eb62017-10-31 17:44:39 -0700390 if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
391 Maybe<std::string> maybe_split_name = ExtractCompiledString(*split_name_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700392 if (!maybe_split_name) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700393 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700394 << "invalid split name: " << error_msg);
395 return {};
396 }
397 app_info.split_name = maybe_split_name.value();
398 }
399
Adam Lesinski8780eb62017-10-31 17:44:39 -0700400 if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
401 if (const xml::Attribute* min_sdk =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700402 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700403 Maybe<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700404 if (!maybe_sdk) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700405 diag->Error(DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700406 << "invalid android:minSdkVersion: " << error_msg);
407 return {};
408 }
409 app_info.min_sdk_version = maybe_sdk.value();
410 }
411 }
412 return app_info;
413}
414
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700415void SetLongVersionCode(xml::Element* manifest, uint64_t version) {
416 // Write the low bits of the version code to android:versionCode
417 auto version_code = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCode");
418 version_code->value = StringPrintf("0x%08x", (uint32_t) (version & 0xffffffff));
419 version_code->compiled_value = ResourceUtils::TryParseInt(version_code->value);
420
421 auto version_high = (uint32_t) (version >> 32);
422 if (version_high != 0) {
423 // Write the high bits of the version code to android:versionCodeMajor
424 auto version_major = manifest->FindOrCreateAttribute(xml::kSchemaAndroid, "versionCodeMajor");
425 version_major->value = StringPrintf("0x%08x", version_high);
426 version_major->compiled_value = ResourceUtils::TryParseInt(version_major->value);
427 } else {
428 manifest->RemoveAttribute(xml::kSchemaAndroid, "versionCodeMajor");
429 }
430}
431
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700432} // namespace aapt