Home | History | Annotate | Download | only in java
      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 
     20 #include <map>
     21 #include <ostream>
     22 #include <set>
     23 #include <string>
     24 
     25 #include "androidfw/StringPiece.h"
     26 
     27 #include "Resource.h"
     28 #include "ResourceTable.h"
     29 #include "Source.h"
     30 #include "ValueVisitor.h"
     31 #include "io/Io.h"
     32 #include "process/IResourceTableConsumer.h"
     33 #include "xml/XmlDom.h"
     34 
     35 namespace aapt {
     36 namespace proguard {
     37 
     38 struct UsageLocation {
     39   ResourceName name;
     40   Source source;
     41 };
     42 
     43 class KeepSet {
     44  public:
     45   KeepSet() = default;
     46 
     47   KeepSet(bool conditional_keep_rules) : conditional_keep_rules_(conditional_keep_rules) {
     48   }
     49 
     50   inline void AddManifestClass(const UsageLocation& file, const std::string& class_name) {
     51     manifest_class_set_[class_name].insert(file);
     52   }
     53 
     54   inline void AddConditionalClass(const UsageLocation& file, const std::string& class_name) {
     55     conditional_class_set_[class_name].insert(file);
     56   }
     57 
     58   inline void AddMethod(const UsageLocation& file, const std::string& method_name) {
     59     method_set_[method_name].insert(file);
     60   }
     61 
     62   inline void AddReference(const UsageLocation& file, const ResourceName& resource_name) {
     63     reference_set_[resource_name].insert(file);
     64   }
     65 
     66  private:
     67   friend void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out);
     68 
     69   friend bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
     70                                std::set<UsageLocation>* locations);
     71 
     72   bool conditional_keep_rules_ = false;
     73   std::map<std::string, std::set<UsageLocation>> manifest_class_set_;
     74   std::map<std::string, std::set<UsageLocation>> method_set_;
     75   std::map<std::string, std::set<UsageLocation>> conditional_class_set_;
     76   std::map<ResourceName, std::set<UsageLocation>> reference_set_;
     77 };
     78 
     79 bool CollectProguardRulesForManifest(xml::XmlResource* res, KeepSet* keep_set,
     80                                      bool main_dex_only = false);
     81 
     82 bool CollectProguardRules(xml::XmlResource* res, KeepSet* keep_set);
     83 
     84 bool CollectResourceReferences(IAaptContext* context, ResourceTable* table, KeepSet* keep_set);
     85 
     86 void WriteKeepSet(const KeepSet& keep_set, io::OutputStream* out);
     87 
     88 bool CollectLocations(const UsageLocation& location, const KeepSet& keep_set,
     89                       std::set<UsageLocation>* locations);
     90 
     91 //
     92 // UsageLocation implementation.
     93 //
     94 
     95 inline bool operator==(const UsageLocation& lhs, const UsageLocation& rhs) {
     96   return lhs.name == rhs.name;
     97 }
     98 
     99 inline int operator<(const UsageLocation& lhs, const UsageLocation& rhs) {
    100   return lhs.name.compare(rhs.name);
    101 }
    102 
    103 }  // namespace proguard
    104 }  // namespace aapt
    105 
    106 #endif  // AAPT_PROGUARD_RULES_H
    107