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