blob: 708bed80555b1a3556ccaa1598002285203c5afa [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 Lesinski5c33fb52017-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 Lesinski5c33fb52017-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 Chai50bae7912017-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 Lesinski5c33fb52017-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 Chai50bae7912017-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 Lesinski5c33fb52017-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 Lesinski55f27612017-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 Lesinski5c33fb52017-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
217static Maybe<std::string> ExtractCompiledString(xml::Attribute* attr, std::string* out_error) {
218 if (attr->compiled_value != nullptr) {
219 String* compiled_str = ValueCast<String>(attr->compiled_value.get());
220 if (compiled_str != nullptr) {
221 if (!compiled_str->value->empty()) {
222 return *compiled_str->value;
223 } else {
224 *out_error = "compiled value is an empty string";
225 return {};
226 }
227 }
228 *out_error = "compiled value is not a string";
229 return {};
230 }
231
232 // Fallback to the plain text value if there is one.
233 if (!attr->value.empty()) {
234 return attr->value;
235 }
236 *out_error = "value is an empty string";
237 return {};
238}
239
240static Maybe<uint32_t> ExtractCompiledInt(xml::Attribute* attr, std::string* out_error) {
241 if (attr->compiled_value != nullptr) {
242 BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr->compiled_value.get());
243 if (compiled_prim != nullptr) {
244 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
245 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
246 return compiled_prim->value.data;
247 }
248 }
249 *out_error = "compiled value is not an integer";
250 return {};
251 }
252
253 // Fallback to the plain text value if there is one.
254 Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr->value);
255 if (integer) {
256 return integer;
257 }
258 std::stringstream error_msg;
259 error_msg << "'" << attr->value << "' is not a valid integer";
260 *out_error = error_msg.str();
261 return {};
262}
263
264static Maybe<int> ExtractSdkVersion(xml::Attribute* attr, std::string* out_error) {
265 if (attr->compiled_value != nullptr) {
266 BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr->compiled_value.get());
267 if (compiled_prim != nullptr) {
268 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
269 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
270 return compiled_prim->value.data;
271 }
272 *out_error = "compiled value is not an integer or string";
273 return {};
274 }
275
276 String* compiled_str = ValueCast<String>(attr->compiled_value.get());
277 if (compiled_str != nullptr) {
278 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
279 if (sdk_version) {
280 return sdk_version;
281 }
282
283 *out_error = "compiled string value is not a valid SDK version";
284 return {};
285 }
286 *out_error = "compiled value is not an integer or string";
287 return {};
288 }
289
290 // Fallback to the plain text value if there is one.
291 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr->value);
292 if (sdk_version) {
293 return sdk_version;
294 }
295 std::stringstream error_msg;
296 error_msg << "'" << attr->value << "' is not a valid SDK version";
297 *out_error = error_msg.str();
298 return {};
299}
300
301Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
302 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski5c33fb52017-08-09 10:54:23 -0700303 xml::Element* manifest_el = xml_res->root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700304 if (manifest_el == nullptr) {
305 return {};
306 }
307
308 AppInfo app_info;
309
310 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
311 diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>");
312 return {};
313 }
314
315 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
316 if (!package_attr) {
317 diag->Error(DiagMessage(xml_res->file.source) << "<manifest> must have a 'package' attribute");
318 return {};
319 }
320
321 std::string error_msg;
322 Maybe<std::string> maybe_package = ExtractCompiledString(package_attr, &error_msg);
323 if (!maybe_package) {
324 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
325 << "invalid package name: " << error_msg);
326 return {};
327 }
328 app_info.package = maybe_package.value();
329
330 if (xml::Attribute* version_code_attr =
331 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
332 Maybe<uint32_t> maybe_code = ExtractCompiledInt(version_code_attr, &error_msg);
333 if (!maybe_code) {
334 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
335 << "invalid android:versionCode: " << error_msg);
336 return {};
337 }
338 app_info.version_code = maybe_code.value();
339 }
340
341 if (xml::Attribute* revision_code_attr =
342 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
343 Maybe<uint32_t> maybe_code = ExtractCompiledInt(revision_code_attr, &error_msg);
344 if (!maybe_code) {
345 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
346 << "invalid android:revisionCode: " << error_msg);
347 return {};
348 }
349 app_info.revision_code = maybe_code.value();
350 }
351
352 if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
353 Maybe<std::string> maybe_split_name = ExtractCompiledString(split_name_attr, &error_msg);
354 if (!maybe_split_name) {
355 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
356 << "invalid split name: " << error_msg);
357 return {};
358 }
359 app_info.split_name = maybe_split_name.value();
360 }
361
362 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
363 if (xml::Attribute* min_sdk =
364 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
365 Maybe<int> maybe_sdk = ExtractSdkVersion(min_sdk, &error_msg);
366 if (!maybe_sdk) {
367 diag->Error(DiagMessage(xml_res->file.source.WithLine(uses_sdk_el->line_number))
368 << "invalid android:minSdkVersion: " << error_msg);
369 return {};
370 }
371 app_info.min_sdk_version = maybe_sdk.value();
372 }
373 }
374 return app_info;
375}
376
377} // namespace aapt