blob: 267f7ede274a6ea2efdf4b11c7f98f964e657f04 [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 Lesinskice5e56e2016-10-21 17:56:45 -070025#include "Resource.h"
Adam Koskidc21dea2017-07-21 10:55:27 -070026#include "ResourceTable.h"
Adam Koskidc21dea2017-07-21 10:55:27 -070027#include "ValueVisitor.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000028#include "androidfw/Source.h"
29#include "androidfw/StringPiece.h"
Adam Lesinskia693c4a2017-11-09 11:29:39 -080030#include "io/Io.h"
Adam Koskidc21dea2017-07-21 10:55:27 -070031#include "process/IResourceTableConsumer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032#include "xml/XmlDom.h"
33
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070034namespace aapt {
35namespace proguard {
36
Adam Koskidc21dea2017-07-21 10:55:27 -070037struct UsageLocation {
38 ResourceName name;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000039 android::Source source;
Adam Koskidc21dea2017-07-21 10:55:27 -070040};
41
Jake Wharton3001f032018-06-11 12:24:11 -040042struct NameAndSignature {
43 std::string name;
44 std::string signature;
45};
46
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070047class KeepSet {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 public:
Adam Koskidc21dea2017-07-21 10:55:27 -070049 KeepSet() = default;
50
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -080051 explicit KeepSet(bool conditional_keep_rules) : conditional_keep_rules_(conditional_keep_rules) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070053
Adam Koskidc21dea2017-07-21 10:55:27 -070054 inline void AddManifestClass(const UsageLocation& file, const std::string& class_name) {
55 manifest_class_set_[class_name].insert(file);
56 }
57
Jake Wharton98100c32018-06-11 15:46:03 -040058 inline void AddConditionalClass(const UsageLocation& file,
59 const NameAndSignature& class_and_signature) {
60 conditional_class_set_[class_and_signature].insert(file);
Adam Koskidc21dea2017-07-21 10:55:27 -070061 }
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:
Jean-Luc Coelho181cbfd2019-11-27 12:37:48 -080072 friend void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out, bool minimal_keep,
73 bool no_location_reference);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070074
Adam Koskidc21dea2017-07-21 10:55:27 -070075 friend bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
76 std::set<UsageLocation>* locations);
77
78 bool conditional_keep_rules_ = false;
79 std::map<std::string, std::set<UsageLocation>> manifest_class_set_;
Jake Wharton3001f032018-06-11 12:24:11 -040080 std::map<NameAndSignature, std::set<UsageLocation>> method_set_;
Jake Wharton98100c32018-06-11 15:46:03 -040081 std::map<NameAndSignature, std::set<UsageLocation>> conditional_class_set_;
Adam Koskidc21dea2017-07-21 10:55:27 -070082 std::map<ResourceName, std::set<UsageLocation>> reference_set_;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070083};
84
Adam Koskidc21dea2017-07-21 10:55:27 -070085bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 bool main_dex_only = false);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070087
Ryan Mitchell9a2f6e62018-05-23 14:23:18 -070088bool CollectProguardRules(IAaptContext* context, xml::XmlResource* res, KeepSet* keep_set);
Adam Lesinskia693c4a2017-11-09 11:29:39 -080089
90bool CollectResourceReferences(IAaptContext* context, ResourceTable* table, KeepSet* keep_set);
91
Jean-Luc Coelho181cbfd2019-11-27 12:37:48 -080092void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out, bool minimal_keep,
93 bool no_location_reference);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070094
Adam Koskidc21dea2017-07-21 10:55:27 -070095bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
96 std::set<UsageLocation>* locations);
97
98//
99// UsageLocation implementation.
100//
101
102inline bool operator==(const UsageLocation& lhs, const UsageLocation& rhs) {
Donald Chai44fa3422019-05-31 22:13:00 -0700103 // The "source" member is ignored because we only need "name" for outputting
104 // keep rules; "source" is used for comments.
Adam Koskidc21dea2017-07-21 10:55:27 -0700105 return lhs.name == rhs.name;
106}
107
Donald Chai44fa3422019-05-31 22:13:00 -0700108inline bool operator<(const UsageLocation& lhs, const UsageLocation& rhs) {
109 return lhs.name.compare(rhs.name) < 0;
Adam Koskidc21dea2017-07-21 10:55:27 -0700110}
111
Jake Wharton3001f032018-06-11 12:24:11 -0400112//
113// NameAndSignature implementation.
114//
115
116inline bool operator<(const NameAndSignature& lhs, const NameAndSignature& rhs) {
117 if (lhs.name < rhs.name) {
118 return true;
119 }
120 if (lhs.name == rhs.name) {
121 return lhs.signature < rhs.signature;
122 }
123 return false;
124}
125
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126} // namespace proguard
127} // namespace aapt
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700128
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129#endif // AAPT_PROGUARD_RULES_H