blob: ff7f5c12404c9c3ee722181216a48e8191811904 [file] [log] [blame]
Adam Lesinskia1ad4a82015-06-08 11:41:09 -07001/*
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
Adam Lesinskica5638f2015-10-21 14:42:43 -070017#include "java/ProguardRules.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070019#include <memory>
20#include <string>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "android-base/macros.h"
Adam Lesinskia693c4a2017-11-09 11:29:39 -080023#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024
Adam Koskidc21dea2017-07-21 10:55:27 -070025#include "JavaClassGenerator.h"
26#include "ResourceUtils.h"
27#include "ValueVisitor.h"
Adam Lesinskia693c4a2017-11-09 11:29:39 -080028#include "text/Printer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029#include "util/Util.h"
30#include "xml/XmlDom.h"
31
Adam Lesinskia693c4a2017-11-09 11:29:39 -080032using ::aapt::io::OutputStream;
33using ::aapt::text::Printer;
34
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070035namespace aapt {
36namespace proguard {
37
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070038class BaseVisitor : public xml::Visitor {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 public:
Adam Lesinski6b372992017-08-09 10:54:23 -070040 using xml::Visitor::Visit;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041
Jake Whartoncc65b8d2018-06-11 17:05:35 -040042 BaseVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set, "...") {
43 }
44
45 BaseVisitor(const ResourceFile& file, KeepSet* keep_set, const std::string& ctor_signature)
46 : file_(file), keep_set_(keep_set), ctor_signature_(ctor_signature) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070048
Adam Lesinski6b372992017-08-09 10:54:23 -070049 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 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)) {
Jake Whartoncc65b8d2018-06-11 17:05:35 -040057 AddClass(node->line_number, package, ctor_signature_);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070058 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 }
60 } else if (util::IsJavaClassName(node->name)) {
Jake Whartoncc65b8d2018-06-11 17:05:35 -040061 AddClass(node->line_number, node->name, ctor_signature_);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070062 }
63
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 for (const auto& child : node->children) {
65 child->Accept(this);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070066 }
Adam Koskidc21dea2017-07-21 10:55:27 -070067
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 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 protected:
Adam Koskidc21dea2017-07-21 10:55:27 -070079 ResourceFile file_;
80 KeepSet* keep_set_;
Jake Whartoncc65b8d2018-06-11 17:05:35 -040081 std::string ctor_signature_;
Adam Koskidc21dea2017-07-21 10:55:27 -070082
Jake Wharton98100c32018-06-11 15:46:03 -040083 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});
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070088
Jake Wharton3001f032018-06-11 12:24:11 -040089 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});
Adam Koskidc21dea2017-07-21 10:55:27 -070093 }
94
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 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700104
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 private:
106 DISALLOW_COPY_AND_ASSIGN(BaseVisitor);
107
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700108};
109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110class LayoutVisitor : public BaseVisitor {
111 public:
Jake Whartoncc65b8d2018-06-11 17:05:35 -0400112 LayoutVisitor(const ResourceFile& file, KeepSet* keep_set)
113 : BaseVisitor(file, keep_set, "android.content.Context, android.util.AttributeSet") {
Adam Lesinski6b372992017-08-09 10:54:23 -0700114 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115
Adam Lesinski6b372992017-08-09 10:54:23 -0700116 void Visit(xml::Element* node) override {
Jake Wharton98100c32018-06-11 15:46:03 -0400117 bool is_view = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 if (node->namespace_uri.empty()) {
Adam Lesinskif762df22017-06-26 16:39:03 -0700119 if (node->name == "view") {
Jake Wharton98100c32018-06-11 15:46:03 -0400120 is_view = true;
Adam Lesinskif762df22017-06-26 16:39:03 -0700121 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700122 }
123
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 for (const auto& attr : node->attributes) {
Jake Wharton98100c32018-06-11 15:46:03 -0400125 if (attr.namespace_uri.empty() && attr.name == "class") {
126 if (util::IsJavaClassName(attr.value)) {
127 if (is_view) {
128 AddClass(node->line_number, attr.value,
129 "android.content.Context, android.util.AttributeSet");
Jeremy Woods4bdc6982020-02-28 19:42:38 -0800130 } else {
Jake Wharton98100c32018-06-11 15:46:03 -0400131 AddClass(node->line_number, attr.value, "");
132 }
133 }
134 } else if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "name") {
Jeremy Woods4bdc6982020-02-28 19:42:38 -0800135 if (util::IsJavaClassName(attr.value)) {
Jake Wharton98100c32018-06-11 15:46:03 -0400136 AddClass(node->line_number, attr.value, "");
137 }
138 } else if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "onClick") {
Jake Wharton3001f032018-06-11 12:24:11 -0400139 AddMethod(node->line_number, attr.value, "android.view.View");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700141 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142
143 BaseVisitor::Visit(node);
144 }
145
146 private:
147 DISALLOW_COPY_AND_ASSIGN(LayoutVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700148};
149
Adam Lesinskif762df22017-06-26 16:39:03 -0700150class MenuVisitor : public BaseVisitor {
151 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700152 MenuVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinskif762df22017-06-26 16:39:03 -0700153 }
154
Adam Lesinski6b372992017-08-09 10:54:23 -0700155 void Visit(xml::Element* node) override {
Adam Lesinskif762df22017-06-26 16:39:03 -0700156 if (node->namespace_uri.empty() && node->name == "item") {
157 for (const auto& attr : node->attributes) {
158 if (attr.namespace_uri == xml::kSchemaAndroid) {
159 if ((attr.name == "actionViewClass" || attr.name == "actionProviderClass") &&
160 util::IsJavaClassName(attr.value)) {
Jake Wharton98100c32018-06-11 15:46:03 -0400161 AddClass(node->line_number, attr.value, "android.content.Context");
Adam Lesinskif762df22017-06-26 16:39:03 -0700162 } else if (attr.name == "onClick") {
Jake Wharton3001f032018-06-11 12:24:11 -0400163 AddMethod(node->line_number, attr.value, "android.view.MenuItem");
Adam Lesinskif762df22017-06-26 16:39:03 -0700164 }
165 }
166 }
167 }
168
169 BaseVisitor::Visit(node);
170 }
171
172 private:
173 DISALLOW_COPY_AND_ASSIGN(MenuVisitor);
174};
175
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176class XmlResourceVisitor : public BaseVisitor {
177 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700178 XmlResourceVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700179 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180
Adam Lesinski6b372992017-08-09 10:54:23 -0700181 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 bool check_fragment = false;
183 if (node->namespace_uri.empty()) {
184 check_fragment =
185 node->name == "PreferenceScreen" || node->name == "header";
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700186 }
187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 if (check_fragment) {
189 xml::Attribute* attr =
190 node->FindAttribute(xml::kSchemaAndroid, "fragment");
191 if (attr && util::IsJavaClassName(attr->value)) {
Jake Wharton98100c32018-06-11 15:46:03 -0400192 AddClass(node->line_number, attr->value, "");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700194 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195
196 BaseVisitor::Visit(node);
197 }
198
199 private:
200 DISALLOW_COPY_AND_ASSIGN(XmlResourceVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700201};
202
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700203class NavigationVisitor : public BaseVisitor {
204 public:
205 NavigationVisitor(const ResourceFile& file, KeepSet* keep_set, const std::string& package)
206 : BaseVisitor(file, keep_set), package_(package) {
207 }
208
209 void Visit(xml::Element* node) override {
210 const auto& attr = node->FindAttribute(xml::kSchemaAndroid, "name");
211 if (attr != nullptr && !attr->value.empty()) {
212 std::string name = (attr->value[0] == '.') ? package_ + attr->value : attr->value;
213 if (util::IsJavaClassName(name)) {
Jake Wharton98100c32018-06-11 15:46:03 -0400214 AddClass(node->line_number, name, "...");
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700215 }
216 }
217
218 BaseVisitor::Visit(node);
219 }
220
221 private:
222 DISALLOW_COPY_AND_ASSIGN(NavigationVisitor);
223 const std::string package_;
224};
225
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226class TransitionVisitor : public BaseVisitor {
227 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700228 TransitionVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700229 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230
Adam Lesinski6b372992017-08-09 10:54:23 -0700231 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 bool check_class =
Adam Lesinski6b372992017-08-09 10:54:23 -0700233 node->namespace_uri.empty() && (node->name == "transition" || node->name == "pathMotion");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 if (check_class) {
235 xml::Attribute* attr = node->FindAttribute({}, "class");
236 if (attr && util::IsJavaClassName(attr->value)) {
Jake Wharton98100c32018-06-11 15:46:03 -0400237 AddClass(node->line_number, attr->value,
238 "android.content.Context, android.util.AttributeSet");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700240 }
241
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 BaseVisitor::Visit(node);
243 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700244
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 private:
246 DISALLOW_COPY_AND_ASSIGN(TransitionVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700247};
248
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249class ManifestVisitor : public BaseVisitor {
250 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700251 ManifestVisitor(const ResourceFile& file, KeepSet* keep_set, bool main_dex_only)
252 : BaseVisitor(file, keep_set), main_dex_only_(main_dex_only) {
253 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700254
Adam Lesinski6b372992017-08-09 10:54:23 -0700255 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 if (node->namespace_uri.empty()) {
257 bool get_name = false;
258 if (node->name == "manifest") {
259 xml::Attribute* attr = node->FindAttribute({}, "package");
260 if (attr) {
261 package_ = attr->value;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700262 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 } else if (node->name == "application") {
264 get_name = true;
Adam Lesinski6b372992017-08-09 10:54:23 -0700265 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "backupAgent");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 if (attr) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700267 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 if (result) {
Jake Wharton98100c32018-06-11 15:46:03 -0400269 AddClass(node->line_number, result.value(), "");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 }
271 }
Jake Whartone4bd1602018-06-12 09:39:14 -0400272 attr = node->FindAttribute(xml::kSchemaAndroid, "appComponentFactory");
273 if (attr) {
274 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
275 if (result) {
Jake Wharton98100c32018-06-11 15:46:03 -0400276 AddClass(node->line_number, result.value(), "");
Jake Whartone4bd1602018-06-12 09:39:14 -0400277 }
278 }
Martijn Coenen6c83e0c2019-03-06 15:06:17 +0100279
280 attr = node->FindAttribute(xml::kSchemaAndroid, "zygotePreloadName");
281 if (attr) {
282 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
283 if (result) {
284 AddClass(node->line_number, result.value(), "");
285 }
286 }
287
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 if (main_dex_only_) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700289 xml::Attribute* default_process = node->FindAttribute(xml::kSchemaAndroid, "process");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 if (default_process) {
291 default_process_ = default_process->value;
292 }
293 }
294 } else if (node->name == "activity" || node->name == "service" ||
295 node->name == "receiver" || node->name == "provider") {
296 get_name = true;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700297
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 if (main_dex_only_) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700299 xml::Attribute* component_process = node->FindAttribute(xml::kSchemaAndroid, "process");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300
301 const std::string& process =
302 component_process ? component_process->value : default_process_;
303 get_name = !process.empty() && process[0] != ':';
304 }
305 } else if (node->name == "instrumentation") {
306 get_name = true;
307 }
308
309 if (get_name) {
310 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "name");
311 get_name = attr != nullptr;
312
313 if (get_name) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700314 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 if (result) {
Jake Wharton98100c32018-06-11 15:46:03 -0400316 AddClass(node->line_number, result.value(), "");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 }
318 }
319 }
320 }
321 BaseVisitor::Visit(node);
322 }
323
Jake Wharton98100c32018-06-11 15:46:03 -0400324 virtual void AddClass(size_t line_number, const std::string& class_name,
325 const std::string& ctor_signature) override {
Adam Koskidc21dea2017-07-21 10:55:27 -0700326 keep_set_->AddManifestClass({file_.name, file_.source.WithLine(line_number)}, class_name);
327 }
328
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329 private:
330 DISALLOW_COPY_AND_ASSIGN(ManifestVisitor);
331
332 std::string package_;
333 const bool main_dex_only_;
334 std::string default_process_;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700335};
336
Adam Koskidc21dea2017-07-21 10:55:27 -0700337bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, bool main_dex_only) {
338 ManifestVisitor visitor(res->file, keep_set, main_dex_only);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 if (res->root) {
340 res->root->Accept(&visitor);
341 return true;
342 }
343 return false;
344}
345
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700346bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet* keep_set) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700347 if (!res->root) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700348 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 }
350
351 switch (res->file.name.type) {
352 case ResourceType::kLayout: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700353 LayoutVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354 res->root->Accept(&visitor);
355 break;
356 }
357
358 case ResourceType::kXml: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700359 XmlResourceVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 res->root->Accept(&visitor);
361 break;
362 }
363
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700364 case ResourceType::kNavigation: {
365 NavigationVisitor visitor(res->file, keep_set, context_->GetCompilationPackage());
366 res->root->Accept(&visitor);
367 break;
368 }
369
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 case ResourceType::kTransition: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700371 TransitionVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 res->root->Accept(&visitor);
373 break;
374 }
375
Adam Lesinskif762df22017-06-26 16:39:03 -0700376 case ResourceType::kMenu: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700377 MenuVisitor visitor(res->file, keep_set);
Adam Lesinskif762df22017-06-26 16:39:03 -0700378 res->root->Accept(&visitor);
379 break;
380 }
381
Adam Koskidc21dea2017-07-21 10:55:27 -0700382 default: {
383 BaseVisitor visitor(res->file, keep_set);
384 res->root->Accept(&visitor);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 break;
Adam Koskidc21dea2017-07-21 10:55:27 -0700386 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 }
388 return true;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700389}
390
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700391void WriteKeepSet(const KeepSet& keep_set, OutputStream* out, bool minimal_keep) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800392 Printer printer(out);
Adam Koskidc21dea2017-07-21 10:55:27 -0700393 for (const auto& entry : keep_set.manifest_class_set_) {
394 for (const UsageLocation& location : entry.second) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800395 printer.Print("# Referenced at ").Println(location.source.to_string());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700396 }
Jake Whartonab660a72018-06-08 17:56:55 -0400397 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(); }");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700399
Adam Koskidc21dea2017-07-21 10:55:27 -0700400 for (const auto& entry : keep_set.conditional_class_set_) {
401 std::set<UsageLocation> locations;
Brian Duff2f7e4d12019-11-10 18:34:11 -0800402 bool can_be_conditional = false;
403 if (keep_set.conditional_keep_rules_) {
404 can_be_conditional = true;
405 for (const UsageLocation& location : entry.second) {
406 can_be_conditional &= CollectLocations(location, keep_set, &locations);
407 }
Adam Koskidc21dea2017-07-21 10:55:27 -0700408 }
409
Brian Duff2f7e4d12019-11-10 18:34:11 -0800410 if (can_be_conditional) {
Adam Koskidc21dea2017-07-21 10:55:27 -0700411 for (const UsageLocation& location : locations) {
Adam Koskif85eec82017-11-15 12:48:49 -0800412 printer.Print("# Referenced at ").Println(location.source.to_string());
413 printer.Print("-if class **.R$layout { int ")
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800414 .Print(JavaClassGenerator::TransformToFieldName(location.name.entry))
Adam Koskif85eec82017-11-15 12:48:49 -0800415 .Println("; }");
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700416
417 printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>(");
418 printer.Print((minimal_keep) ? entry.first.signature : "...");
419 printer.Println("); }");
Adam Koskidc21dea2017-07-21 10:55:27 -0700420 }
Adam Koskif85eec82017-11-15 12:48:49 -0800421 } else {
422 for (const UsageLocation& location : entry.second) {
423 printer.Print("# Referenced at ").Println(location.source.to_string());
424 }
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700425
426 printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>(");
427 printer.Print((minimal_keep) ? entry.first.signature : "...");
428 printer.Println("); }");
Adam Koskidc21dea2017-07-21 10:55:27 -0700429 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800430 printer.Println();
Adam Koskidc21dea2017-07-21 10:55:27 -0700431 }
432
433 for (const auto& entry : keep_set.method_set_) {
434 for (const UsageLocation& location : entry.second) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800435 printer.Print("# Referenced at ").Println(location.source.to_string());
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700436 }
Jake Wharton3001f032018-06-11 12:24:11 -0400437 printer.Print("-keepclassmembers class * { *** ").Print(entry.first.name)
438 .Print("(").Print(entry.first.signature).Println("); }");
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800439 printer.Println();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700441}
442
Adam Koskidc21dea2017-07-21 10:55:27 -0700443bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
444 std::set<UsageLocation>* locations) {
445 locations->insert(location);
446
447 // TODO: allow for more reference types if we can determine its safe.
448 if (location.name.type != ResourceType::kLayout) {
449 return false;
450 }
451
452 for (const auto& entry : keep_set.reference_set_) {
453 if (entry.first == location.name) {
454 for (auto& refLocation : entry.second) {
455 // Don't get stuck in loops
456 if (locations->find(refLocation) != locations->end()) {
457 return false;
458 }
459 if (!CollectLocations(refLocation, keep_set, locations)) {
460 return false;
461 }
462 }
463 }
464 }
465
466 return true;
467}
468
469class ReferenceVisitor : public ValueVisitor {
470 public:
471 using ValueVisitor::Visit;
472
473 ReferenceVisitor(aapt::IAaptContext* context, ResourceName from, KeepSet* keep_set)
474 : context_(context), from_(from), keep_set_(keep_set) {
475 }
476
477 void Visit(Reference* reference) override {
478 if (reference->name) {
479 ResourceName reference_name = reference->name.value();
480 if (reference_name.package.empty()) {
481 reference_name = ResourceName(context_->GetCompilationPackage(), reference_name.type,
482 reference_name.entry);
483 }
484 keep_set_->AddReference({from_, reference->GetSource()}, reference_name);
485 }
486 }
487
488 private:
489 aapt::IAaptContext* context_;
490 ResourceName from_;
491 KeepSet* keep_set_;
492};
493
494bool CollectResourceReferences(aapt::IAaptContext* context, ResourceTable* table,
495 KeepSet* keep_set) {
496 for (auto& pkg : table->packages) {
497 for (auto& type : pkg->types) {
498 for (auto& entry : type->entries) {
499 for (auto& config_value : entry->values) {
500 ResourceName from(pkg->name, type->type, entry->name);
501 ReferenceVisitor visitor(context, from, keep_set);
502 config_value->value->Accept(&visitor);
503 }
504 }
505 }
506 }
507 return true;
508}
509
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700510} // namespace proguard
511} // namespace aapt