blob: 8accfa84c0a6c8567960f7b60539d0e8244723c5 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include <sys/stat.h>
18
19#include <fstream>
20#include <queue>
21#include <unordered_map>
22#include <vector>
23
24#include "android-base/errors.h"
25#include "android-base/file.h"
Adam Lesinskif34b6f42017-03-03 16:33:26 -080026#include "android-base/stringprintf.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080027#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028#include "google/protobuf/io/coded_stream.h"
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "AppInfo.h"
31#include "Debug.h"
32#include "Flags.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080033#include "Locale.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034#include "NameMangler.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080035#include "ResourceUtils.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070036#include "cmd/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037#include "compile/IdAssigner.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080038#include "filter/ConfigFilter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039#include "flatten/Archive.h"
40#include "flatten/TableFlattener.h"
41#include "flatten/XmlFlattener.h"
Adam Lesinski06460ef2017-03-14 18:52:13 -070042#include "io/BigBufferInputStream.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080043#include "io/FileSystem.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070044#include "io/Util.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080045#include "io/ZipArchive.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070046#include "java/JavaClassGenerator.h"
47#include "java/ManifestClassGenerator.h"
48#include "java/ProguardRules.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049#include "link/Linkers.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050#include "link/ManifestFixer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080051#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052#include "link/TableMerger.h"
Adam Lesinskid48944a2017-02-21 14:22:30 -080053#include "optimize/ResourceDeduper.h"
54#include "optimize/VersionCollapser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055#include "process/IResourceTableConsumer.h"
56#include "process/SymbolTable.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080057#include "proto/ProtoSerialize.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080058#include "split/TableSplitter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059#include "unflatten/BinaryResourceParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060#include "util/Files.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080061#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062
Adam Lesinskid5083f62017-01-16 15:07:21 -080063using android::StringPiece;
Adam Lesinskif34b6f42017-03-03 16:33:26 -080064using android::base::StringPrintf;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070065
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066namespace aapt {
67
68struct LinkOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 std::string output_path;
70 std::string manifest_path;
71 std::vector<std::string> include_paths;
72 std::vector<std::string> overlay_files;
Adam Lesinskib39ad7c2017-03-13 11:40:48 -070073 std::vector<std::string> assets_dirs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 bool output_to_directory = false;
75 bool auto_add_overlay = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 // Java/Proguard options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 Maybe<std::string> generate_java_class_path;
79 Maybe<std::string> custom_java_package;
80 std::set<std::string> extra_java_packages;
Adam Lesinski418763f2017-04-11 17:36:53 -070081 Maybe<std::string> generate_text_symbols_path;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 Maybe<std::string> generate_proguard_rules_path;
83 Maybe<std::string> generate_main_dex_proguard_rules_path;
84 bool generate_non_final_ids = false;
85 std::vector<std::string> javadoc_annotations;
86 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 // Optimizations/features.
89 bool no_auto_version = false;
90 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +090091 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 bool no_resource_deduping = false;
93 bool no_xml_namespaces = false;
94 bool do_not_compress_anything = false;
95 std::unordered_set<std::string> extensions_to_not_compress;
96
97 // Static lib options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 bool no_static_lib_packages = false;
99
100 // AndroidManifest.xml massaging options.
101 ManifestFixerOptions manifest_fixer_options;
102
103 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700105
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800106 // Flattening options.
107 TableFlattenerOptions table_flattener_options;
108
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 TableSplitterOptions table_splitter_options;
111 std::vector<SplitConstraints> split_constraints;
112 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700113
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 std::unordered_map<ResourceName, ResourceId> stable_id_map;
116 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117};
118
Adam Lesinski64587af2016-02-18 18:33:06 -0800119class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 public:
Chris Warrington820d72a2017-04-27 15:27:01 +0100121 LinkContext(IDiagnostics* diagnostics)
122 : diagnostics_(diagnostics), name_mangler_({}), symbols_(&name_mangler_) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700123 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124
Adam Lesinskib522f042017-04-21 16:57:59 -0700125 PackageType GetPackageType() override {
126 return package_type_;
127 }
128
129 void SetPackageType(PackageType type) {
130 package_type_ = type;
131 }
132
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700133 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100134 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700135 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700137 NameMangler* GetNameMangler() override {
138 return &name_mangler_;
139 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
142 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 const std::string& GetCompilationPackage() override {
146 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700148
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800150 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800152
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700153 uint8_t GetPackageId() override {
154 return package_id_;
155 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700157 void SetPackageId(uint8_t id) {
158 package_id_ = id;
159 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800160
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700161 SymbolTable* GetExternalSymbols() override {
162 return &symbols_;
163 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800164
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700165 bool IsVerbose() override {
166 return verbose_;
167 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800168
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700169 void SetVerbose(bool val) {
170 verbose_ = val;
171 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800172
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700173 int GetMinSdkVersion() override {
174 return min_sdk_version_;
175 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700176
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700177 void SetMinSdkVersion(int minSdk) {
178 min_sdk_version_ = minSdk;
179 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700180
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 DISALLOW_COPY_AND_ASSIGN(LinkContext);
183
Adam Lesinskib522f042017-04-21 16:57:59 -0700184 PackageType package_type_ = PackageType::kApp;
Chris Warrington820d72a2017-04-27 15:27:01 +0100185 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 NameMangler name_mangler_;
187 std::string compilation_package_;
188 uint8_t package_id_ = 0x0;
189 SymbolTable symbols_;
190 bool verbose_ = false;
191 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700192};
193
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700194// A custom delegate that generates compatible pre-O IDs for use with feature splits.
195// Feature splits use package IDs > 7f, which in Java (since Java doesn't have unsigned ints)
196// is interpreted as a negative number. Some verification was wrongly assuming negative values
197// were invalid.
198//
199// This delegate will attempt to masquerade any '@id/' references with ID 0xPPTTEEEE,
200// where PP > 7f, as 0x7fPPEEEE. Any potential overlapping is verified and an error occurs if such
201// an overlap exists.
202class FeatureSplitSymbolTableDelegate : public DefaultSymbolTableDelegate {
203 public:
204 FeatureSplitSymbolTableDelegate(IAaptContext* context) : context_(context) {
205 }
206
207 virtual ~FeatureSplitSymbolTableDelegate() = default;
208
209 virtual std::unique_ptr<SymbolTable::Symbol> FindByName(
210 const ResourceName& name,
211 const std::vector<std::unique_ptr<ISymbolSource>>& sources) override {
212 std::unique_ptr<SymbolTable::Symbol> symbol =
213 DefaultSymbolTableDelegate::FindByName(name, sources);
214 if (symbol == nullptr) {
215 return {};
216 }
217
218 // Check to see if this is an 'id' with the target package.
219 if (name.type == ResourceType::kId && symbol->id) {
220 ResourceId* id = &symbol->id.value();
221 if (id->package_id() > kAppPackageId) {
222 // Rewrite the resource ID to be compatible pre-O.
223 ResourceId rewritten_id(kAppPackageId, id->package_id(), id->entry_id());
224
225 // Check that this doesn't overlap another resource.
226 if (DefaultSymbolTableDelegate::FindById(rewritten_id, sources) != nullptr) {
227 // The ID overlaps, so log a message (since this is a weird failure) and fail.
228 context_->GetDiagnostics()->Error(DiagMessage() << "Failed to rewrite " << name
229 << " for pre-O feature split support");
230 return {};
231 }
232
233 if (context_->IsVerbose()) {
234 context_->GetDiagnostics()->Note(DiagMessage() << "rewriting " << name << " (" << *id
235 << ") -> (" << rewritten_id << ")");
236 }
237
238 *id = rewritten_id;
239 }
240 }
241 return symbol;
242 }
243
244 private:
245 DISALLOW_COPY_AND_ASSIGN(FeatureSplitSymbolTableDelegate);
246
247 IAaptContext* context_;
248};
249
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700251 Maybe<size_t> max_sdk_level, bool keep_raw_values, IArchiveWriter* writer,
252 IAaptContext* context) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 BigBuffer buffer(1024);
254 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 options.keep_raw_values = keep_raw_values;
256 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800259 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 }
261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 DiagMessage msg;
264 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 if (max_sdk_level) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700266 msg << " maxSdkLevel=" << max_sdk_level.value() << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 }
270
Adam Lesinski06460ef2017-03-14 18:52:13 -0700271 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700272 return io::CopyInputStreamToArchive(context, &input_stream, path.to_string(),
273 ArchiveEntry::kCompress, writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800274}
275
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700276static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source, const void* data,
277 size_t len, IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 pb::ResourceTable pb_table;
279 if (!pb_table.ParseFromArray(data, len)) {
280 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 return {};
282 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800283
Adam Lesinski06460ef2017-03-14 18:52:13 -0700284 std::unique_ptr<ResourceTable> table = DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 if (!table) {
286 return {};
287 }
288 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800289}
290
291/**
292 * Inflates an XML file from the source path.
293 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700294static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 std::ifstream fin(path, std::ifstream::binary);
296 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 return {};
299 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800301}
302
Adam Lesinski355f2852016-02-13 20:26:45 -0800303struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 bool no_auto_version = false;
305 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900306 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 bool no_xml_namespaces = false;
308 bool keep_raw_values = false;
309 bool do_not_compress_anything = false;
310 bool update_proguard_spec = false;
311 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800312};
313
314class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 public:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700316 ResourceFileFlattener(const ResourceFileFlattenerOptions& options, IAaptContext* context,
317 proguard::KeepSet* keep_set)
318 : options_(options), context_(context), keep_set_(keep_set) {
319 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800320
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800322
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 private:
324 struct FileOperation {
325 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700326
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 // The entry this file came from.
328 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700329
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700332
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700335
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 std::string dst_path;
338 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800340
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800342
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
344 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800345
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 ResourceFileFlattenerOptions options_;
347 IAaptContext* context_;
348 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800349};
350
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
352 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 return 0;
354 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800355
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 for (const std::string& extension : options_.extensions_to_not_compress) {
357 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800359 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 }
361 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800362}
363
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900364static bool IsTransitionElement(const std::string& name) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700365 return name == "fade" || name == "changeBounds" || name == "slide" || name == "explode" ||
366 name == "changeImageTransform" || name == "changeTransform" ||
367 name == "changeClipBounds" || name == "autoTransition" || name == "recolor" ||
368 name == "changeScroll" || name == "transitionSet" || name == "transition" ||
369 name == "transitionManager";
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900370}
371
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700372bool ResourceFileFlattener::LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
373 std::queue<FileOperation>* out_file_op_queue) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 if (context_->IsVerbose()) {
378 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700379 }
380
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 XmlReferenceLinker xml_linker;
382 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383 return false;
384 }
385
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700386 if (options_.update_proguard_spec && !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 return false;
388 }
389
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390 if (options_.no_xml_namespaces) {
391 XmlNamespaceRemover namespace_remover;
392 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800394 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800396
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 if (!options_.no_auto_version) {
398 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400 xml::Element* el = xml::FindRootElement(doc);
401 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 if (el->name == "vector" || el->name == "animated-vector") {
403 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700406 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700408 }
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900409 if (options_.no_version_transitions) {
410 // Skip this if it is a transition resource.
411 xml::Element* el = xml::FindRootElement(doc);
412 if (el && el->namespace_uri.empty()) {
413 if (IsTransitionElement(el->name)) {
414 // We are NOT going to version this file.
415 file_op->skip_version = true;
416 return true;
417 }
418 }
419 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700420
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422
423 // Find the first SDK level used that is higher than this defined config and
424 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 const int min_sdk_version = context_->GetMinSdkVersion();
426 for (int sdk_level : xml_linker.sdk_levels()) {
427 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700428 if (!ShouldGenerateVersionedResource(file_op->entry, config, sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 // If we shouldn't generate a versioned resource, stop checking.
430 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800431 }
432
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 ResourceFile versioned_file_desc = doc->file;
434 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700435
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436 FileOperation new_file_op;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700437 new_file_op.xml_to_flatten = util::make_unique<xml::XmlResource>(
438 versioned_file_desc, StringPool{}, doc->root->Clone());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 new_file_op.config = versioned_file_desc.config;
440 new_file_op.entry = file_op->entry;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700441 new_file_op.dst_path =
442 ResourceUtils::BuildResourceFileName(versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800443
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700445 context_->GetDiagnostics()->Note(DiagMessage(versioned_file_desc.source)
446 << "auto-versioning resource from config '" << config
447 << "' -> '" << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800448 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 bool added = table->AddFileReferenceAllowMangled(
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700451 versioned_file_desc.name, versioned_file_desc.config, versioned_file_desc.source,
452 new_file_op.dst_path, nullptr, context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 if (!added) {
454 return false;
455 }
456
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 break;
459 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800460 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 }
462 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800463}
464
465/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 * Do not insert or remove any resources while executing in this function. It
467 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800468 * corrupt the iteration order.
469 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700470bool ResourceFileFlattener::Flatten(ResourceTable* table, IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 bool error = false;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700472 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation> config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800473
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 for (auto& pkg : table->packages) {
475 for (auto& type : pkg->types) {
476 // Sort by config and name, so that we get better locality in the zip
477 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 config_sorted_files.clear();
479 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800480
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 // Populate the queue with all files in the ResourceTable.
482 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 for (auto& config_value : entry->values) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700484 FileReference* file_ref = ValueCast<FileReference>(config_value->value.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700485 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700486 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700488
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700493 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 }
495
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 FileOperation file_op;
497 file_op.entry = entry.get();
498 file_op.dst_path = *file_ref->path;
499 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700501 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 if (type->type != ResourceType::kRaw &&
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700503 (util::EndsWith(src_path, ".xml.flat") || util::EndsWith(src_path, ".xml"))) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 << "failed to open file");
508 return false;
509 }
510
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700511 file_op.xml_to_flatten = xml::Inflate(data->data(), data->size(),
512 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700513
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700514 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 return false;
516 }
517
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700518 file_op.xml_to_flatten->file.config = config_value->config;
519 file_op.xml_to_flatten->file.source = file_ref->GetSource();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700520 file_op.xml_to_flatten->file.name = ResourceName(pkg->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521
522 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700525 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526
527 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 // else we end up copying the string in the std::make_pair() method,
529 // then creating a StringPiece from the copy, which would cause us
530 // to end up referencing garbage in the map.
531 const StringPiece entry_name(entry->name);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700532 config_sorted_files[std::make_pair(config_value->config, entry_name)] =
533 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700534 }
535 }
536 }
537
538 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700539 for (; !file_operations.empty(); file_operations.pop()) {
540 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 error = true;
544 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700545 }
546
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
548 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 // creating a StringPiece from the copy, which would cause us to end up
550 // referencing garbage in the map.
551 const StringPiece entry_name(file_op.entry->name);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700552 config_sorted_files[std::make_pair(file_op.config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 }
554
555 if (error) {
556 return false;
557 }
558
559 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 for (auto& map_entry : config_sorted_files) {
561 const ConfigDescription& config = map_entry.first.first;
562 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 if (file_op.xml_to_flatten) {
565 Maybe<size_t> max_sdk_level;
566 if (!options_.no_auto_version && !file_op.skip_version) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700567 max_sdk_level = std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
568 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 }
570
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700571 bool result = FlattenXml(file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
572 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 if (!result) {
574 error = true;
575 }
576 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700577 bool result =
578 io::CopyFileToArchive(context_, file_op.file_to_copy, file_op.dst_path,
579 GetCompressionFlags(file_op.dst_path), archive_writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 if (!result) {
581 error = true;
582 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700583 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700585 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586 }
587 return !error;
588}
589
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700590static bool WriteStableIdMapToPath(IDiagnostics* diag,
591 const std::unordered_map<ResourceName, ResourceId>& id_map,
592 const std::string& id_map_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700595 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 return false;
597 }
598
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 const ResourceName& name = entry.first;
601 const ResourceId& id = entry.second;
602 fout << name << " = " << id << "\n";
603 }
604
605 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700606 diag->Error(DiagMessage(id_map_path) << "failed writing to file: "
607 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 return false;
609 }
610
611 return true;
612}
613
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700614static bool LoadStableIdMap(IDiagnostics* diag, const std::string& path,
615 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 std::string content;
617 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 return false;
620 }
621
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622 out_id_map->clear();
623 size_t line_no = 0;
624 for (StringPiece line : util::Tokenize(content, '\n')) {
625 line_no++;
626 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627 if (line.empty()) {
628 continue;
629 }
630
631 auto iter = std::find(line.begin(), line.end(), '=');
632 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700634 return false;
635 }
636
637 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700638 StringPiece res_name_str =
639 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
640 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700641 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource name '" << res_name_str
642 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 return false;
644 }
645
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700646 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
647 const size_t res_id_str_len = line.size() - res_id_start_idx;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700648 StringPiece res_id_str = util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700650 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
651 if (!maybe_id) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700652 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '" << res_id_str
653 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700654 return false;
655 }
656
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 }
659 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700660}
661
Adam Lesinskifb48d292015-11-07 15:52:13 -0800662class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 public:
664 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 : options_(options),
666 context_(context),
667 final_table_(),
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700668 file_collection_(util::make_unique<io::FileCollection>()) {
669 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700670
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 /**
672 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 bool LoadSymbolsFromIncludePaths() {
676 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678 for (const std::string& path : options_.include_paths) {
679 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700680 context_->GetDiagnostics()->Note(DiagMessage(path) << "loading include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700681 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700682
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700684 std::string error_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800685 std::unique_ptr<ResourceTable> include_static = LoadStaticLibrary(path, &error_str);
686 if (include_static) {
Adam Lesinskib522f042017-04-21 16:57:59 -0700687 if (context_->GetPackageType() != PackageType::kStaticLib) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800688 // Can't include static libraries when not building a static library (they have no IDs
689 // assigned).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700690 context_->GetDiagnostics()->Error(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800691 DiagMessage(path) << "can't include static library when not building a static lib");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700692 return false;
693 }
694
695 // If we are using --no-static-lib-packages, we need to rename the
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800696 // package of this table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700697 if (options_.no_static_lib_packages) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800698 // Since package names can differ, and multiple packages can exist in a ResourceTable,
699 // we place the requirement that all static libraries are built with the package
700 // ID 0x7f. So if one is not found, this is an error.
701 if (ResourceTablePackage* pkg = include_static->FindPackageById(kAppPackageId)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700702 pkg->name = context_->GetCompilationPackage();
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800703 } else {
704 context_->GetDiagnostics()->Error(DiagMessage(path)
705 << "no package with ID 0x7f found in static library");
706 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 }
708 }
709
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700710 context_->GetExternalSymbols()->AppendSource(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800711 util::make_unique<ResourceTableSymbolSource>(include_static.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700712
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800713 static_table_includes_.push_back(std::move(include_static));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700715 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718 return false;
719 }
720
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700721 if (!asset_source->AddAssetPath(path)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800722 context_->GetDiagnostics()->Error(DiagMessage(path) << "failed to load include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 return false;
724 }
725 }
726
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800727 // Capture the shared libraries so that the final resource table can be properly flattened
728 // with support for shared libraries.
729 for (auto& entry : asset_source->GetAssignedPackageIds()) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800730 if (entry.first > kFrameworkPackageId && entry.first < kAppPackageId) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800731 final_table_.included_packages_[entry.first] = entry.second;
732 }
733 }
734
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700735 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700736 return true;
737 }
738
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800739 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800741 xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
742 if (manifest_el == nullptr) {
743 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700744 }
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800745
746 AppInfo app_info;
747
748 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
749 diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>");
750 return {};
751 }
752
753 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
754 if (!package_attr) {
755 diag->Error(DiagMessage(xml_res->file.source)
756 << "<manifest> must have a 'package' attribute");
757 return {};
758 }
759 app_info.package = package_attr->value;
760
761 if (xml::Attribute* version_code_attr =
762 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
763 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_attr->value);
764 if (!maybe_code) {
765 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
766 << "invalid android:versionCode '" << version_code_attr->value << "'");
767 return {};
768 }
769 app_info.version_code = maybe_code.value();
770 }
771
772 if (xml::Attribute* revision_code_attr =
773 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
774 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(revision_code_attr->value);
775 if (!maybe_code) {
776 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
777 << "invalid android:revisionCode '" << revision_code_attr->value << "'");
778 return {};
779 }
780 app_info.revision_code = maybe_code.value();
781 }
782
783 if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
784 if (!split_name_attr->value.empty()) {
785 app_info.split_name = split_name_attr->value;
786 }
787 }
788
789 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
790 if (xml::Attribute* min_sdk =
791 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700792 app_info.min_sdk_version = ResourceUtils::ParseSdkVersion(min_sdk->value);
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800793 }
794 }
795 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700796 }
797
798 /**
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800799 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700800 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700801 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700802 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700803 bool VerifyNoExternalPackages() {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700804 auto is_ext_package_func = [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700805 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
806 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700807 };
808
809 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700810 for (const auto& package : final_table_.packages) {
811 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700812 // We have a package that is not related to the one we're building!
813 for (const auto& type : package->types) {
814 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700815 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700816
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700817 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700818 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700819 // for the 'android' package. This is due to legacy reasons.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700820 if (ValueCast<Id>(config_value->value.get()) && package->name == "android") {
821 context_->GetDiagnostics()->Warn(DiagMessage(config_value->value->GetSource())
822 << "generated id '" << res_name
823 << "' for external package '" << package->name
824 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700825 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700826 context_->GetDiagnostics()->Error(DiagMessage(config_value->value->GetSource())
827 << "defined resource '" << res_name
828 << "' for external package '" << package->name
829 << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700830 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700831 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700832 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 }
834 }
835 }
836 }
837
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700838 auto new_end_iter = std::remove_if(final_table_.packages.begin(), final_table_.packages.end(),
839 is_ext_package_func);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700840 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700841 return !error;
842 }
843
844 /**
845 * Returns true if no IDs have been set, false otherwise.
846 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700847 bool VerifyNoIdsSet() {
848 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700849 for (const auto& type : package->types) {
850 if (type->id) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800851 context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID "
852 << StringPrintf("%02x", type->id.value())
853 << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700854 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700855 }
856
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857 for (const auto& entry : type->entries) {
858 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700859 ResourceNameRef res_name(package->name, type->type, entry->name);
860 context_->GetDiagnostics()->Error(
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800861 DiagMessage() << "entry " << res_name << " has ID "
862 << StringPrintf("%02x", entry->id.value()) << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700863 return false;
864 }
865 }
866 }
867 }
868 return true;
869 }
870
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
872 if (options_.output_to_directory) {
873 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700875 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700876 }
877 }
878
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700879 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800881 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700882 if (!flattener.Consume(context_, table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700883 context_->GetDiagnostics()->Error(DiagMessage() << "failed to flatten resource table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700884 return false;
885 }
886
Adam Lesinski06460ef2017-03-14 18:52:13 -0700887 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700888 return io::CopyInputStreamToArchive(context_, &input_stream, "resources.arsc",
889 ArchiveEntry::kAlign, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700890 }
891
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700892 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700893 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700894 return io::CopyProtoToArchive(context_, pb_table.get(), "resources.arsc.flat", 0, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700895 }
896
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700897 bool WriteJavaFile(ResourceTable* table, const StringPiece& package_name_to_generate,
Adam Lesinski418763f2017-04-11 17:36:53 -0700898 const StringPiece& out_package, const JavaClassGeneratorOptions& java_options,
899 const Maybe<std::string> out_text_symbols_path = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 return true;
902 }
903
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700904 std::string out_path = options_.generate_java_class_path.value();
905 file::AppendPath(&out_path, file::PackageToPath(out_package));
906 if (!file::mkdirs(out_path)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700907 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path
908 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700909 return false;
910 }
911
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700912 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700913
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700914 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700915 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700916 context_->GetDiagnostics()->Error(DiagMessage()
917 << "failed writing to '" << out_path
918 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700919 return false;
920 }
921
Adam Lesinski418763f2017-04-11 17:36:53 -0700922 std::unique_ptr<std::ofstream> fout_text;
923 if (out_text_symbols_path) {
924 fout_text =
925 util::make_unique<std::ofstream>(out_text_symbols_path.value(), std::ofstream::binary);
926 if (!*fout_text) {
927 context_->GetDiagnostics()->Error(
928 DiagMessage() << "failed writing to '" << out_text_symbols_path.value()
929 << "': " << android::base::SystemErrorCodeToString(errno));
930 return false;
931 }
932 }
933
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700934 JavaClassGenerator generator(context_, table, java_options);
Adam Lesinski418763f2017-04-11 17:36:53 -0700935 if (!generator.Generate(package_name_to_generate, out_package, &fout, fout_text.get())) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700936 context_->GetDiagnostics()->Error(DiagMessage(out_path) << generator.getError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700937 return false;
938 }
939
940 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700941 context_->GetDiagnostics()->Error(DiagMessage()
942 << "failed writing to '" << out_path
943 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700944 }
945 return true;
946 }
947
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700948 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
949 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700950 return true;
951 }
952
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700953 std::unique_ptr<ClassDefinition> manifest_class =
954 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700955
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700956 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700957 // Something bad happened, but we already logged it, so exit.
958 return false;
959 }
960
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700961 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700962 // Empty Manifest class, no need to generate it.
963 return true;
964 }
965
966 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700967 for (const std::string& annotation : options_.javadoc_annotations) {
968 std::string proper_annotation = "@";
969 proper_annotation += annotation;
970 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700971 }
972
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700973 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700974
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700975 std::string out_path = options_.generate_java_class_path.value();
976 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700978 if (!file::mkdirs(out_path)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700979 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path
980 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700981 return false;
982 }
983
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700986 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700987 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700988 context_->GetDiagnostics()->Error(DiagMessage()
989 << "failed writing to '" << out_path
990 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700991 return false;
992 }
993
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700994 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8, true, &fout)) {
995 context_->GetDiagnostics()->Error(DiagMessage()
996 << "failed writing to '" << out_path
997 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700998 return false;
999 }
1000 return true;
1001 }
1002
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001003 bool WriteProguardFile(const Maybe<std::string>& out, const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001004 if (!out) {
1005 return true;
1006 }
1007
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001008 const std::string& out_path = out.value();
1009 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001011 context_->GetDiagnostics()->Error(DiagMessage()
1012 << "failed to open '" << out_path
1013 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001014 return false;
1015 }
1016
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001018 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001019 context_->GetDiagnostics()->Error(DiagMessage()
1020 << "failed writing to '" << out_path
1021 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001022 return false;
1023 }
1024 return true;
1025 }
1026
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1028 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001029 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001030 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001031 if (!collection) {
1032 return {};
1033 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001034 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001035 }
1036
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001037 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001038 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039 if (!file) {
1040 return {};
1041 }
1042
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001043 std::unique_ptr<io::IData> data = file->OpenAsData();
1044 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1045 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001046 }
1047
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001048 bool MergeStaticLibrary(const std::string& input, bool override) {
1049 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001050 context_->GetDiagnostics()->Note(DiagMessage() << "merging static library " << input);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001051 }
1052
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001053 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001054 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001055 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001056 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001057 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001058 return false;
1059 }
1060
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001061 std::unique_ptr<ResourceTable> table = LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001062 if (!table) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001063 context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001064 return false;
1065 }
1066
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001067 ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001068 if (!pkg) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001069 context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001070 return false;
1071 }
1072
1073 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001074 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001076 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001077
1078 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001079 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001080 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001081 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001082 }
1083
1084 pkg->name = "";
1085 if (override) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001086 result = table_merger_->MergeOverlay(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001087 } else {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001088 result = table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001089 }
1090
1091 } else {
1092 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001093 // preserved and resource names are mangled.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001094 result =
1095 table_merger_->MergeAndMangle(Source(input), pkg->name, table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001096 }
1097
1098 if (!result) {
1099 return false;
1100 }
1101
1102 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001103 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001104 return true;
1105 }
1106
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001107 bool MergeResourceTable(io::IFile* file, bool override) {
1108 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001109 context_->GetDiagnostics()->Note(DiagMessage() << "merging resource table "
1110 << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001111 }
1112
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001113 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001114 if (!data) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001115 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001116 return false;
1117 }
1118
1119 std::unique_ptr<ResourceTable> table =
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001120 LoadTableFromPb(file->GetSource(), data->data(), data->size(), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001121 if (!table) {
1122 return false;
1123 }
1124
1125 bool result = false;
1126 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001127 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001128 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001129 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001130 }
1131 return result;
1132 }
1133
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001134 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc, bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001135 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001136 context_->GetDiagnostics()->Note(DiagMessage() << "merging '" << file_desc->name
1137 << "' from compiled file "
1138 << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001139 }
1140
1141 bool result = false;
1142 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001143 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001144 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001145 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001146 }
1147
1148 if (!result) {
1149 return false;
1150 }
1151
1152 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001153 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1154 if (exported_symbol.name.package.empty()) {
1155 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001156 }
1157
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001158 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001159
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001160 Maybe<ResourceName> mangled_name =
1161 context_->GetNameMangler()->MangleName(exported_symbol.name);
1162 if (mangled_name) {
1163 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001164 }
1165
1166 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001167 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1168 bool result = final_table_.AddResourceAllowMangled(
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001169 res_name, ConfigDescription::DefaultConfig(), std::string(), std::move(id),
1170 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001171 if (!result) {
1172 return false;
1173 }
1174 }
1175 return true;
1176 }
1177
1178 /**
1179 * Takes a path to load as a ZIP file and merges the files within into the
1180 * master ResourceTable.
1181 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001182 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001183 *
1184 * An io::IFileCollection is created from the ZIP file and added to the set of
1185 * io::IFileCollections that are open.
1186 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001187 bool MergeArchive(const std::string& input, bool override) {
1188 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001189 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive " << input);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001190 }
1191
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001192 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001193 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001194 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001195 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001196 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001197 return false;
1198 }
1199
1200 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001201 for (auto iter = collection->Iterator(); iter->HasNext();) {
1202 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001203 error = true;
1204 }
1205 }
1206
1207 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001208 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001209 return !error;
1210 }
1211
1212 /**
1213 * Takes a path to load and merge into the master ResourceTable. If override
1214 * is true,
1215 * conflicting resources are allowed to override each other, in order of last
1216 * seen.
1217 *
1218 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1219 * as ZIP archive
1220 * and the files within are merged individually.
1221 *
1222 * Otherwise the files is processed on its own.
1223 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001224 bool MergePath(const std::string& path, bool override) {
1225 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1226 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1227 return MergeArchive(path, override);
1228 } else if (util::EndsWith(path, ".apk")) {
1229 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001230 }
1231
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001232 io::IFile* file = file_collection_->InsertFile(path);
1233 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001234 }
1235
1236 /**
1237 * Takes a file to load and merge into the master ResourceTable. If override
1238 * is true,
1239 * conflicting resources are allowed to override each other, in order of last
1240 * seen.
1241 *
1242 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1243 * merged into the
1244 * master ResourceTable. If the file ends with .flat, then it is treated like
1245 * a compiled file
1246 * and the header data is read and merged into the final ResourceTable.
1247 *
1248 * All other file types are ignored. This is because these files could be
1249 * coming from a zip,
1250 * where we could have other files like classes.dex.
1251 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001252 bool MergeFile(io::IFile* file, bool override) {
1253 const Source& src = file->GetSource();
1254 if (util::EndsWith(src.path, ".arsc.flat")) {
1255 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001256
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001257 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001258 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001259 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001260 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001261 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001262 return false;
1263 }
1264
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001265 CompiledFileInputStream input_stream(data->data(), data->size());
1266 uint32_t num_files = 0;
1267 if (!input_stream.ReadLittleEndian32(&num_files)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001268 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed read num files");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001269 return false;
1270 }
1271
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001272 for (uint32_t i = 0; i < num_files; i++) {
1273 pb::CompiledFile compiled_file;
1274 if (!input_stream.ReadCompiledFile(&compiled_file)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001275 context_->GetDiagnostics()->Error(DiagMessage(src)
1276 << "failed to read compiled file header");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001277 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001278 }
1279
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001280 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001281 if (!input_stream.ReadDataMetaData(&offset, &len)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001282 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to read data meta data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001283 return false;
1284 }
1285
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001286 std::unique_ptr<ResourceFile> resource_file = DeserializeCompiledFileFromPb(
1287 compiled_file, file->GetSource(), context_->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001288 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001289 return false;
1290 }
1291
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001292 if (!MergeCompiledFile(file->CreateFileSegment(offset, len), resource_file.get(),
1293 override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001294 return false;
1295 }
1296 }
1297 return true;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001298 } else if (util::EndsWith(src.path, ".xml") || util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001299 // Since AAPT compiles these file types and appends .flat to them, seeing
1300 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001301 const StringPiece file_type = util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1302 context_->GetDiagnostics()->Error(DiagMessage(src) << "uncompiled " << file_type
1303 << " file passed as argument. Must be "
1304 "compiled first into .flat file.");
Adam Lesinski6a396c12016-10-20 14:38:23 -07001305 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306 }
1307
1308 // Ignore non .flat files. This could be classes.dex or something else that
1309 // happens
1310 // to be in an archive.
1311 return true;
1312 }
1313
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001314 bool CopyAssetsDirsToApk(IArchiveWriter* writer) {
1315 std::map<std::string, std::unique_ptr<io::RegularFile>> merged_assets;
1316 for (const std::string& assets_dir : options_.assets_dirs) {
1317 Maybe<std::vector<std::string>> files =
1318 file::FindFiles(assets_dir, context_->GetDiagnostics(), nullptr);
1319 if (!files) {
1320 return false;
1321 }
1322
1323 for (const std::string& file : files.value()) {
1324 std::string full_key = "assets/" + file;
1325 std::string full_path = assets_dir;
1326 file::AppendPath(&full_path, file);
1327
1328 auto iter = merged_assets.find(full_key);
1329 if (iter == merged_assets.end()) {
1330 merged_assets.emplace(std::move(full_key),
1331 util::make_unique<io::RegularFile>(Source(std::move(full_path))));
1332 } else if (context_->IsVerbose()) {
1333 context_->GetDiagnostics()->Warn(DiagMessage(iter->second->GetSource())
1334 << "asset file overrides '" << full_path << "'");
1335 }
1336 }
1337 }
1338
1339 for (auto& entry : merged_assets) {
1340 uint32_t compression_flags = ArchiveEntry::kCompress;
1341 std::string extension = file::GetExtension(entry.first).to_string();
1342 if (options_.extensions_to_not_compress.count(extension) > 0) {
1343 compression_flags = 0u;
1344 }
1345
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001346 if (!io::CopyFileToArchive(context_, entry.second.get(), entry.first, compression_flags,
1347 writer)) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001348 return false;
1349 }
1350 }
1351 return true;
1352 }
1353
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001354 /**
1355 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001356 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001357 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001358 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set, xml::XmlResource* manifest,
1359 ResourceTable* table) {
Adam Lesinskib522f042017-04-21 16:57:59 -07001360 const bool keep_raw_values = context_->GetPackageType() == PackageType::kStaticLib;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001361 bool result =
1362 FlattenXml(manifest, "AndroidManifest.xml", {}, keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001363 if (!result) {
1364 return false;
1365 }
1366
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001367 ResourceFileFlattenerOptions file_flattener_options;
1368 file_flattener_options.keep_raw_values = keep_raw_values;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001369 file_flattener_options.do_not_compress_anything = options_.do_not_compress_anything;
1370 file_flattener_options.extensions_to_not_compress = options_.extensions_to_not_compress;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001371 file_flattener_options.no_auto_version = options_.no_auto_version;
1372 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001373 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001374 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1375 file_flattener_options.update_proguard_spec =
1376 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001377
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001378 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001379
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001380 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001381 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001382 return false;
1383 }
1384
Adam Lesinskib522f042017-04-21 16:57:59 -07001385 if (context_->GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001386 if (!FlattenTableToPb(table, writer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001387 return false;
1388 }
1389 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001390 if (!FlattenTable(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001391 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001392 return false;
1393 }
1394 }
1395 return true;
1396 }
1397
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001398 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001399 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400 std::unique_ptr<xml::XmlResource> manifest_xml =
1401 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1402 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403 return 1;
1404 }
1405
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001406 // First extract the Package name without modifying it (via --rename-manifest-package).
1407 if (Maybe<AppInfo> maybe_app_info =
1408 ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001409 const AppInfo& app_info = maybe_app_info.value();
1410 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001411 }
1412
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001413 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1414 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001415 return 1;
1416 }
1417
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001418 Maybe<AppInfo> maybe_app_info =
1419 ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001420 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001421 return 1;
1422 }
1423
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001424 const AppInfo& app_info = maybe_app_info.value();
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001425 context_->SetMinSdkVersion(app_info.min_sdk_version.value_or_default(0));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001426
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001427 context_->SetNameManglerPolicy(NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001428
1429 // Override the package ID when it is "android".
1430 if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001431 context_->SetPackageId(0x01);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001432
1433 // Verify we're building a regular app.
Adam Lesinskib522f042017-04-21 16:57:59 -07001434 if (context_->GetPackageType() != PackageType::kApp) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001435 context_->GetDiagnostics()->Error(
1436 DiagMessage() << "package 'android' can only be built as a regular app");
1437 return 1;
1438 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439 }
1440
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001441 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001442 return 1;
1443 }
1444
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001445 TableMergerOptions table_merger_options;
1446 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001447 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_, table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001448
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001449 if (context_->IsVerbose()) {
1450 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001451 << StringPrintf("linking package '%s' using package ID %02x",
1452 context_->GetCompilationPackage().data(),
1453 context_->GetPackageId()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001454 }
1455
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001456 for (const std::string& input : input_files) {
1457 if (!MergePath(input, false)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001458 context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing input");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001459 return 1;
1460 }
1461 }
1462
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001463 for (const std::string& input : options_.overlay_files) {
1464 if (!MergePath(input, true)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001465 context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing overlays");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001466 return 1;
1467 }
1468 }
1469
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001470 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001471 return 1;
1472 }
1473
Adam Lesinskib522f042017-04-21 16:57:59 -07001474 if (context_->GetPackageType() != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001475 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001476 if (!mover.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001477 context_->GetDiagnostics()->Error(DiagMessage() << "failed moving private attributes");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001478 return 1;
1479 }
1480
1481 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001482 IdAssigner id_assigner(&options_.stable_id_map);
1483 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001484 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001485 return 1;
1486 }
1487
1488 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001489 if (options_.resource_id_map_path) {
1490 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001491 for (auto& type : package->types) {
1492 for (auto& entry : type->entries) {
1493 ResourceName name(package->name, type->type, entry->name);
1494 // The IDs are guaranteed to exist.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001495 options_.stable_id_map[std::move(name)] =
1496 ResourceId(package->id.value(), type->id.value(), entry->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001497 }
1498 }
1499 }
1500
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001501 if (!WriteStableIdMapToPath(context_->GetDiagnostics(), options_.stable_id_map,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001502 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001503 return 1;
1504 }
1505 }
1506 } else {
1507 // Static libs are merged with other apps, and ID collisions are bad, so
1508 // verify that
1509 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001510 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001511 return 1;
1512 }
1513 }
1514
1515 // Add the names to mangle based on our source merge earlier.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001516 context_->SetNameManglerPolicy(
1517 NameManglerPolicy{context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001518
1519 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001520 context_->GetExternalSymbols()->PrependSource(
1521 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001522
Adam Lesinski1e4b0e52017-04-27 15:01:10 -07001523 // Workaround for pre-O runtime that would treat negative resource IDs
1524 // (any ID with a package ID > 7f) as invalid. Intercept any ID (PPTTEEEE) with PP > 0x7f
1525 // and type == 'id', and return the ID 0x7fPPEEEE. IDs don't need to be real resources, they
1526 // are just identifiers.
1527 if (context_->GetMinSdkVersion() < SDK_O && context_->GetPackageType() == PackageType::kApp) {
1528 if (context_->IsVerbose()) {
1529 context_->GetDiagnostics()->Note(DiagMessage()
1530 << "enabling pre-O feature split ID rewriting");
1531 }
1532 context_->GetExternalSymbols()->SetDelegate(
1533 util::make_unique<FeatureSplitSymbolTableDelegate>(context_));
1534 }
1535
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001536 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001537 if (!linker.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001538 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking references");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001539 return 1;
1540 }
1541
Adam Lesinskib522f042017-04-21 16:57:59 -07001542 if (context_->GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001543 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001544 context_->GetDiagnostics()->Warn(DiagMessage()
1545 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001546 }
1547 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001548 ProductFilter product_filter(options_.products);
1549 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001550 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001551 return 1;
1552 }
1553 }
1554
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001555 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001556 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001557 if (!versioner.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001558 context_->GetDiagnostics()->Error(DiagMessage() << "failed versioning styles");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001559 return 1;
1560 }
1561 }
1562
Adam Lesinskib522f042017-04-21 16:57:59 -07001563 if (context_->GetPackageType() != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001564 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001565 context_->GetDiagnostics()->Note(DiagMessage()
1566 << "collapsing resource versions for minimum SDK "
1567 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001568 }
1569
1570 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001571 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001572 return 1;
1573 }
1574 }
1575
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001576 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001577 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001578 if (!deduper.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001579 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001580 return 1;
1581 }
1582 }
1583
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001584 proguard::KeepSet proguard_keep_set;
1585 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001586
Adam Lesinskib522f042017-04-21 16:57:59 -07001587 if (context_->GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001588 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001589 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001590 context_->GetDiagnostics()->Warn(DiagMessage()
1591 << "can't strip resources when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001592 }
1593 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001594 // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or
1595 // equal to the minSdk.
1596 options_.split_constraints =
1597 AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001598
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001599 TableSplitter table_splitter(options_.split_constraints, options_.table_splitter_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001600 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001601 return 1;
1602 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001603 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001604
1605 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001606 auto path_iter = options_.split_paths.begin();
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001607 auto split_constraints_iter = options_.split_constraints.begin();
1608 for (std::unique_ptr<ResourceTable>& split_table : table_splitter.splits()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001609 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001610 context_->GetDiagnostics()->Note(DiagMessage(*path_iter)
1611 << "generating split with configurations '"
1612 << util::Joiner(split_constraints_iter->configs, ", ")
1613 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001614 }
1615
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001616 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(*path_iter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001617 if (!archive_writer) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001618 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001619 return 1;
1620 }
1621
1622 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001623 std::unique_ptr<xml::XmlResource> split_manifest =
1624 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001625
1626 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001627 if (!linker.Consume(context_, split_manifest.get())) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001628 context_->GetDiagnostics()->Error(DiagMessage()
1629 << "failed to create Split AndroidManifest.xml");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001630 return 1;
1631 }
1632
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001633 if (!WriteApk(archive_writer.get(), &proguard_keep_set, split_manifest.get(),
1634 split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001635 return 1;
1636 }
1637
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001638 ++path_iter;
1639 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001640 }
1641 }
1642
1643 // Start writing the base APK.
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001644 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(options_.output_path);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001645 if (!archive_writer) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001646 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001647 return 1;
1648 }
1649
1650 bool error = false;
1651 {
1652 // AndroidManifest.xml has no resource name, but the CallSite is built
1653 // from the name
1654 // (aka, which package the AndroidManifest.xml is coming from).
1655 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001656 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001657
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001658 XmlReferenceLinker manifest_linker;
1659 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1660 if (options_.generate_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001661 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1662 manifest_xml.get(), &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001663 error = true;
1664 }
1665
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001666 if (options_.generate_main_dex_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001667 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1668 manifest_xml.get(),
1669 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001670 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001671 }
1672
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001673 if (options_.generate_java_class_path) {
1674 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001675 error = true;
1676 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001677 }
1678
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001679 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001680 // PackageParser will fail if URIs are removed from
1681 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001682 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1683 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001684 error = true;
1685 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001686 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001687 } else {
1688 error = true;
1689 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001690 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001691
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001692 if (error) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001693 context_->GetDiagnostics()->Error(DiagMessage() << "failed processing manifest");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001694 return 1;
1695 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001696
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001697 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(), &final_table_)) {
1698 return 1;
1699 }
1700
1701 if (!CopyAssetsDirsToApk(archive_writer.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001702 return 1;
1703 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001704
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001705 if (options_.generate_java_class_path) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001706 // The set of packages whose R class to call in the main classes
1707 // onResourcesLoaded callback.
1708 std::vector<std::string> packages_to_callback;
1709
1710 JavaClassGeneratorOptions template_options;
1711 template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1712 template_options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001713
Adam Lesinskib522f042017-04-21 16:57:59 -07001714 if (context_->GetPackageType() == PackageType::kStaticLib ||
1715 options_.generate_non_final_ids) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001716 template_options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001717 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001718
Adam Lesinskib522f042017-04-21 16:57:59 -07001719 if (context_->GetPackageType() == PackageType::kSharedLib) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001720 template_options.use_final = false;
1721 template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{};
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001722 }
1723
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001724 const StringPiece actual_package = context_->GetCompilationPackage();
1725 StringPiece output_package = context_->GetCompilationPackage();
1726 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001727 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001728 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001729 }
1730
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001731 // Generate the private symbols if required.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001732 if (options_.private_symbols) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001733 packages_to_callback.push_back(options_.private_symbols.value());
1734
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001735 // If we defined a private symbols package, we only emit Public symbols
1736 // to the original package, and private and public symbols to the
1737 // private package.
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001738 JavaClassGeneratorOptions options = template_options;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001739 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001740 if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001741 options)) {
1742 return 1;
1743 }
1744 }
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001745
1746 // Generate all the symbols for all extra packages.
1747 for (const std::string& extra_package : options_.extra_java_packages) {
1748 packages_to_callback.push_back(extra_package);
1749
1750 JavaClassGeneratorOptions options = template_options;
1751 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1752 if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) {
1753 return 1;
1754 }
1755 }
1756
1757 // Generate the main public R class.
1758 JavaClassGeneratorOptions options = template_options;
1759
1760 // Only generate public symbols if we have a private package.
1761 if (options_.private_symbols) {
1762 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
1763 }
1764
1765 if (options.rewrite_callback_options) {
1766 options.rewrite_callback_options.value().packages_to_callback =
1767 std::move(packages_to_callback);
1768 }
1769
Adam Lesinski418763f2017-04-11 17:36:53 -07001770 if (!WriteJavaFile(&final_table_, actual_package, output_package, options,
1771 options_.generate_text_symbols_path)) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001772 return 1;
1773 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001774 }
1775
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001776 if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001777 return 1;
1778 }
1779
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001780 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1781 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001782 return 1;
1783 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001784 return 0;
1785 }
1786
1787 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001788 LinkOptions options_;
1789 LinkContext* context_;
1790 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001791
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001792 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001793
1794 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001795 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001796
1797 // A vector of IFileCollections. This is mainly here to keep ownership of the
1798 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001799 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001800
1801 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001802 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001803 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001804
1805 // The set of shared libraries being used, mapping their assigned package ID to package name.
1806 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001807};
1808
Chris Warrington820d72a2017-04-27 15:27:01 +01001809int Link(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
1810 LinkContext context(diagnostics);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001811 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001812 std::vector<std::string> overlay_arg_list;
1813 std::vector<std::string> extra_java_packages;
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001814 Maybe<std::string> package_id;
Adam Lesinski113ee092017-04-03 19:38:25 -07001815 std::vector<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001816 Maybe<std::string> preferred_density;
1817 Maybe<std::string> product_list;
1818 bool legacy_x_flag = false;
1819 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001820 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001821 bool shared_lib = false;
1822 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001823 Maybe<std::string> stable_id_file_path;
1824 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001825 Flags flags =
1826 Flags()
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001827 .RequiredFlag("-o", "Output path.", &options.output_path)
1828 .RequiredFlag("--manifest", "Path to the Android manifest to build.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001829 &options.manifest_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001830 .OptionalFlagList("-I", "Adds an Android APK to link against.", &options.include_paths)
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001831 .OptionalFlagList("-A",
1832 "An assets directory to include in the APK. These are unprocessed.",
1833 &options.assets_dirs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001834 .OptionalFlagList("-R",
1835 "Compilation unit to link, using `overlay` semantics.\n"
1836 "The last conflicting resource given takes precedence.",
1837 &overlay_arg_list)
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001838 .OptionalFlag("--package-id",
1839 "Specify the package ID to use for this app. Must be greater or equal to\n"
1840 "0x7f and can't be used with --static-lib or --shared-lib.",
1841 &package_id)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001842 .OptionalFlag("--java", "Directory in which to generate R.java.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001843 &options.generate_java_class_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001844 .OptionalFlag("--proguard", "Output file for generated Proguard rules.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001845 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001846 .OptionalFlag("--proguard-main-dex",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001847 "Output file for generated Proguard rules for the main dex.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001848 &options.generate_main_dex_proguard_rules_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001849 .OptionalSwitch("--no-auto-version",
1850 "Disables automatic style and layout SDK versioning.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001851 &options.no_auto_version)
1852 .OptionalSwitch("--no-version-vectors",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001853 "Disables automatic versioning of vector drawables. Use this only\n"
1854 "when building with vector drawable support library.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001855 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001856 .OptionalSwitch("--no-version-transitions",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001857 "Disables automatic versioning of transition resources. Use this only\n"
1858 "when building with transition support library.",
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001859 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001860 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001861 "Disables automatic deduping of resources with\n"
1862 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001863 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001864 .OptionalSwitch("--enable-sparse-encoding",
1865 "Enables encoding sparse entries using a binary search tree.\n"
1866 "This decreases APK size at the cost of resource retrieval performance.",
1867 &options.table_flattener_options.use_sparse_entries)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001868 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001869 &legacy_x_flag)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001870 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001871 &require_localization)
Adam Lesinski113ee092017-04-03 19:38:25 -07001872 .OptionalFlagList("-c",
Adam Lesinski418763f2017-04-11 17:36:53 -07001873 "Comma separated list of configurations to include. The default\n"
1874 "is all configurations.",
1875 &configs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001876 .OptionalFlag("--preferred-density",
1877 "Selects the closest matching density and strips out all others.",
1878 &preferred_density)
1879 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001880 .OptionalSwitch("--output-to-dir",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001881 "Outputs the APK contents to a directory specified by -o.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001882 &options.output_to_directory)
1883 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001884 "Removes XML namespace prefix and URI information from\n"
1885 "AndroidManifest.xml and XML binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001886 &options.no_xml_namespaces)
1887 .OptionalFlag("--min-sdk-version",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001888 "Default minimum SDK version to use for AndroidManifest.xml.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001889 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001890 .OptionalFlag("--target-sdk-version",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001891 "Default target SDK version to use for AndroidManifest.xml.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001892 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001893 .OptionalFlag("--version-code",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001894 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
1895 "present.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001896 &options.manifest_fixer_options.version_code_default)
1897 .OptionalFlag("--version-name",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001898 "Version name to inject into the AndroidManifest.xml if none is present.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001899 &options.manifest_fixer_options.version_name_default)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001900 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
1901 &shared_lib)
1902 .OptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001903 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001904 "Merge all library resources under the app's package.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001905 &options.no_static_lib_packages)
1906 .OptionalSwitch("--non-final-ids",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001907 "Generates R.java without the final modifier. This is implied when\n"
1908 "--static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001909 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001910 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001911 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001912 .OptionalFlag("--emit-ids",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001913 "Emit a file at the given path with a list of name to ID mappings,\n"
1914 "suitable for use with --stable-ids.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001915 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001916 .OptionalFlag("--private-symbols",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001917 "Package name to use when generating R.java for private symbols.\n"
1918 "If not specified, public and private symbols will use the application's\n"
1919 "package name.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001920 &options.private_symbols)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001921 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001922 &options.custom_java_package)
1923 .OptionalFlagList("--extra-packages",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001924 "Generate the same R.java but with different package names.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001925 &extra_java_packages)
1926 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001927 "Adds a JavaDoc annotation to all generated Java classes.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001928 &options.javadoc_annotations)
Adam Lesinski418763f2017-04-11 17:36:53 -07001929 .OptionalFlag("--output-text-symbols",
1930 "Generates a text file containing the resource symbols of the R class in\n"
1931 "the specified folder.",
1932 &options.generate_text_symbols_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001933 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001934 "Allows the addition of new resources in overlays without\n"
1935 "<add-resource> tags.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001936 &options.auto_add_overlay)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001937 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001938 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001939 .OptionalFlag("--rename-instrumentation-target-package",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001940 "Changes the name of the target package for instrumentation. Most useful\n"
1941 "when used in conjunction with --rename-manifest-package.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001942 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001943 .OptionalFlagList("-0", "File extensions not to compress.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001944 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001945 .OptionalFlagList("--split",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001946 "Split resources matching a set of configs out to a Split APK.\n"
Adam Lesinskidb091572017-04-13 12:48:56 -07001947 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
1948 "On Windows, use a semicolon ';' separator instead.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001949 &split_args)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001950 .OptionalSwitch("-v", "Enables verbose logging.", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001951
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001952 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001953 return 1;
1954 }
1955
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001956 // Expand all argument-files passed into the command line. These start with '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001957 std::vector<std::string> arg_list;
1958 for (const std::string& arg : flags.GetArgs()) {
1959 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001960 const std::string path = arg.substr(1, arg.size() - 1);
1961 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001962 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
1963 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001964 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001965 }
1966 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001967 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001968 }
1969 }
1970
1971 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001972 for (const std::string& arg : overlay_arg_list) {
1973 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001974 const std::string path = arg.substr(1, arg.size() - 1);
1975 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001976 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
1977 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001978 return 1;
1979 }
1980 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001981 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001982 }
1983 }
1984
1985 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001986 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001987 }
1988
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001989 if (shared_lib && static_lib) {
1990 context.GetDiagnostics()->Error(DiagMessage()
1991 << "only one of --shared-lib and --static-lib can be defined");
1992 return 1;
1993 }
1994
1995 if (shared_lib) {
Adam Lesinskib522f042017-04-21 16:57:59 -07001996 context.SetPackageType(PackageType::kSharedLib);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001997 context.SetPackageId(0x00);
1998 } else if (static_lib) {
Adam Lesinskib522f042017-04-21 16:57:59 -07001999 context.SetPackageType(PackageType::kStaticLib);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002000 context.SetPackageId(kAppPackageId);
2001 } else {
Adam Lesinskib522f042017-04-21 16:57:59 -07002002 context.SetPackageType(PackageType::kApp);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002003 context.SetPackageId(kAppPackageId);
2004 }
2005
2006 if (package_id) {
Adam Lesinskib522f042017-04-21 16:57:59 -07002007 if (context.GetPackageType() != PackageType::kApp) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002008 context.GetDiagnostics()->Error(
2009 DiagMessage() << "can't specify --package-id when not building a regular app");
2010 return 1;
2011 }
2012
2013 const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id.value());
2014 if (!maybe_package_id_int) {
2015 context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id.value()
2016 << "' is not a valid integer");
2017 return 1;
2018 }
2019
2020 const uint32_t package_id_int = maybe_package_id_int.value();
2021 if (package_id_int < kAppPackageId || package_id_int > std::numeric_limits<uint8_t>::max()) {
2022 context.GetDiagnostics()->Error(
2023 DiagMessage() << StringPrintf(
2024 "invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int));
2025 return 1;
2026 }
2027 context.SetPackageId(static_cast<uint8_t>(package_id_int));
2028 }
2029
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002030 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002031 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002032 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002033 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002034 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002035 }
2036 }
2037
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002038 if (product_list) {
2039 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002040 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002041 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002042 }
2043 }
2044 }
2045
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002046 std::unique_ptr<IConfigFilter> filter;
Mihai Nitaf4dacf22017-04-07 08:25:06 -07002047 if (!configs.empty()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002048 filter = ParseConfigFilterParameters(configs, context.GetDiagnostics());
2049 if (filter == nullptr) {
2050 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002051 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002052 options.table_splitter_options.config_filter = filter.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002053 }
2054
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002055 if (preferred_density) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002056 Maybe<uint16_t> density =
2057 ParseTargetDensityParameter(preferred_density.value(), context.GetDiagnostics());
2058 if (!density) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002059 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002060 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002061 options.table_splitter_options.preferred_densities.push_back(density.value());
2062 }
Adam Lesinskic51562c2016-04-28 11:12:38 -07002063
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002064 // Parse the split parameters.
2065 for (const std::string& split_arg : split_args) {
2066 options.split_paths.push_back({});
2067 options.split_constraints.push_back({});
2068 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(), &options.split_paths.back(),
2069 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002070 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002071 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002072 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002073
Adam Lesinskib522f042017-04-21 16:57:59 -07002074 if (context.GetPackageType() != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002075 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2076 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002077 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002078 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002079 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002080
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002081 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002082 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002083 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2084 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2085 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2086 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2087
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002088 // Turn off auto versioning for static-libs.
Adam Lesinskib522f042017-04-21 16:57:59 -07002089 if (context.GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002090 options.no_auto_version = true;
2091 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002092 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002093 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002094
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002095 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002096 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002097}
2098
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002099} // namespace aapt