blob: acaceac41237cf1e9f635abefee2d7dcb72ba6ff [file] [log] [blame]
Adam Lesinskia1ad4a82015-06-08 11:41:09 -07001/*
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 Lesinskia1ad4a82015-06-08 11:41:09 -070020#include <map>
21#include <ostream>
22#include <set>
23#include <string>
24
Adam Lesinskia693c4a2017-11-09 11:29:39 -080025#include "androidfw/StringPiece.h"
26
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "Resource.h"
Adam Koskidc21dea2017-07-21 10:55:27 -070028#include "ResourceTable.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029#include "Source.h"
Adam Koskidc21dea2017-07-21 10:55:27 -070030#include "ValueVisitor.h"
Adam Lesinskia693c4a2017-11-09 11:29:39 -080031#include "io/Io.h"
Adam Koskidc21dea2017-07-21 10:55:27 -070032#include "process/IResourceTableConsumer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033#include "xml/XmlDom.h"
34
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070035namespace aapt {
36namespace proguard {
37
Adam Koskidc21dea2017-07-21 10:55:27 -070038struct UsageLocation {
39 ResourceName name;
40 Source source;
41};
42
Jake Wharton3001f032018-06-11 12:24:11 -040043struct NameAndSignature {
44 std::string name;
45 std::string signature;
46};
47
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070048class KeepSet {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 public:
Adam Koskidc21dea2017-07-21 10:55:27 -070050 KeepSet() = default;
51
52 KeepSet(bool conditional_keep_rules) : conditional_keep_rules_(conditional_keep_rules) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070054
Adam Koskidc21dea2017-07-21 10:55:27 -070055 inline void AddManifestClass(const UsageLocation& file, const std::string& class_name) {
56 manifest_class_set_[class_name].insert(file);
57 }
58
59 inline void AddConditionalClass(const UsageLocation& file, const std::string& class_name) {
60 conditional_class_set_[class_name].insert(file);
61 }
62
Jake Wharton3001f032018-06-11 12:24:11 -040063 inline void AddMethod(const UsageLocation& file, const NameAndSignature& name_and_signature) {
64 method_set_[name_and_signature].insert(file);
Adam Koskidc21dea2017-07-21 10:55:27 -070065 }
66
67 inline void AddReference(const UsageLocation& file, const ResourceName& resource_name) {
68 reference_set_[resource_name].insert(file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 private:
Adam Lesinskia693c4a2017-11-09 11:29:39 -080072 friend void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070073
Adam Koskidc21dea2017-07-21 10:55:27 -070074 friend bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
75 std::set<UsageLocation>* locations);
76
77 bool conditional_keep_rules_ = false;
78 std::map<std::string, std::set<UsageLocation>> manifest_class_set_;
Jake Wharton3001f032018-06-11 12:24:11 -040079 std::map<NameAndSignature, std::set<UsageLocation>> method_set_;
Adam Koskidc21dea2017-07-21 10:55:27 -070080 std::map<std::string, std::set<UsageLocation>> conditional_class_set_;
81 std::map<ResourceName, std::set<UsageLocation>> reference_set_;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070082};
83
Adam Koskidc21dea2017-07-21 10:55:27 -070084bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 bool main_dex_only = false);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070086
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -070087bool CollectProguardRules(IAaptContext* context, xml::XmlResource* res, KeepSet* keep_set);
Adam Lesinskia693c4a2017-11-09 11:29:39 -080088
89bool CollectResourceReferences(IAaptContext* context, ResourceTable* table, KeepSet* keep_set);
90
91void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070092
Adam Koskidc21dea2017-07-21 10:55:27 -070093bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
94 std::set<UsageLocation>* locations);
95
96//
97// UsageLocation implementation.
98//
99
100inline bool operator==(const UsageLocation& lhs, const UsageLocation& rhs) {
101 return lhs.name == rhs.name;
102}
103
104inline int operator<(const UsageLocation& lhs, const UsageLocation& rhs) {
105 return lhs.name.compare(rhs.name);
106}
107
Jake Wharton3001f032018-06-11 12:24:11 -0400108//
109// NameAndSignature implementation.
110//
111
112inline bool operator<(const NameAndSignature& lhs, const NameAndSignature& rhs) {
113 if (lhs.name < rhs.name) {
114 return true;
115 }
116 if (lhs.name == rhs.name) {
117 return lhs.signature < rhs.signature;
118 }
119 return false;
120}
121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122} // namespace proguard
123} // namespace aapt
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125#endif // AAPT_PROGUARD_RULES_H