blob: d39f43e8bf08385d71dc142af71a08b0c22269ba [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 Chaib8f078c2017-10-18 23:51:18 -0700143static std::string MakePackageSafeName(const std::string &name) {
144 std::string result(name);
145 for (char &c : result) {
146 if (c == '-') {
147 c = '_';
148 }
149 }
150 return result;
151}
152
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700153std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
154 const SplitConstraints& constraints) {
155 const ResourceId kVersionCode(0x0101021b);
156 const ResourceId kRevisionCode(0x010104d5);
157 const ResourceId kHasCode(0x0101000c);
158
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700159 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700160 manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700161 manifest_el->name = "manifest";
162 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
163
164 if (app_info.version_code) {
165 const uint32_t version_code = app_info.version_code.value();
166 manifest_el->attributes.push_back(xml::Attribute{
167 xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
168 CreateAttributeWithId(kVersionCode),
169 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
170 }
171
172 if (app_info.revision_code) {
173 const uint32_t revision_code = app_info.revision_code.value();
174 manifest_el->attributes.push_back(xml::Attribute{
175 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
176 CreateAttributeWithId(kRevisionCode),
177 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
178 }
179
180 std::stringstream split_name;
181 if (app_info.split_name) {
182 split_name << app_info.split_name.value() << ".";
183 }
Donald Chaib8f078c2017-10-18 23:51:18 -0700184 std::vector<std::string> sanitized_config_names;
185 for (const auto &config : constraints.configs) {
186 sanitized_config_names.push_back(MakePackageSafeName(config.toString().string()));
187 }
188 split_name << "config." << util::Joiner(sanitized_config_names, "_");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700189
190 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
191
192 if (app_info.split_name) {
193 manifest_el->attributes.push_back(
194 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
195 }
196
Adam Lesinski6b372992017-08-09 10:54:23 -0700197 // Splits may contain more configurations than originally desired (fall-back densities, etc.).
198 // This makes programmatic discovery of split targeting difficult. Encode the original
Adam Lesinski3d632392017-07-24 17:08:32 -0700199 // split constraints intended for this split.
200 std::stringstream target_config_str;
201 target_config_str << util::Joiner(constraints.configs, ",");
202 manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
203
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700204 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
205 application_el->name = "application";
206 application_el->attributes.push_back(
207 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
208 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
209
210 manifest_el->AppendChild(std::move(application_el));
Adam Lesinski6b372992017-08-09 10:54:23 -0700211
212 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
213 doc->root = std::move(manifest_el);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700214 return doc;
215}
216
Adam Lesinski8780eb62017-10-31 17:44:39 -0700217static Maybe<std::string> ExtractCompiledString(const xml::Attribute& attr,
218 std::string* out_error) {
219 if (attr.compiled_value != nullptr) {
220 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700221 if (compiled_str != nullptr) {
222 if (!compiled_str->value->empty()) {
223 return *compiled_str->value;
224 } else {
225 *out_error = "compiled value is an empty string";
226 return {};
227 }
228 }
229 *out_error = "compiled value is not a string";
230 return {};
231 }
232
233 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700234 if (!attr.value.empty()) {
235 return attr.value;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700236 }
237 *out_error = "value is an empty string";
238 return {};
239}
240
Adam Lesinski8780eb62017-10-31 17:44:39 -0700241static Maybe<uint32_t> ExtractCompiledInt(const xml::Attribute& attr, std::string* out_error) {
242 if (attr.compiled_value != nullptr) {
243 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700244 if (compiled_prim != nullptr) {
245 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
246 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
247 return compiled_prim->value.data;
248 }
249 }
250 *out_error = "compiled value is not an integer";
251 return {};
252 }
253
254 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700255 Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700256 if (integer) {
257 return integer;
258 }
259 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700260 error_msg << "'" << attr.value << "' is not a valid integer";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700261 *out_error = error_msg.str();
262 return {};
263}
264
Adam Lesinski8780eb62017-10-31 17:44:39 -0700265static Maybe<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
266 if (attr.compiled_value != nullptr) {
267 const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700268 if (compiled_prim != nullptr) {
269 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
270 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
271 return compiled_prim->value.data;
272 }
273 *out_error = "compiled value is not an integer or string";
274 return {};
275 }
276
Adam Lesinski8780eb62017-10-31 17:44:39 -0700277 const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700278 if (compiled_str != nullptr) {
279 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
280 if (sdk_version) {
281 return sdk_version;
282 }
283
284 *out_error = "compiled string value is not a valid SDK version";
285 return {};
286 }
287 *out_error = "compiled value is not an integer or string";
288 return {};
289 }
290
291 // Fallback to the plain text value if there is one.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700292 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700293 if (sdk_version) {
294 return sdk_version;
295 }
296 std::stringstream error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700297 error_msg << "'" << attr.value << "' is not a valid SDK version";
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700298 *out_error = error_msg.str();
299 return {};
300}
301
Adam Lesinski8780eb62017-10-31 17:44:39 -0700302Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
303 IDiagnostics* diag) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700304 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski8780eb62017-10-31 17:44:39 -0700305 const xml::Element* manifest_el = xml_res.root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700306 if (manifest_el == nullptr) {
307 return {};
308 }
309
310 AppInfo app_info;
311
312 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700313 diag->Error(DiagMessage(xml_res.file.source) << "root tag must be <manifest>");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700314 return {};
315 }
316
Adam Lesinski8780eb62017-10-31 17:44:39 -0700317 const xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700318 if (!package_attr) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700319 diag->Error(DiagMessage(xml_res.file.source) << "<manifest> must have a 'package' attribute");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700320 return {};
321 }
322
323 std::string error_msg;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700324 Maybe<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700325 if (!maybe_package) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700326 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700327 << "invalid package name: " << error_msg);
328 return {};
329 }
330 app_info.package = maybe_package.value();
331
Adam Lesinski8780eb62017-10-31 17:44:39 -0700332 if (const xml::Attribute* version_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700333 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700334 Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700335 if (!maybe_code) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700336 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700337 << "invalid android:versionCode: " << error_msg);
338 return {};
339 }
340 app_info.version_code = maybe_code.value();
341 }
342
Adam Lesinski8780eb62017-10-31 17:44:39 -0700343 if (const xml::Attribute* revision_code_attr =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700344 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700345 Maybe<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700346 if (!maybe_code) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700347 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700348 << "invalid android:revisionCode: " << error_msg);
349 return {};
350 }
351 app_info.revision_code = maybe_code.value();
352 }
353
Adam Lesinski8780eb62017-10-31 17:44:39 -0700354 if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
355 Maybe<std::string> maybe_split_name = ExtractCompiledString(*split_name_attr, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700356 if (!maybe_split_name) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700357 diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700358 << "invalid split name: " << error_msg);
359 return {};
360 }
361 app_info.split_name = maybe_split_name.value();
362 }
363
Adam Lesinski8780eb62017-10-31 17:44:39 -0700364 if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
365 if (const xml::Attribute* min_sdk =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700366 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700367 Maybe<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700368 if (!maybe_sdk) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700369 diag->Error(DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700370 << "invalid android:minSdkVersion: " << error_msg);
371 return {};
372 }
373 app_info.min_sdk_version = maybe_sdk.value();
374 }
375 }
376 return app_info;
377}
378
379} // namespace aapt