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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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::text::Printer; |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 33 | using ::android::OutputStream; |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 50 | if (!node->namespace_uri.empty()) { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 51 | std::optional<xml::ExtractedPackage> maybe_package = |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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 | ce5e56e2 | 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) { |
Donald Chai | db19f51 | 2019-09-09 15:45:34 -0700 | [diff] [blame] | 158 | // AppCompat-v7 defines its own versions of Android attributes if |
| 159 | // they're defined after SDK 7 (the below are from 11 and 14, |
| 160 | // respectively), so don't bother checking the XML namespace. |
| 161 | // |
| 162 | // Given the names of the containing XML files and the attribute |
| 163 | // names, it's unlikely that keeping these classes would be wrong. |
| 164 | if ((attr.name == "actionViewClass" || attr.name == "actionProviderClass") && |
| 165 | util::IsJavaClassName(attr.value)) { |
| 166 | AddClass(node->line_number, attr.value, "android.content.Context"); |
| 167 | } |
| 168 | |
| 169 | if (attr.namespace_uri == xml::kSchemaAndroid && attr.name == "onClick") { |
| 170 | AddMethod(node->line_number, attr.value, "android.view.MenuItem"); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | BaseVisitor::Visit(node); |
| 176 | } |
| 177 | |
| 178 | private: |
| 179 | DISALLOW_COPY_AND_ASSIGN(MenuVisitor); |
| 180 | }; |
| 181 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | class XmlResourceVisitor : public BaseVisitor { |
| 183 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 184 | XmlResourceVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 185 | } |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 187 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 188 | bool check_fragment = false; |
| 189 | if (node->namespace_uri.empty()) { |
| 190 | check_fragment = |
| 191 | node->name == "PreferenceScreen" || node->name == "header"; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 194 | if (check_fragment) { |
| 195 | xml::Attribute* attr = |
| 196 | node->FindAttribute(xml::kSchemaAndroid, "fragment"); |
| 197 | if (attr && util::IsJavaClassName(attr->value)) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 198 | AddClass(node->line_number, attr->value, ""); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 199 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 200 | } |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 201 | |
| 202 | BaseVisitor::Visit(node); |
| 203 | } |
| 204 | |
| 205 | private: |
| 206 | DISALLOW_COPY_AND_ASSIGN(XmlResourceVisitor); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 209 | class NavigationVisitor : public BaseVisitor { |
| 210 | public: |
| 211 | NavigationVisitor(const ResourceFile& file, KeepSet* keep_set, const std::string& package) |
| 212 | : BaseVisitor(file, keep_set), package_(package) { |
| 213 | } |
| 214 | |
| 215 | void Visit(xml::Element* node) override { |
| 216 | const auto& attr = node->FindAttribute(xml::kSchemaAndroid, "name"); |
| 217 | if (attr != nullptr && !attr->value.empty()) { |
| 218 | std::string name = (attr->value[0] == '.') ? package_ + attr->value : attr->value; |
| 219 | if (util::IsJavaClassName(name)) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 220 | AddClass(node->line_number, name, "..."); |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | BaseVisitor::Visit(node); |
| 225 | } |
| 226 | |
| 227 | private: |
| 228 | DISALLOW_COPY_AND_ASSIGN(NavigationVisitor); |
| 229 | const std::string package_; |
| 230 | }; |
| 231 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | class TransitionVisitor : public BaseVisitor { |
| 233 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 234 | TransitionVisitor(const ResourceFile& file, KeepSet* keep_set) : BaseVisitor(file, keep_set) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 235 | } |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 236 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 237 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 238 | bool check_class = |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 239 | node->namespace_uri.empty() && (node->name == "transition" || node->name == "pathMotion"); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 240 | if (check_class) { |
| 241 | xml::Attribute* attr = node->FindAttribute({}, "class"); |
| 242 | if (attr && util::IsJavaClassName(attr->value)) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 243 | AddClass(node->line_number, attr->value, |
| 244 | "android.content.Context, android.util.AttributeSet"); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 245 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 248 | BaseVisitor::Visit(node); |
| 249 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 250 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | private: |
| 252 | DISALLOW_COPY_AND_ASSIGN(TransitionVisitor); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 255 | class ManifestVisitor : public BaseVisitor { |
| 256 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 257 | ManifestVisitor(const ResourceFile& file, KeepSet* keep_set, bool main_dex_only) |
| 258 | : BaseVisitor(file, keep_set), main_dex_only_(main_dex_only) { |
| 259 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 260 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 261 | void Visit(xml::Element* node) override { |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | if (node->namespace_uri.empty()) { |
| 263 | bool get_name = false; |
| 264 | if (node->name == "manifest") { |
| 265 | xml::Attribute* attr = node->FindAttribute({}, "package"); |
| 266 | if (attr) { |
| 267 | package_ = attr->value; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 268 | } |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 269 | } else if (node->name == "application") { |
| 270 | get_name = true; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 271 | xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "backupAgent"); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | if (attr) { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 273 | std::optional<std::string> result = |
| 274 | util::GetFullyQualifiedClassName(package_, attr->value); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 275 | if (result) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 276 | AddClass(node->line_number, result.value(), ""); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
Jake Wharton | e4bd160 | 2018-06-12 09:39:14 -0400 | [diff] [blame] | 279 | attr = node->FindAttribute(xml::kSchemaAndroid, "appComponentFactory"); |
| 280 | if (attr) { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 281 | std::optional<std::string> result = |
| 282 | util::GetFullyQualifiedClassName(package_, attr->value); |
Jake Wharton | e4bd160 | 2018-06-12 09:39:14 -0400 | [diff] [blame] | 283 | if (result) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 284 | AddClass(node->line_number, result.value(), ""); |
Jake Wharton | e4bd160 | 2018-06-12 09:39:14 -0400 | [diff] [blame] | 285 | } |
| 286 | } |
Martijn Coenen | 6c83e0c | 2019-03-06 15:06:17 +0100 | [diff] [blame] | 287 | |
| 288 | attr = node->FindAttribute(xml::kSchemaAndroid, "zygotePreloadName"); |
| 289 | if (attr) { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 290 | std::optional<std::string> result = |
| 291 | util::GetFullyQualifiedClassName(package_, attr->value); |
Martijn Coenen | 6c83e0c | 2019-03-06 15:06:17 +0100 | [diff] [blame] | 292 | if (result) { |
| 293 | AddClass(node->line_number, result.value(), ""); |
| 294 | } |
| 295 | } |
| 296 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 297 | if (main_dex_only_) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 298 | xml::Attribute* default_process = node->FindAttribute(xml::kSchemaAndroid, "process"); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 299 | if (default_process) { |
| 300 | default_process_ = default_process->value; |
| 301 | } |
| 302 | } |
| 303 | } else if (node->name == "activity" || node->name == "service" || |
| 304 | node->name == "receiver" || node->name == "provider") { |
| 305 | get_name = true; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 306 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 307 | if (main_dex_only_) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 308 | xml::Attribute* component_process = node->FindAttribute(xml::kSchemaAndroid, "process"); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 309 | |
| 310 | const std::string& process = |
| 311 | component_process ? component_process->value : default_process_; |
| 312 | get_name = !process.empty() && process[0] != ':'; |
| 313 | } |
Makoto Onuki | 22a931e | 2022-01-12 16:03:50 -0800 | [diff] [blame] | 314 | } else if (node->name == "instrumentation" || node->name == "process") { |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 315 | get_name = true; |
| 316 | } |
| 317 | |
| 318 | if (get_name) { |
| 319 | xml::Attribute* attr = node->FindAttribute(xml::kSchemaAndroid, "name"); |
| 320 | get_name = attr != nullptr; |
| 321 | |
| 322 | if (get_name) { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 323 | std::optional<std::string> result = |
| 324 | util::GetFullyQualifiedClassName(package_, attr->value); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 325 | if (result) { |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 326 | AddClass(node->line_number, result.value(), ""); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | BaseVisitor::Visit(node); |
| 332 | } |
| 333 | |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 334 | virtual void AddClass(size_t line_number, const std::string& class_name, |
| 335 | const std::string& ctor_signature) override { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 336 | keep_set_->AddManifestClass({file_.name, file_.source.WithLine(line_number)}, class_name); |
| 337 | } |
| 338 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 339 | private: |
| 340 | DISALLOW_COPY_AND_ASSIGN(ManifestVisitor); |
| 341 | |
| 342 | std::string package_; |
| 343 | const bool main_dex_only_; |
| 344 | std::string default_process_; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 345 | }; |
| 346 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 347 | bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, bool main_dex_only) { |
| 348 | ManifestVisitor visitor(res->file, keep_set, main_dex_only); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 349 | if (res->root) { |
| 350 | res->root->Accept(&visitor); |
| 351 | return true; |
| 352 | } |
| 353 | return false; |
| 354 | } |
| 355 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 356 | bool CollectProguardRules(IAaptContext* context_, xml::XmlResource* res, KeepSet* keep_set) { |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 357 | if (!res->root) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 358 | return false; |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Iurii Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 361 | switch (res->file.name.type.type) { |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 362 | case ResourceType::kLayout: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 363 | LayoutVisitor visitor(res->file, keep_set); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 364 | res->root->Accept(&visitor); |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | case ResourceType::kXml: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 369 | XmlResourceVisitor visitor(res->file, keep_set); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 370 | res->root->Accept(&visitor); |
| 371 | break; |
| 372 | } |
| 373 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 374 | case ResourceType::kNavigation: { |
| 375 | NavigationVisitor visitor(res->file, keep_set, context_->GetCompilationPackage()); |
| 376 | res->root->Accept(&visitor); |
| 377 | break; |
| 378 | } |
| 379 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 380 | case ResourceType::kTransition: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 381 | TransitionVisitor visitor(res->file, keep_set); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 382 | res->root->Accept(&visitor); |
| 383 | break; |
| 384 | } |
| 385 | |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 386 | case ResourceType::kMenu: { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 387 | MenuVisitor visitor(res->file, keep_set); |
Adam Lesinski | f762df2 | 2017-06-26 16:39:03 -0700 | [diff] [blame] | 388 | res->root->Accept(&visitor); |
| 389 | break; |
| 390 | } |
| 391 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 392 | default: { |
| 393 | BaseVisitor visitor(res->file, keep_set); |
| 394 | res->root->Accept(&visitor); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 395 | break; |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 396 | } |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 397 | } |
| 398 | return true; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Jean-Luc Coelho | 181cbfd | 2019-11-27 12:37:48 -0800 | [diff] [blame] | 401 | void WriteKeepSet(const KeepSet& keep_set, OutputStream* out, bool minimal_keep, |
| 402 | bool no_location_reference) { |
| 403 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 404 | Printer printer(out); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 405 | for (const auto& entry : keep_set.manifest_class_set_) { |
Jean-Luc Coelho | 181cbfd | 2019-11-27 12:37:48 -0800 | [diff] [blame] | 406 | if (!no_location_reference) { |
| 407 | for (const UsageLocation& location : entry.second) { |
| 408 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
| 409 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 410 | } |
Jake Wharton | ab660a7 | 2018-06-08 17:56:55 -0400 | [diff] [blame] | 411 | printer.Print("-keep class ").Print(entry.first).Println(" { <init>(); }"); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 412 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 413 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 414 | for (const auto& entry : keep_set.conditional_class_set_) { |
| 415 | std::set<UsageLocation> locations; |
Brian Duff | 2f7e4d1 | 2019-11-10 18:34:11 -0800 | [diff] [blame] | 416 | bool can_be_conditional = false; |
| 417 | if (keep_set.conditional_keep_rules_) { |
| 418 | can_be_conditional = true; |
| 419 | for (const UsageLocation& location : entry.second) { |
| 420 | can_be_conditional &= CollectLocations(location, keep_set, &locations); |
| 421 | } |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Brian Duff | 2f7e4d1 | 2019-11-10 18:34:11 -0800 | [diff] [blame] | 424 | if (can_be_conditional) { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 425 | for (const UsageLocation& location : locations) { |
Jean-Luc Coelho | 181cbfd | 2019-11-27 12:37:48 -0800 | [diff] [blame] | 426 | if (!no_location_reference) { |
| 427 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
| 428 | } |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 429 | printer.Print("-if class **.R$layout { int ") |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 430 | .Print(JavaClassGenerator::TransformToFieldName(location.name.entry)) |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 431 | .Println("; }"); |
Ryan Mitchell | 7e5236d | 2018-09-25 15:20:59 -0700 | [diff] [blame] | 432 | |
| 433 | printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>("); |
| 434 | printer.Print((minimal_keep) ? entry.first.signature : "..."); |
| 435 | printer.Println("); }"); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 436 | } |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 437 | } else { |
Jean-Luc Coelho | 181cbfd | 2019-11-27 12:37:48 -0800 | [diff] [blame] | 438 | if (!no_location_reference) { |
| 439 | for (const UsageLocation& location : entry.second) { |
| 440 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
| 441 | } |
Adam Koski | f85eec8 | 2017-11-15 12:48:49 -0800 | [diff] [blame] | 442 | } |
Ryan Mitchell | 7e5236d | 2018-09-25 15:20:59 -0700 | [diff] [blame] | 443 | |
| 444 | printer.Print("-keep class ").Print(entry.first.name).Print(" { <init>("); |
| 445 | printer.Print((minimal_keep) ? entry.first.signature : "..."); |
| 446 | printer.Println("); }"); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 447 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 448 | printer.Println(); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | for (const auto& entry : keep_set.method_set_) { |
Jean-Luc Coelho | 181cbfd | 2019-11-27 12:37:48 -0800 | [diff] [blame] | 452 | if (!no_location_reference) { |
| 453 | for (const UsageLocation& location : entry.second) { |
| 454 | printer.Print("# Referenced at ").Println(location.source.to_string()); |
| 455 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 456 | } |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 457 | printer.Print("-keepclassmembers class * { *** ").Print(entry.first.name) |
| 458 | .Print("(").Print(entry.first.signature).Println("); }"); |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 459 | printer.Println(); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 460 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 463 | bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set, |
| 464 | std::set<UsageLocation>* locations) { |
| 465 | locations->insert(location); |
| 466 | |
| 467 | // TODO: allow for more reference types if we can determine its safe. |
Iurii Makhno | cff10ce | 2022-02-15 19:33:50 +0000 | [diff] [blame] | 468 | if (location.name.type.type != ResourceType::kLayout) { |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 469 | return false; |
| 470 | } |
| 471 | |
| 472 | for (const auto& entry : keep_set.reference_set_) { |
| 473 | if (entry.first == location.name) { |
| 474 | for (auto& refLocation : entry.second) { |
| 475 | // Don't get stuck in loops |
| 476 | if (locations->find(refLocation) != locations->end()) { |
| 477 | return false; |
| 478 | } |
| 479 | if (!CollectLocations(refLocation, keep_set, locations)) { |
| 480 | return false; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | class ReferenceVisitor : public ValueVisitor { |
| 490 | public: |
| 491 | using ValueVisitor::Visit; |
| 492 | |
| 493 | ReferenceVisitor(aapt::IAaptContext* context, ResourceName from, KeepSet* keep_set) |
| 494 | : context_(context), from_(from), keep_set_(keep_set) { |
| 495 | } |
| 496 | |
| 497 | void Visit(Reference* reference) override { |
| 498 | if (reference->name) { |
| 499 | ResourceName reference_name = reference->name.value(); |
| 500 | if (reference_name.package.empty()) { |
| 501 | reference_name = ResourceName(context_->GetCompilationPackage(), reference_name.type, |
| 502 | reference_name.entry); |
| 503 | } |
| 504 | keep_set_->AddReference({from_, reference->GetSource()}, reference_name); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | private: |
| 509 | aapt::IAaptContext* context_; |
| 510 | ResourceName from_; |
| 511 | KeepSet* keep_set_; |
| 512 | }; |
| 513 | |
| 514 | bool CollectResourceReferences(aapt::IAaptContext* context, ResourceTable* table, |
| 515 | KeepSet* keep_set) { |
| 516 | for (auto& pkg : table->packages) { |
| 517 | for (auto& type : pkg->types) { |
| 518 | for (auto& entry : type->entries) { |
| 519 | for (auto& config_value : entry->values) { |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 520 | ResourceName from(pkg->name, type->named_type, entry->name); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 521 | ReferenceVisitor visitor(context, from, keep_set); |
| 522 | config_value->value->Accept(&visitor); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | return true; |
| 528 | } |
| 529 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 530 | } // namespace proguard |
| 531 | } // namespace aapt |