blob: 63f9dd327c0b63099c9a1970cae98308d555770a [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)) {
Jake Wharton98100c32018-06-11 15:46:03 -040053 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)) {
Jake Wharton98100c32018-06-11 15:46:03 -040057 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
Jake Wharton98100c32018-06-11 15:46:03 -040078 virtual void AddClass(size_t line_number, const std::string& class_name,
79 const std::string& ctor_signature) {
80 keep_set_->AddConditionalClass({file_.name, file_.source.WithLine(line_number)},
81 {class_name, ctor_signature});
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070083
Jake Wharton3001f032018-06-11 12:24:11 -040084 void AddMethod(size_t line_number, const std::string& method_name,
85 const std::string& method_signature) {
86 keep_set_->AddMethod({file_.name, file_.source.WithLine(line_number)},
87 {method_name, method_signature});
Adam Koskidc21dea2017-07-21 10:55:27 -070088 }
89
90 void AddReference(size_t line_number, Reference* ref) {
91 if (ref && ref->name) {
92 ResourceName ref_name = ref->name.value();
93 if (ref_name.package.empty()) {
94 ref_name = ResourceName(file_.name.package, ref_name.type, ref_name.entry);
95 }
96 keep_set_->AddReference({file_.name, file_.source.WithLine(line_number)}, ref_name);
97 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070099
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 private:
101 DISALLOW_COPY_AND_ASSIGN(BaseVisitor);
102
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700103};
104
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105class LayoutVisitor : public BaseVisitor {
106 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700107 LayoutVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700108 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109
Adam Lesinski6b372992017-08-09 10:54:23 -0700110 void Visit(xml::Element* node) override {
Jake Wharton98100c32018-06-11 15:46:03 -0400111 bool is_view = false;
112 bool is_fragment = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 if (node->namespace_uri.empty()) {
Adam Lesinskif762df22017-06-26 16:39:03 -0700114 if (node->name == "view") {
Jake Wharton98100c32018-06-11 15:46:03 -0400115 is_view = true;
Adam Lesinskif762df22017-06-26 16:39:03 -0700116 } else if (node->name == "fragment") {
Jake Wharton98100c32018-06-11 15:46:03 -0400117 is_fragment = true;
Adam Lesinskif762df22017-06-26 16:39:03 -0700118 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 } else if (node->namespace_uri == xml::kSchemaAndroid) {
Jake Wharton98100c32018-06-11 15:46:03 -0400120 is_fragment = node->name == "fragment";
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700121 }
122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 for (const auto& attr : node->attributes) {
Jake Wharton98100c32018-06-11 15:46:03 -0400124 if (attr.namespace_uri.empty() && attr.name == "class") {
125 if (util::IsJavaClassName(attr.value)) {
126 if (is_view) {
127 AddClass(node->line_number, attr.value,
128 "android.content.Context, android.util.AttributeSet");
129 } else if (is_fragment) {
130 AddClass(node->line_number, attr.value, "");
131 }
132 }
133 } else if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "name") {
134 if (is_fragment && util::IsJavaClassName(attr.value)) {
135 AddClass(node->line_number, attr.value, "");
136 }
137 } else if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "onClick") {
Jake Wharton3001f032018-06-11 12:24:11 -0400138 AddMethod(node->line_number, attr.value, "android.view.View");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700140 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141
142 BaseVisitor::Visit(node);
143 }
144
145 private:
146 DISALLOW_COPY_AND_ASSIGN(LayoutVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700147};
148
Adam Lesinskif762df22017-06-26 16:39:03 -0700149class MenuVisitor : public BaseVisitor {
150 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700151 MenuVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinskif762df22017-06-26 16:39:03 -0700152 }
153
Adam Lesinski6b372992017-08-09 10:54:23 -0700154 void Visit(xml::Element* node) override {
Adam Lesinskif762df22017-06-26 16:39:03 -0700155 if (node->namespace_uri.empty() && node->name == "item") {
156 for (const auto& attr : node->attributes) {
157 if (attr.namespace_uri == xml::kSchemaAndroid) {
158 if ((attr.name == "actionViewClass" || attr.name == "actionProviderClass") &&
159 util::IsJavaClassName(attr.value)) {
Jake Wharton98100c32018-06-11 15:46:03 -0400160 AddClass(node->line_number, attr.value, "android.content.Context");
Adam Lesinskif762df22017-06-26 16:39:03 -0700161 } else if (attr.name == "onClick") {
Jake Wharton3001f032018-06-11 12:24:11 -0400162 AddMethod(node->line_number, attr.value, "android.view.MenuItem");
Adam Lesinskif762df22017-06-26 16:39:03 -0700163 }
164 }
165 }
166 }
167
168 BaseVisitor::Visit(node);
169 }
170
171 private:
172 DISALLOW_COPY_AND_ASSIGN(MenuVisitor);
173};
174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175class XmlResourceVisitor : public BaseVisitor {
176 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700177 XmlResourceVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700178 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179
Adam Lesinski6b372992017-08-09 10:54:23 -0700180 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 bool check_fragment = false;
182 if (node->namespace_uri.empty()) {
183 check_fragment =
184 node->name == "PreferenceScreen" || node->name == "header";
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700185 }
186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 if (check_fragment) {
188 xml::Attribute* attr =
189 node->FindAttribute(xml::kSchemaAndroid, "fragment");
190 if (attr && util::IsJavaClassName(attr->value)) {
Jake Wharton98100c32018-06-11 15:46:03 -0400191 AddClass(node->line_number, attr->value, "");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700193 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194
195 BaseVisitor::Visit(node);
196 }
197
198 private:
199 DISALLOW_COPY_AND_ASSIGN(XmlResourceVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700200};
201
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700202class NavigationVisitor : public BaseVisitor {
203 public:
204 NavigationVisitor(const ResourceFile& file, KeepSet* keep_set, const std::string& package)
205 : BaseVisitor(file, keep_set), package_(package) {
206 }
207
208 void Visit(xml::Element* node) override {
209 const auto& attr = node->FindAttribute(xml::kSchemaAndroid, "name");
210 if (attr != nullptr && !attr->value.empty()) {
211 std::string name = (attr->value[0] == '.') ? package_ + attr->value : attr->value;
212 if (util::IsJavaClassName(name)) {
Jake Wharton98100c32018-06-11 15:46:03 -0400213 AddClass(node->line_number, name, "...");
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700214 }
215 }
216
217 BaseVisitor::Visit(node);
218 }
219
220 private:
221 DISALLOW_COPY_AND_ASSIGN(NavigationVisitor);
222 const std::string package_;
223};
224
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225class TransitionVisitor : public BaseVisitor {
226 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700227 TransitionVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700228 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229
Adam Lesinski6b372992017-08-09 10:54:23 -0700230 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 bool check_class =
Adam Lesinski6b372992017-08-09 10:54:23 -0700232 node->namespace_uri.empty() && (node->name == "transition" || node->name == "pathMotion");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 if (check_class) {
234 xml::Attribute* attr = node->FindAttribute({}, "class");
235 if (attr && util::IsJavaClassName(attr->value)) {
Jake Wharton98100c32018-06-11 15:46:03 -0400236 AddClass(node->line_number, attr->value,
237 "android.content.Context, android.util.AttributeSet");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700239 }
240
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 BaseVisitor::Visit(node);
242 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700243
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 private:
245 DISALLOW_COPY_AND_ASSIGN(TransitionVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700246};
247
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248class ManifestVisitor : public BaseVisitor {
249 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700250 ManifestVisitor(const ResourceFile& file, KeepSet* keep_set, bool main_dex_only)
251 : BaseVisitor(file, keep_set), main_dex_only_(main_dex_only) {
252 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700253
Adam Lesinski6b372992017-08-09 10:54:23 -0700254 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 if (node->namespace_uri.empty()) {
256 bool get_name = false;
257 if (node->name == "manifest") {
258 xml::Attribute* attr = node->FindAttribute({}, "package");
259 if (attr) {
260 package_ = attr->value;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700261 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 } else if (node->name == "application") {
263 get_name = true;
Adam Lesinski6b372992017-08-09 10:54:23 -0700264 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "backupAgent");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 if (attr) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700266 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 if (result) {
Jake Wharton98100c32018-06-11 15:46:03 -0400268 AddClass(node->line_number, result.value(), "");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 }
270 }
Jake Whartone4bd1602018-06-12 09:39:14 -0400271 attr = node->FindAttribute(xml::kSchemaAndroid, "appComponentFactory");
272 if (attr) {
273 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
274 if (result) {
Jake Wharton98100c32018-06-11 15:46:03 -0400275 AddClass(node->line_number, result.value(), "");
Jake Whartone4bd1602018-06-12 09:39:14 -0400276 }
277 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 if (main_dex_only_) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700279 xml::Attribute* default_process = node->FindAttribute(xml::kSchemaAndroid, "process");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 if (default_process) {
281 default_process_ = default_process->value;
282 }
283 }
284 } else if (node->name == "activity" || node->name == "service" ||
285 node->name == "receiver" || node->name == "provider") {
286 get_name = true;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700287
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 if (main_dex_only_) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700289 xml::Attribute* component_process = node->FindAttribute(xml::kSchemaAndroid, "process");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290
291 const std::string& process =
292 component_process ? component_process->value : default_process_;
293 get_name = !process.empty() && process[0] != ':';
294 }
295 } else if (node->name == "instrumentation") {
296 get_name = true;
297 }
298
299 if (get_name) {
300 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "name");
301 get_name = attr != nullptr;
302
303 if (get_name) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700304 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 if (result) {
Jake Wharton98100c32018-06-11 15:46:03 -0400306 AddClass(node->line_number, result.value(), "");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 }
308 }
309 }
310 }
311 BaseVisitor::Visit(node);
312 }
313
Jake Wharton98100c32018-06-11 15:46:03 -0400314 virtual void AddClass(size_t line_number, const std::string& class_name,
315 const std::string& ctor_signature) override {
Adam Koskidc21dea2017-07-21 10:55:27 -0700316 keep_set_->AddManifestClass({file_.name, file_.source.WithLine(line_number)}, class_name);
317 }
318
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 private:
320 DISALLOW_COPY_AND_ASSIGN(ManifestVisitor);
321
322 std::string package_;
323 const bool main_dex_only_;
324 std::string default_process_;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700325};
326
Adam Koskidc21dea2017-07-21 10:55:27 -0700327bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, bool main_dex_only) {
328 ManifestVisitor visitor(res->file, keep_set, main_dex_only);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329 if (res->root) {
330 res->root->Accept(&visitor);
331 return true;
332 }
333 return false;
334}
335
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700336bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet* keep_set) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 if (!res->root) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700338 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 }
340
341 switch (res->file.name.type) {
342 case ResourceType::kLayout: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700343 LayoutVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 res->root->Accept(&visitor);
345 break;
346 }
347
348 case ResourceType::kXml: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700349 XmlResourceVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 res->root->Accept(&visitor);
351 break;
352 }
353
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700354 case ResourceType::kNavigation: {
355 NavigationVisitor visitor(res->file, keep_set, context_->GetCompilationPackage());
356 res->root->Accept(&visitor);
357 break;
358 }
359
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 case ResourceType::kTransition: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700361 TransitionVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362 res->root->Accept(&visitor);
363 break;
364 }
365
Adam Lesinskif762df22017-06-26 16:39:03 -0700366 case ResourceType::kMenu: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700367 MenuVisitor visitor(res->file, keep_set);
Adam Lesinskif762df22017-06-26 16:39:03 -0700368 res->root->Accept(&visitor);
369 break;
370 }
371
Adam Koskidc21dea2017-07-21 10:55:27 -0700372 default: {
373 BaseVisitor visitor(res->file, keep_set);
374 res->root->Accept(&visitor);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 break;
Adam Koskidc21dea2017-07-21 10:55:27 -0700376 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 }
378 return true;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700379}
380
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800381void WriteKeepSet(const KeepSet& keep_set, OutputStream* out) {
382 Printer printer(out);
Adam Koskidc21dea2017-07-21 10:55:27 -0700383 for (const auto& entry : keep_set.manifest_class_set_) {
384 for (const UsageLocation& location : entry.second) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800385 printer.Print("# Referenced at ").Println(location.source.to_string());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700386 }
Jake Whartonab660a72018-06-08 17:56:55 -0400387 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(); }");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700389
Adam Koskidc21dea2017-07-21 10:55:27 -0700390 for (const auto& entry : keep_set.conditional_class_set_) {
391 std::set<UsageLocation> locations;
392 bool can_be_conditional = true;
393 for (const UsageLocation& location : entry.second) {
394 can_be_conditional &= CollectLocations(location, keep_set, &locations);
395 }
396
Adam Koskidc21dea2017-07-21 10:55:27 -0700397 if (keep_set.conditional_keep_rules_ && can_be_conditional) {
Adam Koskidc21dea2017-07-21 10:55:27 -0700398 for (const UsageLocation& location : locations) {
Adam Koskif85eec82017-11-15 12:48:49 -0800399 printer.Print("# Referenced at ").Println(location.source.to_string());
400 printer.Print("-if class **.R$layout { int ")
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800401 .Print(JavaClassGenerator::TransformToFieldName(location.name.entry))
Adam Koskif85eec82017-11-15 12:48:49 -0800402 .Println("; }");
Jake Wharton98100c32018-06-11 15:46:03 -0400403 printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>(")
404 .Print(entry.first.signature).Println("); }");
Adam Koskidc21dea2017-07-21 10:55:27 -0700405 }
Adam Koskif85eec82017-11-15 12:48:49 -0800406 } else {
407 for (const UsageLocation& location : entry.second) {
408 printer.Print("# Referenced at ").Println(location.source.to_string());
409 }
Jake Wharton98100c32018-06-11 15:46:03 -0400410 printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>(")
411 .Print(entry.first.signature).Println("); }");
Adam Koskidc21dea2017-07-21 10:55:27 -0700412 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800413 printer.Println();
Adam Koskidc21dea2017-07-21 10:55:27 -0700414 }
415
416 for (const auto& entry : keep_set.method_set_) {
417 for (const UsageLocation& location : entry.second) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800418 printer.Print("# Referenced at ").Println(location.source.to_string());
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700419 }
Jake Wharton3001f032018-06-11 12:24:11 -0400420 printer.Print("-keepclassmembers class * { *** ").Print(entry.first.name)
421 .Print("(").Print(entry.first.signature).Println("); }");
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800422 printer.Println();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700424}
425
Adam Koskidc21dea2017-07-21 10:55:27 -0700426bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
427 std::set<UsageLocation>* locations) {
428 locations->insert(location);
429
430 // TODO: allow for more reference types if we can determine its safe.
431 if (location.name.type != ResourceType::kLayout) {
432 return false;
433 }
434
435 for (const auto& entry : keep_set.reference_set_) {
436 if (entry.first == location.name) {
437 for (auto& refLocation : entry.second) {
438 // Don't get stuck in loops
439 if (locations->find(refLocation) != locations->end()) {
440 return false;
441 }
442 if (!CollectLocations(refLocation, keep_set, locations)) {
443 return false;
444 }
445 }
446 }
447 }
448
449 return true;
450}
451
452class ReferenceVisitor : public ValueVisitor {
453 public:
454 using ValueVisitor::Visit;
455
456 ReferenceVisitor(aapt::IAaptContext* context, ResourceName from, KeepSet* keep_set)
457 : context_(context), from_(from), keep_set_(keep_set) {
458 }
459
460 void Visit(Reference* reference) override {
461 if (reference->name) {
462 ResourceName reference_name = reference->name.value();
463 if (reference_name.package.empty()) {
464 reference_name = ResourceName(context_->GetCompilationPackage(), reference_name.type,
465 reference_name.entry);
466 }
467 keep_set_->AddReference({from_, reference->GetSource()}, reference_name);
468 }
469 }
470
471 private:
472 aapt::IAaptContext* context_;
473 ResourceName from_;
474 KeepSet* keep_set_;
475};
476
477bool CollectResourceReferences(aapt::IAaptContext* context, ResourceTable* table,
478 KeepSet* keep_set) {
479 for (auto& pkg : table->packages) {
480 for (auto& type : pkg->types) {
481 for (auto& entry : type->entries) {
482 for (auto& config_value : entry->values) {
483 ResourceName from(pkg->name, type->type, entry->name);
484 ReferenceVisitor visitor(context, from, keep_set);
485 config_value->value->Accept(&visitor);
486 }
487 }
488 }
489 }
490 return true;
491}
492
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493} // namespace proguard
494} // namespace aapt