blob: 25010c52235cd2c45d3d4ba57d478af0fade94d3 [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 Kongstad5c541f62018-06-20 08:46:41 +020022#include "androidfw/ConfigDescription.h"
23#include "androidfw/Locale.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070024
Adam Lesinskid0f492d2017-04-03 18:12:45 -070025#include "ResourceUtils.h"
26#include "ValueVisitor.h"
27#include "split/TableSplitter.h"
28#include "util/Maybe.h"
29#include "util/Util.h"
30
Mårten Kongstad5c541f62018-06-20 08:46:41 +020031using ::android::ConfigDescription;
32using ::android::LocaleValue;
Adam Lesinski6b372992017-08-09 10:54:23 -070033using ::android::StringPiece;
Adam Lesinskid0f492d2017-04-03 18:12:45 -070034
35namespace aapt {
36
37Maybe<uint16_t> ParseTargetDensityParameter(const StringPiece& arg, IDiagnostics* diag) {
38 ConfigDescription preferred_density_config;
39 if (!ConfigDescription::Parse(arg, &preferred_density_config)) {
40 diag->Error(DiagMessage() << "invalid density '" << arg << "' for --preferred-density option");
41 return {};
42 }
43
44 // Clear the version that can be automatically added.
45 preferred_density_config.sdkVersion = 0;
46
47 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
48 ConfigDescription::CONFIG_DENSITY) {
49 diag->Error(DiagMessage() << "invalid preferred density '" << arg << "'. "
50 << "Preferred density must only be a density value");
51 return {};
52 }
53 return preferred_density_config.density;
54}
55
56bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag, std::string* out_path,
57 SplitConstraints* out_split) {
58 CHECK(diag != nullptr);
59 CHECK(out_path != nullptr);
60 CHECK(out_split != nullptr);
61
Adam Lesinskidb091572017-04-13 12:48:56 -070062#ifdef _WIN32
63 const char sSeparator = ';';
64#else
65 const char sSeparator = ':';
66#endif
67
68 std::vector<std::string> parts = util::Split(arg, sSeparator);
Adam Lesinskid0f492d2017-04-03 18:12:45 -070069 if (parts.size() != 2) {
70 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
Adam Lesinskidb091572017-04-13 12:48:56 -070071 diag->Note(DiagMessage() << "should be --split path/to/output.apk" << sSeparator
72 << "<config>[,<config>...].");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070073 return false;
74 }
75
76 *out_path = parts[0];
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) {
123 if (config.sdkVersion <= min_sdk) {
124 constraint.configs.insert(config.CopyWithoutSdkVersion());
125 } else {
126 constraint.configs.insert(config);
127 }
128 }
129 adjusted_constraints.push_back(std::move(constraint));
130 }
131 return adjusted_constraints;
132}
133
134static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700135 return xml::AaptAttribute(Attribute(), id);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700136}
137
Adam Lesinski6b372992017-08-09 10:54:23 -0700138static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
139 xml::NamespaceDecl decl;
140 decl.prefix = "android";
141 decl.uri = xml::kSchemaAndroid;
142 return decl;
143}
144
Donald Chai414e48a2017-11-09 21:06:52 -0800145// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
146// replacing nonconforming characters with underscores.
147//
148// See frameworks/base/core/java/android/content/pm/PackageParser.java which
149// checks this at runtime.
Donald Chaib8f078c2017-10-18 23:51:18 -0700150static std::string MakePackageSafeName(const std::string &name) {
151 std::string result(name);
Donald Chai414e48a2017-11-09 21:06:52 -0800152 bool first = true;
Donald Chaib8f078c2017-10-18 23:51:18 -0700153 for (char &c : result) {
Donald Chai414e48a2017-11-09 21:06:52 -0800154 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
155 first = false;
156 continue;
Donald Chaib8f078c2017-10-18 23:51:18 -0700157 }
Donald Chai414e48a2017-11-09 21:06:52 -0800158 if (!first) {
159 if (c >= '0' && c <= '9') {
160 continue;
161 }
162 }
163
164 c = '_';
165 first = false;
Donald Chaib8f078c2017-10-18 23:51:18 -0700166 }
167 return result;
168}
169
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700170std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
171 const SplitConstraints& constraints) {
172 const ResourceId kVersionCode(0x0101021b);
173 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
189 if (app_info.revision_code) {
190 const uint32_t revision_code = app_info.revision_code.value();
191 manifest_el->attributes.push_back(xml::Attribute{
192 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
193 CreateAttributeWithId(kRevisionCode),
194 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
195 }
196
197 std::stringstream split_name;
198 if (app_info.split_name) {
199 split_name << app_info.split_name.value() << ".";
200 }
Donald Chaib8f078c2017-10-18 23:51:18 -0700201 std::vector<std::string> sanitized_config_names;
202 for (const auto &config : constraints.configs) {
203 sanitized_config_names.push_back(MakePackageSafeName(config.toString().string()));
204 }
205 split_name << "config." << util::Joiner(sanitized_config_names, "_");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700206
207 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
208
209 if (app_info.split_name) {
210 manifest_el->attributes.push_back(
211 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
212 }
213
Adam Lesinski6b372992017-08-09 10:54:23 -0700214 // Splits may contain more configurations than originally desired (fall-back densities, etc.).
215 // This makes programmatic discovery of split targeting difficult. Encode the original
Adam Lesinski3d632392017-07-24 17:08:32 -0700216 // split constraints intended for this split.
217 std::stringstream target_config_str;
218 target_config_str << util::Joiner(constraints.configs, ",");
219 manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
220
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700221 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
222 application_el->name = "application";
223 application_el->attributes.push_back(
224 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
225 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
226
227 manifest_el->AppendChild(std::move(application_el));
Adam Lesinski6b372992017-08-09 10:54:23 -0700228
229 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
230 doc->root = std::move(manifest_el);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700231 return doc;
232}
233
Adam Lesinski8780eb62017-10-31 17:44:39 -0700234static Maybe<std::string> ExtractCompiledString(const xml::Attribute& attr,
235 std::string* out_error) {
236 if (attr.compiled_value != nullptr) {
237 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700238 if (compiled_str != nullptr) {
239 if (!compiled_str->value->empty()) {
240 return *compiled_str->value;
241 } else {
242 *out_error = "compiled value is an empty string";
243 return {};
244 }
245 }
246 *out_error = "compiled value is not a string";
247 return {};
248 }
249
250 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700251 if (!attr.value.empty()) {
252 return attr.value;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700253 }
254 *out_error = "value is an empty string";
255 return {};
256}
257
Adam Lesinski8780eb62017-10-31 17:44:39 -0700258static Maybe<uint32_t> ExtractCompiledInt(const xml::Attribute& attr, std::string* out_error) {
259 if (attr.compiled_value != nullptr) {
260 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700261 if (compiled_prim != nullptr) {
262 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
263 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
264 return compiled_prim->value.data;
265 }
266 }
267 *out_error = "compiled value is not an integer";
268 return {};
269 }
270
271 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700272 Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700273 if (integer) {
274 return integer;
275 }
276 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700277 error_msg << "'" << attr.value << "' is not a valid integer";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700278 *out_error = error_msg.str();
279 return {};
280}
281
Adam Lesinski8780eb62017-10-31 17:44:39 -0700282static Maybe<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
283 if (attr.compiled_value != nullptr) {
284 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700285 if (compiled_prim != nullptr) {
286 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
287 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
288 return compiled_prim->value.data;
289 }
290 *out_error = "compiled value is not an integer or string";
291 return {};
292 }
293
Adam Lesinski8780eb62017-10-31 17:44:39 -0700294 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700295 if (compiled_str != nullptr) {
296 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
297 if (sdk_version) {
298 return sdk_version;
299 }
300
301 *out_error = "compiled string value is not a valid SDK version";
302 return {};
303 }
304 *out_error = "compiled value is not an integer or string";
305 return {};
306 }
307
308 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700309 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700310 if (sdk_version) {
311 return sdk_version;
312 }
313 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700314 error_msg << "'" << attr.value << "' is not a valid SDK version";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700315 *out_error = error_msg.str();
316 return {};
317}
318
Adam Lesinski8780eb62017-10-31 17:44:39 -0700319Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
320 IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700321 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700322 const xml::Element* manifest_el = xml_res.root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700323 if (manifest_el == nullptr) {
324 return {};
325 }
326
327 AppInfo app_info;
328
329 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700330 diag->Error(DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700331 return {};
332 }
333
Adam Lesinski8780eb62017-10-31 17:44:39 -0700334 const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700335 if (!package_attr) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700336 diag->Error(DiagMessage(xml_res.file.source) << "<manifest> must have a 'package' attribute");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700337 return {};
338 }
339
340 std::string error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700341 Maybe<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700342 if (!maybe_package) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700343 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700344 << "invalid package name: " << error_msg);
345 return {};
346 }
347 app_info.package = maybe_package.value();
348
Adam Lesinski8780eb62017-10-31 17:44:39 -0700349 if (const xml::Attribute* version_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700350 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700351 Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700352 if (!maybe_code) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700353 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700354 << "invalid android:versionCode: " << error_msg);
355 return {};
356 }
357 app_info.version_code = maybe_code.value();
358 }
359
Adam Lesinski8780eb62017-10-31 17:44:39 -0700360 if (const xml::Attribute* revision_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700361 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700362 Maybe<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700363 if (!maybe_code) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700364 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700365 << "invalid android:revisionCode: " << error_msg);
366 return {};
367 }
368 app_info.revision_code = maybe_code.value();
369 }
370
Adam Lesinski8780eb62017-10-31 17:44:39 -0700371 if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
372 Maybe<std::string> maybe_split_name = ExtractCompiledString(*split_name_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700373 if (!maybe_split_name) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700374 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700375 << "invalid split name: " << error_msg);
376 return {};
377 }
378 app_info.split_name = maybe_split_name.value();
379 }
380
Adam Lesinski8780eb62017-10-31 17:44:39 -0700381 if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
382 if (const xml::Attribute* min_sdk =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700383 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700384 Maybe<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700385 if (!maybe_sdk) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700386 diag->Error(DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700387 << "invalid android:minSdkVersion: " << error_msg);
388 return {};
389 }
390 app_info.min_sdk_version = maybe_sdk.value();
391 }
392 }
393 return app_info;
394}
395
396} // namespace aapt