blob: 49f1b3cd31f30ea011e81b76a1741a8981dceae0 [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;
118 bool is_fragment = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 if (node->namespace_uri.empty()) {
Adam Lesinskif762df22017-06-26 16:39:03 -0700120 if (node->name == "view") {
Jake Wharton98100c32018-06-11 15:46:03 -0400121 is_view = true;
Adam Lesinskif762df22017-06-26 16:39:03 -0700122 } else if (node->name == "fragment") {
Jake Wharton98100c32018-06-11 15:46:03 -0400123 is_fragment = true;
Adam Lesinskif762df22017-06-26 16:39:03 -0700124 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 } else if (node->namespace_uri == xml::kSchemaAndroid) {
Jake Wharton98100c32018-06-11 15:46:03 -0400126 is_fragment = node->name == "fragment";
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700127 }
128
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 for (const auto& attr : node->attributes) {
Jake Wharton98100c32018-06-11 15:46:03 -0400130 if (attr.namespace_uri.empty() && attr.name == "class") {
131 if (util::IsJavaClassName(attr.value)) {
132 if (is_view) {
133 AddClass(node->line_number, attr.value,
134 "android.content.Context, android.util.AttributeSet");
135 } else if (is_fragment) {
136 AddClass(node->line_number, attr.value, "");
137 }
138 }
139 } else if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "name") {
140 if (is_fragment && util::IsJavaClassName(attr.value)) {
141 AddClass(node->line_number, attr.value, "");
142 }
143 } else if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "onClick") {
Jake Wharton3001f032018-06-11 12:24:11 -0400144 AddMethod(node->line_number, attr.value, "android.view.View");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700146 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147
148 BaseVisitor::Visit(node);
149 }
150
151 private:
152 DISALLOW_COPY_AND_ASSIGN(LayoutVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700153};
154
Adam Lesinskif762df22017-06-26 16:39:03 -0700155class MenuVisitor : public BaseVisitor {
156 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700157 MenuVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinskif762df22017-06-26 16:39:03 -0700158 }
159
Adam Lesinski6b372992017-08-09 10:54:23 -0700160 void Visit(xml::Element* node) override {
Adam Lesinskif762df22017-06-26 16:39:03 -0700161 if (node->namespace_uri.empty() && node->name == "item") {
162 for (const auto& attr : node->attributes) {
Donald Chaidb19f512019-09-09 15:45:34 -0700163 // AppCompat-v7 defines its own versions of Android attributes if
164 // they're defined after SDK 7 (the below are from 11 and 14,
165 // respectively), so don't bother checking the XML namespace.
166 //
167 // Given the names of the containing XML files and the attribute
168 // names, it's unlikely that keeping these classes would be wrong.
169 if ((attr.name == "actionViewClass" || attr.name == "actionProviderClass") &&
170 util::IsJavaClassName(attr.value)) {
171 AddClass(node->line_number, attr.value, "android.content.Context");
172 }
173
174 if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "onClick") {
175 AddMethod(node->line_number, attr.value, "android.view.MenuItem");
Adam Lesinskif762df22017-06-26 16:39:03 -0700176 }
177 }
178 }
179
180 BaseVisitor::Visit(node);
181 }
182
183 private:
184 DISALLOW_COPY_AND_ASSIGN(MenuVisitor);
185};
186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187class XmlResourceVisitor : public BaseVisitor {
188 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700189 XmlResourceVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700190 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191
Adam Lesinski6b372992017-08-09 10:54:23 -0700192 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 bool check_fragment = false;
194 if (node->namespace_uri.empty()) {
195 check_fragment =
196 node->name == "PreferenceScreen" || node->name == "header";
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700197 }
198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 if (check_fragment) {
200 xml::Attribute* attr =
201 node->FindAttribute(xml::kSchemaAndroid, "fragment");
202 if (attr && util::IsJavaClassName(attr->value)) {
Jake Wharton98100c32018-06-11 15:46:03 -0400203 AddClass(node->line_number, attr->value, "");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700205 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206
207 BaseVisitor::Visit(node);
208 }
209
210 private:
211 DISALLOW_COPY_AND_ASSIGN(XmlResourceVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700212};
213
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700214class NavigationVisitor : public BaseVisitor {
215 public:
216 NavigationVisitor(const ResourceFile& file, KeepSet* keep_set, const std::string& package)
217 : BaseVisitor(file, keep_set), package_(package) {
218 }
219
220 void Visit(xml::Element* node) override {
221 const auto& attr = node->FindAttribute(xml::kSchemaAndroid, "name");
222 if (attr != nullptr && !attr->value.empty()) {
223 std::string name = (attr->value[0] == '.') ? package_ + attr->value : attr->value;
224 if (util::IsJavaClassName(name)) {
Jake Wharton98100c32018-06-11 15:46:03 -0400225 AddClass(node->line_number, name, "...");
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700226 }
227 }
228
229 BaseVisitor::Visit(node);
230 }
231
232 private:
233 DISALLOW_COPY_AND_ASSIGN(NavigationVisitor);
234 const std::string package_;
235};
236
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237class TransitionVisitor : public BaseVisitor {
238 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700239 TransitionVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700240 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241
Adam Lesinski6b372992017-08-09 10:54:23 -0700242 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 bool check_class =
Adam Lesinski6b372992017-08-09 10:54:23 -0700244 node->namespace_uri.empty() && (node->name == "transition" || node->name == "pathMotion");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 if (check_class) {
246 xml::Attribute* attr = node->FindAttribute({}, "class");
247 if (attr && util::IsJavaClassName(attr->value)) {
Jake Wharton98100c32018-06-11 15:46:03 -0400248 AddClass(node->line_number, attr->value,
249 "android.content.Context, android.util.AttributeSet");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700251 }
252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 BaseVisitor::Visit(node);
254 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700255
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 private:
257 DISALLOW_COPY_AND_ASSIGN(TransitionVisitor);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700258};
259
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260class ManifestVisitor : public BaseVisitor {
261 public:
Adam Koskidc21dea2017-07-21 10:55:27 -0700262 ManifestVisitor(const ResourceFile& file, KeepSet* keep_set, bool main_dex_only)
263 : BaseVisitor(file, keep_set), main_dex_only_(main_dex_only) {
264 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700265
Adam Lesinski6b372992017-08-09 10:54:23 -0700266 void Visit(xml::Element* node) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 if (node->namespace_uri.empty()) {
268 bool get_name = false;
269 if (node->name == "manifest") {
270 xml::Attribute* attr = node->FindAttribute({}, "package");
271 if (attr) {
272 package_ = attr->value;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700273 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 } else if (node->name == "application") {
275 get_name = true;
Adam Lesinski6b372992017-08-09 10:54:23 -0700276 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "backupAgent");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 if (attr) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700278 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 if (result) {
Jake Wharton98100c32018-06-11 15:46:03 -0400280 AddClass(node->line_number, result.value(), "");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 }
282 }
Jake Whartone4bd1602018-06-12 09:39:14 -0400283 attr = node->FindAttribute(xml::kSchemaAndroid, "appComponentFactory");
284 if (attr) {
285 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
286 if (result) {
Jake Wharton98100c32018-06-11 15:46:03 -0400287 AddClass(node->line_number, result.value(), "");
Jake Whartone4bd1602018-06-12 09:39:14 -0400288 }
289 }
Martijn Coenen6c83e0c2019-03-06 15:06:17 +0100290
291 attr = node->FindAttribute(xml::kSchemaAndroid, "zygotePreloadName");
292 if (attr) {
293 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
294 if (result) {
295 AddClass(node->line_number, result.value(), "");
296 }
297 }
298
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 if (main_dex_only_) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700300 xml::Attribute* default_process = node->FindAttribute(xml::kSchemaAndroid, "process");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 if (default_process) {
302 default_process_ = default_process->value;
303 }
304 }
305 } else if (node->name == "activity" || node->name == "service" ||
306 node->name == "receiver" || node->name == "provider") {
307 get_name = true;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 if (main_dex_only_) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700310 xml::Attribute* component_process = node->FindAttribute(xml::kSchemaAndroid, "process");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311
312 const std::string& process =
313 component_process ? component_process->value : default_process_;
314 get_name = !process.empty() && process[0] != ':';
315 }
316 } else if (node->name == "instrumentation") {
317 get_name = true;
318 }
319
320 if (get_name) {
321 xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "name");
322 get_name = attr != nullptr;
323
324 if (get_name) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700325 Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 if (result) {
Jake Wharton98100c32018-06-11 15:46:03 -0400327 AddClass(node->line_number, result.value(), "");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 }
329 }
330 }
331 }
332 BaseVisitor::Visit(node);
333 }
334
Jake Wharton98100c32018-06-11 15:46:03 -0400335 virtual void AddClass(size_t line_number, const std::string& class_name,
336 const std::string& ctor_signature) override {
Adam Koskidc21dea2017-07-21 10:55:27 -0700337 keep_set_->AddManifestClass({file_.name, file_.source.WithLine(line_number)}, class_name);
338 }
339
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 private:
341 DISALLOW_COPY_AND_ASSIGN(ManifestVisitor);
342
343 std::string package_;
344 const bool main_dex_only_;
345 std::string default_process_;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700346};
347
Adam Koskidc21dea2017-07-21 10:55:27 -0700348bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, bool main_dex_only) {
349 ManifestVisitor visitor(res->file, keep_set, main_dex_only);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 if (res->root) {
351 res->root->Accept(&visitor);
352 return true;
353 }
354 return false;
355}
356
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700357bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet* keep_set) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 if (!res->root) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700359 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 }
361
362 switch (res->file.name.type) {
363 case ResourceType::kLayout: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700364 LayoutVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 res->root->Accept(&visitor);
366 break;
367 }
368
369 case ResourceType::kXml: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700370 XmlResourceVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 res->root->Accept(&visitor);
372 break;
373 }
374
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -0700375 case ResourceType::kNavigation: {
376 NavigationVisitor visitor(res->file, keep_set, context_->GetCompilationPackage());
377 res->root->Accept(&visitor);
378 break;
379 }
380
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 case ResourceType::kTransition: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700382 TransitionVisitor visitor(res->file, keep_set);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700383 res->root->Accept(&visitor);
384 break;
385 }
386
Adam Lesinskif762df22017-06-26 16:39:03 -0700387 case ResourceType::kMenu: {
Adam Koskidc21dea2017-07-21 10:55:27 -0700388 MenuVisitor visitor(res->file, keep_set);
Adam Lesinskif762df22017-06-26 16:39:03 -0700389 res->root->Accept(&visitor);
390 break;
391 }
392
Adam Koskidc21dea2017-07-21 10:55:27 -0700393 default: {
394 BaseVisitor visitor(res->file, keep_set);
395 res->root->Accept(&visitor);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 break;
Adam Koskidc21dea2017-07-21 10:55:27 -0700397 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398 }
399 return true;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700400}
401
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700402void WriteKeepSet(const KeepSet& keep_set, OutputStream* out, bool minimal_keep) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800403 Printer printer(out);
Adam Koskidc21dea2017-07-21 10:55:27 -0700404 for (const auto& entry : keep_set.manifest_class_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 Lesinski1ab598f2015-08-14 14:26:04 -0700407 }
Jake Whartonab660a72018-06-08 17:56:55 -0400408 printer.Print("-keep class ").Print(entry.first).Println(" { <init>(); }");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700410
Adam Koskidc21dea2017-07-21 10:55:27 -0700411 for (const auto& entry : keep_set.conditional_class_set_) {
412 std::set<UsageLocation> locations;
413 bool can_be_conditional = true;
414 for (const UsageLocation& location : entry.second) {
415 can_be_conditional &= CollectLocations(location, keep_set, &locations);
416 }
417
Adam Koskidc21dea2017-07-21 10:55:27 -0700418 if (keep_set.conditional_keep_rules_ && can_be_conditional) {
Adam Koskidc21dea2017-07-21 10:55:27 -0700419 for (const UsageLocation& location : locations) {
Adam Koskif85eec82017-11-15 12:48:49 -0800420 printer.Print("# Referenced at ").Println(location.source.to_string());
421 printer.Print("-if class **.R$layout { int ")
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800422 .Print(JavaClassGenerator::TransformToFieldName(location.name.entry))
Adam Koskif85eec82017-11-15 12:48:49 -0800423 .Println("; }");
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700424
425 printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>(");
426 printer.Print((minimal_keep) ? entry.first.signature : "...");
427 printer.Println("); }");
Adam Koskidc21dea2017-07-21 10:55:27 -0700428 }
Adam Koskif85eec82017-11-15 12:48:49 -0800429 } else {
430 for (const UsageLocation& location : entry.second) {
431 printer.Print("# Referenced at ").Println(location.source.to_string());
432 }
Ryan Mitchell7e5236d2018-09-25 15:20:59 -0700433
434 printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>(");
435 printer.Print((minimal_keep) ? entry.first.signature : "...");
436 printer.Println("); }");
Adam Koskidc21dea2017-07-21 10:55:27 -0700437 }
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800438 printer.Println();
Adam Koskidc21dea2017-07-21 10:55:27 -0700439 }
440
441 for (const auto& entry : keep_set.method_set_) {
442 for (const UsageLocation& location : entry.second) {
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800443 printer.Print("# Referenced at ").Println(location.source.to_string());
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700444 }
Jake Wharton3001f032018-06-11 12:24:11 -0400445 printer.Print("-keepclassmembers class * { *** ").Print(entry.first.name)
446 .Print("(").Print(entry.first.signature).Println("); }");
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800447 printer.Println();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700449}
450
Adam Koskidc21dea2017-07-21 10:55:27 -0700451bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
452 std::set<UsageLocation>* locations) {
453 locations->insert(location);
454
455 // TODO: allow for more reference types if we can determine its safe.
456 if (location.name.type != ResourceType::kLayout) {
457 return false;
458 }
459
460 for (const auto& entry : keep_set.reference_set_) {
461 if (entry.first == location.name) {
462 for (auto& refLocation : entry.second) {
463 // Don't get stuck in loops
464 if (locations->find(refLocation) != locations->end()) {
465 return false;
466 }
467 if (!CollectLocations(refLocation, keep_set, locations)) {
468 return false;
469 }
470 }
471 }
472 }
473
474 return true;
475}
476
477class ReferenceVisitor : public ValueVisitor {
478 public:
479 using ValueVisitor::Visit;
480
481 ReferenceVisitor(aapt::IAaptContext* context, ResourceName from, KeepSet* keep_set)
482 : context_(context), from_(from), keep_set_(keep_set) {
483 }
484
485 void Visit(Reference* reference) override {
486 if (reference->name) {
487 ResourceName reference_name = reference->name.value();
488 if (reference_name.package.empty()) {
489 reference_name = ResourceName(context_->GetCompilationPackage(), reference_name.type,
490 reference_name.entry);
491 }
492 keep_set_->AddReference({from_, reference->GetSource()}, reference_name);
493 }
494 }
495
496 private:
497 aapt::IAaptContext* context_;
498 ResourceName from_;
499 KeepSet* keep_set_;
500};
501
502bool CollectResourceReferences(aapt::IAaptContext* context, ResourceTable* table,
503 KeepSet* keep_set) {
504 for (auto& pkg : table->packages) {
505 for (auto& type : pkg->types) {
506 for (auto& entry : type->entries) {
507 for (auto& config_value : entry->values) {
508 ResourceName from(pkg->name, type->type, entry->name);
509 ReferenceVisitor visitor(context, from, keep_set);
510 config_value->value->Accept(&visitor);
511 }
512 }
513 }
514 }
515 return true;
516}
517
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700518} // namespace proguard
519} // namespace aapt