xref: /aosp_15_r20/frameworks/base/tools/aapt2/LoadedApk.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1*d57664e9SAndroid Build Coastguard Worker /*
2*d57664e9SAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*d57664e9SAndroid Build Coastguard Worker  *
4*d57664e9SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*d57664e9SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*d57664e9SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*d57664e9SAndroid Build Coastguard Worker  *
8*d57664e9SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*d57664e9SAndroid Build Coastguard Worker  *
10*d57664e9SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*d57664e9SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*d57664e9SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*d57664e9SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*d57664e9SAndroid Build Coastguard Worker  * limitations under the License.
15*d57664e9SAndroid Build Coastguard Worker  */
16*d57664e9SAndroid Build Coastguard Worker 
17*d57664e9SAndroid Build Coastguard Worker #ifndef AAPT_LOADEDAPK_H
18*d57664e9SAndroid Build Coastguard Worker #define AAPT_LOADEDAPK_H
19*d57664e9SAndroid Build Coastguard Worker 
20*d57664e9SAndroid Build Coastguard Worker #include "androidfw/StringPiece.h"
21*d57664e9SAndroid Build Coastguard Worker 
22*d57664e9SAndroid Build Coastguard Worker #include "ResourceTable.h"
23*d57664e9SAndroid Build Coastguard Worker #include "filter/Filter.h"
24*d57664e9SAndroid Build Coastguard Worker #include "format/Archive.h"
25*d57664e9SAndroid Build Coastguard Worker #include "format/binary/BinaryResourceParser.h"
26*d57664e9SAndroid Build Coastguard Worker #include "format/binary/TableFlattener.h"
27*d57664e9SAndroid Build Coastguard Worker #include "io/ZipArchive.h"
28*d57664e9SAndroid Build Coastguard Worker #include "xml/XmlDom.h"
29*d57664e9SAndroid Build Coastguard Worker 
30*d57664e9SAndroid Build Coastguard Worker namespace aapt {
31*d57664e9SAndroid Build Coastguard Worker 
32*d57664e9SAndroid Build Coastguard Worker constexpr static const char kApkResourceTablePath[] = "resources.arsc";
33*d57664e9SAndroid Build Coastguard Worker constexpr static const char kProtoResourceTablePath[] = "resources.pb";
34*d57664e9SAndroid Build Coastguard Worker constexpr static const char kAndroidManifestPath[] = "AndroidManifest.xml";
35*d57664e9SAndroid Build Coastguard Worker 
36*d57664e9SAndroid Build Coastguard Worker enum ApkFormat {
37*d57664e9SAndroid Build Coastguard Worker   kUnknown,
38*d57664e9SAndroid Build Coastguard Worker   kBinary,
39*d57664e9SAndroid Build Coastguard Worker   kProto,
40*d57664e9SAndroid Build Coastguard Worker };
41*d57664e9SAndroid Build Coastguard Worker 
42*d57664e9SAndroid Build Coastguard Worker // Info about an APK loaded in memory.
43*d57664e9SAndroid Build Coastguard Worker class LoadedApk final {
44*d57664e9SAndroid Build Coastguard Worker  public:
45*d57664e9SAndroid Build Coastguard Worker   // Loads both binary and proto APKs from disk.
46*d57664e9SAndroid Build Coastguard Worker   static std::unique_ptr<LoadedApk> LoadApkFromPath(android::StringPiece path,
47*d57664e9SAndroid Build Coastguard Worker                                                     android::IDiagnostics* diag);
48*d57664e9SAndroid Build Coastguard Worker 
49*d57664e9SAndroid Build Coastguard Worker   // Loads a proto APK from the given file collection.
50*d57664e9SAndroid Build Coastguard Worker   static std::unique_ptr<LoadedApk> LoadProtoApkFromFileCollection(
51*d57664e9SAndroid Build Coastguard Worker       const android::Source& source, std::unique_ptr<io::IFileCollection> collection,
52*d57664e9SAndroid Build Coastguard Worker       android::IDiagnostics* diag);
53*d57664e9SAndroid Build Coastguard Worker 
54*d57664e9SAndroid Build Coastguard Worker   // Loads a binary APK from the given file collection.
55*d57664e9SAndroid Build Coastguard Worker   static std::unique_ptr<LoadedApk> LoadBinaryApkFromFileCollection(
56*d57664e9SAndroid Build Coastguard Worker       const android::Source& source, std::unique_ptr<io::IFileCollection> collection,
57*d57664e9SAndroid Build Coastguard Worker       android::IDiagnostics* diag);
58*d57664e9SAndroid Build Coastguard Worker 
LoadedApk(const android::Source & source,std::unique_ptr<io::IFileCollection> apk,std::unique_ptr<ResourceTable> table,std::unique_ptr<xml::XmlResource> manifest,const ApkFormat & format)59*d57664e9SAndroid Build Coastguard Worker   LoadedApk(const android::Source& source, std::unique_ptr<io::IFileCollection> apk,
60*d57664e9SAndroid Build Coastguard Worker             std::unique_ptr<ResourceTable> table, std::unique_ptr<xml::XmlResource> manifest,
61*d57664e9SAndroid Build Coastguard Worker             const ApkFormat& format)
62*d57664e9SAndroid Build Coastguard Worker       : source_(source),
63*d57664e9SAndroid Build Coastguard Worker         apk_(std::move(apk)),
64*d57664e9SAndroid Build Coastguard Worker         table_(std::move(table)),
65*d57664e9SAndroid Build Coastguard Worker         manifest_(std::move(manifest)),
66*d57664e9SAndroid Build Coastguard Worker         format_(format) {
67*d57664e9SAndroid Build Coastguard Worker   }
68*d57664e9SAndroid Build Coastguard Worker 
GetFileCollection()69*d57664e9SAndroid Build Coastguard Worker   io::IFileCollection* GetFileCollection() {
70*d57664e9SAndroid Build Coastguard Worker     return apk_.get();
71*d57664e9SAndroid Build Coastguard Worker   }
72*d57664e9SAndroid Build Coastguard Worker 
GetApkFormat()73*d57664e9SAndroid Build Coastguard Worker   ApkFormat GetApkFormat() {
74*d57664e9SAndroid Build Coastguard Worker     return format_;
75*d57664e9SAndroid Build Coastguard Worker   }
76*d57664e9SAndroid Build Coastguard Worker 
GetResourceTable()77*d57664e9SAndroid Build Coastguard Worker   const ResourceTable* GetResourceTable() const {
78*d57664e9SAndroid Build Coastguard Worker     return table_.get();
79*d57664e9SAndroid Build Coastguard Worker   }
80*d57664e9SAndroid Build Coastguard Worker 
GetResourceTable()81*d57664e9SAndroid Build Coastguard Worker   ResourceTable* GetResourceTable() {
82*d57664e9SAndroid Build Coastguard Worker     return table_.get();
83*d57664e9SAndroid Build Coastguard Worker   }
84*d57664e9SAndroid Build Coastguard Worker 
GetSource()85*d57664e9SAndroid Build Coastguard Worker   const android::Source& GetSource() {
86*d57664e9SAndroid Build Coastguard Worker     return source_;
87*d57664e9SAndroid Build Coastguard Worker   }
88*d57664e9SAndroid Build Coastguard Worker 
GetManifest()89*d57664e9SAndroid Build Coastguard Worker   const xml::XmlResource* GetManifest() const {
90*d57664e9SAndroid Build Coastguard Worker     return manifest_.get();
91*d57664e9SAndroid Build Coastguard Worker   }
92*d57664e9SAndroid Build Coastguard Worker 
93*d57664e9SAndroid Build Coastguard Worker   /**
94*d57664e9SAndroid Build Coastguard Worker    * Writes the APK on disk at the given path, while also removing the resource
95*d57664e9SAndroid Build Coastguard Worker    * files that are not referenced in the resource table.
96*d57664e9SAndroid Build Coastguard Worker    */
97*d57664e9SAndroid Build Coastguard Worker   bool WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
98*d57664e9SAndroid Build Coastguard Worker                       IArchiveWriter* writer);
99*d57664e9SAndroid Build Coastguard Worker 
100*d57664e9SAndroid Build Coastguard Worker   /**
101*d57664e9SAndroid Build Coastguard Worker    * Writes the APK on disk at the given path, while also removing the resource files that are not
102*d57664e9SAndroid Build Coastguard Worker    * referenced in the resource table. The provided filter chain is applied to each entry in the APK
103*d57664e9SAndroid Build Coastguard Worker    * file.
104*d57664e9SAndroid Build Coastguard Worker    *
105*d57664e9SAndroid Build Coastguard Worker    * If the manifest is also provided, it will be written to the new APK file, otherwise the
106*d57664e9SAndroid Build Coastguard Worker    * original manifest will be written. The manifest is only required if the contents of the new APK
107*d57664e9SAndroid Build Coastguard Worker    * have been modified in a way that require the AndroidManifest.xml to also be modified.
108*d57664e9SAndroid Build Coastguard Worker    */
109*d57664e9SAndroid Build Coastguard Worker   bool WriteToArchive(IAaptContext* context, ResourceTable* split_table,
110*d57664e9SAndroid Build Coastguard Worker                       const TableFlattenerOptions& options, FilterChain* filters,
111*d57664e9SAndroid Build Coastguard Worker                       IArchiveWriter* writer, xml::XmlResource* manifest = nullptr);
112*d57664e9SAndroid Build Coastguard Worker 
113*d57664e9SAndroid Build Coastguard Worker   /** Loads the file as an xml document. */
114*d57664e9SAndroid Build Coastguard Worker   std::unique_ptr<xml::XmlResource> LoadXml(const std::string& file_path,
115*d57664e9SAndroid Build Coastguard Worker                                             android::IDiagnostics* diag) const;
116*d57664e9SAndroid Build Coastguard Worker 
117*d57664e9SAndroid Build Coastguard Worker  private:
118*d57664e9SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(LoadedApk);
119*d57664e9SAndroid Build Coastguard Worker 
120*d57664e9SAndroid Build Coastguard Worker   android::Source source_;
121*d57664e9SAndroid Build Coastguard Worker   std::unique_ptr<io::IFileCollection> apk_;
122*d57664e9SAndroid Build Coastguard Worker   std::unique_ptr<ResourceTable> table_;
123*d57664e9SAndroid Build Coastguard Worker   std::unique_ptr<xml::XmlResource> manifest_;
124*d57664e9SAndroid Build Coastguard Worker   ApkFormat format_;
125*d57664e9SAndroid Build Coastguard Worker };
126*d57664e9SAndroid Build Coastguard Worker 
127*d57664e9SAndroid Build Coastguard Worker }  // namespace aapt
128*d57664e9SAndroid Build Coastguard Worker 
129*d57664e9SAndroid Build Coastguard Worker #endif /* AAPT_LOADEDAPK_H */
130