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