blob: 40d71a3429d0bb73a9506c65246f04d8a53298a0 [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 Lesinskid3ffa8442017-09-28 13:34:35 -070036#include "ResourceValues.h"
37#include "ValueVisitor.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070038#include "cmd/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039#include "compile/IdAssigner.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080040#include "filter/ConfigFilter.h"
Adam Lesinski46708052017-09-29 14:49:15 -070041#include "format/Archive.h"
42#include "format/binary/BinaryResourceParser.h"
43#include "format/binary/TableFlattener.h"
44#include "format/binary/XmlFlattener.h"
45#include "format/proto/ProtoDeserialize.h"
46#include "format/proto/ProtoSerialize.h"
Adam Lesinski06460ef2017-03-14 18:52:13 -070047#include "io/BigBufferInputStream.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070048#include "io/FileInputStream.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080049#include "io/FileSystem.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070050#include "io/Util.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080051#include "io/ZipArchive.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070052#include "java/JavaClassGenerator.h"
53#include "java/ManifestClassGenerator.h"
54#include "java/ProguardRules.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055#include "link/Linkers.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056#include "link/ManifestFixer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080057#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058#include "link/TableMerger.h"
Adam Lesinskic744ae82017-05-17 19:28:38 -070059#include "link/XmlCompatVersioner.h"
Adam Lesinskid48944a2017-02-21 14:22:30 -080060#include "optimize/ResourceDeduper.h"
61#include "optimize/VersionCollapser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062#include "process/IResourceTableConsumer.h"
63#include "process/SymbolTable.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080064#include "split/TableSplitter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065#include "util/Files.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080066#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070068using ::aapt::io::FileInputStream;
69using ::android::StringPiece;
70using ::android::base::StringPrintf;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070071
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072namespace aapt {
73
74struct LinkOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 std::string output_path;
76 std::string manifest_path;
77 std::vector<std::string> include_paths;
78 std::vector<std::string> overlay_files;
Adam Lesinskib39ad7c2017-03-13 11:40:48 -070079 std::vector<std::string> assets_dirs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 bool output_to_directory = false;
81 bool auto_add_overlay = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 // Java/Proguard options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 Maybe<std::string> generate_java_class_path;
85 Maybe<std::string> custom_java_package;
86 std::set<std::string> extra_java_packages;
Adam Lesinski418763f2017-04-11 17:36:53 -070087 Maybe<std::string> generate_text_symbols_path;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 Maybe<std::string> generate_proguard_rules_path;
89 Maybe<std::string> generate_main_dex_proguard_rules_path;
90 bool generate_non_final_ids = false;
91 std::vector<std::string> javadoc_annotations;
92 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070093
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 // Optimizations/features.
95 bool no_auto_version = false;
96 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +090097 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 bool no_resource_deduping = false;
99 bool no_xml_namespaces = false;
100 bool do_not_compress_anything = false;
101 std::unordered_set<std::string> extensions_to_not_compress;
102
103 // Static lib options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 bool no_static_lib_packages = false;
105
106 // AndroidManifest.xml massaging options.
107 ManifestFixerOptions manifest_fixer_options;
108
109 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700111
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800112 // Flattening options.
113 TableFlattenerOptions table_flattener_options;
114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 TableSplitterOptions table_splitter_options;
117 std::vector<SplitConstraints> split_constraints;
118 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700119
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 std::unordered_map<ResourceName, ResourceId> stable_id_map;
122 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123};
124
Adam Lesinski64587af2016-02-18 18:33:06 -0800125class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 public:
Chris Warrington820d72a2017-04-27 15:27:01 +0100127 LinkContext(IDiagnostics* diagnostics)
128 : diagnostics_(diagnostics), name_mangler_({}), symbols_(&name_mangler_) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700129 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700130
Adam Lesinskib522f042017-04-21 16:57:59 -0700131 PackageType GetPackageType() override {
132 return package_type_;
133 }
134
135 void SetPackageType(PackageType type) {
136 package_type_ = type;
137 }
138
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700139 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100140 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700141 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700142
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700143 NameMangler* GetNameMangler() override {
144 return &name_mangler_;
145 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
148 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800150
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 const std::string& GetCompilationPackage() override {
152 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800156 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800158
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700159 uint8_t GetPackageId() override {
160 return package_id_;
161 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700163 void SetPackageId(uint8_t id) {
164 package_id_ = id;
165 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800166
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700167 SymbolTable* GetExternalSymbols() override {
168 return &symbols_;
169 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800170
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700171 bool IsVerbose() override {
172 return verbose_;
173 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800174
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700175 void SetVerbose(bool val) {
176 verbose_ = val;
177 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800178
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700179 int GetMinSdkVersion() override {
180 return min_sdk_version_;
181 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700182
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700183 void SetMinSdkVersion(int minSdk) {
184 min_sdk_version_ = minSdk;
185 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700186
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 DISALLOW_COPY_AND_ASSIGN(LinkContext);
189
Adam Lesinskib522f042017-04-21 16:57:59 -0700190 PackageType package_type_ = PackageType::kApp;
Chris Warrington820d72a2017-04-27 15:27:01 +0100191 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 NameMangler name_mangler_;
193 std::string compilation_package_;
194 uint8_t package_id_ = 0x0;
195 SymbolTable symbols_;
196 bool verbose_ = false;
197 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198};
199
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700200// A custom delegate that generates compatible pre-O IDs for use with feature splits.
201// Feature splits use package IDs > 7f, which in Java (since Java doesn't have unsigned ints)
202// is interpreted as a negative number. Some verification was wrongly assuming negative values
203// were invalid.
204//
205// This delegate will attempt to masquerade any '@id/' references with ID 0xPPTTEEEE,
206// where PP > 7f, as 0x7fPPEEEE. Any potential overlapping is verified and an error occurs if such
207// an overlap exists.
208class FeatureSplitSymbolTableDelegate : public DefaultSymbolTableDelegate {
209 public:
210 FeatureSplitSymbolTableDelegate(IAaptContext* context) : context_(context) {
211 }
212
213 virtual ~FeatureSplitSymbolTableDelegate() = default;
214
215 virtual std::unique_ptr<SymbolTable::Symbol> FindByName(
216 const ResourceName& name,
217 const std::vector<std::unique_ptr<ISymbolSource>>& sources) override {
218 std::unique_ptr<SymbolTable::Symbol> symbol =
219 DefaultSymbolTableDelegate::FindByName(name, sources);
220 if (symbol == nullptr) {
221 return {};
222 }
223
224 // Check to see if this is an 'id' with the target package.
225 if (name.type == ResourceType::kId && symbol->id) {
226 ResourceId* id = &symbol->id.value();
227 if (id->package_id() > kAppPackageId) {
228 // Rewrite the resource ID to be compatible pre-O.
229 ResourceId rewritten_id(kAppPackageId, id->package_id(), id->entry_id());
230
231 // Check that this doesn't overlap another resource.
232 if (DefaultSymbolTableDelegate::FindById(rewritten_id, sources) != nullptr) {
233 // The ID overlaps, so log a message (since this is a weird failure) and fail.
234 context_->GetDiagnostics()->Error(DiagMessage() << "Failed to rewrite " << name
235 << " for pre-O feature split support");
236 return {};
237 }
238
239 if (context_->IsVerbose()) {
240 context_->GetDiagnostics()->Note(DiagMessage() << "rewriting " << name << " (" << *id
241 << ") -> (" << rewritten_id << ")");
242 }
243
244 *id = rewritten_id;
245 }
246 }
247 return symbol;
248 }
249
250 private:
251 DISALLOW_COPY_AND_ASSIGN(FeatureSplitSymbolTableDelegate);
252
253 IAaptContext* context_;
254};
255
Adam Lesinskic744ae82017-05-17 19:28:38 -0700256static bool FlattenXml(IAaptContext* context, xml::XmlResource* xml_res, const StringPiece& path,
Adam Lesinskiea13c1a2017-10-13 12:40:37 -0700257 bool keep_raw_values, bool utf16, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 BigBuffer buffer(1024);
259 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 options.keep_raw_values = keep_raw_values;
Adam Lesinskiea13c1a2017-10-13 12:40:37 -0700261 options.use_utf16 = utf16;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800264 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 }
266
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 if (context->IsVerbose()) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700268 context->GetDiagnostics()->Note(DiagMessage(path) << "writing to archive (keep_raw_values="
269 << (keep_raw_values ? "true" : "false")
270 << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 }
272
Adam Lesinski06460ef2017-03-14 18:52:13 -0700273 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700274 return io::CopyInputStreamToArchive(context, &input_stream, path.to_string(),
275 ArchiveEntry::kCompress, writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800276}
277
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700278static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source, const void* data,
279 size_t len, IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 pb::ResourceTable pb_table;
281 if (!pb_table.ParseFromArray(data, len)) {
282 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 return {};
284 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800285
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700286 std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>();
287 std::string error;
288 if (!DeserializeTableFromPb(pb_table, table.get(), &error)) {
289 diag->Error(DiagMessage(source) << "invalid compiled table: " << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 return {};
291 }
292 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800293}
294
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700295// Inflates an XML file from the source path.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700296static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path, IDiagnostics* diag) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700297 FileInputStream fin(path);
298 if (fin.HadError()) {
299 diag->Error(DiagMessage(path) << "failed to load XML file: " << fin.GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 return {};
301 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800303}
304
Adam Lesinski355f2852016-02-13 20:26:45 -0800305struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306 bool no_auto_version = false;
307 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900308 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 bool no_xml_namespaces = false;
310 bool keep_raw_values = false;
311 bool do_not_compress_anything = false;
312 bool update_proguard_spec = false;
313 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800314};
315
Adam Lesinskic744ae82017-05-17 19:28:38 -0700316// A sampling of public framework resource IDs.
317struct R {
318 struct attr {
319 enum : uint32_t {
320 paddingLeft = 0x010100d6u,
321 paddingRight = 0x010100d8u,
322 paddingHorizontal = 0x0101053du,
323
324 paddingTop = 0x010100d7u,
325 paddingBottom = 0x010100d9u,
326 paddingVertical = 0x0101053eu,
327
328 layout_marginLeft = 0x010100f7u,
329 layout_marginRight = 0x010100f9u,
330 layout_marginHorizontal = 0x0101053bu,
331
332 layout_marginTop = 0x010100f8u,
333 layout_marginBottom = 0x010100fau,
334 layout_marginVertical = 0x0101053cu,
335 };
336 };
337};
338
Adam Lesinski355f2852016-02-13 20:26:45 -0800339class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 public:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700341 ResourceFileFlattener(const ResourceFileFlattenerOptions& options, IAaptContext* context,
Adam Lesinskic744ae82017-05-17 19:28:38 -0700342 proguard::KeepSet* keep_set);
Adam Lesinski355f2852016-02-13 20:26:45 -0800343
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800345
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700346 private:
347 struct FileOperation {
348 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700349
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 // The entry this file came from.
Adam Lesinskibb94f322017-07-12 07:41:55 -0700351 ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700352
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 // The file to copy as-is.
Adam Lesinskibb94f322017-07-12 07:41:55 -0700354 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700355
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700358
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 std::string dst_path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800362
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800364
Adam Lesinskic744ae82017-05-17 19:28:38 -0700365 std::vector<std::unique_ptr<xml::XmlResource>> LinkAndVersionXmlFile(ResourceTable* table,
366 FileOperation* file_op);
Adam Lesinski355f2852016-02-13 20:26:45 -0800367
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 ResourceFileFlattenerOptions options_;
369 IAaptContext* context_;
370 proguard::KeepSet* keep_set_;
Adam Lesinskic744ae82017-05-17 19:28:38 -0700371 XmlCompatVersioner::Rules rules_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800372};
373
Adam Lesinskic744ae82017-05-17 19:28:38 -0700374ResourceFileFlattener::ResourceFileFlattener(const ResourceFileFlattenerOptions& options,
375 IAaptContext* context, proguard::KeepSet* keep_set)
376 : options_(options), context_(context), keep_set_(keep_set) {
377 SymbolTable* symm = context_->GetExternalSymbols();
378
379 // Build up the rules for degrading newer attributes to older ones.
380 // NOTE(adamlesinski): These rules are hardcoded right now, but they should be
381 // generated from the attribute definitions themselves (b/62028956).
382 if (const SymbolTable::Symbol* s = symm->FindById(R::attr::paddingHorizontal)) {
383 std::vector<ReplacementAttr> replacements{
384 {"paddingLeft", R::attr::paddingLeft,
385 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
386 {"paddingRight", R::attr::paddingRight,
387 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
388 };
389 rules_[R::attr::paddingHorizontal] =
390 util::make_unique<DegradeToManyRule>(std::move(replacements));
391 }
392
393 if (const SymbolTable::Symbol* s = symm->FindById(R::attr::paddingVertical)) {
394 std::vector<ReplacementAttr> replacements{
395 {"paddingTop", R::attr::paddingTop,
396 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
397 {"paddingBottom", R::attr::paddingBottom,
398 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
399 };
400 rules_[R::attr::paddingVertical] =
401 util::make_unique<DegradeToManyRule>(std::move(replacements));
402 }
403
404 if (const SymbolTable::Symbol* s = symm->FindById(R::attr::layout_marginHorizontal)) {
405 std::vector<ReplacementAttr> replacements{
406 {"layout_marginLeft", R::attr::layout_marginLeft,
407 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
408 {"layout_marginRight", R::attr::layout_marginRight,
409 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
410 };
411 rules_[R::attr::layout_marginHorizontal] =
412 util::make_unique<DegradeToManyRule>(std::move(replacements));
413 }
414
415 if (const SymbolTable::Symbol* s = symm->FindById(R::attr::layout_marginVertical)) {
416 std::vector<ReplacementAttr> replacements{
417 {"layout_marginTop", R::attr::layout_marginTop,
418 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
419 {"layout_marginBottom", R::attr::layout_marginBottom,
420 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
421 };
422 rules_[R::attr::layout_marginVertical] =
423 util::make_unique<DegradeToManyRule>(std::move(replacements));
424 }
425}
426
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
428 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 return 0;
430 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800431
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 for (const std::string& extension : options_.extensions_to_not_compress) {
433 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800435 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 }
437 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800438}
439
Adam Lesinskibb94f322017-07-12 07:41:55 -0700440static bool IsTransitionElement(const std::string& name) {
441 return name == "fade" || name == "changeBounds" || name == "slide" || name == "explode" ||
442 name == "changeImageTransform" || name == "changeTransform" ||
443 name == "changeClipBounds" || name == "autoTransition" || name == "recolor" ||
444 name == "changeScroll" || name == "transitionSet" || name == "transition" ||
445 name == "transitionManager";
446}
447
448static bool IsVectorElement(const std::string& name) {
449 return name == "vector" || name == "animated-vector" || name == "pathInterpolator" ||
ztenghuiab2a38c2017-10-13 15:56:08 -0700450 name == "objectAnimator" || name == "gradient";
Adam Lesinskibb94f322017-07-12 07:41:55 -0700451}
452
Adam Lesinskic744ae82017-05-17 19:28:38 -0700453template <typename T>
454std::vector<T> make_singleton_vec(T&& val) {
455 std::vector<T> vec;
456 vec.emplace_back(std::forward<T>(val));
457 return vec;
458}
459
460std::vector<std::unique_ptr<xml::XmlResource>> ResourceFileFlattener::LinkAndVersionXmlFile(
461 ResourceTable* table, FileOperation* file_op) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700462 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700464
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 if (context_->IsVerbose()) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700466 context_->GetDiagnostics()->Note(DiagMessage()
467 << "linking " << src.path << " (" << doc->file.name << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 }
469
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 XmlReferenceLinker xml_linker;
471 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700472 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 }
474
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700475 if (options_.update_proguard_spec && !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700476 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 }
478
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 if (options_.no_xml_namespaces) {
480 XmlNamespaceRemover namespace_remover;
481 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700482 return {};
Adam Lesinski355f2852016-02-13 20:26:45 -0800483 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800485
Adam Lesinskibb94f322017-07-12 07:41:55 -0700486 if (options_.no_auto_version) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700487 return make_singleton_vec(std::move(file_op->xml_to_flatten));
488 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700489
Adam Lesinskibb94f322017-07-12 07:41:55 -0700490 if (options_.no_version_vectors || options_.no_version_transitions) {
491 // Skip this if it is a vector or animated-vector.
Adam Lesinski6b372992017-08-09 10:54:23 -0700492 xml::Element* el = doc->root.get();
Adam Lesinskibb94f322017-07-12 07:41:55 -0700493 if (el && el->namespace_uri.empty()) {
494 if ((options_.no_version_vectors && IsVectorElement(el->name)) ||
495 (options_.no_version_transitions && IsTransitionElement(el->name))) {
496 return make_singleton_vec(std::move(file_op->xml_to_flatten));
497 }
498 }
499 }
500
Adam Lesinskic744ae82017-05-17 19:28:38 -0700501 const ConfigDescription& config = file_op->config;
502 ResourceEntry* entry = file_op->entry;
503
504 XmlCompatVersioner xml_compat_versioner(&rules_);
505 const util::Range<ApiVersion> api_range{config.sdkVersion,
506 FindNextApiVersionForConfig(entry, config)};
507 return xml_compat_versioner.Process(context_, doc, api_range);
Adam Lesinski355f2852016-02-13 20:26:45 -0800508}
509
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700510bool ResourceFileFlattener::Flatten(ResourceTable* table, IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 bool error = false;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700512 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation> config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800513
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 for (auto& pkg : table->packages) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700515 CHECK(!pkg->name.empty()) << "Packages must have names when being linked";
516
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 for (auto& type : pkg->types) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700518 // Sort by config and name, so that we get better locality in the zip file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 config_sorted_files.clear();
Adam Lesinskibb94f322017-07-12 07:41:55 -0700520 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800521
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522 // Populate the queue with all files in the ResourceTable.
523 for (auto& entry : type->entries) {
Adam Lesinskibb94f322017-07-12 07:41:55 -0700524 for (auto& config_value : entry->values) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700525 // WARNING! Do not insert or remove any resources while executing in this scope. It will
526 // corrupt the iteration order.
527
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700528 FileReference* file_ref = ValueCast<FileReference>(config_value->value.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700530 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700532
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700534 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700537 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 }
539
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700540 FileOperation file_op;
541 file_op.entry = entry.get();
542 file_op.dst_path = *file_ref->path;
543 file_op.config = config_value->config;
Adam Lesinskic744ae82017-05-17 19:28:38 -0700544 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700546 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 if (type->type != ResourceType::kRaw &&
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700548 (util::EndsWith(src_path, ".xml.flat") || util::EndsWith(src_path, ".xml"))) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 << "failed to open file");
553 return false;
554 }
555
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700556 file_op.xml_to_flatten = xml::Inflate(data->data(), data->size(),
557 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700559 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 return false;
561 }
562
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700563 file_op.xml_to_flatten->file.config = config_value->config;
564 file_op.xml_to_flatten->file.source = file_ref->GetSource();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700565 file_op.xml_to_flatten->file.name = ResourceName(pkg->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 }
Adam Lesinskic744ae82017-05-17 19:28:38 -0700567
568 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
569 // else we end up copying the string in the std::make_pair() method,
570 // then creating a StringPiece from the copy, which would cause us
571 // to end up referencing garbage in the map.
572 const StringPiece entry_name(entry->name);
573 config_sorted_files[std::make_pair(config_value->config, entry_name)] =
574 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700575 }
576 }
577
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 for (auto& map_entry : config_sorted_files) {
580 const ConfigDescription& config = map_entry.first.first;
Adam Lesinskic744ae82017-05-17 19:28:38 -0700581 FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 if (file_op.xml_to_flatten) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700584 std::vector<std::unique_ptr<xml::XmlResource>> versioned_docs =
585 LinkAndVersionXmlFile(table, &file_op);
Adam Lesinskic0a5e1e2017-08-07 11:56:32 -0700586 if (versioned_docs.empty()) {
587 error = true;
588 continue;
589 }
590
Adam Lesinskic744ae82017-05-17 19:28:38 -0700591 for (std::unique_ptr<xml::XmlResource>& doc : versioned_docs) {
592 std::string dst_path = file_op.dst_path;
593 if (doc->file.config != file_op.config) {
594 // Only add the new versioned configurations.
595 if (context_->IsVerbose()) {
596 context_->GetDiagnostics()->Note(DiagMessage(doc->file.source)
597 << "auto-versioning resource from config '"
598 << config << "' -> '" << doc->file.config << "'");
599 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600
Adam Lesinskic744ae82017-05-17 19:28:38 -0700601 dst_path =
602 ResourceUtils::BuildResourceFileName(doc->file, context_->GetNameMangler());
603 bool result = table->AddFileReferenceAllowMangled(doc->file.name, doc->file.config,
604 doc->file.source, dst_path, nullptr,
605 context_->GetDiagnostics());
606 if (!result) {
607 return false;
608 }
609 }
610 error |= !FlattenXml(context_, doc.get(), dst_path, options_.keep_raw_values,
Adam Lesinskiea13c1a2017-10-13 12:40:37 -0700611 false /*utf16*/, archive_writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 }
613 } else {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700614 error |= !io::CopyFileToArchive(context_, file_op.file_to_copy, file_op.dst_path,
615 GetCompressionFlags(file_op.dst_path), archive_writer);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700616 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700618 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 }
620 return !error;
621}
622
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700623static bool WriteStableIdMapToPath(IDiagnostics* diag,
624 const std::unordered_map<ResourceName, ResourceId>& id_map,
625 const std::string& id_map_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700626 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700628 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 return false;
630 }
631
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 const ResourceName& name = entry.first;
634 const ResourceId& id = entry.second;
635 fout << name << " = " << id << "\n";
636 }
637
638 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700639 diag->Error(DiagMessage(id_map_path) << "failed writing to file: "
640 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 return false;
642 }
643
644 return true;
645}
646
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700647static bool LoadStableIdMap(IDiagnostics* diag, const std::string& path,
648 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 std::string content;
Adam Lesinski2354b562017-05-26 16:31:38 -0700650 if (!android::base::ReadFileToString(path, &content, true /*follow_symlinks*/)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700651 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 return false;
653 }
654
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700655 out_id_map->clear();
656 size_t line_no = 0;
657 for (StringPiece line : util::Tokenize(content, '\n')) {
658 line_no++;
659 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700660 if (line.empty()) {
661 continue;
662 }
663
664 auto iter = std::find(line.begin(), line.end(), '=');
665 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700666 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 return false;
668 }
669
670 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700671 StringPiece res_name_str =
672 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
673 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700674 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource name '" << res_name_str
675 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 return false;
677 }
678
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
680 const size_t res_id_str_len = line.size() - res_id_start_idx;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700681 StringPiece res_id_str = util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
684 if (!maybe_id) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700685 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '" << res_id_str
686 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 return false;
688 }
689
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700690 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 }
692 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700693}
694
Adam Lesinskifb48d292015-11-07 15:52:13 -0800695class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700696 public:
697 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 : options_(options),
699 context_(context),
700 final_table_(),
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700701 file_collection_(util::make_unique<io::FileCollection>()) {
702 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700703
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700704 /**
705 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700706 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700708 bool LoadSymbolsFromIncludePaths() {
709 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700710 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700711 for (const std::string& path : options_.include_paths) {
712 if (context_->IsVerbose()) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700713 context_->GetDiagnostics()->Note(DiagMessage() << "including " << path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700715
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 std::string error_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800718 std::unique_ptr<ResourceTable> include_static = LoadStaticLibrary(path, &error_str);
719 if (include_static) {
Adam Lesinskib522f042017-04-21 16:57:59 -0700720 if (context_->GetPackageType() != PackageType::kStaticLib) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800721 // Can't include static libraries when not building a static library (they have no IDs
722 // assigned).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723 context_->GetDiagnostics()->Error(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800724 DiagMessage(path) << "can't include static library when not building a static lib");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725 return false;
726 }
727
728 // If we are using --no-static-lib-packages, we need to rename the
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800729 // package of this table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 if (options_.no_static_lib_packages) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800731 // Since package names can differ, and multiple packages can exist in a ResourceTable,
732 // we place the requirement that all static libraries are built with the package
733 // ID 0x7f. So if one is not found, this is an error.
734 if (ResourceTablePackage* pkg = include_static->FindPackageById(kAppPackageId)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700735 pkg->name = context_->GetCompilationPackage();
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800736 } else {
737 context_->GetDiagnostics()->Error(DiagMessage(path)
738 << "no package with ID 0x7f found in static library");
739 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 }
741 }
742
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700743 context_->GetExternalSymbols()->AppendSource(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800744 util::make_unique<ResourceTableSymbolSource>(include_static.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700745
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800746 static_table_includes_.push_back(std::move(include_static));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 return false;
752 }
753
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700754 if (!asset_source->AddAssetPath(path)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800755 context_->GetDiagnostics()->Error(DiagMessage(path) << "failed to load include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700756 return false;
757 }
758 }
759
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800760 // Capture the shared libraries so that the final resource table can be properly flattened
761 // with support for shared libraries.
762 for (auto& entry : asset_source->GetAssignedPackageIds()) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800763 if (entry.first > kFrameworkPackageId && entry.first < kAppPackageId) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800764 final_table_.included_packages_[entry.first] = entry.second;
765 }
766 }
767
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700768 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 return true;
770 }
771
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800772 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800774 xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
775 if (manifest_el == nullptr) {
776 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700777 }
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800778
779 AppInfo app_info;
780
781 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
782 diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>");
783 return {};
784 }
785
786 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
787 if (!package_attr) {
788 diag->Error(DiagMessage(xml_res->file.source)
789 << "<manifest> must have a 'package' attribute");
790 return {};
791 }
792 app_info.package = package_attr->value;
793
794 if (xml::Attribute* version_code_attr =
795 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
796 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_attr->value);
797 if (!maybe_code) {
798 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
799 << "invalid android:versionCode '" << version_code_attr->value << "'");
800 return {};
801 }
802 app_info.version_code = maybe_code.value();
803 }
804
805 if (xml::Attribute* revision_code_attr =
806 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
807 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(revision_code_attr->value);
808 if (!maybe_code) {
809 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
810 << "invalid android:revisionCode '" << revision_code_attr->value << "'");
811 return {};
812 }
813 app_info.revision_code = maybe_code.value();
814 }
815
816 if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
817 if (!split_name_attr->value.empty()) {
818 app_info.split_name = split_name_attr->value;
819 }
820 }
821
822 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
823 if (xml::Attribute* min_sdk =
824 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700825 app_info.min_sdk_version = ResourceUtils::ParseSdkVersion(min_sdk->value);
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800826 }
827 }
828 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 }
830
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700831 // Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked.
832 // Postcondition: ResourceTable has only one package left. All others are
833 // stripped, or there is an error and false is returned.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700834 bool VerifyNoExternalPackages() {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700835 auto is_ext_package_func = [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700836 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
837 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838 };
839
840 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700841 for (const auto& package : final_table_.packages) {
842 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700843 // We have a package that is not related to the one we're building!
844 for (const auto& type : package->types) {
845 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700846 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700847
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700848 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700849 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700850 // for the 'android' package. This is due to legacy reasons.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700851 if (ValueCast<Id>(config_value->value.get()) && package->name == "android") {
852 context_->GetDiagnostics()->Warn(DiagMessage(config_value->value->GetSource())
853 << "generated id '" << res_name
854 << "' for external package '" << package->name
855 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700856 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700857 context_->GetDiagnostics()->Error(DiagMessage(config_value->value->GetSource())
858 << "defined resource '" << res_name
859 << "' for external package '" << package->name
860 << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700861 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700862 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700863 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700864 }
865 }
866 }
867 }
868
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700869 auto new_end_iter = std::remove_if(final_table_.packages.begin(), final_table_.packages.end(),
870 is_ext_package_func);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700872 return !error;
873 }
874
875 /**
876 * Returns true if no IDs have been set, false otherwise.
877 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700878 bool VerifyNoIdsSet() {
879 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 for (const auto& type : package->types) {
881 if (type->id) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800882 context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID "
883 << StringPrintf("%02x", type->id.value())
884 << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700885 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700886 }
887
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888 for (const auto& entry : type->entries) {
889 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700890 ResourceNameRef res_name(package->name, type->type, entry->name);
891 context_->GetDiagnostics()->Error(
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800892 DiagMessage() << "entry " << res_name << " has ID "
893 << StringPrintf("%02x", entry->id.value()) << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700894 return false;
895 }
896 }
897 }
898 }
899 return true;
900 }
901
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700902 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
903 if (options_.output_to_directory) {
904 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700905 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700906 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 }
908 }
909
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700910 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700911 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800912 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700913 if (!flattener.Consume(context_, table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700914 context_->GetDiagnostics()->Error(DiagMessage() << "failed to flatten resource table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700915 return false;
916 }
917
Adam Lesinski06460ef2017-03-14 18:52:13 -0700918 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700919 return io::CopyInputStreamToArchive(context_, &input_stream, "resources.arsc",
920 ArchiveEntry::kAlign, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700921 }
922
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700923 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700924 pb::ResourceTable pb_table;
925 SerializeTableToPb(*table, &pb_table);
926 return io::CopyProtoToArchive(context_, &pb_table, "resources.arsc.flat", 0, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700927 }
928
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700929 bool WriteJavaFile(ResourceTable* table, const StringPiece& package_name_to_generate,
Adam Lesinski418763f2017-04-11 17:36:53 -0700930 const StringPiece& out_package, const JavaClassGeneratorOptions& java_options,
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700931 const Maybe<std::string>& out_text_symbols_path = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700932 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700933 return true;
934 }
935
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700936 std::string out_path = options_.generate_java_class_path.value();
937 file::AppendPath(&out_path, file::PackageToPath(out_package));
938 if (!file::mkdirs(out_path)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700939 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path
940 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700941 return false;
942 }
943
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700944 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700945
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700946 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700947 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700948 context_->GetDiagnostics()->Error(DiagMessage()
949 << "failed writing to '" << out_path
950 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700951 return false;
952 }
953
Adam Lesinski418763f2017-04-11 17:36:53 -0700954 std::unique_ptr<std::ofstream> fout_text;
955 if (out_text_symbols_path) {
956 fout_text =
957 util::make_unique<std::ofstream>(out_text_symbols_path.value(), std::ofstream::binary);
958 if (!*fout_text) {
959 context_->GetDiagnostics()->Error(
960 DiagMessage() << "failed writing to '" << out_text_symbols_path.value()
961 << "': " << android::base::SystemErrorCodeToString(errno));
962 return false;
963 }
964 }
965
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700966 JavaClassGenerator generator(context_, table, java_options);
Adam Lesinski418763f2017-04-11 17:36:53 -0700967 if (!generator.Generate(package_name_to_generate, out_package, &fout, fout_text.get())) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700968 context_->GetDiagnostics()->Error(DiagMessage(out_path) << generator.getError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700969 return false;
970 }
971
972 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700973 context_->GetDiagnostics()->Error(DiagMessage()
974 << "failed writing to '" << out_path
975 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700976 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 }
978 return true;
979 }
980
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700981 bool GenerateJavaClasses() {
982 // The set of packages whose R class to call in the main classes onResourcesLoaded callback.
983 std::vector<std::string> packages_to_callback;
984
985 JavaClassGeneratorOptions template_options;
986 template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
987 template_options.javadoc_annotations = options_.javadoc_annotations;
988
989 if (context_->GetPackageType() == PackageType::kStaticLib || options_.generate_non_final_ids) {
990 template_options.use_final = false;
991 }
992
993 if (context_->GetPackageType() == PackageType::kSharedLib) {
994 template_options.use_final = false;
995 template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{};
996 }
997
998 const StringPiece actual_package = context_->GetCompilationPackage();
999 StringPiece output_package = context_->GetCompilationPackage();
1000 if (options_.custom_java_package) {
1001 // Override the output java package to the custom one.
1002 output_package = options_.custom_java_package.value();
1003 }
1004
1005 // Generate the private symbols if required.
1006 if (options_.private_symbols) {
1007 packages_to_callback.push_back(options_.private_symbols.value());
1008
1009 // If we defined a private symbols package, we only emit Public symbols
1010 // to the original package, and private and public symbols to the private package.
1011 JavaClassGeneratorOptions options = template_options;
1012 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
1013 if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(),
1014 options)) {
1015 return false;
1016 }
1017 }
1018
1019 // Generate copies of the original package R class but with different package names.
1020 // This is to support non-namespaced builds.
1021 for (const std::string& extra_package : options_.extra_java_packages) {
1022 packages_to_callback.push_back(extra_package);
1023
1024 JavaClassGeneratorOptions options = template_options;
1025 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1026 if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) {
1027 return false;
1028 }
1029 }
1030
1031 // Generate R classes for each package that was merged (static library).
1032 // Use the actual package's resources only.
1033 for (const std::string& package : table_merger_->merged_packages()) {
1034 packages_to_callback.push_back(package);
1035
1036 JavaClassGeneratorOptions options = template_options;
1037 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1038 if (!WriteJavaFile(&final_table_, package, package, options)) {
1039 return false;
1040 }
1041 }
1042
1043 // Generate the main public R class.
1044 JavaClassGeneratorOptions options = template_options;
1045
1046 // Only generate public symbols if we have a private package.
1047 if (options_.private_symbols) {
1048 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
1049 }
1050
1051 if (options.rewrite_callback_options) {
1052 options.rewrite_callback_options.value().packages_to_callback =
1053 std::move(packages_to_callback);
1054 }
1055
1056 if (!WriteJavaFile(&final_table_, actual_package, output_package, options,
1057 options_.generate_text_symbols_path)) {
1058 return false;
1059 }
1060
1061 return true;
1062 }
1063
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001064 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
1065 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001066 return true;
1067 }
1068
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001069 std::unique_ptr<ClassDefinition> manifest_class =
1070 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001071
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001072 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001073 // Something bad happened, but we already logged it, so exit.
1074 return false;
1075 }
1076
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001077 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001078 // Empty Manifest class, no need to generate it.
1079 return true;
1080 }
1081
1082 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001083 for (const std::string& annotation : options_.javadoc_annotations) {
1084 std::string proper_annotation = "@";
1085 proper_annotation += annotation;
1086 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001087 }
1088
Adam Lesinski0d81f702017-06-27 15:51:09 -07001089 const std::string package_utf8 =
1090 options_.custom_java_package.value_or_default(context_->GetCompilationPackage());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001092 std::string out_path = options_.generate_java_class_path.value();
1093 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001094
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001095 if (!file::mkdirs(out_path)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001096 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path
1097 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001098 return false;
1099 }
1100
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001101 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001102
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001103 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001104 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001105 context_->GetDiagnostics()->Error(DiagMessage()
1106 << "failed writing to '" << out_path
1107 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001108 return false;
1109 }
1110
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001111 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8, true, &fout)) {
1112 context_->GetDiagnostics()->Error(DiagMessage()
1113 << "failed writing to '" << out_path
1114 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001115 return false;
1116 }
1117 return true;
1118 }
1119
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001120 bool WriteProguardFile(const Maybe<std::string>& out, const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001121 if (!out) {
1122 return true;
1123 }
1124
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001125 const std::string& out_path = out.value();
1126 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001127 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001128 context_->GetDiagnostics()->Error(DiagMessage()
1129 << "failed to open '" << out_path
1130 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001131 return false;
1132 }
1133
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001134 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001135 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001136 context_->GetDiagnostics()->Error(DiagMessage()
1137 << "failed writing to '" << out_path
1138 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001139 return false;
1140 }
1141 return true;
1142 }
1143
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001144 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1145 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001146 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001147 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001148 if (!collection) {
1149 return {};
1150 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001151 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001152 }
1153
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001154 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001155 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001156 if (!file) {
1157 return {};
1158 }
1159
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001160 std::unique_ptr<io::IData> data = file->OpenAsData();
1161 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1162 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001163 }
1164
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001165 bool MergeStaticLibrary(const std::string& input, bool override) {
1166 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001167 context_->GetDiagnostics()->Note(DiagMessage() << "merging static library " << input);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001168 }
1169
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001170 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001171 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001172 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001173 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001174 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001175 return false;
1176 }
1177
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001178 std::unique_ptr<ResourceTable> table = LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001179 if (!table) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001180 context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001181 return false;
1182 }
1183
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001184 ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001185 if (!pkg) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001186 context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001187 return false;
1188 }
1189
1190 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001191 if (options_.no_static_lib_packages) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001192 // Merge all resources as if they were in the compilation package. This is the old behavior
1193 // of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001194
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001195 // Add the package to the set of --extra-packages so we emit an R.java for each library
1196 // package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001197 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001198 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001199 }
1200
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001201 // Clear the package name, so as to make the resources look like they are coming from the
1202 // local package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001203 pkg->name = "";
1204 if (override) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001205 result = table_merger_->MergeOverlay(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001206 } else {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001207 result = table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001208 }
1209
1210 } else {
1211 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001212 // preserved and resource names are mangled.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001213 result =
1214 table_merger_->MergeAndMangle(Source(input), pkg->name, table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001215 }
1216
1217 if (!result) {
1218 return false;
1219 }
1220
1221 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001222 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001223 return true;
1224 }
1225
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001226 bool MergeResourceTable(io::IFile* file, bool override) {
1227 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001228 context_->GetDiagnostics()->Note(DiagMessage() << "merging resource table "
1229 << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001230 }
1231
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001232 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001233 if (!data) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001234 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001235 return false;
1236 }
1237
1238 std::unique_ptr<ResourceTable> table =
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001239 LoadTableFromPb(file->GetSource(), data->data(), data->size(), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001240 if (!table) {
1241 return false;
1242 }
1243
1244 bool result = false;
1245 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001246 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001247 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001248 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001249 }
1250 return result;
1251 }
1252
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001253 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc, bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001254 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001255 context_->GetDiagnostics()->Note(DiagMessage() << "merging '" << file_desc->name
1256 << "' from compiled file "
1257 << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001258 }
1259
1260 bool result = false;
1261 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001262 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001263 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001264 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001265 }
1266
1267 if (!result) {
1268 return false;
1269 }
1270
1271 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001272 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1273 if (exported_symbol.name.package.empty()) {
1274 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001275 }
1276
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001277 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001278
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001279 Maybe<ResourceName> mangled_name =
1280 context_->GetNameMangler()->MangleName(exported_symbol.name);
1281 if (mangled_name) {
1282 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001283 }
1284
1285 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001286 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1287 bool result = final_table_.AddResourceAllowMangled(
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001288 res_name, ConfigDescription::DefaultConfig(), std::string(), std::move(id),
1289 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001290 if (!result) {
1291 return false;
1292 }
1293 }
1294 return true;
1295 }
1296
1297 /**
1298 * Takes a path to load as a ZIP file and merges the files within into the
1299 * master ResourceTable.
1300 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001301 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001302 *
1303 * An io::IFileCollection is created from the ZIP file and added to the set of
1304 * io::IFileCollections that are open.
1305 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001306 bool MergeArchive(const std::string& input, bool override) {
1307 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001308 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive " << input);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309 }
1310
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001311 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001312 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001313 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001314 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001315 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001316 return false;
1317 }
1318
1319 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001320 for (auto iter = collection->Iterator(); iter->HasNext();) {
1321 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001322 error = true;
1323 }
1324 }
1325
1326 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001327 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001328 return !error;
1329 }
1330
1331 /**
1332 * Takes a path to load and merge into the master ResourceTable. If override
1333 * is true,
1334 * conflicting resources are allowed to override each other, in order of last
1335 * seen.
1336 *
1337 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1338 * as ZIP archive
1339 * and the files within are merged individually.
1340 *
1341 * Otherwise the files is processed on its own.
1342 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001343 bool MergePath(const std::string& path, bool override) {
1344 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1345 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1346 return MergeArchive(path, override);
1347 } else if (util::EndsWith(path, ".apk")) {
1348 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001349 }
1350
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001351 io::IFile* file = file_collection_->InsertFile(path);
1352 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001353 }
1354
1355 /**
1356 * Takes a file to load and merge into the master ResourceTable. If override
1357 * is true,
1358 * conflicting resources are allowed to override each other, in order of last
1359 * seen.
1360 *
1361 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1362 * merged into the
1363 * master ResourceTable. If the file ends with .flat, then it is treated like
1364 * a compiled file
1365 * and the header data is read and merged into the final ResourceTable.
1366 *
1367 * All other file types are ignored. This is because these files could be
1368 * coming from a zip,
1369 * where we could have other files like classes.dex.
1370 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001371 bool MergeFile(io::IFile* file, bool override) {
1372 const Source& src = file->GetSource();
1373 if (util::EndsWith(src.path, ".arsc.flat")) {
1374 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001375
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001376 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001377 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001378 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001379 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001380 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001381 return false;
1382 }
1383
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001384 CompiledFileInputStream input_stream(data->data(), data->size());
1385 uint32_t num_files = 0;
1386 if (!input_stream.ReadLittleEndian32(&num_files)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001387 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed read num files");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001388 return false;
1389 }
1390
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001391 for (uint32_t i = 0; i < num_files; i++) {
Adam Lesinski4ffea042017-08-10 15:37:28 -07001392 pb::internal::CompiledFile compiled_file;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001393 if (!input_stream.ReadCompiledFile(&compiled_file)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001394 context_->GetDiagnostics()->Error(DiagMessage(src)
1395 << "failed to read compiled file header");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001396 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001397 }
1398
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001399 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400 if (!input_stream.ReadDataMetaData(&offset, &len)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001401 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to read data meta data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001402 return false;
1403 }
1404
Adam Lesinski8cdca1b2017-09-28 15:50:03 -07001405 ResourceFile resource_file;
1406 std::string error;
1407 if (!DeserializeCompiledFileFromPb(compiled_file, &resource_file, &error)) {
1408 context_->GetDiagnostics()->Error(DiagMessage(src)
1409 << "failed to read compiled header: " << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001410 return false;
1411 }
1412
Adam Lesinski8cdca1b2017-09-28 15:50:03 -07001413 if (!MergeCompiledFile(file->CreateFileSegment(offset, len), &resource_file, override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001414 return false;
1415 }
1416 }
1417 return true;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001418 } else if (util::EndsWith(src.path, ".xml") || util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001419 // Since AAPT compiles these file types and appends .flat to them, seeing
1420 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001421 const StringPiece file_type = util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1422 context_->GetDiagnostics()->Error(DiagMessage(src) << "uncompiled " << file_type
1423 << " file passed as argument. Must be "
1424 "compiled first into .flat file.");
Adam Lesinski6a396c12016-10-20 14:38:23 -07001425 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001426 }
1427
1428 // Ignore non .flat files. This could be classes.dex or something else that
1429 // happens
1430 // to be in an archive.
1431 return true;
1432 }
1433
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001434 bool CopyAssetsDirsToApk(IArchiveWriter* writer) {
1435 std::map<std::string, std::unique_ptr<io::RegularFile>> merged_assets;
1436 for (const std::string& assets_dir : options_.assets_dirs) {
1437 Maybe<std::vector<std::string>> files =
1438 file::FindFiles(assets_dir, context_->GetDiagnostics(), nullptr);
1439 if (!files) {
1440 return false;
1441 }
1442
1443 for (const std::string& file : files.value()) {
1444 std::string full_key = "assets/" + file;
1445 std::string full_path = assets_dir;
1446 file::AppendPath(&full_path, file);
1447
1448 auto iter = merged_assets.find(full_key);
1449 if (iter == merged_assets.end()) {
1450 merged_assets.emplace(std::move(full_key),
1451 util::make_unique<io::RegularFile>(Source(std::move(full_path))));
1452 } else if (context_->IsVerbose()) {
1453 context_->GetDiagnostics()->Warn(DiagMessage(iter->second->GetSource())
1454 << "asset file overrides '" << full_path << "'");
1455 }
1456 }
1457 }
1458
1459 for (auto& entry : merged_assets) {
1460 uint32_t compression_flags = ArchiveEntry::kCompress;
1461 std::string extension = file::GetExtension(entry.first).to_string();
1462 if (options_.extensions_to_not_compress.count(extension) > 0) {
1463 compression_flags = 0u;
1464 }
1465
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001466 if (!io::CopyFileToArchive(context_, entry.second.get(), entry.first, compression_flags,
1467 writer)) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001468 return false;
1469 }
1470 }
1471 return true;
1472 }
1473
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001474 /**
1475 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001476 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001477 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001478 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set, xml::XmlResource* manifest,
1479 ResourceTable* table) {
Adam Lesinskib522f042017-04-21 16:57:59 -07001480 const bool keep_raw_values = context_->GetPackageType() == PackageType::kStaticLib;
Adam Lesinskiea13c1a2017-10-13 12:40:37 -07001481 bool result = FlattenXml(context_, manifest, "AndroidManifest.xml", keep_raw_values,
1482 true /*utf16*/, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001483 if (!result) {
1484 return false;
1485 }
1486
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001487 ResourceFileFlattenerOptions file_flattener_options;
1488 file_flattener_options.keep_raw_values = keep_raw_values;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001489 file_flattener_options.do_not_compress_anything = options_.do_not_compress_anything;
1490 file_flattener_options.extensions_to_not_compress = options_.extensions_to_not_compress;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001491 file_flattener_options.no_auto_version = options_.no_auto_version;
1492 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001493 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001494 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1495 file_flattener_options.update_proguard_spec =
1496 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001497
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001498 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001499
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001500 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001501 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001502 return false;
1503 }
1504
Adam Lesinskib522f042017-04-21 16:57:59 -07001505 if (context_->GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001506 if (!FlattenTableToPb(table, writer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001507 return false;
1508 }
1509 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001510 if (!FlattenTable(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001511 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001512 return false;
1513 }
1514 }
1515 return true;
1516 }
1517
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001518 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001519 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001520 std::unique_ptr<xml::XmlResource> manifest_xml =
1521 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1522 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001523 return 1;
1524 }
1525
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001526 // First extract the Package name without modifying it (via --rename-manifest-package).
1527 if (Maybe<AppInfo> maybe_app_info =
1528 ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001529 const AppInfo& app_info = maybe_app_info.value();
1530 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001531 }
1532
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001533 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1534 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 return 1;
1536 }
1537
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001538 Maybe<AppInfo> maybe_app_info =
1539 ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001540 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001541 return 1;
1542 }
1543
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001544 const AppInfo& app_info = maybe_app_info.value();
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001545 context_->SetMinSdkVersion(app_info.min_sdk_version.value_or_default(0));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001546
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001547 context_->SetNameManglerPolicy(NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001548
1549 // Override the package ID when it is "android".
1550 if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001551 context_->SetPackageId(0x01);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001552
1553 // Verify we're building a regular app.
Adam Lesinskib522f042017-04-21 16:57:59 -07001554 if (context_->GetPackageType() != PackageType::kApp) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001555 context_->GetDiagnostics()->Error(
1556 DiagMessage() << "package 'android' can only be built as a regular app");
1557 return 1;
1558 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001559 }
1560
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001561 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001562 return 1;
1563 }
1564
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001565 TableMergerOptions table_merger_options;
1566 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001567 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_, table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001568
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001569 if (context_->IsVerbose()) {
1570 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001571 << StringPrintf("linking package '%s' using package ID %02x",
1572 context_->GetCompilationPackage().data(),
1573 context_->GetPackageId()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001574 }
1575
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001576 for (const std::string& input : input_files) {
1577 if (!MergePath(input, false)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001578 context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing input");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001579 return 1;
1580 }
1581 }
1582
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001583 for (const std::string& input : options_.overlay_files) {
1584 if (!MergePath(input, true)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001585 context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing overlays");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001586 return 1;
1587 }
1588 }
1589
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001590 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001591 return 1;
1592 }
1593
Adam Lesinskib522f042017-04-21 16:57:59 -07001594 if (context_->GetPackageType() != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001595 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 if (!mover.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001597 context_->GetDiagnostics()->Error(DiagMessage() << "failed moving private attributes");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001598 return 1;
1599 }
1600
1601 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001602 IdAssigner id_assigner(&options_.stable_id_map);
1603 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001604 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001605 return 1;
1606 }
1607
1608 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001609 if (options_.resource_id_map_path) {
1610 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001611 for (auto& type : package->types) {
1612 for (auto& entry : type->entries) {
1613 ResourceName name(package->name, type->type, entry->name);
1614 // The IDs are guaranteed to exist.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001615 options_.stable_id_map[std::move(name)] =
1616 ResourceId(package->id.value(), type->id.value(), entry->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001617 }
1618 }
1619 }
1620
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001621 if (!WriteStableIdMapToPath(context_->GetDiagnostics(), options_.stable_id_map,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001622 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001623 return 1;
1624 }
1625 }
1626 } else {
1627 // Static libs are merged with other apps, and ID collisions are bad, so
1628 // verify that
1629 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001630 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001631 return 1;
1632 }
1633 }
1634
1635 // Add the names to mangle based on our source merge earlier.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001636 context_->SetNameManglerPolicy(
1637 NameManglerPolicy{context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001638
1639 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001640 context_->GetExternalSymbols()->PrependSource(
1641 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001642
Adam Lesinski1e4b0e52017-04-27 15:01:10 -07001643 // Workaround for pre-O runtime that would treat negative resource IDs
1644 // (any ID with a package ID > 7f) as invalid. Intercept any ID (PPTTEEEE) with PP > 0x7f
1645 // and type == 'id', and return the ID 0x7fPPEEEE. IDs don't need to be real resources, they
1646 // are just identifiers.
1647 if (context_->GetMinSdkVersion() < SDK_O && context_->GetPackageType() == PackageType::kApp) {
1648 if (context_->IsVerbose()) {
1649 context_->GetDiagnostics()->Note(DiagMessage()
1650 << "enabling pre-O feature split ID rewriting");
1651 }
1652 context_->GetExternalSymbols()->SetDelegate(
1653 util::make_unique<FeatureSplitSymbolTableDelegate>(context_));
1654 }
1655
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001656 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001657 if (!linker.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001658 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking references");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001659 return 1;
1660 }
1661
Adam Lesinskib522f042017-04-21 16:57:59 -07001662 if (context_->GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001663 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001664 context_->GetDiagnostics()->Warn(DiagMessage()
1665 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001666 }
1667 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001668 ProductFilter product_filter(options_.products);
1669 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001670 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001671 return 1;
1672 }
1673 }
1674
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001675 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001676 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001677 if (!versioner.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001678 context_->GetDiagnostics()->Error(DiagMessage() << "failed versioning styles");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001679 return 1;
1680 }
1681 }
1682
Adam Lesinskib522f042017-04-21 16:57:59 -07001683 if (context_->GetPackageType() != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001684 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001685 context_->GetDiagnostics()->Note(DiagMessage()
1686 << "collapsing resource versions for minimum SDK "
1687 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001688 }
1689
1690 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001691 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001692 return 1;
1693 }
1694 }
1695
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001696 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001697 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001698 if (!deduper.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001699 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001700 return 1;
1701 }
1702 }
1703
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001704 proguard::KeepSet proguard_keep_set;
1705 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001706
Adam Lesinskib522f042017-04-21 16:57:59 -07001707 if (context_->GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001708 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001709 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001710 context_->GetDiagnostics()->Warn(DiagMessage()
1711 << "can't strip resources when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001712 }
1713 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001714 // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or
1715 // equal to the minSdk.
1716 options_.split_constraints =
1717 AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001718
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001719 TableSplitter table_splitter(options_.split_constraints, options_.table_splitter_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001720 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001721 return 1;
1722 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001723 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001724
1725 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001726 auto path_iter = options_.split_paths.begin();
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001727 auto split_constraints_iter = options_.split_constraints.begin();
1728 for (std::unique_ptr<ResourceTable>& split_table : table_splitter.splits()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001729 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001730 context_->GetDiagnostics()->Note(DiagMessage(*path_iter)
1731 << "generating split with configurations '"
1732 << util::Joiner(split_constraints_iter->configs, ", ")
1733 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001734 }
1735
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001736 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(*path_iter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001737 if (!archive_writer) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001738 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001739 return 1;
1740 }
1741
1742 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001743 std::unique_ptr<xml::XmlResource> split_manifest =
1744 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001745
1746 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001747 if (!linker.Consume(context_, split_manifest.get())) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001748 context_->GetDiagnostics()->Error(DiagMessage()
1749 << "failed to create Split AndroidManifest.xml");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001750 return 1;
1751 }
1752
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001753 if (!WriteApk(archive_writer.get(), &proguard_keep_set, split_manifest.get(),
1754 split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001755 return 1;
1756 }
1757
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001758 ++path_iter;
1759 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001760 }
1761 }
1762
1763 // Start writing the base APK.
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001764 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(options_.output_path);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001765 if (!archive_writer) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001766 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001767 return 1;
1768 }
1769
1770 bool error = false;
1771 {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001772 // AndroidManifest.xml has no resource name, but the CallSite is built from the name
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001773 // (aka, which package the AndroidManifest.xml is coming from).
1774 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001775 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001776
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001777 XmlReferenceLinker manifest_linker;
1778 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1779 if (options_.generate_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001780 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1781 manifest_xml.get(), &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001782 error = true;
1783 }
1784
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001785 if (options_.generate_main_dex_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001786 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1787 manifest_xml.get(),
1788 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001789 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001790 }
1791
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001792 if (options_.generate_java_class_path) {
1793 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001794 error = true;
1795 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001796 }
1797
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001798 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001799 // PackageParser will fail if URIs are removed from
1800 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001801 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1802 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001803 error = true;
1804 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001805 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001806 } else {
1807 error = true;
1808 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001809 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001810
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001811 if (error) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001812 context_->GetDiagnostics()->Error(DiagMessage() << "failed processing manifest");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001813 return 1;
1814 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001815
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001816 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(), &final_table_)) {
1817 return 1;
1818 }
1819
1820 if (!CopyAssetsDirsToApk(archive_writer.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001821 return 1;
1822 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001823
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001824 if (options_.generate_java_class_path) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001825 if (!GenerateJavaClasses()) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001826 return 1;
1827 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001828 }
1829
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001830 if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001831 return 1;
1832 }
1833
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001834 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1835 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001836 return 1;
1837 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001838 return 0;
1839 }
1840
1841 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001842 LinkOptions options_;
1843 LinkContext* context_;
1844 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001845
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001846 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001847
1848 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001849 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001850
1851 // A vector of IFileCollections. This is mainly here to keep ownership of the
1852 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001853 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001854
1855 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001856 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001857 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001858
1859 // The set of shared libraries being used, mapping their assigned package ID to package name.
1860 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001861};
1862
Chris Warrington820d72a2017-04-27 15:27:01 +01001863int Link(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
1864 LinkContext context(diagnostics);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001865 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001866 std::vector<std::string> overlay_arg_list;
1867 std::vector<std::string> extra_java_packages;
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001868 Maybe<std::string> package_id;
Adam Lesinski113ee092017-04-03 19:38:25 -07001869 std::vector<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001870 Maybe<std::string> preferred_density;
1871 Maybe<std::string> product_list;
1872 bool legacy_x_flag = false;
1873 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001874 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001875 bool shared_lib = false;
1876 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001877 Maybe<std::string> stable_id_file_path;
1878 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001879 Flags flags =
1880 Flags()
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001881 .RequiredFlag("-o", "Output path.", &options.output_path)
1882 .RequiredFlag("--manifest", "Path to the Android manifest to build.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001883 &options.manifest_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001884 .OptionalFlagList("-I", "Adds an Android APK to link against.", &options.include_paths)
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001885 .OptionalFlagList("-A",
1886 "An assets directory to include in the APK. These are unprocessed.",
1887 &options.assets_dirs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001888 .OptionalFlagList("-R",
1889 "Compilation unit to link, using `overlay` semantics.\n"
1890 "The last conflicting resource given takes precedence.",
1891 &overlay_arg_list)
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001892 .OptionalFlag("--package-id",
1893 "Specify the package ID to use for this app. Must be greater or equal to\n"
1894 "0x7f and can't be used with --static-lib or --shared-lib.",
1895 &package_id)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001896 .OptionalFlag("--java", "Directory in which to generate R.java.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001897 &options.generate_java_class_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001898 .OptionalFlag("--proguard", "Output file for generated Proguard rules.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001899 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001900 .OptionalFlag("--proguard-main-dex",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001901 "Output file for generated Proguard rules for the main dex.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001902 &options.generate_main_dex_proguard_rules_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001903 .OptionalSwitch("--no-auto-version",
1904 "Disables automatic style and layout SDK versioning.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001905 &options.no_auto_version)
1906 .OptionalSwitch("--no-version-vectors",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001907 "Disables automatic versioning of vector drawables. Use this only\n"
1908 "when building with vector drawable support library.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001909 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001910 .OptionalSwitch("--no-version-transitions",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001911 "Disables automatic versioning of transition resources. Use this only\n"
1912 "when building with transition support library.",
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001913 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001914 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001915 "Disables automatic deduping of resources with\n"
1916 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001917 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001918 .OptionalSwitch("--enable-sparse-encoding",
1919 "Enables encoding sparse entries using a binary search tree.\n"
1920 "This decreases APK size at the cost of resource retrieval performance.",
1921 &options.table_flattener_options.use_sparse_entries)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001922 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001923 &legacy_x_flag)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001924 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001925 &require_localization)
Adam Lesinski113ee092017-04-03 19:38:25 -07001926 .OptionalFlagList("-c",
Adam Lesinski418763f2017-04-11 17:36:53 -07001927 "Comma separated list of configurations to include. The default\n"
1928 "is all configurations.",
1929 &configs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001930 .OptionalFlag("--preferred-density",
1931 "Selects the closest matching density and strips out all others.",
1932 &preferred_density)
1933 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001934 .OptionalSwitch("--output-to-dir",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001935 "Outputs the APK contents to a directory specified by -o.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001936 &options.output_to_directory)
1937 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001938 "Removes XML namespace prefix and URI information from\n"
1939 "AndroidManifest.xml and XML binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001940 &options.no_xml_namespaces)
1941 .OptionalFlag("--min-sdk-version",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001942 "Default minimum SDK version to use for AndroidManifest.xml.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001943 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001944 .OptionalFlag("--target-sdk-version",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001945 "Default target SDK version to use for AndroidManifest.xml.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001946 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001947 .OptionalFlag("--version-code",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001948 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
1949 "present.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001950 &options.manifest_fixer_options.version_code_default)
1951 .OptionalFlag("--version-name",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001952 "Version name to inject into the AndroidManifest.xml if none is present.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001953 &options.manifest_fixer_options.version_name_default)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001954 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
1955 &shared_lib)
1956 .OptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001957 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001958 "Merge all library resources under the app's package.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001959 &options.no_static_lib_packages)
1960 .OptionalSwitch("--non-final-ids",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001961 "Generates R.java without the final modifier. This is implied when\n"
1962 "--static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001963 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001964 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001965 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001966 .OptionalFlag("--emit-ids",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001967 "Emit a file at the given path with a list of name to ID mappings,\n"
1968 "suitable for use with --stable-ids.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001969 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001970 .OptionalFlag("--private-symbols",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001971 "Package name to use when generating R.java for private symbols.\n"
1972 "If not specified, public and private symbols will use the application's\n"
1973 "package name.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001974 &options.private_symbols)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001975 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001976 &options.custom_java_package)
1977 .OptionalFlagList("--extra-packages",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001978 "Generate the same R.java but with different package names.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001979 &extra_java_packages)
1980 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001981 "Adds a JavaDoc annotation to all generated Java classes.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001982 &options.javadoc_annotations)
Adam Lesinski418763f2017-04-11 17:36:53 -07001983 .OptionalFlag("--output-text-symbols",
1984 "Generates a text file containing the resource symbols of the R class in\n"
1985 "the specified folder.",
1986 &options.generate_text_symbols_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001987 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001988 "Allows the addition of new resources in overlays without\n"
1989 "<add-resource> tags.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001990 &options.auto_add_overlay)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001991 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001992 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001993 .OptionalFlag("--rename-instrumentation-target-package",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001994 "Changes the name of the target package for instrumentation. Most useful\n"
1995 "when used in conjunction with --rename-manifest-package.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001996 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001997 .OptionalFlagList("-0", "File extensions not to compress.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001998 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001999 .OptionalFlagList("--split",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002000 "Split resources matching a set of configs out to a Split APK.\n"
Adam Lesinskidb091572017-04-13 12:48:56 -07002001 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
2002 "On Windows, use a semicolon ';' separator instead.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002003 &split_args)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002004 .OptionalSwitch("-v", "Enables verbose logging.", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002005
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002006 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002007 return 1;
2008 }
2009
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002010 // Expand all argument-files passed into the command line. These start with '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002011 std::vector<std::string> arg_list;
2012 for (const std::string& arg : flags.GetArgs()) {
2013 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002014 const std::string path = arg.substr(1, arg.size() - 1);
2015 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002016 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
2017 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002018 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002019 }
2020 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002021 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002022 }
2023 }
2024
2025 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002026 for (const std::string& arg : overlay_arg_list) {
2027 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002028 const std::string path = arg.substr(1, arg.size() - 1);
2029 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002030 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
2031 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002032 return 1;
2033 }
2034 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002035 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002036 }
2037 }
2038
2039 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002040 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002041 }
2042
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002043 if (shared_lib && static_lib) {
2044 context.GetDiagnostics()->Error(DiagMessage()
2045 << "only one of --shared-lib and --static-lib can be defined");
2046 return 1;
2047 }
2048
2049 if (shared_lib) {
Adam Lesinskib522f042017-04-21 16:57:59 -07002050 context.SetPackageType(PackageType::kSharedLib);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002051 context.SetPackageId(0x00);
2052 } else if (static_lib) {
Adam Lesinskib522f042017-04-21 16:57:59 -07002053 context.SetPackageType(PackageType::kStaticLib);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002054 context.SetPackageId(kAppPackageId);
2055 } else {
Adam Lesinskib522f042017-04-21 16:57:59 -07002056 context.SetPackageType(PackageType::kApp);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002057 context.SetPackageId(kAppPackageId);
2058 }
2059
2060 if (package_id) {
Adam Lesinskib522f042017-04-21 16:57:59 -07002061 if (context.GetPackageType() != PackageType::kApp) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002062 context.GetDiagnostics()->Error(
2063 DiagMessage() << "can't specify --package-id when not building a regular app");
2064 return 1;
2065 }
2066
2067 const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id.value());
2068 if (!maybe_package_id_int) {
2069 context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id.value()
2070 << "' is not a valid integer");
2071 return 1;
2072 }
2073
2074 const uint32_t package_id_int = maybe_package_id_int.value();
2075 if (package_id_int < kAppPackageId || package_id_int > std::numeric_limits<uint8_t>::max()) {
2076 context.GetDiagnostics()->Error(
2077 DiagMessage() << StringPrintf(
2078 "invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int));
2079 return 1;
2080 }
2081 context.SetPackageId(static_cast<uint8_t>(package_id_int));
2082 }
2083
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002084 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002085 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002086 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002087 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002088 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002089 }
2090 }
2091
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002092 if (product_list) {
2093 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002094 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002095 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002096 }
2097 }
2098 }
2099
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002100 std::unique_ptr<IConfigFilter> filter;
Mihai Nitaf4dacf22017-04-07 08:25:06 -07002101 if (!configs.empty()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002102 filter = ParseConfigFilterParameters(configs, context.GetDiagnostics());
2103 if (filter == nullptr) {
2104 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002105 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002106 options.table_splitter_options.config_filter = filter.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002107 }
2108
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002109 if (preferred_density) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002110 Maybe<uint16_t> density =
2111 ParseTargetDensityParameter(preferred_density.value(), context.GetDiagnostics());
2112 if (!density) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002113 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002114 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002115 options.table_splitter_options.preferred_densities.push_back(density.value());
2116 }
Adam Lesinskic51562c2016-04-28 11:12:38 -07002117
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002118 // Parse the split parameters.
2119 for (const std::string& split_arg : split_args) {
2120 options.split_paths.push_back({});
2121 options.split_constraints.push_back({});
2122 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(), &options.split_paths.back(),
2123 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002124 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002125 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002126 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002127
Adam Lesinskib522f042017-04-21 16:57:59 -07002128 if (context.GetPackageType() != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002129 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2130 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002131 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002132 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002133 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002134
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002135 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002136 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002137 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2138 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2139 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2140 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2141
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002142 // Turn off auto versioning for static-libs.
Adam Lesinskib522f042017-04-21 16:57:59 -07002143 if (context.GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002144 options.no_auto_version = true;
2145 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002146 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002147 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002148
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002149 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002150 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002151}
2152
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002153} // namespace aapt