blob: d7ebd8c1d33ee2dc1bdfa96b22afb63ffb3ebf49 [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 void AddMethod(size_t line_number, const std::string& method_name) {
Adam Koskidc21dea2017-07-21 10:55:27 -070083 keep_set_->AddMethod({file_.name, file_.source.WithLine(line_number)}, method_name);
84 }
85
86 void AddReference(size_t line_number, Reference* ref) {
87 if (ref && ref->name) {
88 ResourceName ref_name = ref->name.value();
89 if (ref_name.package.empty()) {
90 ref_name = ResourceName(file_.name.package, ref_name.type, ref_name.entry);
91 }
92 keep_set_->AddReference({file_.name, file_.source.WithLine(line_number)}, ref_name);
93 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070095
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 private:
97 DISALLOW_COPY_AND_ASSIGN(BaseVisitor);
98
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070099};
100
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101class LayoutVisitor : public BaseVisitor {
102 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700103 LayoutVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700104 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105
Adam Lesinski6b372992017-08-09 10:54:23 -0700106 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 bool check_class = false;
108 bool check_name = false;
109 if (node->namespace_uri.empty()) {
Adam Lesinskif762df22017-06-26 16:39:03 -0700110 if (node->name == "view") {
111 check_class = true;
112 } else if (node->name == "fragment") {
113 check_class = check_name = true;
114 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 } else if (node->namespace_uri == xml::kSchemaAndroid) {
116 check_name = node->name == "fragment";
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700117 }
118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 for (const auto& attr : node->attributes) {
120 if (check_class && attr.namespace_uri.empty() && attr.name == "class" &&
121 util::IsJavaClassName(attr.value)) {
122 AddClass(node->line_number, attr.value);
123 } else if (check_name && attr.namespace_uri == xml::kSchemaAndroid &&
124 attr.name == "name" && util::IsJavaClassName(attr.value)) {
125 AddClass(node->line_number, attr.value);
126 } else if (attr.namespace_uri == xml::kSchemaAndroid &&
127 attr.name == "onClick") {
128 AddMethod(node->line_number, attr.value);
129 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700130 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131
132 BaseVisitor::Visit(node);
133 }
134
135 private:
136 DISALLOW_COPY_AND_ASSIGN(LayoutVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700137};
138
Adam Lesinskif762df22017-06-26 16:39:03 -0700139class MenuVisitor : public BaseVisitor {
140 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700141 MenuVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinskif762df22017-06-26 16:39:03 -0700142 }
143
Adam Lesinski6b372992017-08-09 10:54:23 -0700144 void Visit(xml::Element* node) override {
Adam Lesinskif762df22017-06-26 16:39:03 -0700145 if (node->namespace_uri.empty() && node->name == "item") {
146 for (const auto& attr : node->attributes) {
147 if (attr.namespace_uri == xml::kSchemaAndroid) {
148 if ((attr.name == "actionViewClass" || attr.name == "actionProviderClass") &&
149 util::IsJavaClassName(attr.value)) {
150 AddClass(node->line_number, attr.value);
151 } else if (attr.name == "onClick") {
152 AddMethod(node->line_number, attr.value);
153 }
154 }
155 }
156 }
157
158 BaseVisitor::Visit(node);
159 }
160
161 private:
162 DISALLOW_COPY_AND_ASSIGN(MenuVisitor);
163};
164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165class XmlResourceVisitor : public BaseVisitor {
166 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700167 XmlResourceVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700168 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169
Adam Lesinski6b372992017-08-09 10:54:23 -0700170 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 bool check_fragment = false;
172 if (node->namespace_uri.empty()) {
173 check_fragment =
174 node->name == "PreferenceScreen" || node->name == "header";
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700175 }
176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 if (check_fragment) {
178 xml::Attribute* attr =
179 node->FindAttribute(xml::kSchemaAndroid, "fragment");
180 if (attr && util::IsJavaClassName(attr->value)) {
181 AddClass(node->line_number, attr->value);
182 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700183 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184
185 BaseVisitor::Visit(node);
186 }
187
188 private:
189 DISALLOW_COPY_AND_ASSIGN(XmlResourceVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700190};
191
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700192class NavigationVisitor : public BaseVisitor {
193 public:
194 NavigationVisitor(const ResourceFile& file, KeepSet* keep_set, const std::string& package)
195 : BaseVisitor(file, keep_set), package_(package) {
196 }
197
198 void Visit(xml::Element* node) override {
199 const auto& attr = node->FindAttribute(xml::kSchemaAndroid, "name");
200 if (attr != nullptr && !attr->value.empty()) {
201 std::string name = (attr->value[0] == '.') ? package_ + attr->value : attr->value;
202 if (util::IsJavaClassName(name)) {
203 AddClass(node->line_number, name);
204 }
205 }
206
207 BaseVisitor::Visit(node);
208 }
209
210 private:
211 DISALLOW_COPY_AND_ASSIGN(NavigationVisitor);
212 const std::string package_;
213};
214
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215class TransitionVisitor : public BaseVisitor {
216 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700217 TransitionVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700218 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219
Adam Lesinski6b372992017-08-09 10:54:23 -0700220 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 bool check_class =
Adam Lesinski6b372992017-08-09 10:54:23 -0700222 node->namespace_uri.empty() && (node->name == "transition" || node->name == "pathMotion");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 if (check_class) {
224 xml::Attribute* attr = node->FindAttribute({}, "class");
225 if (attr && util::IsJavaClassName(attr->value)) {
226 AddClass(node->line_number, attr->value);
227 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700228 }
229
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 BaseVisitor::Visit(node);
231 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700232
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 private:
234 DISALLOW_COPY_AND_ASSIGN(TransitionVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700235};
236
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237class ManifestVisitor : public BaseVisitor {
238 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700239 ManifestVisitor(const ResourceFile& file, KeepSet* keep_set, bool main_dex_only)
240 : BaseVisitor(file, keep_set), main_dex_only_(main_dex_only) {
241 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700242
Adam Lesinski6b372992017-08-09 10:54:23 -0700243 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 if (node->namespace_uri.empty()) {
245 bool get_name = false;
246 if (node->name == "manifest") {
247 xml::Attribute* attr = node->FindAttribute({}, "package");
248 if (attr) {
249 package_ = attr->value;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700250 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 } else if (node->name == "application") {
252 get_name = true;
Adam Lesinski6b372992017-08-09 10:54:23 -0700253 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "backupAgent");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 if (attr) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700255 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 if (result) {
257 AddClass(node->line_number, result.value());
258 }
259 }
260 if (main_dex_only_) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700261 xml::Attribute* default_process = node->FindAttribute(xml::kSchemaAndroid, "process");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 if (default_process) {
263 default_process_ = default_process->value;
264 }
265 }
266 } else if (node->name == "activity" || node->name == "service" ||
267 node->name == "receiver" || node->name == "provider") {
268 get_name = true;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700269
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 if (main_dex_only_) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700271 xml::Attribute* component_process = node->FindAttribute(xml::kSchemaAndroid, "process");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272
273 const std::string& process =
274 component_process ? component_process->value : default_process_;
275 get_name = !process.empty() && process[0] != ':';
276 }
277 } else if (node->name == "instrumentation") {
278 get_name = true;
279 }
280
281 if (get_name) {
282 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "name");
283 get_name = attr != nullptr;
284
285 if (get_name) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700286 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 if (result) {
288 AddClass(node->line_number, result.value());
289 }
290 }
291 }
292 }
293 BaseVisitor::Visit(node);
294 }
295
Adam Koskidc21dea2017-07-21 10:55:27 -0700296 virtual void AddClass(size_t line_number, const std::string& class_name) override {
297 keep_set_->AddManifestClass({file_.name, file_.source.WithLine(line_number)}, class_name);
298 }
299
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 private:
301 DISALLOW_COPY_AND_ASSIGN(ManifestVisitor);
302
303 std::string package_;
304 const bool main_dex_only_;
305 std::string default_process_;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700306};
307
Adam Koskidc21dea2017-07-21 10:55:27 -0700308bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, bool main_dex_only) {
309 ManifestVisitor visitor(res->file, keep_set, main_dex_only);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 if (res->root) {
311 res->root->Accept(&visitor);
312 return true;
313 }
314 return false;
315}
316
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700317bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet* keep_set) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 if (!res->root) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700319 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 }
321
322 switch (res->file.name.type) {
323 case ResourceType::kLayout: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700324 LayoutVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 res->root->Accept(&visitor);
326 break;
327 }
328
329 case ResourceType::kXml: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700330 XmlResourceVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 res->root->Accept(&visitor);
332 break;
333 }
334
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700335 case ResourceType::kNavigation: {
336 NavigationVisitor visitor(res->file, keep_set, context_->GetCompilationPackage());
337 res->root->Accept(&visitor);
338 break;
339 }
340
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341 case ResourceType::kTransition: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700342 TransitionVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 res->root->Accept(&visitor);
344 break;
345 }
346
Adam Lesinskif762df22017-06-26 16:39:03 -0700347 case ResourceType::kMenu: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700348 MenuVisitor visitor(res->file, keep_set);
Adam Lesinskif762df22017-06-26 16:39:03 -0700349 res->root->Accept(&visitor);
350 break;
351 }
352
Adam Koskidc21dea2017-07-21 10:55:27 -0700353 default: {
354 BaseVisitor visitor(res->file, keep_set);
355 res->root->Accept(&visitor);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 break;
Adam Koskidc21dea2017-07-21 10:55:27 -0700357 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 }
359 return true;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700360}
361
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800362void WriteKeepSet(const KeepSet& keep_set, OutputStream* out) {
363 Printer printer(out);
Adam Koskidc21dea2017-07-21 10:55:27 -0700364 for (const auto& entry : keep_set.manifest_class_set_) {
365 for (const UsageLocation& location : entry.second) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800366 printer.Print("# Referenced at ").Println(location.source.to_string());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700367 }
Jake Whartonab660a72018-06-08 17:56:55 -0400368 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(); }");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700370
Adam Koskidc21dea2017-07-21 10:55:27 -0700371 for (const auto& entry : keep_set.conditional_class_set_) {
372 std::set<UsageLocation> locations;
373 bool can_be_conditional = true;
374 for (const UsageLocation& location : entry.second) {
375 can_be_conditional &= CollectLocations(location, keep_set, &locations);
376 }
377
Adam Koskidc21dea2017-07-21 10:55:27 -0700378 if (keep_set.conditional_keep_rules_ && can_be_conditional) {
Adam Koskidc21dea2017-07-21 10:55:27 -0700379 for (const UsageLocation& location : locations) {
Adam Koskif85eec82017-11-15 12:48:49 -0800380 printer.Print("# Referenced at ").Println(location.source.to_string());
381 printer.Print("-if class **.R$layout { int ")
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800382 .Print(JavaClassGenerator::TransformToFieldName(location.name.entry))
Adam Koskif85eec82017-11-15 12:48:49 -0800383 .Println("; }");
384 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(...); }");
Adam Koskidc21dea2017-07-21 10:55:27 -0700385 }
Adam Koskif85eec82017-11-15 12:48:49 -0800386 } else {
387 for (const UsageLocation& location : entry.second) {
388 printer.Print("# Referenced at ").Println(location.source.to_string());
389 }
390 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(...); }");
Adam Koskidc21dea2017-07-21 10:55:27 -0700391 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800392 printer.Println();
Adam Koskidc21dea2017-07-21 10:55:27 -0700393 }
394
395 for (const auto& entry : keep_set.method_set_) {
396 for (const UsageLocation& location : entry.second) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800397 printer.Print("# Referenced at ").Println(location.source.to_string());
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700398 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800399 printer.Print("-keepclassmembers class * { *** ").Print(entry.first).Println("(...); }");
400 printer.Println();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700402}
403
Adam Koskidc21dea2017-07-21 10:55:27 -0700404bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
405 std::set<UsageLocation>* locations) {
406 locations->insert(location);
407
408 // TODO: allow for more reference types if we can determine its safe.
409 if (location.name.type != ResourceType::kLayout) {
410 return false;
411 }
412
413 for (const auto& entry : keep_set.reference_set_) {
414 if (entry.first == location.name) {
415 for (auto& refLocation : entry.second) {
416 // Don't get stuck in loops
417 if (locations->find(refLocation) != locations->end()) {
418 return false;
419 }
420 if (!CollectLocations(refLocation, keep_set, locations)) {
421 return false;
422 }
423 }
424 }
425 }
426
427 return true;
428}
429
430class ReferenceVisitor : public ValueVisitor {
431 public:
432 using ValueVisitor::Visit;
433
434 ReferenceVisitor(aapt::IAaptContext* context, ResourceName from, KeepSet* keep_set)
435 : context_(context), from_(from), keep_set_(keep_set) {
436 }
437
438 void Visit(Reference* reference) override {
439 if (reference->name) {
440 ResourceName reference_name = reference->name.value();
441 if (reference_name.package.empty()) {
442 reference_name = ResourceName(context_->GetCompilationPackage(), reference_name.type,
443 reference_name.entry);
444 }
445 keep_set_->AddReference({from_, reference->GetSource()}, reference_name);
446 }
447 }
448
449 private:
450 aapt::IAaptContext* context_;
451 ResourceName from_;
452 KeepSet* keep_set_;
453};
454
455bool CollectResourceReferences(aapt::IAaptContext* context, ResourceTable* table,
456 KeepSet* keep_set) {
457 for (auto& pkg : table->packages) {
458 for (auto& type : pkg->types) {
459 for (auto& entry : type->entries) {
460 for (auto& config_value : entry->values) {
461 ResourceName from(pkg->name, type->type, entry->name);
462 ReferenceVisitor visitor(context, from, keep_set);
463 config_value->value->Accept(&visitor);
464 }
465 }
466 }
467 }
468 return true;
469}
470
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471} // namespace proguard
472} // namespace aapt