Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 1 | /* |
| 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 Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 22 | #include "ResourceTable.h" |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 23 | #include "filter/Filter.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 24 | #include "format/Archive.h" |
| 25 | #include "format/binary/BinaryResourceParser.h" |
| 26 | #include "format/binary/TableFlattener.h" |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 27 | #include "io/ZipArchive.h" |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 28 | #include "xml/XmlDom.h" |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 29 | |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 30 | namespace aapt { |
| 31 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 32 | constexpr static const char kApkResourceTablePath[] = "resources.arsc"; |
| 33 | constexpr static const char kProtoResourceTablePath[] = "resources.pb"; |
| 34 | constexpr static const char kAndroidManifestPath[] = "AndroidManifest.xml"; |
| 35 | |
Pierre Lecesne | f267a40 | 2017-12-01 11:39:01 +0000 | [diff] [blame] | 36 | enum ApkFormat { |
| 37 | kUnknown, |
| 38 | kBinary, |
| 39 | kProto, |
| 40 | }; |
| 41 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 42 | // Info about an APK loaded in memory. |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 43 | class LoadedApk { |
| 44 | public: |
Yi Kong | 0556f33 | 2018-01-09 22:31:45 -0800 | [diff] [blame] | 45 | virtual ~LoadedApk() = default; |
| 46 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 47 | // Loads both binary and proto APKs from disk. |
| 48 | static std::unique_ptr<LoadedApk> LoadApkFromPath(const ::android::StringPiece& path, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 49 | android::IDiagnostics* diag); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 50 | |
| 51 | // Loads a proto APK from the given file collection. |
| 52 | static std::unique_ptr<LoadedApk> LoadProtoApkFromFileCollection( |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 53 | const android::Source& source, std::unique_ptr<io::IFileCollection> collection, |
| 54 | android::IDiagnostics* diag); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 55 | |
| 56 | // Loads a binary APK from the given file collection. |
| 57 | static std::unique_ptr<LoadedApk> LoadBinaryApkFromFileCollection( |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 58 | const android::Source& source, std::unique_ptr<io::IFileCollection> collection, |
| 59 | android::IDiagnostics* diag); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 60 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 61 | LoadedApk(const android::Source& source, std::unique_ptr<io::IFileCollection> apk, |
corysmith@google.com | f7db43e | 2018-03-13 12:04:10 -0400 | [diff] [blame] | 62 | std::unique_ptr<ResourceTable> table, std::unique_ptr<xml::XmlResource> manifest, |
| 63 | const ApkFormat& format) |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 64 | : source_(source), |
| 65 | apk_(std::move(apk)), |
| 66 | table_(std::move(table)), |
corysmith@google.com | f7db43e | 2018-03-13 12:04:10 -0400 | [diff] [blame] | 67 | manifest_(std::move(manifest)), |
| 68 | format_(format) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 69 | } |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 70 | |
| 71 | io::IFileCollection* GetFileCollection() { |
| 72 | return apk_.get(); |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 73 | } |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 74 | |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 75 | ApkFormat GetApkFormat() { |
| 76 | return format_; |
| 77 | } |
| 78 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 79 | const ResourceTable* GetResourceTable() const { |
| 80 | return table_.get(); |
| 81 | } |
| 82 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 83 | ResourceTable* GetResourceTable() { |
| 84 | return table_.get(); |
| 85 | } |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 86 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 87 | const android::Source& GetSource() { |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 88 | return source_; |
| 89 | } |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 90 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 91 | const xml::XmlResource* GetManifest() const { |
| 92 | return manifest_.get(); |
| 93 | } |
| 94 | |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 95 | /** |
| 96 | * Writes the APK on disk at the given path, while also removing the resource |
| 97 | * files that are not referenced in the resource table. |
| 98 | */ |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 99 | virtual bool WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options, |
| 100 | IArchiveWriter* writer); |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 101 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 102 | /** |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 103 | * Writes the APK on disk at the given path, while also removing the resource files that are not |
| 104 | * referenced in the resource table. The provided filter chain is applied to each entry in the APK |
| 105 | * file. |
| 106 | * |
| 107 | * If the manifest is also provided, it will be written to the new APK file, otherwise the |
| 108 | * original manifest will be written. The manifest is only required if the contents of the new APK |
| 109 | * have been modified in a way that require the AndroidManifest.xml to also be modified. |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 110 | */ |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 111 | virtual bool WriteToArchive(IAaptContext* context, ResourceTable* split_table, |
| 112 | const TableFlattenerOptions& options, FilterChain* filters, |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 113 | IArchiveWriter* writer, xml::XmlResource* manifest = nullptr); |
| 114 | |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 115 | /** Loads the file as an xml document. */ |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 116 | std::unique_ptr<xml::XmlResource> LoadXml(const std::string& file_path, |
| 117 | android::IDiagnostics* diag) const; |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 118 | |
| 119 | private: |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 120 | DISALLOW_COPY_AND_ASSIGN(LoadedApk); |
| 121 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 122 | android::Source source_; |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 123 | std::unique_ptr<io::IFileCollection> apk_; |
| 124 | std::unique_ptr<ResourceTable> table_; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 125 | std::unique_ptr<xml::XmlResource> manifest_; |
corysmith@google.com | f7db43e | 2018-03-13 12:04:10 -0400 | [diff] [blame] | 126 | ApkFormat format_; |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | } // namespace aapt |
| 130 | |
| 131 | #endif /* AAPT_LOADEDAPK_H */ |