Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 17 | #include "java/ProguardRules.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 19 | #include <memory> |
| 20 | #include <string> |
| 21 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "android-base/macros.h" |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 23 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 24 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 25 | #include "JavaClassGenerator.h" |
| 26 | #include "ResourceUtils.h" |
| 27 | #include "ValueVisitor.h" |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 28 | #include "text/Printer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 29 | #include "util/Util.h" |
| 30 | #include "xml/XmlDom.h" |
| 31 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 32 | using ::aapt::io::OutputStream; |
| 33 | using ::aapt::text::Printer; |
| 34 | |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 35 | namespace aapt { |
| 36 | namespace proguard { |
| 37 | |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 38 | class BaseVisitor : public xml::Visitor { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | public: |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 40 | using xml::Visitor::Visit; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 42 | BaseVisitor(const ResourceFile& file, KeepSet* keep_set) : file_(file), keep_set_(keep_set) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 44 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 45 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | 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 Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 54 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | } |
| 56 | } else if (util::IsJavaClassName(node->name)) { |
| 57 | AddClass(node->line_number, node->name); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | for (const auto& child : node->children) { |
| 61 | child->Accept(this); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 62 | } |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 63 | |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | protected: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 75 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | void AddMethod(size_t line_number, const std::string& method_name) { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 83 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | private: |
| 97 | DISALLOW_COPY_AND_ASSIGN(BaseVisitor); |
| 98 | |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | class LayoutVisitor : public BaseVisitor { |
| 102 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 103 | LayoutVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 104 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 105 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 106 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | bool check_class = false; |
| 108 | bool check_name = false; |
| 109 | if (node->namespace_uri.empty()) { |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 110 | if (node->name == "view") { |
| 111 | check_class = true; |
| 112 | } else if (node->name == "fragment") { |
| 113 | check_class = check_name = true; |
| 114 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | } else if (node->namespace_uri == xml::kSchemaAndroid) { |
| 116 | check_name = node->name == "fragment"; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | 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 Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 130 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 131 | |
| 132 | BaseVisitor::Visit(node); |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | DISALLOW_COPY_AND_ASSIGN(LayoutVisitor); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 139 | class MenuVisitor : public BaseVisitor { |
| 140 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 141 | MenuVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 144 | void Visit(xml::Element* node) override { |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 145 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | class XmlResourceVisitor : public BaseVisitor { |
| 166 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 167 | XmlResourceVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 168 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 170 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 171 | bool check_fragment = false; |
| 172 | if (node->namespace_uri.empty()) { |
| 173 | check_fragment = |
| 174 | node->name == "PreferenceScreen" || node->name == "header"; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | 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 Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 183 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | |
| 185 | BaseVisitor::Visit(node); |
| 186 | } |
| 187 | |
| 188 | private: |
| 189 | DISALLOW_COPY_AND_ASSIGN(XmlResourceVisitor); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 192 | class 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | class TransitionVisitor : public BaseVisitor { |
| 216 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 217 | TransitionVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 218 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 219 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 220 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 221 | bool check_class = |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 222 | node->namespace_uri.empty() && (node->name == "transition" || node->name == "pathMotion"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 223 | 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 Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 230 | BaseVisitor::Visit(node); |
| 231 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 232 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 233 | private: |
| 234 | DISALLOW_COPY_AND_ASSIGN(TransitionVisitor); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 237 | class ManifestVisitor : public BaseVisitor { |
| 238 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 239 | ManifestVisitor(const ResourceFile& file, KeepSet* keep_set, bool main_dex_only) |
| 240 | : BaseVisitor(file, keep_set), main_dex_only_(main_dex_only) { |
| 241 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 242 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 243 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 244 | 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 Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 250 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | } else if (node->name == "application") { |
| 252 | get_name = true; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 253 | xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "backupAgent"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 254 | if (attr) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 255 | Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 256 | if (result) { |
| 257 | AddClass(node->line_number, result.value()); |
| 258 | } |
| 259 | } |
| 260 | if (main_dex_only_) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 261 | xml::Attribute* default_process = node->FindAttribute(xml::kSchemaAndroid, "process"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | 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 Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 269 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 270 | if (main_dex_only_) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 271 | xml::Attribute* component_process = node->FindAttribute(xml::kSchemaAndroid, "process"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | |
| 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 Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 286 | Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 287 | if (result) { |
| 288 | AddClass(node->line_number, result.value()); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | BaseVisitor::Visit(node); |
| 294 | } |
| 295 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 296 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 300 | private: |
| 301 | DISALLOW_COPY_AND_ASSIGN(ManifestVisitor); |
| 302 | |
| 303 | std::string package_; |
| 304 | const bool main_dex_only_; |
| 305 | std::string default_process_; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 306 | }; |
| 307 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 308 | bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, bool main_dex_only) { |
| 309 | ManifestVisitor visitor(res->file, keep_set, main_dex_only); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 310 | if (res->root) { |
| 311 | res->root->Accept(&visitor); |
| 312 | return true; |
| 313 | } |
| 314 | return false; |
| 315 | } |
| 316 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 317 | bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet* keep_set) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 318 | if (!res->root) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 319 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | switch (res->file.name.type) { |
| 323 | case ResourceType::kLayout: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 324 | LayoutVisitor visitor(res->file, keep_set); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 325 | res->root->Accept(&visitor); |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | case ResourceType::kXml: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 330 | XmlResourceVisitor visitor(res->file, keep_set); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 331 | res->root->Accept(&visitor); |
| 332 | break; |
| 333 | } |
| 334 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 335 | case ResourceType::kNavigation: { |
| 336 | NavigationVisitor visitor(res->file, keep_set, context_->GetCompilationPackage()); |
| 337 | res->root->Accept(&visitor); |
| 338 | break; |
| 339 | } |
| 340 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 341 | case ResourceType::kTransition: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 342 | TransitionVisitor visitor(res->file, keep_set); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 343 | res->root->Accept(&visitor); |
| 344 | break; |
| 345 | } |
| 346 | |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 347 | case ResourceType::kMenu: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 348 | MenuVisitor visitor(res->file, keep_set); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 349 | res->root->Accept(&visitor); |
| 350 | break; |
| 351 | } |
| 352 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 353 | default: { |
| 354 | BaseVisitor visitor(res->file, keep_set); |
| 355 | res->root->Accept(&visitor); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 356 | break; |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 357 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | } |
| 359 | return true; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 362 | void WriteKeepSet(const KeepSet& keep_set, OutputStream* out) { |
| 363 | Printer printer(out); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 364 | for (const auto& entry : keep_set.manifest_class_set_) { |
| 365 | for (const UsageLocation& location : entry.second) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 366 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 367 | } |
Jake Wharton | ab660a7 | 2018-06-08 17:56:55 -0400 | [diff] [blame^] | 368 | printer.Print("-keep class ").Print(entry.first).Println(" { <init>(); }"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 369 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 370 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 371 | 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 Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 378 | if (keep_set.conditional_keep_rules_ && can_be_conditional) { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 379 | for (const UsageLocation& location : locations) { |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 380 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
| 381 | printer.Print("-if class **.R$layout { int ") |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 382 | .Print(JavaClassGenerator::TransformToFieldName(location.name.entry)) |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 383 | .Println("; }"); |
| 384 | printer.Print("-keep class ").Print(entry.first).Println(" { <init>(...); }"); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 385 | } |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 386 | } 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 Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 391 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 392 | printer.Println(); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | for (const auto& entry : keep_set.method_set_) { |
| 396 | for (const UsageLocation& location : entry.second) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 397 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 398 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 399 | printer.Print("-keepclassmembers class * { *** ").Print(entry.first).Println("(...); }"); |
| 400 | printer.Println(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 401 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 404 | bool 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 | |
| 430 | class 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 | |
| 455 | bool 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 471 | } // namespace proguard |
| 472 | } // namespace aapt |