blob: 27c354a7b08ebf40cf9df6ef9eefd00e6b5d6ac0 [file] [log] [blame]
Pierre Lecesneff759e62017-02-01 00:29:25 +00001/*
2 * Copyright (C) 2016 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_LOADEDAPK_H
18#define AAPT_LOADEDAPK_H
19
20#include "androidfw/StringPiece.h"
21
Pierre Lecesneff759e62017-02-01 00:29:25 +000022#include "ResourceTable.h"
Shane Farmer57669432017-06-19 12:52:04 -070023#include "filter/Filter.h"
Adam Lesinski46708052017-09-29 14:49:15 -070024#include "format/Archive.h"
25#include "format/binary/BinaryResourceParser.h"
26#include "format/binary/TableFlattener.h"
Pierre Lecesne2599aa42017-02-01 22:47:03 +000027#include "io/ZipArchive.h"
Shane Farmer3edd4722017-09-01 14:34:22 -070028#include "xml/XmlDom.h"
Pierre Lecesneff759e62017-02-01 00:29:25 +000029
Pierre Lecesneff759e62017-02-01 00:29:25 +000030namespace aapt {
31
Adam Lesinski8780eb62017-10-31 17:44:39 -070032constexpr static const char kApkResourceTablePath[] = "resources.arsc";
33constexpr static const char kProtoResourceTablePath[] = "resources.pb";
34constexpr static const char kAndroidManifestPath[] = "AndroidManifest.xml";
35
Pierre Lecesnef267a402017-12-01 11:39:01 +000036enum ApkFormat {
37 kUnknown,
38 kBinary,
39 kProto,
40};
41
Adam Lesinski46708052017-09-29 14:49:15 -070042// Info about an APK loaded in memory.
Yurii Zubrytskyi711edbd2023-07-12 14:29:21 -070043class LoadedApk final {
Pierre Lecesneff759e62017-02-01 00:29:25 +000044 public:
Adam Lesinski8780eb62017-10-31 17:44:39 -070045 // Loads both binary and proto APKs from disk.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070046 static std::unique_ptr<LoadedApk> LoadApkFromPath(android::StringPiece path,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000047 android::IDiagnostics* diag);
Adam Lesinski8780eb62017-10-31 17:44:39 -070048
49 // Loads a proto APK from the given file collection.
50 static std::unique_ptr<LoadedApk> LoadProtoApkFromFileCollection(
Jeremy Meyer56f36e82022-05-20 20:35:42 +000051 const android::Source& source, std::unique_ptr<io::IFileCollection> collection,
52 android::IDiagnostics* diag);
Adam Lesinski8780eb62017-10-31 17:44:39 -070053
54 // Loads a binary APK from the given file collection.
55 static std::unique_ptr<LoadedApk> LoadBinaryApkFromFileCollection(
Jeremy Meyer56f36e82022-05-20 20:35:42 +000056 const android::Source& source, std::unique_ptr<io::IFileCollection> collection,
57 android::IDiagnostics* diag);
Adam Lesinski8780eb62017-10-31 17:44:39 -070058
Jeremy Meyer56f36e82022-05-20 20:35:42 +000059 LoadedApk(const android::Source& source, std::unique_ptr<io::IFileCollection> apk,
corysmith@google.comf7db43e2018-03-13 12:04:10 -040060 std::unique_ptr<ResourceTable> table, std::unique_ptr<xml::XmlResource> manifest,
61 const ApkFormat& format)
Adam Lesinski8780eb62017-10-31 17:44:39 -070062 : source_(source),
63 apk_(std::move(apk)),
64 table_(std::move(table)),
corysmith@google.comf7db43e2018-03-13 12:04:10 -040065 manifest_(std::move(manifest)),
66 format_(format) {
Adam Lesinski8780eb62017-10-31 17:44:39 -070067 }
Adam Lesinski46708052017-09-29 14:49:15 -070068
69 io::IFileCollection* GetFileCollection() {
70 return apk_.get();
Shane Farmer3edd4722017-09-01 14:34:22 -070071 }
Pierre Lecesneff759e62017-02-01 00:29:25 +000072
Ryan Mitchell5d275512018-07-19 14:29:00 -070073 ApkFormat GetApkFormat() {
74 return format_;
75 }
76
Adam Lesinski8780eb62017-10-31 17:44:39 -070077 const ResourceTable* GetResourceTable() const {
78 return table_.get();
79 }
80
Adam Lesinski46708052017-09-29 14:49:15 -070081 ResourceTable* GetResourceTable() {
82 return table_.get();
83 }
Pierre Lecesneff759e62017-02-01 00:29:25 +000084
Jeremy Meyer56f36e82022-05-20 20:35:42 +000085 const android::Source& GetSource() {
Adam Lesinski46708052017-09-29 14:49:15 -070086 return source_;
87 }
Pierre Lecesneff759e62017-02-01 00:29:25 +000088
Adam Lesinski8780eb62017-10-31 17:44:39 -070089 const xml::XmlResource* GetManifest() const {
90 return manifest_.get();
91 }
92
Pierre Lecesne2599aa42017-02-01 22:47:03 +000093 /**
94 * Writes the APK on disk at the given path, while also removing the resource
95 * files that are not referenced in the resource table.
96 */
Yurii Zubrytskyi711edbd2023-07-12 14:29:21 -070097 bool WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
98 IArchiveWriter* writer);
Pierre Lecesne2599aa42017-02-01 22:47:03 +000099
Shane Farmer57669432017-06-19 12:52:04 -0700100 /**
Shane Farmer3edd4722017-09-01 14:34:22 -0700101 * Writes the APK on disk at the given path, while also removing the resource files that are not
102 * referenced in the resource table. The provided filter chain is applied to each entry in the APK
103 * file.
104 *
105 * If the manifest is also provided, it will be written to the new APK file, otherwise the
106 * original manifest will be written. The manifest is only required if the contents of the new APK
107 * have been modified in a way that require the AndroidManifest.xml to also be modified.
Shane Farmer57669432017-06-19 12:52:04 -0700108 */
Yurii Zubrytskyi711edbd2023-07-12 14:29:21 -0700109 bool WriteToArchive(IAaptContext* context, ResourceTable* split_table,
110 const TableFlattenerOptions& options, FilterChain* filters,
111 IArchiveWriter* writer, xml::XmlResource* manifest = nullptr);
Shane Farmer3edd4722017-09-01 14:34:22 -0700112
Ryan Mitchell5d275512018-07-19 14:29:00 -0700113 /** Loads the file as an xml document. */
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000114 std::unique_ptr<xml::XmlResource> LoadXml(const std::string& file_path,
115 android::IDiagnostics* diag) const;
Pierre Lecesneff759e62017-02-01 00:29:25 +0000116
117 private:
Adam Lesinski46708052017-09-29 14:49:15 -0700118 DISALLOW_COPY_AND_ASSIGN(LoadedApk);
119
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000120 android::Source source_;
Pierre Lecesneff759e62017-02-01 00:29:25 +0000121 std::unique_ptr<io::IFileCollection> apk_;
122 std::unique_ptr<ResourceTable> table_;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700123 std::unique_ptr<xml::XmlResource> manifest_;
corysmith@google.comf7db43e2018-03-13 12:04:10 -0400124 ApkFormat format_;
Pierre Lecesneff759e62017-02-01 00:29:25 +0000125};
126
127} // namespace aapt
128
129#endif /* AAPT_LOADEDAPK_H */