blob: fd94bbc0a1c30b68fd3a61575404697fc5fa5fe1 [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
60 std::vector<std::string> parts = util::Split(arg, ':');
61 if (parts.size() != 2) {
62 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
63 diag->Note(DiagMessage() << "should be --split path/to/output.apk:<config>[,<config>...]");
64 return false;
65 }
66
67 *out_path = parts[0];
68 std::vector<ConfigDescription> configs;
69 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
70 ConfigDescription config;
71 if (!ConfigDescription::Parse(config_str, &config)) {
72 diag->Error(DiagMessage() << "invalid config '" << config_str << "' in split parameter '"
73 << arg << "'");
74 return false;
75 }
76 out_split->configs.insert(config);
77 }
78 return true;
79}
80
81std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
82 IDiagnostics* diag) {
83 std::unique_ptr<AxisConfigFilter> filter = util::make_unique<AxisConfigFilter>();
84 for (const std::string& config_arg : args) {
85 for (const StringPiece& config_str : util::Tokenize(config_arg, ',')) {
86 ConfigDescription config;
87 LocaleValue lv;
88 if (lv.InitFromFilterString(config_str)) {
89 lv.WriteTo(&config);
90 } else if (!ConfigDescription::Parse(config_str, &config)) {
91 diag->Error(DiagMessage() << "invalid config '" << config_str << "' for -c option");
92 return {};
93 }
94
95 if (config.density != 0) {
96 diag->Warn(DiagMessage() << "ignoring density '" << config << "' for -c option");
97 } else {
98 filter->AddConfig(config);
99 }
100 }
101 }
102 return std::move(filter);
103}
104
105// Adjust the SplitConstraints so that their SDK version is stripped if it
106// is less than or equal to the minSdk. Otherwise the resources that have had
107// their SDK version stripped due to minSdk won't ever match.
108std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
109 int min_sdk, const std::vector<SplitConstraints>& split_constraints) {
110 std::vector<SplitConstraints> adjusted_constraints;
111 adjusted_constraints.reserve(split_constraints.size());
112 for (const SplitConstraints& constraints : split_constraints) {
113 SplitConstraints constraint;
114 for (const ConfigDescription& config : constraints.configs) {
115 if (config.sdkVersion <= min_sdk) {
116 constraint.configs.insert(config.CopyWithoutSdkVersion());
117 } else {
118 constraint.configs.insert(config);
119 }
120 }
121 adjusted_constraints.push_back(std::move(constraint));
122 }
123 return adjusted_constraints;
124}
125
126static xml::AaptAttribute CreateAttributeWithId(const ResourceId& id) {
127 return xml::AaptAttribute{id, Attribute(true)};
128}
129
130std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
131 const SplitConstraints& constraints) {
132 const ResourceId kVersionCode(0x0101021b);
133 const ResourceId kRevisionCode(0x010104d5);
134 const ResourceId kHasCode(0x0101000c);
135
136 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
137
138 std::unique_ptr<xml::Namespace> namespace_android = util::make_unique<xml::Namespace>();
139 namespace_android->namespace_uri = xml::kSchemaAndroid;
140 namespace_android->namespace_prefix = "android";
141
142 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
143 manifest_el->name = "manifest";
144 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
145
146 if (app_info.version_code) {
147 const uint32_t version_code = app_info.version_code.value();
148 manifest_el->attributes.push_back(xml::Attribute{
149 xml::kSchemaAndroid, "versionCode", std::to_string(version_code),
150 CreateAttributeWithId(kVersionCode),
151 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, version_code)});
152 }
153
154 if (app_info.revision_code) {
155 const uint32_t revision_code = app_info.revision_code.value();
156 manifest_el->attributes.push_back(xml::Attribute{
157 xml::kSchemaAndroid, "revisionCode", std::to_string(revision_code),
158 CreateAttributeWithId(kRevisionCode),
159 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, revision_code)});
160 }
161
162 std::stringstream split_name;
163 if (app_info.split_name) {
164 split_name << app_info.split_name.value() << ".";
165 }
166 split_name << "config." << util::Joiner(constraints.configs, "_");
167
168 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
169
170 if (app_info.split_name) {
171 manifest_el->attributes.push_back(
172 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
173 }
174
175 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
176 application_el->name = "application";
177 application_el->attributes.push_back(
178 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false", CreateAttributeWithId(kHasCode),
179 util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, 0u)});
180
181 manifest_el->AppendChild(std::move(application_el));
182 namespace_android->AppendChild(std::move(manifest_el));
183 doc->root = std::move(namespace_android);
184 return doc;
185}
186
187static Maybe<std::string> ExtractCompiledString(xml::Attribute* attr, std::string* out_error) {
188 if (attr->compiled_value != nullptr) {
189 String* compiled_str = ValueCast<String>(attr->compiled_value.get());
190 if (compiled_str != nullptr) {
191 if (!compiled_str->value->empty()) {
192 return *compiled_str->value;
193 } else {
194 *out_error = "compiled value is an empty string";
195 return {};
196 }
197 }
198 *out_error = "compiled value is not a string";
199 return {};
200 }
201
202 // Fallback to the plain text value if there is one.
203 if (!attr->value.empty()) {
204 return attr->value;
205 }
206 *out_error = "value is an empty string";
207 return {};
208}
209
210static Maybe<uint32_t> ExtractCompiledInt(xml::Attribute* attr, std::string* out_error) {
211 if (attr->compiled_value != nullptr) {
212 BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr->compiled_value.get());
213 if (compiled_prim != nullptr) {
214 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
215 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
216 return compiled_prim->value.data;
217 }
218 }
219 *out_error = "compiled value is not an integer";
220 return {};
221 }
222
223 // Fallback to the plain text value if there is one.
224 Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr->value);
225 if (integer) {
226 return integer;
227 }
228 std::stringstream error_msg;
229 error_msg << "'" << attr->value << "' is not a valid integer";
230 *out_error = error_msg.str();
231 return {};
232}
233
234static Maybe<int> ExtractSdkVersion(xml::Attribute* attr, std::string* out_error) {
235 if (attr->compiled_value != nullptr) {
236 BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr->compiled_value.get());
237 if (compiled_prim != nullptr) {
238 if (compiled_prim->value.dataType >= android::Res_value::TYPE_FIRST_INT &&
239 compiled_prim->value.dataType <= android::Res_value::TYPE_LAST_INT) {
240 return compiled_prim->value.data;
241 }
242 *out_error = "compiled value is not an integer or string";
243 return {};
244 }
245
246 String* compiled_str = ValueCast<String>(attr->compiled_value.get());
247 if (compiled_str != nullptr) {
248 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
249 if (sdk_version) {
250 return sdk_version;
251 }
252
253 *out_error = "compiled string value is not a valid SDK version";
254 return {};
255 }
256 *out_error = "compiled value is not an integer or string";
257 return {};
258 }
259
260 // Fallback to the plain text value if there is one.
261 Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr->value);
262 if (sdk_version) {
263 return sdk_version;
264 }
265 std::stringstream error_msg;
266 error_msg << "'" << attr->value << "' is not a valid SDK version";
267 *out_error = error_msg.str();
268 return {};
269}
270
271Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
272 // Make sure the first element is <manifest> with package attribute.
273 xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
274 if (manifest_el == nullptr) {
275 return {};
276 }
277
278 AppInfo app_info;
279
280 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
281 diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>");
282 return {};
283 }
284
285 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
286 if (!package_attr) {
287 diag->Error(DiagMessage(xml_res->file.source) << "<manifest> must have a 'package' attribute");
288 return {};
289 }
290
291 std::string error_msg;
292 Maybe<std::string> maybe_package = ExtractCompiledString(package_attr, &error_msg);
293 if (!maybe_package) {
294 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
295 << "invalid package name: " << error_msg);
296 return {};
297 }
298 app_info.package = maybe_package.value();
299
300 if (xml::Attribute* version_code_attr =
301 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
302 Maybe<uint32_t> maybe_code = ExtractCompiledInt(version_code_attr, &error_msg);
303 if (!maybe_code) {
304 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
305 << "invalid android:versionCode: " << error_msg);
306 return {};
307 }
308 app_info.version_code = maybe_code.value();
309 }
310
311 if (xml::Attribute* revision_code_attr =
312 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
313 Maybe<uint32_t> maybe_code = ExtractCompiledInt(revision_code_attr, &error_msg);
314 if (!maybe_code) {
315 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
316 << "invalid android:revisionCode: " << error_msg);
317 return {};
318 }
319 app_info.revision_code = maybe_code.value();
320 }
321
322 if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
323 Maybe<std::string> maybe_split_name = ExtractCompiledString(split_name_attr, &error_msg);
324 if (!maybe_split_name) {
325 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
326 << "invalid split name: " << error_msg);
327 return {};
328 }
329 app_info.split_name = maybe_split_name.value();
330 }
331
332 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
333 if (xml::Attribute* min_sdk =
334 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
335 Maybe<int> maybe_sdk = ExtractSdkVersion(min_sdk, &error_msg);
336 if (!maybe_sdk) {
337 diag->Error(DiagMessage(xml_res->file.source.WithLine(uses_sdk_el->line_number))
338 << "invalid android:minSdkVersion: " << error_msg);
339 return {};
340 }
341 app_info.min_sdk_version = maybe_sdk.value();
342 }
343 }
344 return app_info;
345}
346
347} // namespace aapt