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" |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 24 | #include "flatten/Archive.h" |
Adam Lesinski | d48944a | 2017-02-21 14:22:30 -0800 | [diff] [blame] | 25 | #include "flatten/TableFlattener.h" |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 26 | #include "io/ZipArchive.h" |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 27 | #include "unflatten/BinaryResourceParser.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 | |
| 32 | /** Info about an APK loaded in memory. */ |
| 33 | class LoadedApk { |
| 34 | public: |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 35 | LoadedApk(const Source& source, std::unique_ptr<io::IFileCollection> apk, |
| 36 | std::unique_ptr<ResourceTable> table) |
| 37 | : source_(source), apk_(std::move(apk)), table_(std::move(table)) { |
| 38 | } |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 39 | |
| 40 | io::IFileCollection* GetFileCollection() { return apk_.get(); } |
| 41 | |
| 42 | ResourceTable* GetResourceTable() { return table_.get(); } |
| 43 | |
| 44 | const Source& GetSource() { return source_; } |
| 45 | |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 46 | /** |
| 47 | * Writes the APK on disk at the given path, while also removing the resource |
| 48 | * files that are not referenced in the resource table. |
| 49 | */ |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 50 | virtual bool WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options, |
| 51 | IArchiveWriter* writer); |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 52 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 53 | /** |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 54 | * Writes the APK on disk at the given path, while also removing the resource files that are not |
| 55 | * referenced in the resource table. The provided filter chain is applied to each entry in the APK |
| 56 | * file. |
| 57 | * |
| 58 | * If the manifest is also provided, it will be written to the new APK file, otherwise the |
| 59 | * original manifest will be written. The manifest is only required if the contents of the new APK |
| 60 | * 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] | 61 | */ |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 62 | virtual bool WriteToArchive(IAaptContext* context, ResourceTable* split_table, |
| 63 | const TableFlattenerOptions& options, FilterChain* filters, |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 64 | IArchiveWriter* writer, xml::XmlResource* manifest = nullptr); |
| 65 | |
| 66 | /** Inflates the AndroidManifest.xml file from the APK. */ |
| 67 | std::unique_ptr<xml::XmlResource> InflateManifest(IAaptContext* context); |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 68 | |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 69 | static std::unique_ptr<LoadedApk> LoadApkFromPath(IAaptContext* context, |
| 70 | const android::StringPiece& path); |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | Source source_; |
| 74 | std::unique_ptr<io::IFileCollection> apk_; |
| 75 | std::unique_ptr<ResourceTable> table_; |
| 76 | |
| 77 | DISALLOW_COPY_AND_ASSIGN(LoadedApk); |
| 78 | }; |
| 79 | |
| 80 | } // namespace aapt |
| 81 | |
| 82 | #endif /* AAPT_LOADEDAPK_H */ |