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