1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "java/ProguardRules.h"
#include <memory>
#include <string>
#include "android-base/macros.h"
#include "util/Util.h"
#include "xml/XmlDom.h"
namespace aapt {
namespace proguard {
class BaseVisitor : public xml::Visitor {
public:
using xml::Visitor::Visit;
BaseVisitor(const Source& source, KeepSet* keep_set) : source_(source), keep_set_(keep_set) {
}
void Visit(xml::Element* node) override {
if (!node->namespace_uri.empty()) {
Maybe<xml::ExtractedPackage> maybe_package =
xml::ExtractPackageFromNamespace(node->namespace_uri);
if (maybe_package) {
// This is a custom view, let's figure out the class name from this.
std::string package = maybe_package.value().package + "." + node->name;
if (util::IsJavaClassName(package)) {
AddClass(node->line_number, package);
}
}
} else if (util::IsJavaClassName(node->name)) {
AddClass(node->line_number, node->name);
}
for (const auto& child : node->children) {
child->Accept(this);
}
}
protected:
void AddClass(size_t line_number, const std::string& class_name) {
keep_set_->AddClass(Source(source_.path, line_number), class_name);
}
void AddMethod(size_t line_number, const std::string& method_name) {
keep_set_->AddMethod(Source(source_.path, line_number), method_name);
}
private:
DISALLOW_COPY_AND_ASSIGN(BaseVisitor);
Source source_;
KeepSet* keep_set_;
};
class LayoutVisitor : public BaseVisitor {
public:
LayoutVisitor(const Source& source, KeepSet* keep_set) : BaseVisitor(source, keep_set) {
}
void Visit(xml::Element* node) override {
bool check_class = false;
bool check_name = false;
if (node->namespace_uri.empty()) {
if (node->name == "view") {
check_class = true;
} else if (node->name == "fragment") {
check_class = check_name = true;
}
} else if (node->namespace_uri == xml::kSchemaAndroid) {
check_name = node->name == "fragment";
}
for (const auto& attr : node->attributes) {
if (check_class && attr.namespace_uri.empty() && attr.name == "class" &&
util::IsJavaClassName(attr.value)) {
AddClass(node->line_number, attr.value);
} else if (check_name && attr.namespace_uri == xml::kSchemaAndroid &&
attr.name == "name" && util::IsJavaClassName(attr.value)) {
AddClass(node->line_number, attr.value);
} else if (attr.namespace_uri == xml::kSchemaAndroid &&
attr.name == "onClick") {
AddMethod(node->line_number, attr.value);
}
}
BaseVisitor::Visit(node);
}
private:
DISALLOW_COPY_AND_ASSIGN(LayoutVisitor);
};
class MenuVisitor : public BaseVisitor {
public:
MenuVisitor(const Source& source, KeepSet* keep_set) : BaseVisitor(source, keep_set) {
}
void Visit(xml::Element* node) override {
if (node->namespace_uri.empty() && node->name == "item") {
for (const auto& attr : node->attributes) {
if (attr.namespace_uri == xml::kSchemaAndroid) {
if ((attr.name == "actionViewClass" || attr.name == "actionProviderClass") &&
util::IsJavaClassName(attr.value)) {
AddClass(node->line_number, attr.value);
} else if (attr.name == "onClick") {
AddMethod(node->line_number, attr.value);
}
}
}
}
BaseVisitor::Visit(node);
}
private:
DISALLOW_COPY_AND_ASSIGN(MenuVisitor);
};
class XmlResourceVisitor : public BaseVisitor {
public:
XmlResourceVisitor(const Source& source, KeepSet* keep_set) : BaseVisitor(source, keep_set) {
}
void Visit(xml::Element* node) override {
bool check_fragment = false;
if (node->namespace_uri.empty()) {
check_fragment =
node->name == "PreferenceScreen" || node->name == "header";
}
if (check_fragment) {
xml::Attribute* attr =
node->FindAttribute(xml::kSchemaAndroid, "fragment");
if (attr && util::IsJavaClassName(attr->value)) {
AddClass(node->line_number, attr->value);
}
}
BaseVisitor::Visit(node);
}
private:
DISALLOW_COPY_AND_ASSIGN(XmlResourceVisitor);
};
class TransitionVisitor : public BaseVisitor {
public:
TransitionVisitor(const Source& source, KeepSet* keep_set) : BaseVisitor(source, keep_set) {
}
void Visit(xml::Element* node) override {
bool check_class =
node->namespace_uri.empty() && (node->name == "transition" || node->name == "pathMotion");
if (check_class) {
xml::Attribute* attr = node->FindAttribute({}, "class");
if (attr && util::IsJavaClassName(attr->value)) {
AddClass(node->line_number, attr->value);
}
}
BaseVisitor::Visit(node);
}
private:
DISALLOW_COPY_AND_ASSIGN(TransitionVisitor);
};
class ManifestVisitor : public BaseVisitor {
public:
ManifestVisitor(const Source& source, KeepSet* keep_set, bool main_dex_only)
: BaseVisitor(source, keep_set), main_dex_only_(main_dex_only) {}
void Visit(xml::Element* node) override {
if (node->namespace_uri.empty()) {
bool get_name = false;
if (node->name == "manifest") {
xml::Attribute* attr = node->FindAttribute({}, "package");
if (attr) {
package_ = attr->value;
}
} else if (node->name == "application") {
get_name = true;
xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "backupAgent");
if (attr) {
Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
if (result) {
AddClass(node->line_number, result.value());
}
}
if (main_dex_only_) {
xml::Attribute* default_process = node->FindAttribute(xml::kSchemaAndroid, "process");
if (default_process) {
default_process_ = default_process->value;
}
}
} else if (node->name == "activity" || node->name == "service" ||
node->name == "receiver" || node->name == "provider") {
get_name = true;
if (main_dex_only_) {
xml::Attribute* component_process = node->FindAttribute(xml::kSchemaAndroid, "process");
const std::string& process =
component_process ? component_process->value : default_process_;
get_name = !process.empty() && process[0] != ':';
}
} else if (node->name == "instrumentation") {
get_name = true;
}
if (get_name) {
xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "name");
get_name = attr != nullptr;
if (get_name) {
Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
if (result) {
AddClass(node->line_number, result.value());
}
}
}
}
BaseVisitor::Visit(node);
}
private:
DISALLOW_COPY_AND_ASSIGN(ManifestVisitor);
std::string package_;
const bool main_dex_only_;
std::string default_process_;
};
bool CollectProguardRulesForManifest(const Source& source, xml::XmlResource* res, KeepSet* keep_set,
bool main_dex_only) {
ManifestVisitor visitor(source, keep_set, main_dex_only);
if (res->root) {
res->root->Accept(&visitor);
return true;
}
return false;
}
bool CollectProguardRules(const Source& source, xml::XmlResource* res, KeepSet* keep_set) {
if (!res->root) {
return false;
}
switch (res->file.name.type) {
case ResourceType::kLayout: {
LayoutVisitor visitor(source, keep_set);
res->root->Accept(&visitor);
break;
}
case ResourceType::kXml: {
XmlResourceVisitor visitor(source, keep_set);
res->root->Accept(&visitor);
break;
}
case ResourceType::kTransition: {
TransitionVisitor visitor(source, keep_set);
res->root->Accept(&visitor);
break;
}
case ResourceType::kMenu: {
MenuVisitor visitor(source, keep_set);
res->root->Accept(&visitor);
break;
}
default:
break;
}
return true;
}
bool WriteKeepSet(std::ostream* out, const KeepSet& keep_set) {
for (const auto& entry : keep_set.keep_set_) {
for (const Source& source : entry.second) {
*out << "# Referenced at " << source << "\n";
}
*out << "-keep class " << entry.first << " { <init>(...); }\n" << std::endl;
}
for (const auto& entry : keep_set.keep_method_set_) {
for (const Source& source : entry.second) {
*out << "# Referenced at " << source << "\n";
}
*out << "-keepclassmembers class * { *** " << entry.first << "(...); }\n" << std::endl;
}
return true;
}
} // namespace proguard
} // namespace aapt
|