1 /*
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 "java/ProguardRules.h"
18
19 #include <memory>
20 #include <string>
21
22 #include "android-base/macros.h"
23 #include "androidfw/StringPiece.h"
24
25 #include "JavaClassGenerator.h"
26 #include "ResourceUtils.h"
27 #include "ValueVisitor.h"
28 #include "text/Printer.h"
29 #include "util/Util.h"
30 #include "xml/XmlDom.h"
31
32 using ::aapt::io::OutputStream;
33 using ::aapt::text::Printer;
34
35 namespace aapt {
36 namespace proguard {
37
38 class BaseVisitor : public xml::Visitor {
39 public:
40 using xml::Visitor::Visit;
41
BaseVisitor(const ResourceFile & file,KeepSet * keep_set)42 BaseVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set, "...") {
43 }
44
BaseVisitor(const ResourceFile & file,KeepSet * keep_set,const std::string & ctor_signature)45 BaseVisitor(const ResourceFile& file, KeepSet* keep_set, const std::string& ctor_signature)
46 : file_(file), keep_set_(keep_set), ctor_signature_(ctor_signature) {
47 }
48
Visit(xml::Element * node)49 void Visit(xml::Element* node) override {
50 if (!node->namespace_uri.empty()) {
51 Maybe<xml::ExtractedPackage> maybe_package =
52 xml::ExtractPackageFromNamespace(node->namespace_uri);
53 if (maybe_package) {
54 // This is a custom view, let's figure out the class name from this.
55 std::string package = maybe_package.value().package + "." + node->name;
56 if (util::IsJavaClassName(package)) {
57 AddClass(node->line_number, package, ctor_signature_);
58 }
59 }
60 } else if (util::IsJavaClassName(node->name)) {
61 AddClass(node->line_number, node->name, ctor_signature_);
62 }
63
64 for (const auto& child : node->children) {
65 child->Accept(this);
66 }
67
68 for (const auto& attr : node->attributes) {
69 if (attr.compiled_value) {
70 auto ref = ValueCast<Reference>(attr.compiled_value.get());
71 if (ref) {
72 AddReference(node->line_number, ref);
73 }
74 }
75 }
76 }
77
78 protected:
79 ResourceFile file_;
80 KeepSet* keep_set_;
81 std::string ctor_signature_;
82
AddClass(size_t line_number,const std::string & class_name,const std::string & ctor_signature)83 virtual void AddClass(size_t line_number, const std::string& class_name,
84 const std::string& ctor_signature) {
85 keep_set_->AddConditionalClass({file_.name, file_.source.WithLine(line_number)},
86 {class_name, ctor_signature});
87 }
88
AddMethod(size_t line_number,const std::string & method_name,const std::string & method_signature)89 void AddMethod(size_t line_number, const std::string& method_name,
90 const std::string& method_signature) {
91 keep_set_->AddMethod({file_.name, file_.source.WithLine(line_number)},
92 {method_name, method_signature});
93 }
94
AddReference(size_t line_number,Reference * ref)95 void AddReference(size_t line_number, Reference* ref) {
96 if (ref && ref->name) {
97 ResourceName ref_name = ref->name.value();
98 if (ref_name.package.empty()) {
99 ref_name = ResourceName(file_.name.package, ref_name.type, ref_name.entry);
100 }
101 keep_set_->AddReference({file_.name, file_.source.WithLine(line_number)}, ref_name);
102 }
103 }
104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(BaseVisitor);
107
108 };
109
110 class LayoutVisitor : public BaseVisitor {
111 public:
LayoutVisitor(const ResourceFile & file,KeepSet * keep_set)112 LayoutVisitor(const ResourceFile& file, KeepSet* keep_set)
113 : BaseVisitor(file, keep_set, "android.content.Context, android.util.AttributeSet") {
114 }
115
Visit(xml::Element * node)116 void Visit(xml::Element* node) override {
117 bool is_view = false;
118 bool is_fragment = false;
119 if (node->namespace_uri.empty()) {
120 if (node->name == "view") {
121 is_view = true;
122 } else if (node->name == "fragment") {
123 is_fragment = true;
124 }
125 } else if (node->namespace_uri == xml::kSchemaAndroid) {
126 is_fragment = node->name == "fragment";
127 }
128
129 for (const auto& attr : node->attributes) {
130 if (attr.namespace_uri.empty() && attr.name == "class") {
131 if (util::IsJavaClassName(attr.value)) {
132 if (is_view) {
133 AddClass(node->line_number, attr.value,
134 "android.content.Context, android.util.AttributeSet");
135 } else if (is_fragment) {
136 AddClass(node->line_number, attr.value, "");
137 }
138 }
139 } else if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "name") {
140 if (is_fragment && util::IsJavaClassName(attr.value)) {
141 AddClass(node->line_number, attr.value, "");
142 }
143 } else if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "onClick") {
144 AddMethod(node->line_number, attr.value, "android.view.View");
145 }
146 }
147
148 BaseVisitor::Visit(node);
149 }
150
151 private:
152 DISALLOW_COPY_AND_ASSIGN(LayoutVisitor);
153 };
154
155 class MenuVisitor : public BaseVisitor {
156 public:
MenuVisitor(const ResourceFile & file,KeepSet * keep_set)157 MenuVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
158 }
159
Visit(xml::Element * node)160 void Visit(xml::Element* node) override {
161 if (node->namespace_uri.empty() && node->name == "item") {
162 for (const auto& attr : node->attributes) {
163 if (attr.namespace_uri == xml::kSchemaAndroid) {
164 if ((attr.name == "actionViewClass" || attr.name == "actionProviderClass") &&
165 util::IsJavaClassName(attr.value)) {
166 AddClass(node->line_number, attr.value, "android.content.Context");
167 } else if (attr.name == "onClick") {
168 AddMethod(node->line_number, attr.value, "android.view.MenuItem");
169 }
170 }
171 }
172 }
173
174 BaseVisitor::Visit(node);
175 }
176
177 private:
178 DISALLOW_COPY_AND_ASSIGN(MenuVisitor);
179 };
180
181 class XmlResourceVisitor : public BaseVisitor {
182 public:
XmlResourceVisitor(const ResourceFile & file,KeepSet * keep_set)183 XmlResourceVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
184 }
185
Visit(xml::Element * node)186 void Visit(xml::Element* node) override {
187 bool check_fragment = false;
188 if (node->namespace_uri.empty()) {
189 check_fragment =
190 node->name == "PreferenceScreen" || node->name == "header";
191 }
192
193 if (check_fragment) {
194 xml::Attribute* attr =
195 node->FindAttribute(xml::kSchemaAndroid, "fragment");
196 if (attr && util::IsJavaClassName(attr->value)) {
197 AddClass(node->line_number, attr->value, "");
198 }
199 }
200
201 BaseVisitor::Visit(node);
202 }
203
204 private:
205 DISALLOW_COPY_AND_ASSIGN(XmlResourceVisitor);
206 };
207
208 class NavigationVisitor : public BaseVisitor {
209 public:
NavigationVisitor(const ResourceFile & file,KeepSet * keep_set,const std::string & package)210 NavigationVisitor(const ResourceFile& file, KeepSet* keep_set, const std::string& package)
211 : BaseVisitor(file, keep_set), package_(package) {
212 }
213
Visit(xml::Element * node)214 void Visit(xml::Element* node) override {
215 const auto& attr = node->FindAttribute(xml::kSchemaAndroid, "name");
216 if (attr != nullptr && !attr->value.empty()) {
217 std::string name = (attr->value[0] == '.') ? package_ + attr->value : attr->value;
218 if (util::IsJavaClassName(name)) {
219 AddClass(node->line_number, name, "...");
220 }
221 }
222
223 BaseVisitor::Visit(node);
224 }
225
226 private:
227 DISALLOW_COPY_AND_ASSIGN(NavigationVisitor);
228 const std::string package_;
229 };
230
231 class TransitionVisitor : public BaseVisitor {
232 public:
TransitionVisitor(const ResourceFile & file,KeepSet * keep_set)233 TransitionVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
234 }
235
Visit(xml::Element * node)236 void Visit(xml::Element* node) override {
237 bool check_class =
238 node->namespace_uri.empty() && (node->name == "transition" || node->name == "pathMotion");
239 if (check_class) {
240 xml::Attribute* attr = node->FindAttribute({}, "class");
241 if (attr && util::IsJavaClassName(attr->value)) {
242 AddClass(node->line_number, attr->value,
243 "android.content.Context, android.util.AttributeSet");
244 }
245 }
246
247 BaseVisitor::Visit(node);
248 }
249
250 private:
251 DISALLOW_COPY_AND_ASSIGN(TransitionVisitor);
252 };
253
254 class ManifestVisitor : public BaseVisitor {
255 public:
ManifestVisitor(const ResourceFile & file,KeepSet * keep_set,bool main_dex_only)256 ManifestVisitor(const ResourceFile& file, KeepSet* keep_set, bool main_dex_only)
257 : BaseVisitor(file, keep_set), main_dex_only_(main_dex_only) {
258 }
259
Visit(xml::Element * node)260 void Visit(xml::Element* node) override {
261 if (node->namespace_uri.empty()) {
262 bool get_name = false;
263 if (node->name == "manifest") {
264 xml::Attribute* attr = node->FindAttribute({}, "package");
265 if (attr) {
266 package_ = attr->value;
267 }
268 } else if (node->name == "application") {
269 get_name = true;
270 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "backupAgent");
271 if (attr) {
272 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
273 if (result) {
274 AddClass(node->line_number, result.value(), "");
275 }
276 }
277 attr = node->FindAttribute(xml::kSchemaAndroid, "appComponentFactory");
278 if (attr) {
279 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
280 if (result) {
281 AddClass(node->line_number, result.value(), "");
282 }
283 }
284
285 attr = node->FindAttribute(xml::kSchemaAndroid, "zygotePreloadName");
286 if (attr) {
287 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
288 if (result) {
289 AddClass(node->line_number, result.value(), "");
290 }
291 }
292
293 if (main_dex_only_) {
294 xml::Attribute* default_process = node->FindAttribute(xml::kSchemaAndroid, "process");
295 if (default_process) {
296 default_process_ = default_process->value;
297 }
298 }
299 } else if (node->name == "activity" || node->name == "service" ||
300 node->name == "receiver" || node->name == "provider") {
301 get_name = true;
302
303 if (main_dex_only_) {
304 xml::Attribute* component_process = node->FindAttribute(xml::kSchemaAndroid, "process");
305
306 const std::string& process =
307 component_process ? component_process->value : default_process_;
308 get_name = !process.empty() && process[0] != ':';
309 }
310 } else if (node->name == "instrumentation") {
311 get_name = true;
312 }
313
314 if (get_name) {
315 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "name");
316 get_name = attr != nullptr;
317
318 if (get_name) {
319 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
320 if (result) {
321 AddClass(node->line_number, result.value(), "");
322 }
323 }
324 }
325 }
326 BaseVisitor::Visit(node);
327 }
328
AddClass(size_t line_number,const std::string & class_name,const std::string & ctor_signature)329 virtual void AddClass(size_t line_number, const std::string& class_name,
330 const std::string& ctor_signature) override {
331 keep_set_->AddManifestClass({file_.name, file_.source.WithLine(line_number)}, class_name);
332 }
333
334 private:
335 DISALLOW_COPY_AND_ASSIGN(ManifestVisitor);
336
337 std::string package_;
338 const bool main_dex_only_;
339 std::string default_process_;
340 };
341
CollectProguardRulesForManifest(xml::XmlResource * res,KeepSet * keep_set,bool main_dex_only)342 bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, bool main_dex_only) {
343 ManifestVisitor visitor(res->file, keep_set, main_dex_only);
344 if (res->root) {
345 res->root->Accept(&visitor);
346 return true;
347 }
348 return false;
349 }
350
CollectProguardRules(IAaptContext * context_,xml::XmlResource * res,KeepSet * keep_set)351 bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet* keep_set) {
352 if (!res->root) {
353 return false;
354 }
355
356 switch (res->file.name.type) {
357 case ResourceType::kLayout: {
358 LayoutVisitor visitor(res->file, keep_set);
359 res->root->Accept(&visitor);
360 break;
361 }
362
363 case ResourceType::kXml: {
364 XmlResourceVisitor visitor(res->file, keep_set);
365 res->root->Accept(&visitor);
366 break;
367 }
368
369 case ResourceType::kNavigation: {
370 NavigationVisitor visitor(res->file, keep_set, context_->GetCompilationPackage());
371 res->root->Accept(&visitor);
372 break;
373 }
374
375 case ResourceType::kTransition: {
376 TransitionVisitor visitor(res->file, keep_set);
377 res->root->Accept(&visitor);
378 break;
379 }
380
381 case ResourceType::kMenu: {
382 MenuVisitor visitor(res->file, keep_set);
383 res->root->Accept(&visitor);
384 break;
385 }
386
387 default: {
388 BaseVisitor visitor(res->file, keep_set);
389 res->root->Accept(&visitor);
390 break;
391 }
392 }
393 return true;
394 }
395
WriteKeepSet(const KeepSet & keep_set,OutputStream * out,bool minimal_keep)396 void WriteKeepSet(const KeepSet& keep_set, OutputStream* out, bool minimal_keep) {
397 Printer printer(out);
398 for (const auto& entry : keep_set.manifest_class_set_) {
399 for (const UsageLocation& location : entry.second) {
400 printer.Print("# Referenced at ").Println(location.source.to_string());
401 }
402 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(); }");
403 }
404
405 for (const auto& entry : keep_set.conditional_class_set_) {
406 std::set<UsageLocation> locations;
407 bool can_be_conditional = true;
408 for (const UsageLocation& location : entry.second) {
409 can_be_conditional &= CollectLocations(location, keep_set, &locations);
410 }
411
412 if (keep_set.conditional_keep_rules_ && can_be_conditional) {
413 for (const UsageLocation& location : locations) {
414 printer.Print("# Referenced at ").Println(location.source.to_string());
415 printer.Print("-if class **.R$layout { int ")
416 .Print(JavaClassGenerator::TransformToFieldName(location.name.entry))
417 .Println("; }");
418
419 printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>(");
420 printer.Print((minimal_keep) ? entry.first.signature : "...");
421 printer.Println("); }");
422 }
423 } else {
424 for (const UsageLocation& location : entry.second) {
425 printer.Print("# Referenced at ").Println(location.source.to_string());
426 }
427
428 printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>(");
429 printer.Print((minimal_keep) ? entry.first.signature : "...");
430 printer.Println("); }");
431 }
432 printer.Println();
433 }
434
435 for (const auto& entry : keep_set.method_set_) {
436 for (const UsageLocation& location : entry.second) {
437 printer.Print("# Referenced at ").Println(location.source.to_string());
438 }
439 printer.Print("-keepclassmembers class * { *** ").Print(entry.first.name)
440 .Print("(").Print(entry.first.signature).Println("); }");
441 printer.Println();
442 }
443 }
444
CollectLocations(const UsageLocation & location,const KeepSet & keep_set,std::set<UsageLocation> * locations)445 bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
446 std::set<UsageLocation>* locations) {
447 locations->insert(location);
448
449 // TODO: allow for more reference types if we can determine its safe.
450 if (location.name.type != ResourceType::kLayout) {
451 return false;
452 }
453
454 for (const auto& entry : keep_set.reference_set_) {
455 if (entry.first == location.name) {
456 for (auto& refLocation : entry.second) {
457 // Don't get stuck in loops
458 if (locations->find(refLocation) != locations->end()) {
459 return false;
460 }
461 if (!CollectLocations(refLocation, keep_set, locations)) {
462 return false;
463 }
464 }
465 }
466 }
467
468 return true;
469 }
470
471 class ReferenceVisitor : public ValueVisitor {
472 public:
473 using ValueVisitor::Visit;
474
ReferenceVisitor(aapt::IAaptContext * context,ResourceName from,KeepSet * keep_set)475 ReferenceVisitor(aapt::IAaptContext* context, ResourceName from, KeepSet* keep_set)
476 : context_(context), from_(from), keep_set_(keep_set) {
477 }
478
Visit(Reference * reference)479 void Visit(Reference* reference) override {
480 if (reference->name) {
481 ResourceName reference_name = reference->name.value();
482 if (reference_name.package.empty()) {
483 reference_name = ResourceName(context_->GetCompilationPackage(), reference_name.type,
484 reference_name.entry);
485 }
486 keep_set_->AddReference({from_, reference->GetSource()}, reference_name);
487 }
488 }
489
490 private:
491 aapt::IAaptContext* context_;
492 ResourceName from_;
493 KeepSet* keep_set_;
494 };
495
CollectResourceReferences(aapt::IAaptContext * context,ResourceTable * table,KeepSet * keep_set)496 bool CollectResourceReferences(aapt::IAaptContext* context, ResourceTable* table,
497 KeepSet* keep_set) {
498 for (auto& pkg : table->packages) {
499 for (auto& type : pkg->types) {
500 for (auto& entry : type->entries) {
501 for (auto& config_value : entry->values) {
502 ResourceName from(pkg->name, type->type, entry->name);
503 ReferenceVisitor visitor(context, from, keep_set);
504 config_value->value->Accept(&visitor);
505 }
506 }
507 }
508 }
509 return true;
510 }
511
512 } // namespace proguard
513 } // namespace aapt
514