• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TABLEMERGER_H
18 #define AAPT_TABLEMERGER_H
19 
20 #include <functional>
21 #include <map>
22 
23 #include "android-base/macros.h"
24 
25 #include "Resource.h"
26 #include "ResourceTable.h"
27 #include "ResourceValues.h"
28 #include "filter/ConfigFilter.h"
29 #include "io/File.h"
30 #include "process/IResourceTableConsumer.h"
31 #include "util/Util.h"
32 
33 namespace aapt {
34 
35 struct TableMergerOptions {
36   // If true, resources in overlays can be added without previously having existed.
37   bool auto_add_overlay = false;
38   // If true, resource overlays with conflicting visibility are not allowed.
39   bool strict_visibility = false;
40   // If true, styles specified via "aapt2 link -R" completely replace any previously-seen resources.
41   bool override_styles_instead_of_overlaying = false;
42 };
43 
44 // TableMerger takes resource tables and merges all packages within the tables that have the same
45 // package ID.
46 //
47 // It is assumed that any FileReference values have their io::IFile pointer set to point to the
48 // file they represent.
49 //
50 // If a package has a different name, all the entries in that table have their names mangled
51 // to include the package name. This way there are no collisions. In order to do this correctly,
52 // the TableMerger needs to also mangle any FileReference paths. Once these are mangled, the
53 // `IFile` pointer in `FileReference` will point to the original file.
54 //
55 // Once the merging is complete, a separate phase can go collect the files from the various
56 // source APKs and either copy or process their XML and put them in the correct location in the
57 // final APK.
58 class TableMerger {
59  public:
60   // Note: The out_table ResourceTable must live longer than this TableMerger.
61   // References are made to this ResourceTable for efficiency reasons.
62   TableMerger(IAaptContext* context, ResourceTable* out_table, const TableMergerOptions& options);
63 
merged_packages()64   inline const std::set<std::string>& merged_packages() const {
65     return merged_packages_;
66   }
67 
68   // Merges resources from the same or empty package. This is for local sources.
69   // If overlay is true, the resources are treated as overlays.
70   bool Merge(const Source& src, ResourceTable* table, bool overlay);
71 
72   // Merges resources from the given package, mangling the name. This is for static libraries.
73   // All FileReference values must have their io::IFile set.
74   bool MergeAndMangle(const Source& src, const android::StringPiece& package, ResourceTable* table);
75 
76   // Merges a compiled file that belongs to this same or empty package.
77   bool MergeFile(const ResourceFile& fileDesc, bool overlay, io::IFile* file);
78 
79  private:
80   DISALLOW_COPY_AND_ASSIGN(TableMerger);
81 
82   IAaptContext* context_;
83   ResourceTable* main_table_;
84   TableMergerOptions options_;
85   ResourceTablePackage* main_package_;
86   std::set<std::string> merged_packages_;
87 
88   bool MergeImpl(const Source& src, ResourceTable* src_table, bool overlay, bool allow_new);
89 
90   bool DoMerge(const Source& src, ResourceTablePackage* src_package, bool mangle_package,
91                bool overlay, bool allow_new_resources);
92 
93   std::unique_ptr<FileReference> CloneAndMangleFile(const std::string& package,
94                                                     const FileReference& value);
95 };
96 
97 }  // namespace aapt
98 
99 #endif /* AAPT_TABLEMERGER_H */
100