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 | |
Jake Wharton | cc65b8d | 2018-06-11 17:05:35 -0400 | [diff] [blame] | 42 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 49 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 50 | 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 Wharton | cc65b8d | 2018-06-11 17:05:35 -0400 | [diff] [blame] | 57 | AddClass(node->line_number, package, ctor_signature_); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 58 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | } |
| 60 | } else if (util::IsJavaClassName(node->name)) { |
Jake Wharton | cc65b8d | 2018-06-11 17:05:35 -0400 | [diff] [blame] | 61 | AddClass(node->line_number, node->name, ctor_signature_); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | for (const auto& child : node->children) { |
| 65 | child->Accept(this); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 66 | } |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 67 | |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 77 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | protected: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 79 | ResourceFile file_; |
| 80 | KeepSet* keep_set_; |
Jake Wharton | cc65b8d | 2018-06-11 17:05:35 -0400 | [diff] [blame] | 81 | std::string ctor_signature_; |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 82 | |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 83 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 88 | |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 89 | 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 Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 93 | } |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 104 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 105 | private: |
| 106 | DISALLOW_COPY_AND_ASSIGN(BaseVisitor); |
| 107 | |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | class LayoutVisitor : public BaseVisitor { |
| 111 | public: |
Jake Wharton | cc65b8d | 2018-06-11 17:05:35 -0400 | [diff] [blame] | 112 | LayoutVisitor(const ResourceFile& file, KeepSet* keep_set) |
| 113 | : BaseVisitor(file, keep_set, "android.content.Context, android.util.AttributeSet") { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 114 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 116 | void Visit(xml::Element* node) override { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 117 | bool is_view = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 118 | if (node->namespace_uri.empty()) { |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 119 | if (node->name == "view") { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 120 | is_view = true; |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 121 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | for (const auto& attr : node->attributes) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 125 | if (attr.namespace_uri.empty() && attr.name == "class") { |
| 126 | if (util::IsJavaClassName(attr.value)) { |
| 127 | if (is_view) { |
| 128 | AddClass(node->line_number, attr.value, |
| 129 | "android.content.Context, android.util.AttributeSet"); |
Jeremy Woods | 4bdc698 | 2020-02-28 19:42:38 -0800 | [diff] [blame^] | 130 | } else { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 131 | AddClass(node->line_number, attr.value, ""); |
| 132 | } |
| 133 | } |
| 134 | } else if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "name") { |
Jeremy Woods | 4bdc698 | 2020-02-28 19:42:38 -0800 | [diff] [blame^] | 135 | if (util::IsJavaClassName(attr.value)) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 136 | AddClass(node->line_number, attr.value, ""); |
| 137 | } |
| 138 | } else if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "onClick") { |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 139 | AddMethod(node->line_number, attr.value, "android.view.View"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 140 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 141 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | |
| 143 | BaseVisitor::Visit(node); |
| 144 | } |
| 145 | |
| 146 | private: |
| 147 | DISALLOW_COPY_AND_ASSIGN(LayoutVisitor); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 150 | class MenuVisitor : public BaseVisitor { |
| 151 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 152 | MenuVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 155 | void Visit(xml::Element* node) override { |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 156 | if (node->namespace_uri.empty() && node->name == "item") { |
| 157 | for (const auto& attr : node->attributes) { |
| 158 | if (attr.namespace_uri == xml::kSchemaAndroid) { |
| 159 | if ((attr.name == "actionViewClass" || attr.name == "actionProviderClass") && |
| 160 | util::IsJavaClassName(attr.value)) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 161 | AddClass(node->line_number, attr.value, "android.content.Context"); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 162 | } else if (attr.name == "onClick") { |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 163 | AddMethod(node->line_number, attr.value, "android.view.MenuItem"); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | BaseVisitor::Visit(node); |
| 170 | } |
| 171 | |
| 172 | private: |
| 173 | DISALLOW_COPY_AND_ASSIGN(MenuVisitor); |
| 174 | }; |
| 175 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 176 | class XmlResourceVisitor : public BaseVisitor { |
| 177 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 178 | XmlResourceVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 179 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 180 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 181 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | bool check_fragment = false; |
| 183 | if (node->namespace_uri.empty()) { |
| 184 | check_fragment = |
| 185 | node->name == "PreferenceScreen" || node->name == "header"; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 188 | if (check_fragment) { |
| 189 | xml::Attribute* attr = |
| 190 | node->FindAttribute(xml::kSchemaAndroid, "fragment"); |
| 191 | if (attr && util::IsJavaClassName(attr->value)) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 192 | AddClass(node->line_number, attr->value, ""); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 194 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 195 | |
| 196 | BaseVisitor::Visit(node); |
| 197 | } |
| 198 | |
| 199 | private: |
| 200 | DISALLOW_COPY_AND_ASSIGN(XmlResourceVisitor); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 203 | class NavigationVisitor : public BaseVisitor { |
| 204 | public: |
| 205 | NavigationVisitor(const ResourceFile& file, KeepSet* keep_set, const std::string& package) |
| 206 | : BaseVisitor(file, keep_set), package_(package) { |
| 207 | } |
| 208 | |
| 209 | void Visit(xml::Element* node) override { |
| 210 | const auto& attr = node->FindAttribute(xml::kSchemaAndroid, "name"); |
| 211 | if (attr != nullptr && !attr->value.empty()) { |
| 212 | std::string name = (attr->value[0] == '.') ? package_ + attr->value : attr->value; |
| 213 | if (util::IsJavaClassName(name)) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 214 | AddClass(node->line_number, name, "..."); |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | BaseVisitor::Visit(node); |
| 219 | } |
| 220 | |
| 221 | private: |
| 222 | DISALLOW_COPY_AND_ASSIGN(NavigationVisitor); |
| 223 | const std::string package_; |
| 224 | }; |
| 225 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 226 | class TransitionVisitor : public BaseVisitor { |
| 227 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 228 | TransitionVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 229 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 230 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 231 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | bool check_class = |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 233 | node->namespace_uri.empty() && (node->name == "transition" || node->name == "pathMotion"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 234 | if (check_class) { |
| 235 | xml::Attribute* attr = node->FindAttribute({}, "class"); |
| 236 | if (attr && util::IsJavaClassName(attr->value)) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 237 | AddClass(node->line_number, attr->value, |
| 238 | "android.content.Context, android.util.AttributeSet"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 239 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 242 | BaseVisitor::Visit(node); |
| 243 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 244 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 245 | private: |
| 246 | DISALLOW_COPY_AND_ASSIGN(TransitionVisitor); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | class ManifestVisitor : public BaseVisitor { |
| 250 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 251 | ManifestVisitor(const ResourceFile& file, KeepSet* keep_set, bool main_dex_only) |
| 252 | : BaseVisitor(file, keep_set), main_dex_only_(main_dex_only) { |
| 253 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 254 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 255 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 256 | if (node->namespace_uri.empty()) { |
| 257 | bool get_name = false; |
| 258 | if (node->name == "manifest") { |
| 259 | xml::Attribute* attr = node->FindAttribute({}, "package"); |
| 260 | if (attr) { |
| 261 | package_ = attr->value; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 262 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 263 | } else if (node->name == "application") { |
| 264 | get_name = true; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 265 | xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "backupAgent"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | if (attr) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 267 | Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 268 | if (result) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 269 | AddClass(node->line_number, result.value(), ""); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
Jake Wharton | e4bd160 | 2018-06-12 09:39:14 -0400 | [diff] [blame] | 272 | attr = node->FindAttribute(xml::kSchemaAndroid, "appComponentFactory"); |
| 273 | if (attr) { |
| 274 | Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value); |
| 275 | if (result) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 276 | AddClass(node->line_number, result.value(), ""); |
Jake Wharton | e4bd160 | 2018-06-12 09:39:14 -0400 | [diff] [blame] | 277 | } |
| 278 | } |
Martijn Coenen | 6c83e0c | 2019-03-06 15:06:17 +0100 | [diff] [blame] | 279 | |
| 280 | attr = node->FindAttribute(xml::kSchemaAndroid, "zygotePreloadName"); |
| 281 | if (attr) { |
| 282 | Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value); |
| 283 | if (result) { |
| 284 | AddClass(node->line_number, result.value(), ""); |
| 285 | } |
| 286 | } |
| 287 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 288 | if (main_dex_only_) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 289 | xml::Attribute* default_process = node->FindAttribute(xml::kSchemaAndroid, "process"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 | if (default_process) { |
| 291 | default_process_ = default_process->value; |
| 292 | } |
| 293 | } |
| 294 | } else if (node->name == "activity" || node->name == "service" || |
| 295 | node->name == "receiver" || node->name == "provider") { |
| 296 | get_name = true; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 297 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | if (main_dex_only_) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 299 | xml::Attribute* component_process = node->FindAttribute(xml::kSchemaAndroid, "process"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 300 | |
| 301 | const std::string& process = |
| 302 | component_process ? component_process->value : default_process_; |
| 303 | get_name = !process.empty() && process[0] != ':'; |
| 304 | } |
| 305 | } else if (node->name == "instrumentation") { |
| 306 | get_name = true; |
| 307 | } |
| 308 | |
| 309 | if (get_name) { |
| 310 | xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "name"); |
| 311 | get_name = attr != nullptr; |
| 312 | |
| 313 | if (get_name) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 314 | Maybe<std::string> result = util::GetFullyQualifiedClassName(package_, attr->value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 315 | if (result) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 316 | AddClass(node->line_number, result.value(), ""); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | BaseVisitor::Visit(node); |
| 322 | } |
| 323 | |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 324 | virtual void AddClass(size_t line_number, const std::string& class_name, |
| 325 | const std::string& ctor_signature) override { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 326 | keep_set_->AddManifestClass({file_.name, file_.source.WithLine(line_number)}, class_name); |
| 327 | } |
| 328 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 329 | private: |
| 330 | DISALLOW_COPY_AND_ASSIGN(ManifestVisitor); |
| 331 | |
| 332 | std::string package_; |
| 333 | const bool main_dex_only_; |
| 334 | std::string default_process_; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 335 | }; |
| 336 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 337 | bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, bool main_dex_only) { |
| 338 | ManifestVisitor visitor(res->file, keep_set, main_dex_only); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 339 | if (res->root) { |
| 340 | res->root->Accept(&visitor); |
| 341 | return true; |
| 342 | } |
| 343 | return false; |
| 344 | } |
| 345 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 346 | bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet* keep_set) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 347 | if (!res->root) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 348 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | switch (res->file.name.type) { |
| 352 | case ResourceType::kLayout: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 353 | LayoutVisitor visitor(res->file, keep_set); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 354 | res->root->Accept(&visitor); |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | case ResourceType::kXml: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 359 | XmlResourceVisitor visitor(res->file, keep_set); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 360 | res->root->Accept(&visitor); |
| 361 | break; |
| 362 | } |
| 363 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 364 | case ResourceType::kNavigation: { |
| 365 | NavigationVisitor visitor(res->file, keep_set, context_->GetCompilationPackage()); |
| 366 | res->root->Accept(&visitor); |
| 367 | break; |
| 368 | } |
| 369 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 370 | case ResourceType::kTransition: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 371 | TransitionVisitor visitor(res->file, keep_set); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 372 | res->root->Accept(&visitor); |
| 373 | break; |
| 374 | } |
| 375 | |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 376 | case ResourceType::kMenu: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 377 | MenuVisitor visitor(res->file, keep_set); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 378 | res->root->Accept(&visitor); |
| 379 | break; |
| 380 | } |
| 381 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 382 | default: { |
| 383 | BaseVisitor visitor(res->file, keep_set); |
| 384 | res->root->Accept(&visitor); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 385 | break; |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 386 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 387 | } |
| 388 | return true; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Ryan Mitchell | 7e5236d | 2018-09-25 15:20:59 -0700 | [diff] [blame] | 391 | void WriteKeepSet(const KeepSet& keep_set, OutputStream* out, bool minimal_keep) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 392 | Printer printer(out); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 393 | for (const auto& entry : keep_set.manifest_class_set_) { |
| 394 | for (const UsageLocation& location : entry.second) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 395 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 396 | } |
Jake Wharton | ab660a7 | 2018-06-08 17:56:55 -0400 | [diff] [blame] | 397 | printer.Print("-keep class ").Print(entry.first).Println(" { <init>(); }"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 398 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 399 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 400 | for (const auto& entry : keep_set.conditional_class_set_) { |
| 401 | std::set<UsageLocation> locations; |
Brian Duff | 2f7e4d1 | 2019-11-10 18:34:11 -0800 | [diff] [blame] | 402 | bool can_be_conditional = false; |
| 403 | if (keep_set.conditional_keep_rules_) { |
| 404 | can_be_conditional = true; |
| 405 | for (const UsageLocation& location : entry.second) { |
| 406 | can_be_conditional &= CollectLocations(location, keep_set, &locations); |
| 407 | } |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Brian Duff | 2f7e4d1 | 2019-11-10 18:34:11 -0800 | [diff] [blame] | 410 | if (can_be_conditional) { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 411 | for (const UsageLocation& location : locations) { |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 412 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
| 413 | printer.Print("-if class **.R$layout { int ") |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 414 | .Print(JavaClassGenerator::TransformToFieldName(location.name.entry)) |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 415 | .Println("; }"); |
Ryan Mitchell | 7e5236d | 2018-09-25 15:20:59 -0700 | [diff] [blame] | 416 | |
| 417 | printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>("); |
| 418 | printer.Print((minimal_keep) ? entry.first.signature : "..."); |
| 419 | printer.Println("); }"); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 420 | } |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 421 | } else { |
| 422 | for (const UsageLocation& location : entry.second) { |
| 423 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
| 424 | } |
Ryan Mitchell | 7e5236d | 2018-09-25 15:20:59 -0700 | [diff] [blame] | 425 | |
| 426 | printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>("); |
| 427 | printer.Print((minimal_keep) ? entry.first.signature : "..."); |
| 428 | printer.Println("); }"); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 429 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 430 | printer.Println(); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | for (const auto& entry : keep_set.method_set_) { |
| 434 | for (const UsageLocation& location : entry.second) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 435 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 436 | } |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 437 | printer.Print("-keepclassmembers class * { *** ").Print(entry.first.name) |
| 438 | .Print("(").Print(entry.first.signature).Println("); }"); |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 439 | printer.Println(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 440 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 443 | bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set, |
| 444 | std::set<UsageLocation>* locations) { |
| 445 | locations->insert(location); |
| 446 | |
| 447 | // TODO: allow for more reference types if we can determine its safe. |
| 448 | if (location.name.type != ResourceType::kLayout) { |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | for (const auto& entry : keep_set.reference_set_) { |
| 453 | if (entry.first == location.name) { |
| 454 | for (auto& refLocation : entry.second) { |
| 455 | // Don't get stuck in loops |
| 456 | if (locations->find(refLocation) != locations->end()) { |
| 457 | return false; |
| 458 | } |
| 459 | if (!CollectLocations(refLocation, keep_set, locations)) { |
| 460 | return false; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return true; |
| 467 | } |
| 468 | |
| 469 | class ReferenceVisitor : public ValueVisitor { |
| 470 | public: |
| 471 | using ValueVisitor::Visit; |
| 472 | |
| 473 | ReferenceVisitor(aapt::IAaptContext* context, ResourceName from, KeepSet* keep_set) |
| 474 | : context_(context), from_(from), keep_set_(keep_set) { |
| 475 | } |
| 476 | |
| 477 | void Visit(Reference* reference) override { |
| 478 | if (reference->name) { |
| 479 | ResourceName reference_name = reference->name.value(); |
| 480 | if (reference_name.package.empty()) { |
| 481 | reference_name = ResourceName(context_->GetCompilationPackage(), reference_name.type, |
| 482 | reference_name.entry); |
| 483 | } |
| 484 | keep_set_->AddReference({from_, reference->GetSource()}, reference_name); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | private: |
| 489 | aapt::IAaptContext* context_; |
| 490 | ResourceName from_; |
| 491 | KeepSet* keep_set_; |
| 492 | }; |
| 493 | |
| 494 | bool CollectResourceReferences(aapt::IAaptContext* context, ResourceTable* table, |
| 495 | KeepSet* keep_set) { |
| 496 | for (auto& pkg : table->packages) { |
| 497 | for (auto& type : pkg->types) { |
| 498 | for (auto& entry : type->entries) { |
| 499 | for (auto& config_value : entry->values) { |
| 500 | ResourceName from(pkg->name, type->type, entry->name); |
| 501 | ReferenceVisitor visitor(context, from, keep_set); |
| 502 | config_value->value->Accept(&visitor); |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | return true; |
| 508 | } |
| 509 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 510 | } // namespace proguard |
| 511 | } // namespace aapt |