blob: d17858d45d08afdb758e491f50f36db08eed997e [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];
75 std::vector<ConfigDescription> configs;
76 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
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700144std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
145 const SplitConstraints& constraints) {
146 const ResourceId kVersionCode(0x0101021b);
147 const ResourceId kRevisionCode(0x010104d5);
148 const ResourceId kHasCode(0x0101000c);
149
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700150 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700151 manifest_el->namespace_decls.push_back(CreateAndroidNamespaceDecl());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700152 manifest_el->name = "manifest";
153 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
154
155 if (app_info.version_code) {
156 const uint32_t version_code = app_info.version_code.value();
157 manifest_el->attributes.push_back(xml::Attribute{
158 xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
159 CreateAttributeWithId(kVersionCode),
160 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
161 }
162
163 if (app_info.revision_code) {
164 const uint32_t revision_code = app_info.revision_code.value();
165 manifest_el->attributes.push_back(xml::Attribute{
166 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
167 CreateAttributeWithId(kRevisionCode),
168 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
169 }
170
171 std::stringstream split_name;
172 if (app_info.split_name) {
173 split_name << app_info.split_name.value() << ".";
174 }
175 split_name << "config." << util::Joiner(constraints.configs, "_");
176
177 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
178
179 if (app_info.split_name) {
180 manifest_el->attributes.push_back(
181 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
182 }
183
Adam Lesinski6b372992017-08-09 10:54:23 -0700184 // Splits may contain more configurations than originally desired (fall-back densities, etc.).
185 // This makes programmatic discovery of split targeting difficult. Encode the original
Adam Lesinski3d632392017-07-24 17:08:32 -0700186 // split constraints intended for this split.
187 std::stringstream target_config_str;
188 target_config_str << util::Joiner(constraints.configs, ",");
189 manifest_el->attributes.push_back(xml::Attribute{"", "targetConfig", target_config_str.str()});
190
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700191 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
192 application_el->name = "application";
193 application_el->attributes.push_back(
194 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
195 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
196
197 manifest_el->AppendChild(std::move(application_el));
Adam Lesinski6b372992017-08-09 10:54:23 -0700198
199 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
200 doc->root = std::move(manifest_el);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700201 return doc;
202}
203
204static Maybe<std::string> ExtractCompiledString(xml::Attribute* attr, std::string* out_error) {
205 if (attr->compiled_value != nullptr) {
206 String* compiled_str = ValueCast<String>(attr->compiled_value.get());
207 if (compiled_str != nullptr) {
208 if (!compiled_str->value->empty()) {
209 return *compiled_str->value;
210 } else {
211 *out_error = "compiled value is an empty string";
212 return {};
213 }
214 }
215 *out_error = "compiled value is not a string";
216 return {};
217 }
218
219 // Fallback to the plain text value if there is one.
220 if (!attr->value.empty()) {
221 return attr->value;
222 }
223 *out_error = "value is an empty string";
224 return {};
225}
226
227static Maybe<uint32_t> ExtractCompiledInt(xml::Attribute* attr, std::string* out_error) {
228 if (attr->compiled_value != nullptr) {
229 BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr->compiled_value.get());
230 if (compiled_prim != nullptr) {
231 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
232 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
233 return compiled_prim->value.data;
234 }
235 }
236 *out_error = "compiled value is not an integer";
237 return {};
238 }
239
240 // Fallback to the plain text value if there is one.
241 Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr->value);
242 if (integer) {
243 return integer;
244 }
245 std::stringstream error_msg;
246 error_msg << "'" << attr->value << "' is not a valid integer";
247 *out_error = error_msg.str();
248 return {};
249}
250
251static Maybe<int> ExtractSdkVersion(xml::Attribute* attr, std::string* out_error) {
252 if (attr->compiled_value != nullptr) {
253 BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr->compiled_value.get());
254 if (compiled_prim != nullptr) {
255 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
256 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
257 return compiled_prim->value.data;
258 }
259 *out_error = "compiled value is not an integer or string";
260 return {};
261 }
262
263 String* compiled_str = ValueCast<String>(attr->compiled_value.get());
264 if (compiled_str != nullptr) {
265 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
266 if (sdk_version) {
267 return sdk_version;
268 }
269
270 *out_error = "compiled string value is not a valid SDK version";
271 return {};
272 }
273 *out_error = "compiled value is not an integer or string";
274 return {};
275 }
276
277 // Fallback to the plain text value if there is one.
278 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr->value);
279 if (sdk_version) {
280 return sdk_version;
281 }
282 std::stringstream error_msg;
283 error_msg << "'" << attr->value << "' is not a valid SDK version";
284 *out_error = error_msg.str();
285 return {};
286}
287
288Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
289 // Make sure the first element is <manifest> with package attribute.
Adam Lesinski6b372992017-08-09 10:54:23 -0700290 xml::Element* manifest_el = xml_res->root.get();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700291 if (manifest_el == nullptr) {
292 return {};
293 }
294
295 AppInfo app_info;
296
297 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
298 diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>");
299 return {};
300 }
301
302 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
303 if (!package_attr) {
304 diag->Error(DiagMessage(xml_res->file.source) << "<manifest> must have a 'package' attribute");
305 return {};
306 }
307
308 std::string error_msg;
309 Maybe<std::string> maybe_package = ExtractCompiledString(package_attr, &error_msg);
310 if (!maybe_package) {
311 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
312 << "invalid package name: " << error_msg);
313 return {};
314 }
315 app_info.package = maybe_package.value();
316
317 if (xml::Attribute* version_code_attr =
318 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
319 Maybe<uint32_t> maybe_code = ExtractCompiledInt(version_code_attr, &error_msg);
320 if (!maybe_code) {
321 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
322 << "invalid android:versionCode: " << error_msg);
323 return {};
324 }
325 app_info.version_code = maybe_code.value();
326 }
327
328 if (xml::Attribute* revision_code_attr =
329 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
330 Maybe<uint32_t> maybe_code = ExtractCompiledInt(revision_code_attr, &error_msg);
331 if (!maybe_code) {
332 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
333 << "invalid android:revisionCode: " << error_msg);
334 return {};
335 }
336 app_info.revision_code = maybe_code.value();
337 }
338
339 if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
340 Maybe<std::string> maybe_split_name = ExtractCompiledString(split_name_attr, &error_msg);
341 if (!maybe_split_name) {
342 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
343 << "invalid split name: " << error_msg);
344 return {};
345 }
346 app_info.split_name = maybe_split_name.value();
347 }
348
349 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
350 if (xml::Attribute* min_sdk =
351 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
352 Maybe<int> maybe_sdk = ExtractSdkVersion(min_sdk, &error_msg);
353 if (!maybe_sdk) {
354 diag->Error(DiagMessage(xml_res->file.source.WithLine(uses_sdk_el->line_number))
355 << "invalid android:minSdkVersion: " << error_msg);
356 return {};
357 }
358 app_info.min_sdk_version = maybe_sdk.value();
359 }
360 }
361 return app_info;
362}
363
364} // namespace aapt