blob: 92487f4e6854f9bc617aed3f252036d54641604a [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 }
Jake Whartone4bd1602018-06-12 09:39:14 -0400262 attr = node->FindAttribute(xml::kSchemaAndroid, "appComponentFactory");
263 if (attr) {
264 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
265 if (result) {
266 AddClass(node->line_number, result.value());
267 }
268 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 if (main_dex_only_) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700270 xml::Attribute* default_process = node->FindAttribute(xml::kSchemaAndroid, "process");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 if (default_process) {
272 default_process_ = default_process->value;
273 }
274 }
275 } else if (node->name == "activity" || node->name == "service" ||
276 node->name == "receiver" || node->name == "provider") {
277 get_name = true;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 if (main_dex_only_) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700280 xml::Attribute* component_process = node->FindAttribute(xml::kSchemaAndroid, "process");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281
282 const std::string& process =
283 component_process ? component_process->value : default_process_;
284 get_name = !process.empty() && process[0] != ':';
285 }
286 } else if (node->name == "instrumentation") {
287 get_name = true;
288 }
289
290 if (get_name) {
291 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "name");
292 get_name = attr != nullptr;
293
294 if (get_name) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700295 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 if (result) {
297 AddClass(node->line_number, result.value());
298 }
299 }
300 }
301 }
302 BaseVisitor::Visit(node);
303 }
304
Adam Koskidc21dea2017-07-21 10:55:27 -0700305 virtual void AddClass(size_t line_number, const std::string& class_name) override {
306 keep_set_->AddManifestClass({file_.name, file_.source.WithLine(line_number)}, class_name);
307 }
308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 private:
310 DISALLOW_COPY_AND_ASSIGN(ManifestVisitor);
311
312 std::string package_;
313 const bool main_dex_only_;
314 std::string default_process_;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700315};
316
Adam Koskidc21dea2017-07-21 10:55:27 -0700317bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, bool main_dex_only) {
318 ManifestVisitor visitor(res->file, keep_set, main_dex_only);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 if (res->root) {
320 res->root->Accept(&visitor);
321 return true;
322 }
323 return false;
324}
325
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700326bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet* keep_set) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327 if (!res->root) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700328 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329 }
330
331 switch (res->file.name.type) {
332 case ResourceType::kLayout: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700333 LayoutVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 res->root->Accept(&visitor);
335 break;
336 }
337
338 case ResourceType::kXml: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700339 XmlResourceVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 res->root->Accept(&visitor);
341 break;
342 }
343
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700344 case ResourceType::kNavigation: {
345 NavigationVisitor visitor(res->file, keep_set, context_->GetCompilationPackage());
346 res->root->Accept(&visitor);
347 break;
348 }
349
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 case ResourceType::kTransition: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700351 TransitionVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700352 res->root->Accept(&visitor);
353 break;
354 }
355
Adam Lesinskif762df22017-06-26 16:39:03 -0700356 case ResourceType::kMenu: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700357 MenuVisitor visitor(res->file, keep_set);
Adam Lesinskif762df22017-06-26 16:39:03 -0700358 res->root->Accept(&visitor);
359 break;
360 }
361
Adam Koskidc21dea2017-07-21 10:55:27 -0700362 default: {
363 BaseVisitor visitor(res->file, keep_set);
364 res->root->Accept(&visitor);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 break;
Adam Koskidc21dea2017-07-21 10:55:27 -0700366 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700367 }
368 return true;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700369}
370
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800371void WriteKeepSet(const KeepSet& keep_set, OutputStream* out) {
372 Printer printer(out);
Adam Koskidc21dea2017-07-21 10:55:27 -0700373 for (const auto& entry : keep_set.manifest_class_set_) {
374 for (const UsageLocation& location : entry.second) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800375 printer.Print("# Referenced at ").Println(location.source.to_string());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700376 }
Jake Whartonab660a72018-06-08 17:56:55 -0400377 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(); }");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700379
Adam Koskidc21dea2017-07-21 10:55:27 -0700380 for (const auto& entry : keep_set.conditional_class_set_) {
381 std::set<UsageLocation> locations;
382 bool can_be_conditional = true;
383 for (const UsageLocation& location : entry.second) {
384 can_be_conditional &= CollectLocations(location, keep_set, &locations);
385 }
386
Adam Koskidc21dea2017-07-21 10:55:27 -0700387 if (keep_set.conditional_keep_rules_ && can_be_conditional) {
Adam Koskidc21dea2017-07-21 10:55:27 -0700388 for (const UsageLocation& location : locations) {
Adam Koskif85eec82017-11-15 12:48:49 -0800389 printer.Print("# Referenced at ").Println(location.source.to_string());
390 printer.Print("-if class **.R$layout { int ")
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800391 .Print(JavaClassGenerator::TransformToFieldName(location.name.entry))
Adam Koskif85eec82017-11-15 12:48:49 -0800392 .Println("; }");
393 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(...); }");
Adam Koskidc21dea2017-07-21 10:55:27 -0700394 }
Adam Koskif85eec82017-11-15 12:48:49 -0800395 } else {
396 for (const UsageLocation& location : entry.second) {
397 printer.Print("# Referenced at ").Println(location.source.to_string());
398 }
399 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(...); }");
Adam Koskidc21dea2017-07-21 10:55:27 -0700400 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800401 printer.Println();
Adam Koskidc21dea2017-07-21 10:55:27 -0700402 }
403
404 for (const auto& entry : keep_set.method_set_) {
405 for (const UsageLocation& location : entry.second) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800406 printer.Print("# Referenced at ").Println(location.source.to_string());
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700407 }
Jake Wharton3001f032018-06-11 12:24:11 -0400408 printer.Print("-keepclassmembers class * { *** ").Print(entry.first.name)
409 .Print("(").Print(entry.first.signature).Println("); }");
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800410 printer.Println();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700412}
413
Adam Koskidc21dea2017-07-21 10:55:27 -0700414bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
415 std::set<UsageLocation>* locations) {
416 locations->insert(location);
417
418 // TODO: allow for more reference types if we can determine its safe.
419 if (location.name.type != ResourceType::kLayout) {
420 return false;
421 }
422
423 for (const auto& entry : keep_set.reference_set_) {
424 if (entry.first == location.name) {
425 for (auto& refLocation : entry.second) {
426 // Don't get stuck in loops
427 if (locations->find(refLocation) != locations->end()) {
428 return false;
429 }
430 if (!CollectLocations(refLocation, keep_set, locations)) {
431 return false;
432 }
433 }
434 }
435 }
436
437 return true;
438}
439
440class ReferenceVisitor : public ValueVisitor {
441 public:
442 using ValueVisitor::Visit;
443
444 ReferenceVisitor(aapt::IAaptContext* context, ResourceName from, KeepSet* keep_set)
445 : context_(context), from_(from), keep_set_(keep_set) {
446 }
447
448 void Visit(Reference* reference) override {
449 if (reference->name) {
450 ResourceName reference_name = reference->name.value();
451 if (reference_name.package.empty()) {
452 reference_name = ResourceName(context_->GetCompilationPackage(), reference_name.type,
453 reference_name.entry);
454 }
455 keep_set_->AddReference({from_, reference->GetSource()}, reference_name);
456 }
457 }
458
459 private:
460 aapt::IAaptContext* context_;
461 ResourceName from_;
462 KeepSet* keep_set_;
463};
464
465bool CollectResourceReferences(aapt::IAaptContext* context, ResourceTable* table,
466 KeepSet* keep_set) {
467 for (auto& pkg : table->packages) {
468 for (auto& type : pkg->types) {
469 for (auto& entry : type->entries) {
470 for (auto& config_value : entry->values) {
471 ResourceName from(pkg->name, type->type, entry->name);
472 ReferenceVisitor visitor(context, from, keep_set);
473 config_value->value->Accept(&visitor);
474 }
475 }
476 }
477 }
478 return true;
479}
480
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481} // namespace proguard
482} // namespace aapt