blob: d2dd5cf2bc6784417ac46ccabdae813c206a750c [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 Lesinski46708052017-09-29 14:49:15 -070032// Info about an APK loaded in memory.
Pierre Lecesneff759e62017-02-01 00:29:25 +000033class LoadedApk {
34 public:
Adam Lesinski46708052017-09-29 14:49:15 -070035 LoadedApk(
36 const Source& source,
37 std::unique_ptr<io::IFileCollection> apk,
38 std::unique_ptr<ResourceTable> table)
39 : source_(source), apk_(std::move(apk)), table_(std::move(table)) {}
40 virtual ~LoadedApk() = default;
41
42 io::IFileCollection* GetFileCollection() {
43 return apk_.get();
Shane Farmer3edd4722017-09-01 14:34:22 -070044 }
Pierre Lecesneff759e62017-02-01 00:29:25 +000045
Adam Lesinski46708052017-09-29 14:49:15 -070046 ResourceTable* GetResourceTable() {
47 return table_.get();
48 }
Pierre Lecesneff759e62017-02-01 00:29:25 +000049
Adam Lesinski46708052017-09-29 14:49:15 -070050 const Source& GetSource() {
51 return source_;
52 }
Pierre Lecesneff759e62017-02-01 00:29:25 +000053
Pierre Lecesne2599aa42017-02-01 22:47:03 +000054 /**
55 * Writes the APK on disk at the given path, while also removing the resource
56 * files that are not referenced in the resource table.
57 */
Shane Farmer0a5b2012017-06-22 12:24:12 -070058 virtual bool WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
59 IArchiveWriter* writer);
Pierre Lecesne2599aa42017-02-01 22:47:03 +000060
Shane Farmer57669432017-06-19 12:52:04 -070061 /**
Shane Farmer3edd4722017-09-01 14:34:22 -070062 * Writes the APK on disk at the given path, while also removing the resource files that are not
63 * referenced in the resource table. The provided filter chain is applied to each entry in the APK
64 * file.
65 *
66 * If the manifest is also provided, it will be written to the new APK file, otherwise the
67 * original manifest will be written. The manifest is only required if the contents of the new APK
68 * have been modified in a way that require the AndroidManifest.xml to also be modified.
Shane Farmer57669432017-06-19 12:52:04 -070069 */
Shane Farmer0a5b2012017-06-22 12:24:12 -070070 virtual bool WriteToArchive(IAaptContext* context, ResourceTable* split_table,
71 const TableFlattenerOptions& options, FilterChain* filters,
Shane Farmer3edd4722017-09-01 14:34:22 -070072 IArchiveWriter* writer, xml::XmlResource* manifest = nullptr);
73
74 /** Inflates the AndroidManifest.xml file from the APK. */
75 std::unique_ptr<xml::XmlResource> InflateManifest(IAaptContext* context);
Shane Farmer57669432017-06-19 12:52:04 -070076
Pierre Lecesne2599aa42017-02-01 22:47:03 +000077 static std::unique_ptr<LoadedApk> LoadApkFromPath(IAaptContext* context,
78 const android::StringPiece& path);
Pierre Lecesneff759e62017-02-01 00:29:25 +000079
80 private:
Adam Lesinski46708052017-09-29 14:49:15 -070081 DISALLOW_COPY_AND_ASSIGN(LoadedApk);
82
Pierre Lecesneff759e62017-02-01 00:29:25 +000083 Source source_;
84 std::unique_ptr<io::IFileCollection> apk_;
85 std::unique_ptr<ResourceTable> table_;
Pierre Lecesneff759e62017-02-01 00:29:25 +000086};
87
88} // namespace aapt
89
90#endif /* AAPT_LOADEDAPK_H */