blob: 81bcecc3ca5d64660f6cb7d56fbe134b79c3d8b7 [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.
Pierre Lecesneff759e62017-02-01 00:29:25 +000043class LoadedApk {
44 public:
Yi Kong0556f332018-01-09 22:31:45 -080045 virtual ~LoadedApk() = default;
46
Adam Lesinski8780eb62017-10-31 17:44:39 -070047 // Loads both binary and proto APKs from disk.
48 static std::unique_ptr<LoadedApk> LoadApkFromPath(const ::android::StringPiece& path,
49 IDiagnostics* diag);
50
51 // Loads a proto APK from the given file collection.
52 static std::unique_ptr<LoadedApk> LoadProtoApkFromFileCollection(
53 const Source& source, std::unique_ptr<io::IFileCollection> collection, IDiagnostics* diag);
54
55 // Loads a binary APK from the given file collection.
56 static std::unique_ptr<LoadedApk> LoadBinaryApkFromFileCollection(
57 const Source& source, std::unique_ptr<io::IFileCollection> collection, IDiagnostics* diag);
58
59 LoadedApk(const Source& source, std::unique_ptr<io::IFileCollection> apk,
60 std::unique_ptr<ResourceTable> table, std::unique_ptr<xml::XmlResource> manifest)
61 : source_(source),
62 apk_(std::move(apk)),
63 table_(std::move(table)),
64 manifest_(std::move(manifest)) {
65 }
Adam Lesinski46708052017-09-29 14:49:15 -070066
67 io::IFileCollection* GetFileCollection() {
68 return apk_.get();
Shane Farmer3edd4722017-09-01 14:34:22 -070069 }
Pierre Lecesneff759e62017-02-01 00:29:25 +000070
Adam Lesinski8780eb62017-10-31 17:44:39 -070071 const ResourceTable* GetResourceTable() const {
72 return table_.get();
73 }
74
Adam Lesinski46708052017-09-29 14:49:15 -070075 ResourceTable* GetResourceTable() {
76 return table_.get();
77 }
Pierre Lecesneff759e62017-02-01 00:29:25 +000078
Adam Lesinski46708052017-09-29 14:49:15 -070079 const Source& GetSource() {
80 return source_;
81 }
Pierre Lecesneff759e62017-02-01 00:29:25 +000082
Adam Lesinski8780eb62017-10-31 17:44:39 -070083 const xml::XmlResource* GetManifest() const {
84 return manifest_.get();
85 }
86
Pierre Lecesne2599aa42017-02-01 22:47:03 +000087 /**
88 * Writes the APK on disk at the given path, while also removing the resource
89 * files that are not referenced in the resource table.
90 */
Shane Farmer0a5b2012017-06-22 12:24:12 -070091 virtual bool WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
92 IArchiveWriter* writer);
Pierre Lecesne2599aa42017-02-01 22:47:03 +000093
Shane Farmer57669432017-06-19 12:52:04 -070094 /**
Shane Farmer3edd4722017-09-01 14:34:22 -070095 * Writes the APK on disk at the given path, while also removing the resource files that are not
96 * referenced in the resource table. The provided filter chain is applied to each entry in the APK
97 * file.
98 *
99 * If the manifest is also provided, it will be written to the new APK file, otherwise the
100 * original manifest will be written. The manifest is only required if the contents of the new APK
101 * have been modified in a way that require the AndroidManifest.xml to also be modified.
Shane Farmer57669432017-06-19 12:52:04 -0700102 */
Shane Farmer0a5b2012017-06-22 12:24:12 -0700103 virtual bool WriteToArchive(IAaptContext* context, ResourceTable* split_table,
104 const TableFlattenerOptions& options, FilterChain* filters,
Shane Farmer3edd4722017-09-01 14:34:22 -0700105 IArchiveWriter* writer, xml::XmlResource* manifest = nullptr);
106
Pierre Lecesneff759e62017-02-01 00:29:25 +0000107
108 private:
Adam Lesinski46708052017-09-29 14:49:15 -0700109 DISALLOW_COPY_AND_ASSIGN(LoadedApk);
110
Pierre Lecesneff759e62017-02-01 00:29:25 +0000111 Source source_;
112 std::unique_ptr<io::IFileCollection> apk_;
113 std::unique_ptr<ResourceTable> table_;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700114 std::unique_ptr<xml::XmlResource> manifest_;
Pierre Lecesnef267a402017-12-01 11:39:01 +0000115
116 static ApkFormat DetermineApkFormat(io::IFileCollection* apk);
Pierre Lecesneff759e62017-02-01 00:29:25 +0000117};
118
119} // namespace aapt
120
121#endif /* AAPT_LOADEDAPK_H */