blob: d03cdb3d851814e3a77cf87c7d246032a683ba57 [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
Adam Koskidc21dea2017-07-21 10:55:27 -070042 BaseVisitor(const ResourceFile& file, KeepSet* keep_set) : file_(file), keep_set_(keep_set) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070044
Adam Lesinski6b372992017-08-09 10:54:23 -070045 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 if (!node->namespace_uri.empty()) {
47 Maybe<xml::ExtractedPackage> maybe_package =
48 xml::ExtractPackageFromNamespace(node->namespace_uri);
49 if (maybe_package) {
50 // This is a custom view, let's figure out the class name from this.
51 std::string package = maybe_package.value().package + "." + node->name;
52 if (util::IsJavaClassName(package)) {
53 AddClass(node->line_number, package);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070054 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 }
56 } else if (util::IsJavaClassName(node->name)) {
57 AddClass(node->line_number, node->name);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070058 }
59
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 for (const auto& child : node->children) {
61 child->Accept(this);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070062 }
Adam Koskidc21dea2017-07-21 10:55:27 -070063
64 for (const auto& attr : node->attributes) {
65 if (attr.compiled_value) {
66 auto ref = ValueCast<Reference>(attr.compiled_value.get());
67 if (ref) {
68 AddReference(node->line_number, ref);
69 }
70 }
71 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070073
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 protected:
Adam Koskidc21dea2017-07-21 10:55:27 -070075 ResourceFile file_;
76 KeepSet* keep_set_;
77
78 virtual void AddClass(size_t line_number, const std::string& class_name) {
79 keep_set_->AddConditionalClass({file_.name, file_.source.WithLine(line_number)}, class_name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070081
Jake Wharton3001f032018-06-11 12:24:11 -040082 void AddMethod(size_t line_number, const std::string& method_name,
83 const std::string& method_signature) {
84 keep_set_->AddMethod({file_.name, file_.source.WithLine(line_number)},
85 {method_name, method_signature});
Adam Koskidc21dea2017-07-21 10:55:27 -070086 }
87
88 void AddReference(size_t line_number, Reference* ref) {
89 if (ref && ref->name) {
90 ResourceName ref_name = ref->name.value();
91 if (ref_name.package.empty()) {
92 ref_name = ResourceName(file_.name.package, ref_name.type, ref_name.entry);
93 }
94 keep_set_->AddReference({file_.name, file_.source.WithLine(line_number)}, ref_name);
95 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070097
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 private:
99 DISALLOW_COPY_AND_ASSIGN(BaseVisitor);
100
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700101};
102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103class LayoutVisitor : public BaseVisitor {
104 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700105 LayoutVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700106 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107
Adam Lesinski6b372992017-08-09 10:54:23 -0700108 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 bool check_class = false;
110 bool check_name = false;
111 if (node->namespace_uri.empty()) {
Adam Lesinskif762df22017-06-26 16:39:03 -0700112 if (node->name == "view") {
113 check_class = true;
114 } else if (node->name == "fragment") {
115 check_class = check_name = true;
116 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 } else if (node->namespace_uri == xml::kSchemaAndroid) {
118 check_name = node->name == "fragment";
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700119 }
120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 for (const auto& attr : node->attributes) {
122 if (check_class && attr.namespace_uri.empty() && attr.name == "class" &&
123 util::IsJavaClassName(attr.value)) {
124 AddClass(node->line_number, attr.value);
125 } else if (check_name && attr.namespace_uri == xml::kSchemaAndroid &&
126 attr.name == "name" && util::IsJavaClassName(attr.value)) {
127 AddClass(node->line_number, attr.value);
128 } else if (attr.namespace_uri == xml::kSchemaAndroid &&
129 attr.name == "onClick") {
Jake Wharton3001f032018-06-11 12:24:11 -0400130 AddMethod(node->line_number, attr.value, "android.view.View");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700132 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133
134 BaseVisitor::Visit(node);
135 }
136
137 private:
138 DISALLOW_COPY_AND_ASSIGN(LayoutVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700139};
140
Adam Lesinskif762df22017-06-26 16:39:03 -0700141class MenuVisitor : public BaseVisitor {
142 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700143 MenuVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinskif762df22017-06-26 16:39:03 -0700144 }
145
Adam Lesinski6b372992017-08-09 10:54:23 -0700146 void Visit(xml::Element* node) override {
Adam Lesinskif762df22017-06-26 16:39:03 -0700147 if (node->namespace_uri.empty() && node->name == "item") {
148 for (const auto& attr : node->attributes) {
149 if (attr.namespace_uri == xml::kSchemaAndroid) {
150 if ((attr.name == "actionViewClass" || attr.name == "actionProviderClass") &&
151 util::IsJavaClassName(attr.value)) {
152 AddClass(node->line_number, attr.value);
153 } else if (attr.name == "onClick") {
Jake Wharton3001f032018-06-11 12:24:11 -0400154 AddMethod(node->line_number, attr.value, "android.view.MenuItem");
Adam Lesinskif762df22017-06-26 16:39:03 -0700155 }
156 }
157 }
158 }
159
160 BaseVisitor::Visit(node);
161 }
162
163 private:
164 DISALLOW_COPY_AND_ASSIGN(MenuVisitor);
165};
166
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167class XmlResourceVisitor : public BaseVisitor {
168 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700169 XmlResourceVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700170 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171
Adam Lesinski6b372992017-08-09 10:54:23 -0700172 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 bool check_fragment = false;
174 if (node->namespace_uri.empty()) {
175 check_fragment =
176 node->name == "PreferenceScreen" || node->name == "header";
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700177 }
178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 if (check_fragment) {
180 xml::Attribute* attr =
181 node->FindAttribute(xml::kSchemaAndroid, "fragment");
182 if (attr && util::IsJavaClassName(attr->value)) {
183 AddClass(node->line_number, attr->value);
184 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700185 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186
187 BaseVisitor::Visit(node);
188 }
189
190 private:
191 DISALLOW_COPY_AND_ASSIGN(XmlResourceVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700192};
193
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700194class NavigationVisitor : public BaseVisitor {
195 public:
196 NavigationVisitor(const ResourceFile& file, KeepSet* keep_set, const std::string& package)
197 : BaseVisitor(file, keep_set), package_(package) {
198 }
199
200 void Visit(xml::Element* node) override {
201 const auto& attr = node->FindAttribute(xml::kSchemaAndroid, "name");
202 if (attr != nullptr && !attr->value.empty()) {
203 std::string name = (attr->value[0] == '.') ? package_ + attr->value : attr->value;
204 if (util::IsJavaClassName(name)) {
205 AddClass(node->line_number, name);
206 }
207 }
208
209 BaseVisitor::Visit(node);
210 }
211
212 private:
213 DISALLOW_COPY_AND_ASSIGN(NavigationVisitor);
214 const std::string package_;
215};
216
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217class TransitionVisitor : public BaseVisitor {
218 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700219 TransitionVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700220 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221
Adam Lesinski6b372992017-08-09 10:54:23 -0700222 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 bool check_class =
Adam Lesinski6b372992017-08-09 10:54:23 -0700224 node->namespace_uri.empty() && (node->name == "transition" || node->name == "pathMotion");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 if (check_class) {
226 xml::Attribute* attr = node->FindAttribute({}, "class");
227 if (attr && util::IsJavaClassName(attr->value)) {
228 AddClass(node->line_number, attr->value);
229 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700230 }
231
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 BaseVisitor::Visit(node);
233 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700234
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 private:
236 DISALLOW_COPY_AND_ASSIGN(TransitionVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700237};
238
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239class ManifestVisitor : public BaseVisitor {
240 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700241 ManifestVisitor(const ResourceFile& file, KeepSet* keep_set, bool main_dex_only)
242 : BaseVisitor(file, keep_set), main_dex_only_(main_dex_only) {
243 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700244
Adam Lesinski6b372992017-08-09 10:54:23 -0700245 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 if (node->namespace_uri.empty()) {
247 bool get_name = false;
248 if (node->name == "manifest") {
249 xml::Attribute* attr = node->FindAttribute({}, "package");
250 if (attr) {
251 package_ = attr->value;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700252 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 } else if (node->name == "application") {
254 get_name = true;
Adam Lesinski6b372992017-08-09 10:54:23 -0700255 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "backupAgent");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 if (attr) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700257 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 if (result) {
259 AddClass(node->line_number, result.value());
260 }
261 }
262 if (main_dex_only_) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700263 xml::Attribute* default_process = node->FindAttribute(xml::kSchemaAndroid, "process");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 if (default_process) {
265 default_process_ = default_process->value;
266 }
267 }
268 } else if (node->name == "activity" || node->name == "service" ||
269 node->name == "receiver" || node->name == "provider") {
270 get_name = true;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700271
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 if (main_dex_only_) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700273 xml::Attribute* component_process = node->FindAttribute(xml::kSchemaAndroid, "process");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274
275 const std::string& process =
276 component_process ? component_process->value : default_process_;
277 get_name = !process.empty() && process[0] != ':';
278 }
279 } else if (node->name == "instrumentation") {
280 get_name = true;
281 }
282
283 if (get_name) {
284 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "name");
285 get_name = attr != nullptr;
286
287 if (get_name) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700288 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 if (result) {
290 AddClass(node->line_number, result.value());
291 }
292 }
293 }
294 }
295 BaseVisitor::Visit(node);
296 }
297
Adam Koskidc21dea2017-07-21 10:55:27 -0700298 virtual void AddClass(size_t line_number, const std::string& class_name) override {
299 keep_set_->AddManifestClass({file_.name, file_.source.WithLine(line_number)}, class_name);
300 }
301
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 private:
303 DISALLOW_COPY_AND_ASSIGN(ManifestVisitor);
304
305 std::string package_;
306 const bool main_dex_only_;
307 std::string default_process_;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700308};
309
Adam Koskidc21dea2017-07-21 10:55:27 -0700310bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, bool main_dex_only) {
311 ManifestVisitor visitor(res->file, keep_set, main_dex_only);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700312 if (res->root) {
313 res->root->Accept(&visitor);
314 return true;
315 }
316 return false;
317}
318
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700319bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet* keep_set) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 if (!res->root) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700321 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 }
323
324 switch (res->file.name.type) {
325 case ResourceType::kLayout: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700326 LayoutVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327 res->root->Accept(&visitor);
328 break;
329 }
330
331 case ResourceType::kXml: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700332 XmlResourceVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333 res->root->Accept(&visitor);
334 break;
335 }
336
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700337 case ResourceType::kNavigation: {
338 NavigationVisitor visitor(res->file, keep_set, context_->GetCompilationPackage());
339 res->root->Accept(&visitor);
340 break;
341 }
342
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 case ResourceType::kTransition: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700344 TransitionVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700345 res->root->Accept(&visitor);
346 break;
347 }
348
Adam Lesinskif762df22017-06-26 16:39:03 -0700349 case ResourceType::kMenu: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700350 MenuVisitor visitor(res->file, keep_set);
Adam Lesinskif762df22017-06-26 16:39:03 -0700351 res->root->Accept(&visitor);
352 break;
353 }
354
Adam Koskidc21dea2017-07-21 10:55:27 -0700355 default: {
356 BaseVisitor visitor(res->file, keep_set);
357 res->root->Accept(&visitor);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 break;
Adam Koskidc21dea2017-07-21 10:55:27 -0700359 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 }
361 return true;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700362}
363
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800364void WriteKeepSet(const KeepSet& keep_set, OutputStream* out) {
365 Printer printer(out);
Adam Koskidc21dea2017-07-21 10:55:27 -0700366 for (const auto& entry : keep_set.manifest_class_set_) {
367 for (const UsageLocation& location : entry.second) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800368 printer.Print("# Referenced at ").Println(location.source.to_string());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700369 }
Jake Whartonab660a72018-06-08 17:56:55 -0400370 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(); }");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700372
Adam Koskidc21dea2017-07-21 10:55:27 -0700373 for (const auto& entry : keep_set.conditional_class_set_) {
374 std::set<UsageLocation> locations;
375 bool can_be_conditional = true;
376 for (const UsageLocation& location : entry.second) {
377 can_be_conditional &= CollectLocations(location, keep_set, &locations);
378 }
379
Adam Koskidc21dea2017-07-21 10:55:27 -0700380 if (keep_set.conditional_keep_rules_ && can_be_conditional) {
Adam Koskidc21dea2017-07-21 10:55:27 -0700381 for (const UsageLocation& location : locations) {
Adam Koskif85eec82017-11-15 12:48:49 -0800382 printer.Print("# Referenced at ").Println(location.source.to_string());
383 printer.Print("-if class **.R$layout { int ")
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800384 .Print(JavaClassGenerator::TransformToFieldName(location.name.entry))
Adam Koskif85eec82017-11-15 12:48:49 -0800385 .Println("; }");
386 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(...); }");
Adam Koskidc21dea2017-07-21 10:55:27 -0700387 }
Adam Koskif85eec82017-11-15 12:48:49 -0800388 } else {
389 for (const UsageLocation& location : entry.second) {
390 printer.Print("# Referenced at ").Println(location.source.to_string());
391 }
392 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(...); }");
Adam Koskidc21dea2017-07-21 10:55:27 -0700393 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800394 printer.Println();
Adam Koskidc21dea2017-07-21 10:55:27 -0700395 }
396
397 for (const auto& entry : keep_set.method_set_) {
398 for (const UsageLocation& location : entry.second) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800399 printer.Print("# Referenced at ").Println(location.source.to_string());
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700400 }
Jake Wharton3001f032018-06-11 12:24:11 -0400401 printer.Print("-keepclassmembers class * { *** ").Print(entry.first.name)
402 .Print("(").Print(entry.first.signature).Println("); }");
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800403 printer.Println();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700405}
406
Adam Koskidc21dea2017-07-21 10:55:27 -0700407bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
408 std::set<UsageLocation>* locations) {
409 locations->insert(location);
410
411 // TODO: allow for more reference types if we can determine its safe.
412 if (location.name.type != ResourceType::kLayout) {
413 return false;
414 }
415
416 for (const auto& entry : keep_set.reference_set_) {
417 if (entry.first == location.name) {
418 for (auto& refLocation : entry.second) {
419 // Don't get stuck in loops
420 if (locations->find(refLocation) != locations->end()) {
421 return false;
422 }
423 if (!CollectLocations(refLocation, keep_set, locations)) {
424 return false;
425 }
426 }
427 }
428 }
429
430 return true;
431}
432
433class ReferenceVisitor : public ValueVisitor {
434 public:
435 using ValueVisitor::Visit;
436
437 ReferenceVisitor(aapt::IAaptContext* context, ResourceName from, KeepSet* keep_set)
438 : context_(context), from_(from), keep_set_(keep_set) {
439 }
440
441 void Visit(Reference* reference) override {
442 if (reference->name) {
443 ResourceName reference_name = reference->name.value();
444 if (reference_name.package.empty()) {
445 reference_name = ResourceName(context_->GetCompilationPackage(), reference_name.type,
446 reference_name.entry);
447 }
448 keep_set_->AddReference({from_, reference->GetSource()}, reference_name);
449 }
450 }
451
452 private:
453 aapt::IAaptContext* context_;
454 ResourceName from_;
455 KeepSet* keep_set_;
456};
457
458bool CollectResourceReferences(aapt::IAaptContext* context, ResourceTable* table,
459 KeepSet* keep_set) {
460 for (auto& pkg : table->packages) {
461 for (auto& type : pkg->types) {
462 for (auto& entry : type->entries) {
463 for (auto& config_value : entry->values) {
464 ResourceName from(pkg->name, type->type, entry->name);
465 ReferenceVisitor visitor(context, from, keep_set);
466 config_value->value->Accept(&visitor);
467 }
468 }
469 }
470 }
471 return true;
472}
473
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474} // namespace proguard
475} // namespace aapt