blob: 52d94267024386ae50b130e1f26eb61a944e18f7 [file] [log] [blame]
Adam Lesinski2ae4a872015-11-02 16:10:55 -08001/*
2 * Copyright (C) 2015 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 "ResourceUtils.h"
18#include "XmlDom.h"
19
20#include "link/ManifestFixer.h"
21#include "util/Util.h"
22
23namespace aapt {
24
25static bool verifyManifest(IAaptContext* context, const Source& source, xml::Element* manifestEl) {
26 bool error = false;
27
28 xml::Attribute* attr = manifestEl->findAttribute({}, u"package");
29 if (!attr) {
30 context->getDiagnostics()->error(DiagMessage(source.withLine(manifestEl->lineNumber))
31 << "missing 'package' attribute");
32 error = true;
33 } else if (ResourceUtils::isReference(attr->value)) {
34 context->getDiagnostics()->error(DiagMessage(source.withLine(manifestEl->lineNumber))
35 << "value for attribute 'package' must not be a "
36 "reference");
37 error = true;
38 } else if (!util::isJavaPackageName(attr->value)) {
39 context->getDiagnostics()->error(DiagMessage(source.withLine(manifestEl->lineNumber))
40 << "invalid package name '" << attr->value << "'");
41 error = true;
42 }
43
44 return !error;
45}
46
47static bool fixUsesSdk(IAaptContext* context, const Source& source, xml::Element* el,
48 const ManifestFixerOptions& options) {
49 if (options.minSdkVersionDefault &&
50 el->findAttribute(xml::kSchemaAndroid, u"minSdkVersion") == nullptr) {
51 // There was no minSdkVersion defined and we have a default to assign.
52 el->attributes.push_back(xml::Attribute{
53 xml::kSchemaAndroid, u"minSdkVersion", options.minSdkVersionDefault.value() });
54 }
55
56 if (options.targetSdkVersionDefault &&
57 el->findAttribute(xml::kSchemaAndroid, u"targetSdkVersion") == nullptr) {
58 // There was no targetSdkVersion defined and we have a default to assign.
59 el->attributes.push_back(xml::Attribute{
60 xml::kSchemaAndroid, u"targetSdkVersion",
61 options.targetSdkVersionDefault.value() });
62 }
63 return true;
64}
65
66bool ManifestFixer::consume(IAaptContext* context, XmlResource* doc) {
67 xml::Element* root = xml::findRootElement(doc->root.get());
68 if (!root || !root->namespaceUri.empty() || root->name != u"manifest") {
69 context->getDiagnostics()->error(DiagMessage(doc->file.source)
70 << "root tag must be <manifest>");
71 return false;
72 }
73
74 if (!verifyManifest(context, doc->file.source, root)) {
75 return false;
76 }
77
78 bool foundUsesSdk = false;
79 for (xml::Element* el : root->getChildElements()) {
80 if (!el->namespaceUri.empty()) {
81 continue;
82 }
83
84 if (el->name == u"uses-sdk") {
85 foundUsesSdk = true;
86 fixUsesSdk(context, doc->file.source, el, mOptions);
87 }
88 }
89
90 if (!foundUsesSdk && (mOptions.minSdkVersionDefault || mOptions.targetSdkVersionDefault)) {
91 std::unique_ptr<xml::Element> usesSdk = util::make_unique<xml::Element>();
92 usesSdk->name = u"uses-sdk";
93 fixUsesSdk(context, doc->file.source, usesSdk.get(), mOptions);
94 root->addChild(std::move(usesSdk));
95 }
96
97 return true;
98}
99
100} // namespace aapt