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 | |
| 17 | #ifndef AAPT_PROGUARD_RULES_H |
| 18 | #define AAPT_PROGUARD_RULES_H |
| 19 | |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <ostream> |
| 22 | #include <set> |
| 23 | #include <string> |
| 24 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 25 | #include "androidfw/StringPiece.h" |
| 26 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 27 | #include "Resource.h" |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 28 | #include "ResourceTable.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 29 | #include "Source.h" |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 30 | #include "ValueVisitor.h" |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 31 | #include "io/Io.h" |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 32 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | #include "xml/XmlDom.h" |
| 34 | |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 35 | namespace aapt { |
| 36 | namespace proguard { |
| 37 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 38 | struct UsageLocation { |
| 39 | ResourceName name; |
| 40 | Source source; |
| 41 | }; |
| 42 | |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 43 | struct NameAndSignature { |
| 44 | std::string name; |
| 45 | std::string signature; |
| 46 | }; |
| 47 | |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 48 | class KeepSet { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 50 | KeepSet() = default; |
| 51 | |
Chih-Hung Hsieh | 1fc78e1 | 2018-12-20 13:37:44 -0800 | [diff] [blame] | 52 | explicit KeepSet(bool conditional_keep_rules) : conditional_keep_rules_(conditional_keep_rules) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 54 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 55 | inline void AddManifestClass(const UsageLocation& file, const std::string& class_name) { |
| 56 | manifest_class_set_[class_name].insert(file); |
| 57 | } |
| 58 | |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 59 | inline void AddConditionalClass(const UsageLocation& file, |
| 60 | const NameAndSignature& class_and_signature) { |
| 61 | conditional_class_set_[class_and_signature].insert(file); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 64 | inline void AddMethod(const UsageLocation& file, const NameAndSignature& name_and_signature) { |
| 65 | method_set_[name_and_signature].insert(file); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | inline void AddReference(const UsageLocation& file, const ResourceName& resource_name) { |
| 69 | reference_set_[resource_name].insert(file); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 70 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 71 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 72 | private: |
Jean-Luc Coelho | 181cbfd | 2019-11-27 12:37:48 -0800 | [diff] [blame] | 73 | friend void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out, bool minimal_keep, |
| 74 | bool no_location_reference); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 75 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 76 | friend bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set, |
| 77 | std::set<UsageLocation>* locations); |
| 78 | |
| 79 | bool conditional_keep_rules_ = false; |
| 80 | std::map<std::string, std::set<UsageLocation>> manifest_class_set_; |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 81 | std::map<NameAndSignature, std::set<UsageLocation>> method_set_; |
Jake Wharton | 98100c3 | 2018-06-11 15:46:03 -0400 | [diff] [blame] | 82 | std::map<NameAndSignature, std::set<UsageLocation>> conditional_class_set_; |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 83 | std::map<ResourceName, std::set<UsageLocation>> reference_set_; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 86 | bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | bool main_dex_only = false); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 88 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 89 | bool CollectProguardRules(IAaptContext* context, xml::XmlResource* res, KeepSet* keep_set); |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 90 | |
| 91 | bool CollectResourceReferences(IAaptContext* context, ResourceTable* table, KeepSet* keep_set); |
| 92 | |
Jean-Luc Coelho | 181cbfd | 2019-11-27 12:37:48 -0800 | [diff] [blame] | 93 | void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out, bool minimal_keep, |
| 94 | bool no_location_reference); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 95 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 96 | bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set, |
| 97 | std::set<UsageLocation>* locations); |
| 98 | |
| 99 | // |
| 100 | // UsageLocation implementation. |
| 101 | // |
| 102 | |
| 103 | inline bool operator==(const UsageLocation& lhs, const UsageLocation& rhs) { |
Donald Chai | 44fa342 | 2019-05-31 22:13:00 -0700 | [diff] [blame] | 104 | // The "source" member is ignored because we only need "name" for outputting |
| 105 | // keep rules; "source" is used for comments. |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 106 | return lhs.name == rhs.name; |
| 107 | } |
| 108 | |
Donald Chai | 44fa342 | 2019-05-31 22:13:00 -0700 | [diff] [blame] | 109 | inline bool operator<(const UsageLocation& lhs, const UsageLocation& rhs) { |
| 110 | return lhs.name.compare(rhs.name) < 0; |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Jake Wharton | 3001f03 | 2018-06-11 12:24:11 -0400 | [diff] [blame] | 113 | // |
| 114 | // NameAndSignature implementation. |
| 115 | // |
| 116 | |
| 117 | inline bool operator<(const NameAndSignature& lhs, const NameAndSignature& rhs) { |
| 118 | if (lhs.name < rhs.name) { |
| 119 | return true; |
| 120 | } |
| 121 | if (lhs.name == rhs.name) { |
| 122 | return lhs.signature < rhs.signature; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | } // namespace proguard |
| 128 | } // namespace aapt |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 129 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 130 | #endif // AAPT_PROGUARD_RULES_H |