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 | |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 82 | 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 Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 86 | } |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 97 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | private: |
| 99 | DISALLOW_COPY_AND_ASSIGN(BaseVisitor); |
| 100 | |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | class LayoutVisitor : public BaseVisitor { |
| 104 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 105 | LayoutVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 106 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 108 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | bool check_class = false; |
| 110 | bool check_name = false; |
| 111 | if (node->namespace_uri.empty()) { |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 112 | if (node->name == "view") { |
| 113 | check_class = true; |
| 114 | } else if (node->name == "fragment") { |
| 115 | check_class = check_name = true; |
| 116 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | } else if (node->namespace_uri == xml::kSchemaAndroid) { |
| 118 | check_name = node->name == "fragment"; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | 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 Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 130 | AddMethod(node->line_number, attr.value, "android.view.View"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 131 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 132 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | |
| 134 | BaseVisitor::Visit(node); |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | DISALLOW_COPY_AND_ASSIGN(LayoutVisitor); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 141 | class MenuVisitor : public BaseVisitor { |
| 142 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 143 | MenuVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 146 | void Visit(xml::Element* node) override { |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 147 | 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 Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 154 | AddMethod(node->line_number, attr.value, "android.view.MenuItem"); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | BaseVisitor::Visit(node); |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | DISALLOW_COPY_AND_ASSIGN(MenuVisitor); |
| 165 | }; |
| 166 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 167 | class XmlResourceVisitor : public BaseVisitor { |
| 168 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 169 | XmlResourceVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 170 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 171 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 172 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 173 | bool check_fragment = false; |
| 174 | if (node->namespace_uri.empty()) { |
| 175 | check_fragment = |
| 176 | node->name == "PreferenceScreen" || node->name == "header"; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 179 | 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 Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 185 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | |
| 187 | BaseVisitor::Visit(node); |
| 188 | } |
| 189 | |
| 190 | private: |
| 191 | DISALLOW_COPY_AND_ASSIGN(XmlResourceVisitor); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 194 | class 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 217 | class TransitionVisitor : public BaseVisitor { |
| 218 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 219 | TransitionVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 220 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 221 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 222 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 223 | bool check_class = |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 224 | node->namespace_uri.empty() && (node->name == "transition" || node->name == "pathMotion"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 225 | 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 Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | BaseVisitor::Visit(node); |
| 233 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 234 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 235 | private: |
| 236 | DISALLOW_COPY_AND_ASSIGN(TransitionVisitor); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 239 | class ManifestVisitor : public BaseVisitor { |
| 240 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 241 | ManifestVisitor(const ResourceFile& file, KeepSet* keep_set, bool main_dex_only) |
| 242 | : BaseVisitor(file, keep_set), main_dex_only_(main_dex_only) { |
| 243 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 244 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 245 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 246 | 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 Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 252 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 253 | } else if (node->name == "application") { |
| 254 | get_name = true; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 255 | xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "backupAgent"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 256 | if (attr) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 257 | Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 258 | if (result) { |
| 259 | AddClass(node->line_number, result.value()); |
| 260 | } |
| 261 | } |
| 262 | if (main_dex_only_) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 263 | xml::Attribute* default_process = node->FindAttribute(xml::kSchemaAndroid, "process"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 264 | if (default_process) { |
| 265 | default_process_ = default_process->value; |
| 266 | } |
| 267 | } |
| 268 | } else if (node->name == "activity" || node->name == "service" || |
| 269 | node->name == "receiver" || node->name == "provider") { |
| 270 | get_name = true; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 271 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | if (main_dex_only_) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 273 | xml::Attribute* component_process = node->FindAttribute(xml::kSchemaAndroid, "process"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 274 | |
| 275 | const std::string& process = |
| 276 | component_process ? component_process->value : default_process_; |
| 277 | get_name = !process.empty() && process[0] != ':'; |
| 278 | } |
| 279 | } else if (node->name == "instrumentation") { |
| 280 | get_name = true; |
| 281 | } |
| 282 | |
| 283 | if (get_name) { |
| 284 | xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "name"); |
| 285 | get_name = attr != nullptr; |
| 286 | |
| 287 | if (get_name) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 288 | Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 289 | if (result) { |
| 290 | AddClass(node->line_number, result.value()); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | BaseVisitor::Visit(node); |
| 296 | } |
| 297 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 298 | virtual void AddClass(size_t line_number, const std::string& class_name) override { |
| 299 | keep_set_->AddManifestClass({file_.name, file_.source.WithLine(line_number)}, class_name); |
| 300 | } |
| 301 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 302 | private: |
| 303 | DISALLOW_COPY_AND_ASSIGN(ManifestVisitor); |
| 304 | |
| 305 | std::string package_; |
| 306 | const bool main_dex_only_; |
| 307 | std::string default_process_; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 308 | }; |
| 309 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 310 | bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, bool main_dex_only) { |
| 311 | ManifestVisitor visitor(res->file, keep_set, main_dex_only); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 312 | if (res->root) { |
| 313 | res->root->Accept(&visitor); |
| 314 | return true; |
| 315 | } |
| 316 | return false; |
| 317 | } |
| 318 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 319 | bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet* keep_set) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 320 | if (!res->root) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 321 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | switch (res->file.name.type) { |
| 325 | case ResourceType::kLayout: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 326 | LayoutVisitor visitor(res->file, keep_set); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 327 | res->root->Accept(&visitor); |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | case ResourceType::kXml: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 332 | XmlResourceVisitor visitor(res->file, keep_set); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | res->root->Accept(&visitor); |
| 334 | break; |
| 335 | } |
| 336 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 337 | case ResourceType::kNavigation: { |
| 338 | NavigationVisitor visitor(res->file, keep_set, context_->GetCompilationPackage()); |
| 339 | res->root->Accept(&visitor); |
| 340 | break; |
| 341 | } |
| 342 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 343 | case ResourceType::kTransition: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 344 | TransitionVisitor visitor(res->file, keep_set); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 345 | res->root->Accept(&visitor); |
| 346 | break; |
| 347 | } |
| 348 | |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 349 | case ResourceType::kMenu: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 350 | MenuVisitor visitor(res->file, keep_set); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 351 | res->root->Accept(&visitor); |
| 352 | break; |
| 353 | } |
| 354 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 355 | default: { |
| 356 | BaseVisitor visitor(res->file, keep_set); |
| 357 | res->root->Accept(&visitor); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | break; |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 359 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 360 | } |
| 361 | return true; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 364 | void WriteKeepSet(const KeepSet& keep_set, OutputStream* out) { |
| 365 | Printer printer(out); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 366 | for (const auto& entry : keep_set.manifest_class_set_) { |
| 367 | for (const UsageLocation& location : entry.second) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 368 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 369 | } |
Jake Wharton | ab660a7 | 2018-06-08 17:56:55 -0400 | [diff] [blame] | 370 | printer.Print("-keep class ").Print(entry.first).Println(" { <init>(); }"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 371 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 372 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 373 | for (const auto& entry : keep_set.conditional_class_set_) { |
| 374 | std::set<UsageLocation> locations; |
| 375 | bool can_be_conditional = true; |
| 376 | for (const UsageLocation& location : entry.second) { |
| 377 | can_be_conditional &= CollectLocations(location, keep_set, &locations); |
| 378 | } |
| 379 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 380 | if (keep_set.conditional_keep_rules_ && can_be_conditional) { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 381 | for (const UsageLocation& location : locations) { |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 382 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
| 383 | printer.Print("-if class **.R$layout { int ") |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 384 | .Print(JavaClassGenerator::TransformToFieldName(location.name.entry)) |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 385 | .Println("; }"); |
| 386 | printer.Print("-keep class ").Print(entry.first).Println(" { <init>(...); }"); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 387 | } |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 388 | } else { |
| 389 | for (const UsageLocation& location : entry.second) { |
| 390 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
| 391 | } |
| 392 | printer.Print("-keep class ").Print(entry.first).Println(" { <init>(...); }"); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 393 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 394 | printer.Println(); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | for (const auto& entry : keep_set.method_set_) { |
| 398 | for (const UsageLocation& location : entry.second) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 399 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 400 | } |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 401 | printer.Print("-keepclassmembers class * { *** ").Print(entry.first.name) |
| 402 | .Print("(").Print(entry.first.signature).Println("); }"); |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 403 | printer.Println(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 404 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 407 | bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set, |
| 408 | std::set<UsageLocation>* locations) { |
| 409 | locations->insert(location); |
| 410 | |
| 411 | // TODO: allow for more reference types if we can determine its safe. |
| 412 | if (location.name.type != ResourceType::kLayout) { |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | for (const auto& entry : keep_set.reference_set_) { |
| 417 | if (entry.first == location.name) { |
| 418 | for (auto& refLocation : entry.second) { |
| 419 | // Don't get stuck in loops |
| 420 | if (locations->find(refLocation) != locations->end()) { |
| 421 | return false; |
| 422 | } |
| 423 | if (!CollectLocations(refLocation, keep_set, locations)) { |
| 424 | return false; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | class ReferenceVisitor : public ValueVisitor { |
| 434 | public: |
| 435 | using ValueVisitor::Visit; |
| 436 | |
| 437 | ReferenceVisitor(aapt::IAaptContext* context, ResourceName from, KeepSet* keep_set) |
| 438 | : context_(context), from_(from), keep_set_(keep_set) { |
| 439 | } |
| 440 | |
| 441 | void Visit(Reference* reference) override { |
| 442 | if (reference->name) { |
| 443 | ResourceName reference_name = reference->name.value(); |
| 444 | if (reference_name.package.empty()) { |
| 445 | reference_name = ResourceName(context_->GetCompilationPackage(), reference_name.type, |
| 446 | reference_name.entry); |
| 447 | } |
| 448 | keep_set_->AddReference({from_, reference->GetSource()}, reference_name); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | private: |
| 453 | aapt::IAaptContext* context_; |
| 454 | ResourceName from_; |
| 455 | KeepSet* keep_set_; |
| 456 | }; |
| 457 | |
| 458 | bool CollectResourceReferences(aapt::IAaptContext* context, ResourceTable* table, |
| 459 | KeepSet* keep_set) { |
| 460 | for (auto& pkg : table->packages) { |
| 461 | for (auto& type : pkg->types) { |
| 462 | for (auto& entry : type->entries) { |
| 463 | for (auto& config_value : entry->values) { |
| 464 | ResourceName from(pkg->name, type->type, entry->name); |
| 465 | ReferenceVisitor visitor(context, from, keep_set); |
| 466 | config_value->value->Accept(&visitor); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | return true; |
| 472 | } |
| 473 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 474 | } // namespace proguard |
| 475 | } // namespace aapt |