blob: ef97de301ad5642759e412201d224c605b5a3b4a [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
Adam Lesinski46708052017-09-29 14:49:15 -070036// Info about an APK loaded in memory.
Pierre Lecesneff759e62017-02-01 00:29:25 +000037class LoadedApk {
38 public:
Adam Lesinski8780eb62017-10-31 17:44:39 -070039 // Loads both binary and proto APKs from disk.
40 static std::unique_ptr<LoadedApk> LoadApkFromPath(const ::android::StringPiece& path,
41 IDiagnostics* diag);
42
43 // Loads a proto APK from the given file collection.
44 static std::unique_ptr<LoadedApk> LoadProtoApkFromFileCollection(
45 const Source& source, std::unique_ptr<io::IFileCollection> collection, IDiagnostics* diag);
46
47 // Loads a binary APK from the given file collection.
48 static std::unique_ptr<LoadedApk> LoadBinaryApkFromFileCollection(
49 const Source& source, std::unique_ptr<io::IFileCollection> collection, IDiagnostics* diag);
50
51 LoadedApk(const Source& source, std::unique_ptr<io::IFileCollection> apk,
52 std::unique_ptr<ResourceTable> table, std::unique_ptr<xml::XmlResource> manifest)
53 : source_(source),
54 apk_(std::move(apk)),
55 table_(std::move(table)),
56 manifest_(std::move(manifest)) {
57 }
Adam Lesinski46708052017-09-29 14:49:15 -070058
59 io::IFileCollection* GetFileCollection() {
60 return apk_.get();
Shane Farmer3edd4722017-09-01 14:34:22 -070061 }
Pierre Lecesneff759e62017-02-01 00:29:25 +000062
Adam Lesinski8780eb62017-10-31 17:44:39 -070063 const ResourceTable* GetResourceTable() const {
64 return table_.get();
65 }
66
Adam Lesinski46708052017-09-29 14:49:15 -070067 ResourceTable* GetResourceTable() {
68 return table_.get();
69 }
Pierre Lecesneff759e62017-02-01 00:29:25 +000070
Adam Lesinski46708052017-09-29 14:49:15 -070071 const Source& GetSource() {
72 return source_;
73 }
Pierre Lecesneff759e62017-02-01 00:29:25 +000074
Adam Lesinski8780eb62017-10-31 17:44:39 -070075 const xml::XmlResource* GetManifest() const {
76 return manifest_.get();
77 }
78
Pierre Lecesne2599aa42017-02-01 22:47:03 +000079 /**
80 * Writes the APK on disk at the given path, while also removing the resource
81 * files that are not referenced in the resource table.
82 */
Shane Farmer0a5b2012017-06-22 12:24:12 -070083 virtual bool WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
84 IArchiveWriter* writer);
Pierre Lecesne2599aa42017-02-01 22:47:03 +000085
Shane Farmer57669432017-06-19 12:52:04 -070086 /**
Shane Farmer3edd4722017-09-01 14:34:22 -070087 * Writes the APK on disk at the given path, while also removing the resource files that are not
88 * referenced in the resource table. The provided filter chain is applied to each entry in the APK
89 * file.
90 *
91 * If the manifest is also provided, it will be written to the new APK file, otherwise the
92 * original manifest will be written. The manifest is only required if the contents of the new APK
93 * have been modified in a way that require the AndroidManifest.xml to also be modified.
Shane Farmer57669432017-06-19 12:52:04 -070094 */
Shane Farmer0a5b2012017-06-22 12:24:12 -070095 virtual bool WriteToArchive(IAaptContext* context, ResourceTable* split_table,
96 const TableFlattenerOptions& options, FilterChain* filters,
Shane Farmer3edd4722017-09-01 14:34:22 -070097 IArchiveWriter* writer, xml::XmlResource* manifest = nullptr);
98
Pierre Lecesneff759e62017-02-01 00:29:25 +000099
100 private:
Adam Lesinski46708052017-09-29 14:49:15 -0700101 DISALLOW_COPY_AND_ASSIGN(LoadedApk);
102
Pierre Lecesneff759e62017-02-01 00:29:25 +0000103 Source source_;
104 std::unique_ptr<io::IFileCollection> apk_;
105 std::unique_ptr<ResourceTable> table_;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700106 std::unique_ptr<xml::XmlResource> manifest_;
Pierre Lecesneff759e62017-02-01 00:29:25 +0000107};
108
109} // namespace aapt
110
111#endif /* AAPT_LOADEDAPK_H */