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 | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | #include "Resource.h" |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame^] | 26 | #include "ResourceTable.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 27 | #include "Source.h" |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame^] | 28 | #include "ValueVisitor.h" |
| 29 | #include "androidfw/StringPiece.h" |
| 30 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 31 | #include "xml/XmlDom.h" |
| 32 | |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 33 | namespace aapt { |
| 34 | namespace proguard { |
| 35 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame^] | 36 | struct UsageLocation { |
| 37 | ResourceName name; |
| 38 | Source source; |
| 39 | }; |
| 40 | |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 41 | class KeepSet { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | public: |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame^] | 43 | KeepSet() = default; |
| 44 | |
| 45 | KeepSet(bool conditional_keep_rules) : conditional_keep_rules_(conditional_keep_rules) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 47 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame^] | 48 | inline void AddManifestClass(const UsageLocation& file, const std::string& class_name) { |
| 49 | manifest_class_set_[class_name].insert(file); |
| 50 | } |
| 51 | |
| 52 | inline void AddConditionalClass(const UsageLocation& file, const std::string& class_name) { |
| 53 | conditional_class_set_[class_name].insert(file); |
| 54 | } |
| 55 | |
| 56 | inline void AddMethod(const UsageLocation& file, const std::string& method_name) { |
| 57 | method_set_[method_name].insert(file); |
| 58 | } |
| 59 | |
| 60 | inline void AddReference(const UsageLocation& file, const ResourceName& resource_name) { |
| 61 | reference_set_[resource_name].insert(file); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | friend bool WriteKeepSet(std::ostream* out, const KeepSet& keep_set); |
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 | friend bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set, |
| 68 | std::set<UsageLocation>* locations); |
| 69 | |
| 70 | bool conditional_keep_rules_ = false; |
| 71 | std::map<std::string, std::set<UsageLocation>> manifest_class_set_; |
| 72 | std::map<std::string, std::set<UsageLocation>> method_set_; |
| 73 | std::map<std::string, std::set<UsageLocation>> conditional_class_set_; |
| 74 | std::map<ResourceName, std::set<UsageLocation>> reference_set_; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame^] | 77 | bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | bool main_dex_only = false); |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame^] | 79 | bool CollectProguardRules(xml::XmlResource* res, KeepSet* keep_set); |
| 80 | bool CollectResourceReferences(aapt::IAaptContext* context, ResourceTable* table, |
| 81 | KeepSet* keep_set); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | bool WriteKeepSet(std::ostream* out, const KeepSet& keep_set); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 84 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame^] | 85 | bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set, |
| 86 | std::set<UsageLocation>* locations); |
| 87 | |
| 88 | // |
| 89 | // UsageLocation implementation. |
| 90 | // |
| 91 | |
| 92 | inline bool operator==(const UsageLocation& lhs, const UsageLocation& rhs) { |
| 93 | return lhs.name == rhs.name; |
| 94 | } |
| 95 | |
| 96 | inline int operator<(const UsageLocation& lhs, const UsageLocation& rhs) { |
| 97 | return lhs.name.compare(rhs.name); |
| 98 | } |
| 99 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | } // namespace proguard |
| 101 | } // namespace aapt |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 102 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | #endif // AAPT_PROGUARD_RULES_H |