blob: 1d04b357131ad90937860420a9cc27dc8498d831 [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#include "LoadedApk.h"
18
Pierre Lecesne2599aa42017-02-01 22:47:03 +000019#include "ResourceValues.h"
20#include "ValueVisitor.h"
21#include "flatten/Archive.h"
22#include "flatten/TableFlattener.h"
23
Pierre Lecesneff759e62017-02-01 00:29:25 +000024namespace aapt {
25
Pierre Lecesne2599aa42017-02-01 22:47:03 +000026std::unique_ptr<LoadedApk> LoadedApk::LoadApkFromPath(IAaptContext* context,
27 const android::StringPiece& path) {
Pierre Lecesneff759e62017-02-01 00:29:25 +000028 Source source(path);
29 std::string error;
30 std::unique_ptr<io::ZipFileCollection> apk =
31 io::ZipFileCollection::Create(path, &error);
32 if (!apk) {
33 context->GetDiagnostics()->Error(DiagMessage(source) << error);
34 return {};
35 }
36
37 io::IFile* file = apk->FindFile("resources.arsc");
38 if (!file) {
39 context->GetDiagnostics()->Error(DiagMessage(source)
40 << "no resources.arsc found");
41 return {};
42 }
43
44 std::unique_ptr<io::IData> data = file->OpenAsData();
45 if (!data) {
46 context->GetDiagnostics()->Error(DiagMessage(source)
47 << "could not open resources.arsc");
48 return {};
49 }
50
51 std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>();
52 BinaryResourceParser parser(context, table.get(), source, data->data(),
53 data->size());
54 if (!parser.Parse()) {
55 return {};
56 }
57
58 return util::make_unique<LoadedApk>(source, std::move(apk), std::move(table));
59}
60
Adam Lesinskid48944a2017-02-21 14:22:30 -080061bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
62 IArchiveWriter* writer) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +000063 std::set<std::string> referenced_resources;
64 // List the files being referenced in the resource table.
65 for (auto& pkg : table_->packages) {
66 for (auto& type : pkg->types) {
67 for (auto& entry : type->entries) {
68 for (auto& config_value : entry->values) {
69 FileReference* file_ref = ValueCast<FileReference>(config_value->value.get());
70 if (file_ref) {
71 referenced_resources.insert(*file_ref->path);
72 }
73 }
74 }
75 }
76 }
77
78 std::unique_ptr<io::IFileCollectionIterator> iterator = apk_->Iterator();
79 while (iterator->HasNext()) {
80 io::IFile* file = iterator->Next();
81
82 std::string path = file->GetSource().path;
83 // The name of the path has the format "<zip-file-name>@<path-to-file>".
84 path = path.substr(path.find("@") + 1);
85
86 // Skip resources that are not referenced if requested.
87 if (path.find("res/") == 0 && referenced_resources.find(path) == referenced_resources.end()) {
88 if (context->IsVerbose()) {
89 context->GetDiagnostics()->Note(DiagMessage()
Pierre Lecesnefa131d52017-02-03 19:15:03 +000090 << "Removing resource '" << path << "' from APK.");
Pierre Lecesne2599aa42017-02-01 22:47:03 +000091 }
92 continue;
93 }
94
95 // The resource table needs to be reserialized since it might have changed.
96 if (path == "resources.arsc") {
97 BigBuffer buffer = BigBuffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080098 // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode
99 // with sparse entries) b/35389232.
Adam Lesinskid48944a2017-02-21 14:22:30 -0800100 TableFlattener flattener(options, &buffer);
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000101 if (!flattener.Consume(context, table_.get())) {
102 return false;
103 }
104
105 if (!writer->StartEntry(path, ArchiveEntry::kAlign) || !writer->WriteEntry(buffer) ||
106 !writer->FinishEntry()) {
107 context->GetDiagnostics()->Error(DiagMessage()
108 << "Error when writing file '" << path << "' in APK.");
109 return false;
110 }
111 continue;
112 }
113
114 std::unique_ptr<io::IData> data = file->OpenAsData();
Pierre Lecesnefa131d52017-02-03 19:15:03 +0000115 uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u;
116 if (!writer->StartEntry(path, compression_flags) ||
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000117 !writer->WriteEntry(data->data(), data->size()) || !writer->FinishEntry()) {
118 context->GetDiagnostics()->Error(DiagMessage()
119 << "Error when writing file '" << path << "' in APK.");
120 return false;
121 }
122 }
123
124 return true;
125}
126
Pierre Lecesneff759e62017-02-01 00:29:25 +0000127} // namespace aapt