blob: 0215a2b5ddcb1af7bd35135d92991e0b11f43f94 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 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 "AppInfo.h"
18#include "BigBuffer.h"
19#include "BinaryResourceParser.h"
Adam Lesinski4d3a9872015-04-09 19:53:22 -070020#include "BindingXmlPullParser.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021#include "Files.h"
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070022#include "Flag.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include "JavaClassGenerator.h"
24#include "Linker.h"
25#include "ManifestParser.h"
26#include "ManifestValidator.h"
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070027#include "Png.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028#include "ResourceParser.h"
29#include "ResourceTable.h"
30#include "ResourceValues.h"
31#include "SdkConstants.h"
32#include "SourceXmlPullParser.h"
33#include "StringPiece.h"
34#include "TableFlattener.h"
35#include "Util.h"
36#include "XmlFlattener.h"
37
38#include <algorithm>
39#include <androidfw/AssetManager.h>
40#include <cstdlib>
41#include <dirent.h>
42#include <errno.h>
43#include <fstream>
44#include <iostream>
45#include <sstream>
46#include <sys/stat.h>
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070047#include <utils/Errors.h>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048
49using namespace aapt;
50
51void printTable(const ResourceTable& table) {
52 std::cout << "ResourceTable package=" << table.getPackage();
53 if (table.getPackageId() != ResourceTable::kUnsetPackageId) {
54 std::cout << " id=" << std::hex << table.getPackageId() << std::dec;
55 }
56 std::cout << std::endl
57 << "---------------------------------------------------------" << std::endl;
58
59 for (const auto& type : table) {
60 std::cout << "Type " << type->type;
61 if (type->typeId != ResourceTableType::kUnsetTypeId) {
62 std::cout << " [" << type->typeId << "]";
63 }
64 std::cout << " (" << type->entries.size() << " entries)" << std::endl;
65 for (const auto& entry : type->entries) {
66 std::cout << " " << entry->name;
67 if (entry->entryId != ResourceEntry::kUnsetEntryId) {
68 std::cout << " [" << entry->entryId << "]";
69 }
70 std::cout << " (" << entry->values.size() << " configurations)";
71 if (entry->publicStatus.isPublic) {
72 std::cout << " PUBLIC";
73 }
74 std::cout << std::endl;
75 for (const auto& value : entry->values) {
76 std::cout << " " << value.config << " (" << value.source << ") : ";
77 value.value->print(std::cout);
78 std::cout << std::endl;
79 }
80 }
81 }
82}
83
84void printStringPool(const StringPool& pool) {
85 std::cout << "String pool of length " << pool.size() << std::endl
86 << "---------------------------------------------------------" << std::endl;
87
88 size_t i = 0;
89 for (const auto& entry : pool) {
90 std::cout << "[" << i << "]: "
91 << entry->value
92 << " (Priority " << entry->context.priority
93 << ", Config '" << entry->context.config << "')"
94 << std::endl;
95 i++;
96 }
97}
98
99std::unique_ptr<FileReference> makeFileReference(StringPool& pool, const StringPiece& filename,
100 ResourceType type, const ConfigDescription& config) {
101 std::stringstream path;
102 path << "res/" << type;
103 if (config != ConfigDescription{}) {
104 path << "-" << config;
105 }
106 path << "/" << filename;
107 return util::make_unique<FileReference>(pool.makeRef(util::utf8ToUtf16(path.str())));
108}
109
110/**
111 * Collect files from 'root', filtering out any files that do not
112 * match the FileFilter 'filter'.
113 */
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700114bool walkTree(const Source& root, const FileFilter& filter,
115 std::vector<Source>* outEntries) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116 bool error = false;
117
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700118 for (const std::string& dirName : listFiles(root.path)) {
119 std::string dir = root.path;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120 appendPath(&dir, dirName);
121
122 FileType ft = getFileType(dir);
123 if (!filter(dirName, ft)) {
124 continue;
125 }
126
127 if (ft != FileType::kDirectory) {
128 continue;
129 }
130
131 for (const std::string& fileName : listFiles(dir)) {
132 std::string file(dir);
133 appendPath(&file, fileName);
134
135 FileType ft = getFileType(file);
136 if (!filter(fileName, ft)) {
137 continue;
138 }
139
140 if (ft != FileType::kRegular) {
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700141 Logger::error(Source{ file }) << "not a regular file." << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142 error = true;
143 continue;
144 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700145 outEntries->push_back(Source{ file });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146 }
147 }
148 return !error;
149}
150
151bool loadBinaryResourceTable(std::shared_ptr<ResourceTable> table, const Source& source) {
152 std::ifstream ifs(source.path, std::ifstream::in | std::ifstream::binary);
153 if (!ifs) {
154 Logger::error(source) << strerror(errno) << std::endl;
155 return false;
156 }
157
158 std::streampos fsize = ifs.tellg();
159 ifs.seekg(0, std::ios::end);
160 fsize = ifs.tellg() - fsize;
161 ifs.seekg(0, std::ios::beg);
162
163 assert(fsize >= 0);
164 size_t dataSize = static_cast<size_t>(fsize);
165 char* buf = new char[dataSize];
166 ifs.read(buf, dataSize);
167
168 BinaryResourceParser parser(table, source, buf, dataSize);
169 bool result = parser.parse();
170
171 delete [] buf;
172 return result;
173}
174
175bool loadResTable(android::ResTable* table, const Source& source) {
176 std::ifstream ifs(source.path, std::ifstream::in | std::ifstream::binary);
177 if (!ifs) {
178 Logger::error(source) << strerror(errno) << std::endl;
179 return false;
180 }
181
182 std::streampos fsize = ifs.tellg();
183 ifs.seekg(0, std::ios::end);
184 fsize = ifs.tellg() - fsize;
185 ifs.seekg(0, std::ios::beg);
186
187 assert(fsize >= 0);
188 size_t dataSize = static_cast<size_t>(fsize);
189 char* buf = new char[dataSize];
190 ifs.read(buf, dataSize);
191
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700192 bool result = table->add(buf, dataSize, -1, true) == android::NO_ERROR;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193
194 delete [] buf;
195 return result;
196}
197
198void versionStylesForCompat(std::shared_ptr<ResourceTable> table) {
199 for (auto& type : *table) {
200 if (type->type != ResourceType::kStyle) {
201 continue;
202 }
203
204 for (auto& entry : type->entries) {
205 // Add the versioned styles we want to create
206 // here. They are added to the table after
207 // iterating over the original set of styles.
208 //
209 // A stack is used since auto-generated styles
210 // from later versions should override
211 // auto-generated styles from earlier versions.
212 // Iterating over the styles is done in order,
213 // so we will always visit sdkVersions from smallest
214 // to largest.
215 std::stack<ResourceConfigValue> addStack;
216
217 for (ResourceConfigValue& configValue : entry->values) {
218 visitFunc<Style>(*configValue.value, [&](Style& style) {
219 // Collect which entries we've stripped and the smallest
220 // SDK level which was stripped.
221 size_t minSdkStripped = std::numeric_limits<size_t>::max();
222 std::vector<Style::Entry> stripped;
223
224 // Iterate over the style's entries and erase/record the
225 // attributes whose SDK level exceeds the config's sdkVersion.
226 auto iter = style.entries.begin();
227 while (iter != style.entries.end()) {
228 if (iter->key.name.package == u"android") {
229 size_t sdkLevel = findAttributeSdkLevel(iter->key.name.entry);
230 if (sdkLevel > 1 && sdkLevel > configValue.config.sdkVersion) {
231 // Record that we are about to strip this.
232 stripped.emplace_back(std::move(*iter));
233 minSdkStripped = std::min(minSdkStripped, sdkLevel);
234
235 // Erase this from this style.
236 iter = style.entries.erase(iter);
237 continue;
238 }
239 }
240 ++iter;
241 }
242
243 if (!stripped.empty()) {
244 // We have stripped attributes, so let's create a new style to hold them.
245 ConfigDescription versionConfig(configValue.config);
246 versionConfig.sdkVersion = minSdkStripped;
247
248 ResourceConfigValue value = {
249 versionConfig,
250 configValue.source,
251 {},
252
253 // Create a copy of the original style.
254 std::unique_ptr<Value>(configValue.value->clone())
255 };
256
257 Style& newStyle = static_cast<Style&>(*value.value);
258
259 // Move the recorded stripped attributes into this new style.
260 std::move(stripped.begin(), stripped.end(),
261 std::back_inserter(newStyle.entries));
262
263 // We will add this style to the table later. If we do it now, we will
264 // mess up iteration.
265 addStack.push(std::move(value));
266 }
267 });
268 }
269
270 auto comparator =
271 [](const ResourceConfigValue& lhs, const ConfigDescription& rhs) -> bool {
272 return lhs.config < rhs;
273 };
274
275 while (!addStack.empty()) {
276 ResourceConfigValue& value = addStack.top();
277 auto iter = std::lower_bound(entry->values.begin(), entry->values.end(),
278 value.config, comparator);
279 if (iter == entry->values.end() || iter->config != value.config) {
280 entry->values.insert(iter, std::move(value));
281 }
282 addStack.pop();
283 }
284 }
285 }
286}
287
288bool collectXml(std::shared_ptr<ResourceTable> table, const Source& source,
Adam Lesinski4d3a9872015-04-09 19:53:22 -0700289 const ResourceName& name, const ConfigDescription& config) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800290 std::ifstream in(source.path, std::ifstream::binary);
291 if (!in) {
292 Logger::error(source) << strerror(errno) << std::endl;
293 return false;
294 }
295
296 std::set<size_t> sdkLevels;
297
Adam Lesinski4d3a9872015-04-09 19:53:22 -0700298 SourceXmlPullParser parser(in);
299 while (XmlPullParser::isGoodEvent(parser.next())) {
300 if (parser.getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800301 continue;
302 }
303
Adam Lesinski4d3a9872015-04-09 19:53:22 -0700304 const auto endIter = parser.endAttributes();
305 for (auto iter = parser.beginAttributes(); iter != endIter; ++iter) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800306 if (iter->namespaceUri == u"http://schemas.android.com/apk/res/android") {
307 size_t sdkLevel = findAttributeSdkLevel(iter->name);
308 if (sdkLevel > 1) {
309 sdkLevels.insert(sdkLevel);
310 }
311 }
312
313 ResourceNameRef refName;
314 bool create = false;
315 bool privateRef = false;
316 if (ResourceParser::tryParseReference(iter->value, &refName, &create, &privateRef) &&
317 create) {
Adam Lesinski4d3a9872015-04-09 19:53:22 -0700318 table->addResource(refName, {}, source.line(parser.getLineNumber()),
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800319 util::make_unique<Id>());
320 }
321 }
322 }
323
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800324 for (size_t level : sdkLevels) {
325 Logger::note(source)
326 << "creating v" << level << " versioned file."
327 << std::endl;
328 ConfigDescription newConfig = config;
329 newConfig.sdkVersion = level;
330
331 std::unique_ptr<FileReference> fileResource = makeFileReference(
332 table->getValueStringPool(),
333 util::utf16ToUtf8(name.entry) + ".xml",
334 name.type,
335 newConfig);
336 table->addResource(name, newConfig, source.line(0), std::move(fileResource));
337 }
338 return true;
339}
340
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700341struct CompileItem {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800342 Source source;
343 ResourceName name;
344 ConfigDescription config;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700345 std::string extension;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800346};
347
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700348bool compileXml(std::shared_ptr<Resolver> resolver, const CompileItem& item,
349 const Source& outputSource, std::queue<CompileItem>* queue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800350 std::ifstream in(item.source.path, std::ifstream::binary);
351 if (!in) {
352 Logger::error(item.source) << strerror(errno) << std::endl;
353 return false;
354 }
355
Adam Lesinski4d3a9872015-04-09 19:53:22 -0700356 std::shared_ptr<BindingXmlPullParser> binding;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800357 std::shared_ptr<XmlPullParser> xmlParser = std::make_shared<SourceXmlPullParser>(in);
Adam Lesinski4d3a9872015-04-09 19:53:22 -0700358 if (item.name.type == ResourceType::kLayout) {
359 binding = std::make_shared<BindingXmlPullParser>(xmlParser);
360 xmlParser = binding;
361 }
362
363 BigBuffer outBuffer(1024);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800364 XmlFlattener flattener(resolver);
365
366 // We strip attributes that do not belong in this version of the resource.
367 // Non-version qualified resources have an implicit version 1 requirement.
368 XmlFlattener::Options options = { item.config.sdkVersion ? item.config.sdkVersion : 1 };
369 Maybe<size_t> minStrippedSdk = flattener.flatten(item.source, xmlParser, &outBuffer, options);
370 if (!minStrippedSdk) {
371 return false;
372 }
373
374 if (minStrippedSdk.value() > 0) {
375 // Something was stripped, so let's generate a new file
376 // with the version of the smallest SDK version stripped.
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700377 CompileItem newWork = item;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800378 newWork.config.sdkVersion = minStrippedSdk.value();
379 queue->push(newWork);
380 }
381
382 std::ofstream out(outputSource.path, std::ofstream::binary);
383 if (!out) {
384 Logger::error(outputSource) << strerror(errno) << std::endl;
385 return false;
386 }
387
388 if (!util::writeAll(out, outBuffer)) {
389 Logger::error(outputSource) << strerror(errno) << std::endl;
390 return false;
391 }
Adam Lesinski4d3a9872015-04-09 19:53:22 -0700392
393 if (binding) {
394 // We generated a binding xml file, write it out beside the output file.
395 Source bindingOutput = outputSource;
396 bindingOutput.path += ".bind.xml";
397 std::ofstream bout(bindingOutput.path);
398 if (!bout) {
399 Logger::error(bindingOutput) << strerror(errno) << std::endl;
400 return false;
401 }
402
403 if (!binding->writeToFile(bout)) {
404 Logger::error(bindingOutput) << strerror(errno) << std::endl;
405 return false;
406 }
407 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800408 return true;
409}
410
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700411bool compilePng(const Source& source, const Source& output) {
412 std::ifstream in(source.path, std::ifstream::binary);
413 if (!in) {
414 Logger::error(source) << strerror(errno) << std::endl;
415 return false;
416 }
417
418 std::ofstream out(output.path, std::ofstream::binary);
419 if (!out) {
420 Logger::error(output) << strerror(errno) << std::endl;
421 return false;
422 }
423
424 std::string err;
425 Png png;
426 if (!png.process(source, in, out, {}, &err)) {
427 Logger::error(source) << err << std::endl;
428 return false;
429 }
430 return true;
431}
432
433bool copyFile(const Source& source, const Source& output) {
434 std::ifstream in(source.path, std::ifstream::binary);
435 if (!in) {
436 Logger::error(source) << strerror(errno) << std::endl;
437 return false;
438 }
439
440 std::ofstream out(output.path, std::ofstream::binary);
441 if (!out) {
442 Logger::error(output) << strerror(errno) << std::endl;
443 return false;
444 }
445
446 if (out << in.rdbuf()) {
447 Logger::error(output) << strerror(errno) << std::endl;
448 return true;
449 }
450 return false;
451}
452
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800453struct AaptOptions {
454 enum class Phase {
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700455 Full,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800456 Collect,
457 Link,
458 Compile,
459 };
460
461 // The phase to process.
462 Phase phase;
463
464 // Details about the app.
465 AppInfo appInfo;
466
467 // The location of the manifest file.
468 Source manifest;
469
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700470 // The source directories to walk and find resource files.
471 std::vector<Source> sourceDirs;
472
473 // The resource files to process and collect.
474 std::vector<Source> collectFiles;
475
476 // The binary table files to link.
477 std::vector<Source> linkFiles;
478
479 // The resource files to compile.
480 std::vector<Source> compileFiles;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481
482 // The libraries these files may reference.
483 std::vector<Source> libraries;
484
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700485 // Output path. This can be a directory or file
486 // depending on the phase.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800487 Source output;
488
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700489 // Directory to in which to generate R.java.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800490 Maybe<Source> generateJavaClass;
491
492 // Whether to output verbose details about
493 // compilation.
494 bool verbose = false;
495};
496
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700497bool compileAndroidManifest(const std::shared_ptr<Resolver>& resolver,
498 const AaptOptions& options) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800499 Source outSource = options.output;
500 appendPath(&outSource.path, "AndroidManifest.xml");
501
502 if (options.verbose) {
503 Logger::note(outSource) << "compiling AndroidManifest.xml." << std::endl;
504 }
505
506 std::ifstream in(options.manifest.path, std::ifstream::binary);
507 if (!in) {
508 Logger::error(options.manifest) << strerror(errno) << std::endl;
509 return false;
510 }
511
512 BigBuffer outBuffer(1024);
513 std::shared_ptr<XmlPullParser> xmlParser = std::make_shared<SourceXmlPullParser>(in);
514 XmlFlattener flattener(resolver);
515
516 Maybe<size_t> result = flattener.flatten(options.manifest, xmlParser, &outBuffer,
517 XmlFlattener::Options{});
518 if (!result) {
519 return false;
520 }
521
522 std::unique_ptr<uint8_t[]> data = std::unique_ptr<uint8_t[]>(new uint8_t[outBuffer.size()]);
523 uint8_t* p = data.get();
524 for (const auto& b : outBuffer) {
525 memcpy(p, b.buffer.get(), b.size);
526 p += b.size;
527 }
528
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700529 android::ResXMLTree tree;
530 if (tree.setTo(data.get(), outBuffer.size()) != android::NO_ERROR) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800531 return false;
532 }
533
534 ManifestValidator validator(resolver->getResTable());
535 if (!validator.validate(options.manifest, &tree)) {
536 return false;
537 }
538
539 std::ofstream out(outSource.path, std::ofstream::binary);
540 if (!out) {
541 Logger::error(outSource) << strerror(errno) << std::endl;
542 return false;
543 }
544
545 if (!util::writeAll(out, outBuffer)) {
546 Logger::error(outSource) << strerror(errno) << std::endl;
547 return false;
548 }
549 return true;
550}
551
552bool loadAppInfo(const Source& source, AppInfo* outInfo) {
553 std::ifstream ifs(source.path, std::ifstream::in | std::ifstream::binary);
554 if (!ifs) {
555 Logger::error(source) << strerror(errno) << std::endl;
556 return false;
557 }
558
559 ManifestParser parser;
560 std::shared_ptr<XmlPullParser> pullParser = std::make_shared<SourceXmlPullParser>(ifs);
561 return parser.parse(source, pullParser, outInfo);
562}
563
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700564static AaptOptions prepareArgs(int argc, char** argv) {
565 if (argc < 2) {
566 std::cerr << "no command specified." << std::endl;
567 exit(1);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800568 }
569
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700570 const StringPiece command(argv[1]);
571 argc -= 2;
572 argv += 2;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800573
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700574 AaptOptions options;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800575
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700576 StringPiece outputDescription = "place output in file";
577 if (command == "package") {
578 options.phase = AaptOptions::Phase::Full;
579 outputDescription = "place output in directory";
580 } else if (command == "collect") {
581 options.phase = AaptOptions::Phase::Collect;
582 } else if (command == "link") {
583 options.phase = AaptOptions::Phase::Link;
584 } else if (command == "compile") {
585 options.phase = AaptOptions::Phase::Compile;
586 outputDescription = "place output in directory";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800587 } else {
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700588 std::cerr << "invalid command '" << command << "'." << std::endl;
589 exit(1);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800590 }
591
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700592 if (options.phase == AaptOptions::Phase::Full) {
593 flag::requiredFlag("-S", "add a directory in which to find resources",
594 [&options](const StringPiece& arg) {
595 options.sourceDirs.push_back(Source{ arg.toString() });
596 });
597
598 flag::requiredFlag("-M", "path to AndroidManifest.xml",
599 [&options](const StringPiece& arg) {
600 options.manifest = Source{ arg.toString() };
601 });
602
603 flag::optionalFlag("-I", "add an Android APK to link against",
604 [&options](const StringPiece& arg) {
605 options.libraries.push_back(Source{ arg.toString() });
606 });
607
608 flag::optionalFlag("--java", "directory in which to generate R.java",
609 [&options](const StringPiece& arg) {
610 options.generateJavaClass = Source{ arg.toString() };
611 });
612
613 } else {
614 flag::requiredFlag("--package", "Android package name",
615 [&options](const StringPiece& arg) {
616 options.appInfo.package = util::utf8ToUtf16(arg);
617 });
618
619 if (options.phase != AaptOptions::Phase::Collect) {
620 flag::optionalFlag("-I", "add an Android APK to link against",
621 [&options](const StringPiece& arg) {
622 options.libraries.push_back(Source{ arg.toString() });
623 });
624 }
625
626 if (options.phase == AaptOptions::Phase::Link) {
627 flag::optionalFlag("--java", "directory in which to generate R.java",
628 [&options](const StringPiece& arg) {
629 options.generateJavaClass = Source{ arg.toString() };
630 });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800631 }
632 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800633
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700634 // Common flags for all steps.
635 flag::requiredFlag("-o", outputDescription, [&options](const StringPiece& arg) {
636 options.output = Source{ arg.toString() };
637 });
638 flag::optionalSwitch("-v", "enables verbose logging", &options.verbose);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800639
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700640 // Build the command string for output (eg. "aapt2 compile").
641 std::string fullCommand = "aapt2";
642 fullCommand += " ";
643 fullCommand += command.toString();
644
645 // Actually read the command line flags.
646 flag::parse(argc, argv, fullCommand);
647
648 // Copy all the remaining arguments.
649 if (options.phase == AaptOptions::Phase::Collect) {
650 for (const std::string& arg : flag::getArgs()) {
651 options.collectFiles.push_back(Source{ arg });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800652 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700653 } else if (options.phase == AaptOptions::Phase::Compile) {
654 for (const std::string& arg : flag::getArgs()) {
655 options.compileFiles.push_back(Source{ arg });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800656 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700657 } else if (options.phase == AaptOptions::Phase::Link) {
658 for (const std::string& arg : flag::getArgs()) {
659 options.linkFiles.push_back(Source{ arg });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800660 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800661 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700662 return options;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800663}
664
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700665static bool collectValues(const std::shared_ptr<ResourceTable>& table, const Source& source,
666 const ConfigDescription& config) {
667 std::ifstream in(source.path, std::ifstream::binary);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800668 if (!in) {
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700669 Logger::error(source) << strerror(errno) << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800670 return false;
671 }
672
673 std::shared_ptr<XmlPullParser> xmlParser = std::make_shared<SourceXmlPullParser>(in);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700674 ResourceParser parser(table, source, config, xmlParser);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800675 return parser.parse();
676}
677
678struct ResourcePathData {
679 std::u16string resourceDir;
680 std::u16string name;
681 std::string extension;
682 ConfigDescription config;
683};
684
685/**
686 * Resource file paths are expected to look like:
687 * [--/res/]type[-config]/name
688 */
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700689static Maybe<ResourcePathData> extractResourcePathData(const Source& source) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800690 std::vector<std::string> parts = util::splitAndLowercase(source.path, '/');
691 if (parts.size() < 2) {
692 Logger::error(source) << "bad resource path." << std::endl;
693 return {};
694 }
695
696 std::string& dir = parts[parts.size() - 2];
697 StringPiece dirStr = dir;
698
699 ConfigDescription config;
700 size_t dashPos = dir.find('-');
701 if (dashPos != std::string::npos) {
702 StringPiece configStr = dirStr.substr(dashPos + 1, dir.size() - (dashPos + 1));
703 if (!ConfigDescription::parse(configStr, &config)) {
704 Logger::error(source)
705 << "invalid configuration '"
706 << configStr
707 << "'."
708 << std::endl;
709 return {};
710 }
711 dirStr = dirStr.substr(0, dashPos);
712 }
713
714 std::string& filename = parts[parts.size() - 1];
715 StringPiece name = filename;
716 StringPiece extension;
717 size_t dotPos = filename.find('.');
718 if (dotPos != std::string::npos) {
719 extension = name.substr(dotPos + 1, filename.size() - (dotPos + 1));
720 name = name.substr(0, dotPos);
721 }
722
723 return ResourcePathData{
724 util::utf8ToUtf16(dirStr),
725 util::utf8ToUtf16(name),
726 extension.toString(),
727 config
728 };
729}
730
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700731bool doAll(AaptOptions* options, const std::shared_ptr<ResourceTable>& table,
732 const std::shared_ptr<Resolver>& resolver) {
733 const bool versionStyles = (options->phase == AaptOptions::Phase::Full ||
734 options->phase == AaptOptions::Phase::Link);
735 const bool verifyNoMissingSymbols = (options->phase == AaptOptions::Phase::Full ||
736 options->phase == AaptOptions::Phase::Link);
737 const bool compileFiles = (options->phase == AaptOptions::Phase::Full ||
738 options->phase == AaptOptions::Phase::Compile);
739 const bool flattenTable = (options->phase == AaptOptions::Phase::Full ||
740 options->phase == AaptOptions::Phase::Collect ||
741 options->phase == AaptOptions::Phase::Link);
742 const bool useExtendedChunks = options->phase == AaptOptions::Phase::Collect;
743
744 // Build the output table path.
745 Source outputTable = options->output;
746 if (options->phase == AaptOptions::Phase::Full) {
747 appendPath(&outputTable.path, "resources.arsc");
748 }
749
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800750 bool error = false;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700751 std::queue<CompileItem> compileQueue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800752
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700753 // If source directories were specified, walk them looking for resource files.
754 if (!options->sourceDirs.empty()) {
755 const char* customIgnore = getenv("ANDROID_AAPT_IGNORE");
756 FileFilter fileFilter;
757 if (customIgnore && customIgnore[0]) {
758 fileFilter.setPattern(customIgnore);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800759 } else {
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700760 fileFilter.setPattern(
761 "!.svn:!.git:!.ds_store:!*.scc:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800762 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800763
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700764 for (const Source& source : options->sourceDirs) {
765 if (!walkTree(source, fileFilter, &options->collectFiles)) {
766 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800767 }
768 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800769 }
770
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700771 // Load all binary resource tables.
772 for (const Source& source : options->linkFiles) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800773 error |= !loadBinaryResourceTable(table, source);
774 }
775
776 if (error) {
777 return false;
778 }
779
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700780 // Collect all the resource files.
781 // Need to parse the resource type/config/filename.
782 for (const Source& source : options->collectFiles) {
783 Maybe<ResourcePathData> maybePathData = extractResourcePathData(source);
784 if (!maybePathData) {
785 return false;
786 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800787
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700788 const ResourcePathData& pathData = maybePathData.value();
789 if (pathData.resourceDir == u"values") {
790 if (options->verbose) {
791 Logger::note(source) << "collecting values..." << std::endl;
792 }
793
794 error |= !collectValues(table, source, pathData.config);
795 continue;
796 }
797
798 const ResourceType* type = parseResourceType(pathData.resourceDir);
799 if (!type) {
800 Logger::error(source) << "invalid resource type '" << pathData.resourceDir << "'."
801 << std::endl;
802 return false;
803 }
804
805 ResourceName resourceName = { table->getPackage(), *type, pathData.name };
806
807 // Add the file name to the resource table.
808 std::unique_ptr<FileReference> fileReference = makeFileReference(
809 table->getValueStringPool(),
810 util::utf16ToUtf8(pathData.name) + "." + pathData.extension,
811 *type, pathData.config);
812 error |= !table->addResource(resourceName, pathData.config, source.line(0),
813 std::move(fileReference));
814
815 if (pathData.extension == "xml") {
816 error |= !collectXml(table, source, resourceName, pathData.config);
817 }
818
819 compileQueue.push(
820 CompileItem{ source, resourceName, pathData.config, pathData.extension });
821 }
822
823 if (error) {
824 return false;
825 }
826
827 // Version all styles referencing attributes outside of their specified SDK version.
828 if (versionStyles) {
829 versionStylesForCompat(table);
830 }
831
832 // Verify that all references are valid.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800833 Linker linker(table, resolver);
834 if (!linker.linkAndValidate()) {
835 return false;
836 }
837
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700838 // Verify that all symbols exist.
839 if (verifyNoMissingSymbols) {
840 const auto& unresolvedRefs = linker.getUnresolvedReferences();
841 if (!unresolvedRefs.empty()) {
842 for (const auto& entry : unresolvedRefs) {
843 for (const auto& source : entry.second) {
844 Logger::error(source) << "unresolved symbol '" << entry.first << "'."
845 << std::endl;
846 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800847 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700848 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800849 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800850 }
851
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700852 // Compile files.
853 if (compileFiles) {
854 // First process any input compile files.
855 for (const Source& source : options->compileFiles) {
856 Maybe<ResourcePathData> maybePathData = extractResourcePathData(source);
857 if (!maybePathData) {
858 return false;
859 }
860
861 const ResourcePathData& pathData = maybePathData.value();
862 const ResourceType* type = parseResourceType(pathData.resourceDir);
863 if (!type) {
864 Logger::error(source) << "invalid resource type '" << pathData.resourceDir
865 << "'." << std::endl;
866 return false;
867 }
868
869 ResourceName resourceName = { table->getPackage(), *type, pathData.name };
870 compileQueue.push(
871 CompileItem{ source, resourceName, pathData.config, pathData.extension });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800872 }
873
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700874 // Now process the actual compile queue.
875 for (; !compileQueue.empty(); compileQueue.pop()) {
876 const CompileItem& item = compileQueue.front();
877
878 // Create the output directory path from the resource type and config.
879 std::stringstream outputPath;
880 outputPath << item.name.type;
881 if (item.config != ConfigDescription{}) {
882 outputPath << "-" << item.config.toString();
883 }
884
885 Source outSource = options->output;
886 appendPath(&outSource.path, "res");
887 appendPath(&outSource.path, outputPath.str());
888
889 // Make the directory.
890 if (!mkdirs(outSource.path)) {
891 Logger::error(outSource) << strerror(errno) << std::endl;
892 return false;
893 }
894
895 // Add the file name to the directory path.
896 appendPath(&outSource.path, util::utf16ToUtf8(item.name.entry) + "." + item.extension);
897
898 if (item.extension == "xml") {
899 if (options->verbose) {
900 Logger::note(outSource) << "compiling XML file." << std::endl;
901 }
902
903 error |= !compileXml(resolver, item, outSource, &compileQueue);
904 } else if (item.extension == "png" || item.extension == "9.png") {
905 if (options->verbose) {
906 Logger::note(outSource) << "compiling png file." << std::endl;
907 }
908
909 error |= !compilePng(item.source, outSource);
910 } else {
911 error |= !copyFile(item.source, outSource);
912 }
913 }
914
915 if (error) {
916 return false;
917 }
918 }
919
920 // Compile and validate the AndroidManifest.xml.
921 if (!options->manifest.path.empty()) {
922 if (!compileAndroidManifest(resolver, *options)) {
923 return false;
924 }
925 }
926
927 // Generate the Java class file.
928 if (options->generateJavaClass) {
929 Source outPath = options->generateJavaClass.value();
930 if (options->verbose) {
931 Logger::note() << "writing symbols to " << outPath << "." << std::endl;
932 }
933
934 // Build the output directory from the package name.
935 // Eg. com.android.app -> com/android/app
936 const std::string packageUtf8 = util::utf16ToUtf8(table->getPackage());
937 for (StringPiece part : util::tokenize<char>(packageUtf8, '.')) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800938 appendPath(&outPath.path, part);
939 }
940
941 if (!mkdirs(outPath.path)) {
942 Logger::error(outPath) << strerror(errno) << std::endl;
943 return false;
944 }
945
946 appendPath(&outPath.path, "R.java");
947
948 std::ofstream fout(outPath.path);
949 if (!fout) {
950 Logger::error(outPath) << strerror(errno) << std::endl;
951 return false;
952 }
953
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700954 JavaClassGenerator generator(table, {});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800955 if (!generator.generate(fout)) {
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700956 Logger::error(outPath) << generator.getError() << "." << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800957 return false;
958 }
959 }
960
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700961 // Flatten the resource table.
962 if (flattenTable && table->begin() != table->end()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800963 BigBuffer buffer(1024);
964 TableFlattener::Options tableOptions;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700965 tableOptions.useExtendedChunks = useExtendedChunks;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800966 TableFlattener flattener(tableOptions);
967 if (!flattener.flatten(&buffer, *table)) {
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700968 Logger::error() << "failed to flatten resource table." << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800969 return false;
970 }
971
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700972 if (options->verbose) {
973 Logger::note() << "Final resource table size=" << util::formatSize(buffer.size())
974 << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800975 }
976
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700977 std::ofstream fout(outputTable.path, std::ofstream::binary);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800978 if (!fout) {
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700979 Logger::error(outputTable) << strerror(errno) << "." << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800980 return false;
981 }
982
983 if (!util::writeAll(fout, buffer)) {
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700984 Logger::error(outputTable) << strerror(errno) << "." << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800985 return false;
986 }
987 fout.flush();
988 }
989 return true;
990}
991
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800992int main(int argc, char** argv) {
993 Logger::setLog(std::make_shared<Log>(std::cerr, std::cerr));
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700994 AaptOptions options = prepareArgs(argc, argv);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800995
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700996 // If we specified a manifest, go ahead and load the package name from the manifest.
997 if (!options.manifest.path.empty()) {
998 if (!loadAppInfo(options.manifest, &options.appInfo)) {
999 return false;
1000 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001001 }
1002
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001003 // Verify we have some common options set.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001004 if (options.appInfo.package.empty()) {
1005 Logger::error() << "no package name specified." << std::endl;
1006 return false;
1007 }
1008
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001009 // Every phase needs a resource table.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001010 std::shared_ptr<ResourceTable> table = std::make_shared<ResourceTable>();
1011 table->setPackage(options.appInfo.package);
1012 if (options.appInfo.package == u"android") {
1013 table->setPackageId(0x01);
1014 } else {
1015 table->setPackageId(0x7f);
1016 }
1017
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001018 // Load the included libraries.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001019 std::shared_ptr<android::AssetManager> libraries = std::make_shared<android::AssetManager>();
1020 for (const Source& source : options.libraries) {
Adam Lesinski4d3a9872015-04-09 19:53:22 -07001021 if (util::stringEndsWith<char>(source.path, ".arsc")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001022 // We'll process these last so as to avoid a cookie issue.
1023 continue;
1024 }
1025
1026 int32_t cookie;
1027 if (!libraries->addAssetPath(android::String8(source.path.data()), &cookie)) {
1028 Logger::error(source) << "failed to load library." << std::endl;
1029 return false;
1030 }
1031 }
1032
1033 for (const Source& source : options.libraries) {
Adam Lesinski4d3a9872015-04-09 19:53:22 -07001034 if (!util::stringEndsWith<char>(source.path, ".arsc")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001035 // We've already processed this.
1036 continue;
1037 }
1038
1039 // Dirty hack but there is no other way to get a
1040 // writeable ResTable.
1041 if (!loadResTable(const_cast<android::ResTable*>(&libraries->getResources(false)),
1042 source)) {
1043 return false;
1044 }
1045 }
1046
1047 // Make the resolver that will cache IDs for us.
1048 std::shared_ptr<Resolver> resolver = std::make_shared<Resolver>(table, libraries);
1049
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001050 // Do the work.
1051 if (!doAll(&options, table, resolver)) {
1052 Logger::error() << "aapt exiting with failures." << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001053 return 1;
1054 }
1055 return 0;
1056}