blob: 8b3a6701b409c64271a189a2e3e579c5b75f8fbd [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;
Adam Lesinskid0f492d2017-04-03 18:12:45 -070032
33namespace aapt {
34
35Maybe<uint16_t> ParseTargetDensityParameter(const StringPiece& arg, IDiagnostics* diag) {
36 ConfigDescription preferred_density_config;
37 if (!ConfigDescription::Parse(arg, &preferred_density_config)) {
38 diag->Error(DiagMessage() << "invalid density '" << arg << "' for --preferred-density option");
39 return {};
40 }
41
42 // Clear the version that can be automatically added.
43 preferred_density_config.sdkVersion = 0;
44
45 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
46 ConfigDescription::CONFIG_DENSITY) {
47 diag->Error(DiagMessage() << "invalid preferred density '" << arg << "'. "
48 << "Preferred density must only be a density value");
49 return {};
50 }
51 return preferred_density_config.density;
52}
53
54bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag, std::string* out_path,
55 SplitConstraints* out_split) {
56 CHECK(diag != nullptr);
57 CHECK(out_path != nullptr);
58 CHECK(out_split != nullptr);
59
Adam Lesinskidb091572017-04-13 12:48:56 -070060#ifdef _WIN32
61 const char sSeparator = ';';
62#else
63 const char sSeparator = ':';
64#endif
65
66 std::vector<std::string> parts = util::Split(arg, sSeparator);
Adam Lesinskid0f492d2017-04-03 18:12:45 -070067 if (parts.size() != 2) {
68 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
Adam Lesinskidb091572017-04-13 12:48:56 -070069 diag->Note(DiagMessage() << "should be --split path/to/output.apk" << sSeparator
70 << "<config>[,<config>...].");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070071 return false;
72 }
73
74 *out_path = parts[0];
Adam Lesinskid0f492d2017-04-03 18:12:45 -070075 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
76 ConfigDescription config;
77 if (!ConfigDescription::Parse(config_str, &config)) {
78 diag->Error(DiagMessage() << "invalid config '" << config_str << "' in split parameter '"
79 << arg << "'");
80 return false;
81 }
82 out_split->configs.insert(config);
83 }
84 return true;
85}
86
87std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
88 IDiagnostics* diag) {
89 std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>();
90 for (const std::string& config_arg : args) {
91 for (const StringPiece& config_str : util::Tokenize(config_arg, ',')) {
92 ConfigDescription config;
93 LocaleValue lv;
94 if (lv.InitFromFilterString(config_str)) {
95 lv.WriteTo(&config);
96 } else if (!ConfigDescription::Parse(config_str, &config)) {
97 diag->Error(DiagMessage() << "invalid config '" << config_str << "' for -c option");
98 return {};
99 }
100
101 if (config.density != 0) {
102 diag->Warn(DiagMessage() << "ignoring density '" << config << "' for -c option");
103 } else {
104 filter->AddConfig(config);
105 }
106 }
107 }
108 return std::move(filter);
109}
110
111// Adjust the SplitConstraints so that their SDK version is stripped if it
112// is less than or equal to the minSdk. Otherwise the resources that have had
113// their SDK version stripped due to minSdk won't ever match.
114std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
115 int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
116 std::vector<SplitConstraints> adjusted_constraints;
117 adjusted_constraints.reserve(split_constraints.size());
118 for (const SplitConstraints& constraints : split_constraints) {
119 SplitConstraints constraint;
120 for (const ConfigDescription& config : constraints.configs) {
121 if (config.sdkVersion <= min_sdk) {
122 constraint.configs.insert(config.CopyWithoutSdkVersion());
123 } else {
124 constraint.configs.insert(config);
125 }
126 }
127 adjusted_constraints.push_back(std::move(constraint));
128 }
129 return adjusted_constraints;
130}
131
132static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700133 return xml::AaptAttribute(Attribute(), id);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700134}
135
Adam Lesinski6b372992017-08-09 10:54:23 -0700136static xml::NamespaceDecl CreateAndroidNamespaceDecl() {
137 xml::NamespaceDecl decl;
138 decl.prefix = "android";
139 decl.uri = xml::kSchemaAndroid;
140 return decl;
141}
142
Donald Chai414e48a2017-11-09 21:06:52 -0800143// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
144// replacing nonconforming characters with underscores.
145//
146// See frameworks/base/core/java/android/content/pm/PackageParser.java which
147// checks this at runtime.
Donald Chaib8f078c2017-10-18 23:51:18 -0700148static std::string MakePackageSafeName(const std::string &name) {
149 std::string result(name);
Donald Chai414e48a2017-11-09 21:06:52 -0800150 bool first = true;
Donald Chaib8f078c2017-10-18 23:51:18 -0700151 for (char &c : result) {
Donald Chai414e48a2017-11-09 21:06:52 -0800152 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
153 first = false;
154 continue;
Donald Chaib8f078c2017-10-18 23:51:18 -0700155 }
Donald Chai414e48a2017-11-09 21:06:52 -0800156 if (!first) {
157 if (c >= '0' && c <= '9') {
158 continue;
159 }
160 }
161
162 c = '_';
163 first = false;
Donald Chaib8f078c2017-10-18 23:51:18 -0700164 }
165 return result;
166}
167
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700168std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
169 const SplitConstraints& constraints) {
170 const ResourceId kVersionCode(0x0101021b);
171 const ResourceId kRevisionCode(0x010104d5);
172 const ResourceId kHasCode(0x0101000c);
173
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700174 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700175 manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700176 manifest_el->name = "manifest";
177 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
178
179 if (app_info.version_code) {
180 const uint32_t version_code = app_info.version_code.value();
181 manifest_el->attributes.push_back(xml::Attribute{
182 xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
183 CreateAttributeWithId(kVersionCode),
184 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
185 }
186
187 if (app_info.revision_code) {
188 const uint32_t revision_code = app_info.revision_code.value();
189 manifest_el->attributes.push_back(xml::Attribute{
190 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
191 CreateAttributeWithId(kRevisionCode),
192 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
193 }
194
195 std::stringstream split_name;
196 if (app_info.split_name) {
197 split_name << app_info.split_name.value() << ".";
198 }
Donald Chaib8f078c2017-10-18 23:51:18 -0700199 std::vector<std::string> sanitized_config_names;
200 for (const auto &config : constraints.configs) {
201 sanitized_config_names.push_back(MakePackageSafeName(config.toString().string()));
202 }
203 split_name << "config." << util::Joiner(sanitized_config_names, "_");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700204
205 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
206
207 if (app_info.split_name) {
208 manifest_el->attributes.push_back(
209 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
210 }
211
Adam Lesinski6b372992017-08-09 10:54:23 -0700212 // Splits may contain more configurations than originally desired (fall-back densities, etc.).
213 // This makes programmatic discovery of split targeting difficult. Encode the original
Adam Lesinski3d632392017-07-24 17:08:32 -0700214 // split constraints intended for this split.
215 std::stringstream target_config_str;
216 target_config_str << util::Joiner(constraints.configs, ",");
217 manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
218
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700219 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
220 application_el->name = "application";
221 application_el->attributes.push_back(
222 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
223 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
224
225 manifest_el->AppendChild(std::move(application_el));
Adam Lesinski6b372992017-08-09 10:54:23 -0700226
227 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
228 doc->root = std::move(manifest_el);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700229 return doc;
230}
231
Adam Lesinski8780eb62017-10-31 17:44:39 -0700232static Maybe<std::string> ExtractCompiledString(const xml::Attribute& attr,
233 std::string* out_error) {
234 if (attr.compiled_value != nullptr) {
235 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700236 if (compiled_str != nullptr) {
237 if (!compiled_str->value->empty()) {
238 return *compiled_str->value;
239 } else {
240 *out_error = "compiled value is an empty string";
241 return {};
242 }
243 }
244 *out_error = "compiled value is not a string";
245 return {};
246 }
247
248 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700249 if (!attr.value.empty()) {
250 return attr.value;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700251 }
252 *out_error = "value is an empty string";
253 return {};
254}
255
Adam Lesinski8780eb62017-10-31 17:44:39 -0700256static Maybe<uint32_t> ExtractCompiledInt(const xml::Attribute& attr, std::string* out_error) {
257 if (attr.compiled_value != nullptr) {
258 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700259 if (compiled_prim != nullptr) {
260 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
261 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
262 return compiled_prim->value.data;
263 }
264 }
265 *out_error = "compiled value is not an integer";
266 return {};
267 }
268
269 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700270 Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700271 if (integer) {
272 return integer;
273 }
274 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700275 error_msg << "'" << attr.value << "' is not a valid integer";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700276 *out_error = error_msg.str();
277 return {};
278}
279
Adam Lesinski8780eb62017-10-31 17:44:39 -0700280static Maybe<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
281 if (attr.compiled_value != nullptr) {
282 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700283 if (compiled_prim != nullptr) {
284 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
285 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
286 return compiled_prim->value.data;
287 }
288 *out_error = "compiled value is not an integer or string";
289 return {};
290 }
291
Adam Lesinski8780eb62017-10-31 17:44:39 -0700292 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700293 if (compiled_str != nullptr) {
294 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
295 if (sdk_version) {
296 return sdk_version;
297 }
298
299 *out_error = "compiled string value is not a valid SDK version";
300 return {};
301 }
302 *out_error = "compiled value is not an integer or string";
303 return {};
304 }
305
306 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700307 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700308 if (sdk_version) {
309 return sdk_version;
310 }
311 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700312 error_msg << "'" << attr.value << "' is not a valid SDK version";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700313 *out_error = error_msg.str();
314 return {};
315}
316
Adam Lesinski8780eb62017-10-31 17:44:39 -0700317Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
318 IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700319 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700320 const xml::Element* manifest_el = xml_res.root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700321 if (manifest_el == nullptr) {
322 return {};
323 }
324
325 AppInfo app_info;
326
327 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700328 diag->Error(DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700329 return {};
330 }
331
Adam Lesinski8780eb62017-10-31 17:44:39 -0700332 const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700333 if (!package_attr) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700334 diag->Error(DiagMessage(xml_res.file.source) << "<manifest> must have a 'package' attribute");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700335 return {};
336 }
337
338 std::string error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700339 Maybe<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700340 if (!maybe_package) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700341 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700342 << "invalid package name: " << error_msg);
343 return {};
344 }
345 app_info.package = maybe_package.value();
346
Adam Lesinski8780eb62017-10-31 17:44:39 -0700347 if (const xml::Attribute* version_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700348 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700349 Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700350 if (!maybe_code) {
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 android:versionCode: " << error_msg);
353 return {};
354 }
355 app_info.version_code = maybe_code.value();
356 }
357
Adam Lesinski8780eb62017-10-31 17:44:39 -0700358 if (const xml::Attribute* revision_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700359 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700360 Maybe<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700361 if (!maybe_code) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700362 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700363 << "invalid android:revisionCode: " << error_msg);
364 return {};
365 }
366 app_info.revision_code = maybe_code.value();
367 }
368
Adam Lesinski8780eb62017-10-31 17:44:39 -0700369 if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
370 Maybe<std::string> maybe_split_name = ExtractCompiledString(*split_name_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700371 if (!maybe_split_name) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700372 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700373 << "invalid split name: " << error_msg);
374 return {};
375 }
376 app_info.split_name = maybe_split_name.value();
377 }
378
Adam Lesinski8780eb62017-10-31 17:44:39 -0700379 if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
380 if (const xml::Attribute* min_sdk =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700381 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700382 Maybe<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700383 if (!maybe_sdk) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700384 diag->Error(DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700385 << "invalid android:minSdkVersion: " << error_msg);
386 return {};
387 }
388 app_info.min_sdk_version = maybe_sdk.value();
389 }
390 }
391 return app_info;
392}
393
394} // namespace aapt