Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 17 | #include "Convert.h" |
| 18 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 21 | #include "Diagnostics.h" |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 22 | #include "LoadedApk.h" |
| 23 | #include "ValueVisitor.h" |
Iurii Makhno | 054e433 | 2022-10-12 16:03:03 +0000 | [diff] [blame] | 24 | #include "android-base/file.h" |
Iurii Makhno | 549938a | 2022-04-26 19:36:51 +0000 | [diff] [blame] | 25 | #include "android-base/macros.h" |
Iurii Makhno | 054e433 | 2022-10-12 16:03:03 +0000 | [diff] [blame] | 26 | #include "android-base/stringprintf.h" |
Iurii Makhno | 549938a | 2022-04-26 19:36:51 +0000 | [diff] [blame] | 27 | #include "androidfw/StringPiece.h" |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 28 | #include "cmd/Util.h" |
| 29 | #include "format/binary/TableFlattener.h" |
| 30 | #include "format/binary/XmlFlattener.h" |
| 31 | #include "format/proto/ProtoDeserialize.h" |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 32 | #include "format/proto/ProtoSerialize.h" |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 33 | #include "io/BigBufferStream.h" |
| 34 | #include "io/Util.h" |
| 35 | #include "process/IResourceTableConsumer.h" |
| 36 | #include "process/SymbolTable.h" |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 37 | #include "util/Util.h" |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 38 | |
| 39 | using ::android::StringPiece; |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 40 | using ::android::base::StringPrintf; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 41 | using ::std::unique_ptr; |
| 42 | using ::std::vector; |
| 43 | |
| 44 | namespace aapt { |
| 45 | |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 46 | class IApkSerializer { |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 47 | public: |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 48 | IApkSerializer(IAaptContext* context, const android::Source& source) |
| 49 | : context_(context), source_(source) { |
| 50 | } |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 51 | |
| 52 | virtual bool SerializeXml(const xml::XmlResource* xml, const std::string& path, bool utf16, |
Ryan Mitchell | 05aebf4 | 2018-10-09 15:08:41 -0700 | [diff] [blame] | 53 | IArchiveWriter* writer, uint32_t compression_flags) = 0; |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 54 | virtual bool SerializeTable(ResourceTable* table, IArchiveWriter* writer) = 0; |
David Chaloupka | b66db4e | 2018-01-15 12:35:41 +0000 | [diff] [blame] | 55 | virtual bool SerializeFile(FileReference* file, IArchiveWriter* writer) = 0; |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 56 | |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 57 | virtual ~IApkSerializer() = default; |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 58 | |
| 59 | protected: |
| 60 | IAaptContext* context_; |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 61 | android::Source source_; |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 62 | }; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 63 | |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 64 | class BinaryApkSerializer : public IApkSerializer { |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 65 | public: |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 66 | BinaryApkSerializer(IAaptContext* context, const android::Source& source, |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 67 | const TableFlattenerOptions& table_flattener_options, |
| 68 | const XmlFlattenerOptions& xml_flattener_options) |
| 69 | : IApkSerializer(context, source), |
| 70 | table_flattener_options_(table_flattener_options), |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 71 | xml_flattener_options_(xml_flattener_options) { |
| 72 | } |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 73 | |
| 74 | bool SerializeXml(const xml::XmlResource* xml, const std::string& path, bool utf16, |
Ryan Mitchell | 05aebf4 | 2018-10-09 15:08:41 -0700 | [diff] [blame] | 75 | IArchiveWriter* writer, uint32_t compression_flags) override { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 76 | android::BigBuffer buffer(4096); |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 77 | xml_flattener_options_.use_utf16 = utf16; |
| 78 | XmlFlattener flattener(&buffer, xml_flattener_options_); |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 79 | if (!flattener.Consume(context_, xml)) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | io::BigBufferInputStream input_stream(&buffer); |
Ryan Mitchell | 05aebf4 | 2018-10-09 15:08:41 -0700 | [diff] [blame] | 84 | return io::CopyInputStreamToArchive(context_, &input_stream, path, compression_flags, writer); |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 87 | bool SerializeTable(ResourceTable* table, IArchiveWriter* writer) override { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 88 | android::BigBuffer buffer(4096); |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 89 | TableFlattener table_flattener(table_flattener_options_, &buffer); |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 90 | if (!table_flattener.Consume(context_, table)) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | io::BigBufferInputStream input_stream(&buffer); |
| 95 | return io::CopyInputStreamToArchive(context_, &input_stream, kApkResourceTablePath, |
| 96 | ArchiveEntry::kAlign, writer); |
| 97 | } |
| 98 | |
David Chaloupka | b66db4e | 2018-01-15 12:35:41 +0000 | [diff] [blame] | 99 | bool SerializeFile(FileReference* file, IArchiveWriter* writer) override { |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 100 | if (file->type == ResourceFile::Type::kProtoXml) { |
| 101 | unique_ptr<io::InputStream> in = file->file->OpenInputStream(); |
| 102 | if (in == nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 103 | context_->GetDiagnostics()->Error(android::DiagMessage(source_) |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 104 | << "failed to open file " << *file->path); |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | pb::XmlNode pb_node; |
Ryan Mitchell | e0eba7a | 2018-09-12 08:54:07 -0700 | [diff] [blame] | 109 | io::ProtoInputStreamReader proto_reader(in.get()); |
| 110 | if (!proto_reader.ReadMessage(&pb_node)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 111 | context_->GetDiagnostics()->Error(android::DiagMessage(source_) |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 112 | << "failed to parse proto XML " << *file->path); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | std::string error; |
| 117 | unique_ptr<xml::XmlResource> xml = DeserializeXmlResourceFromPb(pb_node, &error); |
| 118 | if (xml == nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 119 | context_->GetDiagnostics()->Error(android::DiagMessage(source_) |
| 120 | << "failed to deserialize proto XML " << *file->path |
| 121 | << ": " << error); |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | |
Ryan Mitchell | 05aebf4 | 2018-10-09 15:08:41 -0700 | [diff] [blame] | 125 | if (!SerializeXml(xml.get(), *file->path, false /*utf16*/, writer, |
| 126 | file->file->WasCompressed() ? ArchiveEntry::kCompress : 0u)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 127 | context_->GetDiagnostics()->Error(android::DiagMessage(source_) |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 128 | << "failed to serialize to binary XML: " << *file->path); |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 129 | return false; |
| 130 | } |
David Chaloupka | b66db4e | 2018-01-15 12:35:41 +0000 | [diff] [blame] | 131 | |
| 132 | file->type = ResourceFile::Type::kBinaryXml; |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 133 | } else { |
Pierre Lecesne | d55bef7 | 2017-11-10 22:31:01 +0000 | [diff] [blame] | 134 | if (!io::CopyFileToArchivePreserveCompression(context_, file->file, *file->path, writer)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 135 | context_->GetDiagnostics()->Error(android::DiagMessage(source_) |
Pierre Lecesne | d55bef7 | 2017-11-10 22:31:01 +0000 | [diff] [blame] | 136 | << "failed to copy file " << *file->path); |
| 137 | return false; |
| 138 | } |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | private: |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 145 | TableFlattenerOptions table_flattener_options_; |
| 146 | XmlFlattenerOptions xml_flattener_options_; |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 147 | |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 148 | DISALLOW_COPY_AND_ASSIGN(BinaryApkSerializer); |
| 149 | }; |
| 150 | |
| 151 | class ProtoApkSerializer : public IApkSerializer { |
| 152 | public: |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 153 | ProtoApkSerializer(IAaptContext* context, const android::Source& source) |
| 154 | : IApkSerializer(context, source) { |
| 155 | } |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 156 | |
| 157 | bool SerializeXml(const xml::XmlResource* xml, const std::string& path, bool utf16, |
Ryan Mitchell | 05aebf4 | 2018-10-09 15:08:41 -0700 | [diff] [blame] | 158 | IArchiveWriter* writer, uint32_t compression_flags) override { |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 159 | pb::XmlNode pb_node; |
| 160 | SerializeXmlResourceToPb(*xml, &pb_node); |
Ryan Mitchell | 05aebf4 | 2018-10-09 15:08:41 -0700 | [diff] [blame] | 161 | return io::CopyProtoToArchive(context_, &pb_node, path, compression_flags, writer); |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | bool SerializeTable(ResourceTable* table, IArchiveWriter* writer) override { |
| 165 | pb::ResourceTable pb_table; |
Ryan Mitchell | a15c2a8 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 166 | SerializeTableToPb(*table, &pb_table, context_->GetDiagnostics()); |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 167 | return io::CopyProtoToArchive(context_, &pb_table, kProtoResourceTablePath, |
| 168 | ArchiveEntry::kCompress, writer); |
| 169 | } |
| 170 | |
David Chaloupka | b66db4e | 2018-01-15 12:35:41 +0000 | [diff] [blame] | 171 | bool SerializeFile(FileReference* file, IArchiveWriter* writer) override { |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 172 | if (file->type == ResourceFile::Type::kBinaryXml) { |
| 173 | std::unique_ptr<io::IData> data = file->file->OpenAsData(); |
| 174 | if (!data) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 175 | context_->GetDiagnostics()->Error(android::DiagMessage(source_) |
Ryan Mitchell | 90b7a08 | 2019-02-15 17:39:58 +0000 | [diff] [blame] | 176 | << "failed to open file " << *file->path); |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
| 180 | std::string error; |
| 181 | std::unique_ptr<xml::XmlResource> xml = xml::Inflate(data->data(), data->size(), &error); |
| 182 | if (xml == nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 183 | context_->GetDiagnostics()->Error(android::DiagMessage(source_) |
| 184 | << "failed to parse binary XML: " << error); |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 185 | return false; |
| 186 | } |
| 187 | |
Ryan Mitchell | 05aebf4 | 2018-10-09 15:08:41 -0700 | [diff] [blame] | 188 | if (!SerializeXml(xml.get(), *file->path, false /*utf16*/, writer, |
| 189 | file->file->WasCompressed() ? ArchiveEntry::kCompress : 0u)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 190 | context_->GetDiagnostics()->Error(android::DiagMessage(source_) |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 191 | << "failed to serialize to proto XML: " << *file->path); |
| 192 | return false; |
| 193 | } |
David Chaloupka | b66db4e | 2018-01-15 12:35:41 +0000 | [diff] [blame] | 194 | |
| 195 | file->type = ResourceFile::Type::kProtoXml; |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 196 | } else { |
| 197 | if (!io::CopyFileToArchivePreserveCompression(context_, file->file, *file->path, writer)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 198 | context_->GetDiagnostics()->Error(android::DiagMessage(source_) |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 199 | << "failed to copy file " << *file->path); |
| 200 | return false; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | private: |
| 208 | DISALLOW_COPY_AND_ASSIGN(ProtoApkSerializer); |
Pierre Lecesne | 7e8549d | 2017-11-10 01:22:12 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 211 | class Context : public IAaptContext { |
| 212 | public: |
| 213 | Context() : mangler_({}), symbols_(&mangler_) { |
| 214 | } |
| 215 | |
| 216 | PackageType GetPackageType() override { |
| 217 | return PackageType::kApp; |
| 218 | } |
| 219 | |
| 220 | SymbolTable* GetExternalSymbols() override { |
| 221 | return &symbols_; |
| 222 | } |
| 223 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 224 | android::IDiagnostics* GetDiagnostics() override { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 225 | return &diag_; |
| 226 | } |
| 227 | |
| 228 | const std::string& GetCompilationPackage() override { |
| 229 | return package_; |
| 230 | } |
| 231 | |
| 232 | uint8_t GetPackageId() override { |
| 233 | // Nothing should call this. |
| 234 | UNIMPLEMENTED(FATAL) << "PackageID should not be necessary"; |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | NameMangler* GetNameMangler() override { |
| 239 | UNIMPLEMENTED(FATAL); |
| 240 | return nullptr; |
| 241 | } |
| 242 | |
| 243 | bool IsVerbose() override { |
| 244 | return verbose_; |
| 245 | } |
| 246 | |
| 247 | int GetMinSdkVersion() override { |
Iurii Makhno | 549938a | 2022-04-26 19:36:51 +0000 | [diff] [blame] | 248 | return min_sdk_; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Udam Saini | b228df3 | 2019-06-18 16:50:34 -0700 | [diff] [blame] | 251 | const std::set<std::string>& GetSplitNameDependencies() override { |
| 252 | UNIMPLEMENTED(FATAL) << "Split Name Dependencies should not be necessary"; |
| 253 | static std::set<std::string> empty; |
| 254 | return empty; |
| 255 | } |
| 256 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 257 | bool verbose_ = false; |
| 258 | std::string package_; |
Iurii Makhno | 549938a | 2022-04-26 19:36:51 +0000 | [diff] [blame] | 259 | int32_t min_sdk_ = 0; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 260 | |
| 261 | private: |
| 262 | DISALLOW_COPY_AND_ASSIGN(Context); |
| 263 | |
| 264 | NameMangler mangler_; |
| 265 | SymbolTable symbols_; |
| 266 | StdErrDiagnostics diag_; |
| 267 | }; |
| 268 | |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 269 | int Convert(IAaptContext* context, LoadedApk* apk, IArchiveWriter* output_writer, |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 270 | ApkFormat output_format, TableFlattenerOptions table_flattener_options, |
| 271 | XmlFlattenerOptions xml_flattener_options) { |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 272 | unique_ptr<IApkSerializer> serializer; |
| 273 | if (output_format == ApkFormat::kBinary) { |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 274 | serializer.reset(new BinaryApkSerializer(context, apk->GetSource(), table_flattener_options, |
| 275 | xml_flattener_options)); |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 276 | } else if (output_format == ApkFormat::kProto) { |
| 277 | serializer.reset(new ProtoApkSerializer(context, apk->GetSource())); |
| 278 | } else { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 279 | context->GetDiagnostics()->Error(android::DiagMessage(apk->GetSource()) |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 280 | << "Cannot convert APK to unknown format"); |
| 281 | return 1; |
| 282 | } |
| 283 | |
| 284 | io::IFile* manifest = apk->GetFileCollection()->FindFile(kAndroidManifestPath); |
| 285 | if (!serializer->SerializeXml(apk->GetManifest(), kAndroidManifestPath, true /*utf16*/, |
| 286 | output_writer, (manifest != nullptr && manifest->WasCompressed()) |
Ryan Mitchell | 90b7a08 | 2019-02-15 17:39:58 +0000 | [diff] [blame] | 287 | ? ArchiveEntry::kCompress : 0u)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 288 | context->GetDiagnostics()->Error(android::DiagMessage(apk->GetSource()) |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 289 | << "failed to serialize AndroidManifest.xml"); |
| 290 | return 1; |
| 291 | } |
| 292 | |
| 293 | if (apk->GetResourceTable() != nullptr) { |
| 294 | // The table might be modified by below code. |
| 295 | auto converted_table = apk->GetResourceTable(); |
| 296 | |
Winson | f54c9a1 | 2019-01-23 12:39:40 -0800 | [diff] [blame] | 297 | std::unordered_set<std::string> files_written; |
| 298 | |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 299 | // Resources |
| 300 | for (const auto& package : converted_table->packages) { |
| 301 | for (const auto& type : package->types) { |
| 302 | for (const auto& entry : type->entries) { |
| 303 | for (const auto& config_value : entry->values) { |
| 304 | FileReference* file = ValueCast<FileReference>(config_value->value.get()); |
| 305 | if (file != nullptr) { |
| 306 | if (file->file == nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 307 | context->GetDiagnostics()->Error(android::DiagMessage(apk->GetSource()) |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 308 | << "no file associated with " << *file); |
| 309 | return 1; |
| 310 | } |
| 311 | |
Winson | f54c9a1 | 2019-01-23 12:39:40 -0800 | [diff] [blame] | 312 | // Only serialize if we haven't seen this file before |
| 313 | if (files_written.insert(*file->path).second) { |
| 314 | if (!serializer->SerializeFile(file, output_writer)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 315 | context->GetDiagnostics()->Error(android::DiagMessage(apk->GetSource()) |
Ryan Mitchell | 90b7a08 | 2019-02-15 17:39:58 +0000 | [diff] [blame] | 316 | << "failed to serialize file " << *file->path); |
Winson | f54c9a1 | 2019-01-23 12:39:40 -0800 | [diff] [blame] | 317 | return 1; |
| 318 | } |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 319 | } |
| 320 | } // file |
| 321 | } // config_value |
| 322 | } // entry |
| 323 | } // type |
| 324 | } // package |
| 325 | |
| 326 | // Converted resource table |
| 327 | if (!serializer->SerializeTable(converted_table, output_writer)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 328 | context->GetDiagnostics()->Error(android::DiagMessage(apk->GetSource()) |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 329 | << "failed to serialize the resource table"); |
| 330 | return 1; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Other files |
| 335 | std::unique_ptr<io::IFileCollectionIterator> iterator = apk->GetFileCollection()->Iterator(); |
| 336 | while (iterator->HasNext()) { |
| 337 | io::IFile* file = iterator->Next(); |
| 338 | std::string path = file->GetSource().path; |
| 339 | |
| 340 | // Manifest, resource table and resources have already been taken care of. |
| 341 | if (path == kAndroidManifestPath || |
| 342 | path == kApkResourceTablePath || |
| 343 | path == kProtoResourceTablePath || |
| 344 | path.find("res/") == 0) { |
| 345 | continue; |
| 346 | } |
| 347 | |
| 348 | if (!io::CopyFileToArchivePreserveCompression(context, file, path, output_writer)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 349 | context->GetDiagnostics()->Error(android::DiagMessage(apk->GetSource()) |
| 350 | << "failed to copy file " << path); |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 351 | return 1; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
Iurii Makhno | 054e433 | 2022-10-12 16:03:03 +0000 | [diff] [blame] | 358 | bool ExtractResourceConfig(const std::string& path, IAaptContext* context, |
| 359 | TableFlattenerOptions& out_options) { |
| 360 | std::string content; |
| 361 | if (!android::base::ReadFileToString(path, &content, true /*follow_symlinks*/)) { |
| 362 | context->GetDiagnostics()->Error(android::DiagMessage(path) << "failed reading config file"); |
| 363 | return false; |
| 364 | } |
| 365 | std::unordered_set<ResourceName> resources_exclude_list; |
| 366 | bool result = ParseResourceConfig(content, context, resources_exclude_list, |
branliu | f1ed523 | 2022-12-16 19:02:29 +0800 | [diff] [blame] | 367 | out_options.name_collapse_exemptions, |
| 368 | out_options.path_shorten_exemptions); |
Iurii Makhno | 054e433 | 2022-10-12 16:03:03 +0000 | [diff] [blame] | 369 | if (!result) { |
| 370 | return false; |
| 371 | } |
| 372 | if (!resources_exclude_list.empty()) { |
| 373 | context->GetDiagnostics()->Error(android::DiagMessage(path) |
| 374 | << "Unsupported '#remove' directive in resource config."); |
| 375 | return false; |
| 376 | } |
| 377 | return true; |
| 378 | } |
| 379 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 380 | const char* ConvertCommand::kOutputFormatProto = "proto"; |
| 381 | const char* ConvertCommand::kOutputFormatBinary = "binary"; |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 382 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 383 | int ConvertCommand::Action(const std::vector<std::string>& args) { |
| 384 | if (args.size() != 1) { |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 385 | std::cerr << "must supply a single APK\n"; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 386 | Usage(&std::cerr); |
| 387 | return 1; |
| 388 | } |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 389 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 390 | Context context; |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 391 | StringPiece path = args[0]; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 392 | unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(path, context.GetDiagnostics()); |
| 393 | if (apk == nullptr) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 394 | context.GetDiagnostics()->Error(android::DiagMessage(path) << "failed to load APK"); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 395 | return 1; |
| 396 | } |
| 397 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 398 | auto app_info = ExtractAppInfoFromBinaryManifest(*apk->GetManifest(), context.GetDiagnostics()); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 399 | if (!app_info) { |
| 400 | return 1; |
| 401 | } |
| 402 | |
| 403 | context.package_ = app_info.value().package; |
Iurii Makhno | 549938a | 2022-04-26 19:36:51 +0000 | [diff] [blame] | 404 | context.min_sdk_ = app_info.value().min_sdk_version.value_or(0); |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 405 | unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), |
| 406 | output_path_); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 407 | if (writer == nullptr) { |
| 408 | return 1; |
| 409 | } |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 410 | |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 411 | ApkFormat format; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 412 | if (!output_format_ || output_format_.value() == ConvertCommand::kOutputFormatBinary) { |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 413 | format = ApkFormat::kBinary; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 414 | } else if (output_format_.value() == ConvertCommand::kOutputFormatProto) { |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 415 | format = ApkFormat::kProto; |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 416 | } else { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 417 | context.GetDiagnostics()->Error(android::DiagMessage(path) |
| 418 | << "Invalid value for flag --output-format: " |
| 419 | << output_format_.value()); |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 420 | return 1; |
| 421 | } |
Iurii Makhno | da06e4d | 2022-08-24 14:17:32 +0000 | [diff] [blame] | 422 | if (enable_sparse_encoding_) { |
| 423 | table_flattener_options_.sparse_entries = SparseEntriesMode::Enabled; |
| 424 | } |
| 425 | if (force_sparse_encoding_) { |
| 426 | table_flattener_options_.sparse_entries = SparseEntriesMode::Forced; |
| 427 | } |
Rico Wind | ec629dd | 2023-08-01 14:11:45 +0200 | [diff] [blame^] | 428 | table_flattener_options_.use_compact_entries = enable_compact_entries_; |
Iurii Makhno | 054e433 | 2022-10-12 16:03:03 +0000 | [diff] [blame] | 429 | if (resources_config_path_) { |
| 430 | if (!ExtractResourceConfig(*resources_config_path_, &context, table_flattener_options_)) { |
| 431 | return 1; |
| 432 | } |
| 433 | } |
Pierre Lecesne | d8b4bea | 2017-11-10 23:50:17 +0000 | [diff] [blame] | 434 | |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 435 | return Convert(&context, apk.get(), writer.get(), format, table_flattener_options_, |
| 436 | xml_flattener_options_); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | } // namespace aapt |