blob: 5fc35b8112132c35a4d1352c3c7dba872bcc0050 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include <sys/stat.h>
18
19#include <fstream>
20#include <queue>
21#include <unordered_map>
22#include <vector>
23
24#include "android-base/errors.h"
25#include "android-base/file.h"
Adam Lesinskif34b6f42017-03-03 16:33:26 -080026#include "android-base/stringprintf.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080027#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028#include "google/protobuf/io/coded_stream.h"
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "AppInfo.h"
31#include "Debug.h"
32#include "Flags.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080033#include "Locale.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034#include "NameMangler.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080035#include "ResourceUtils.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070036#include "cmd/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037#include "compile/IdAssigner.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080038#include "filter/ConfigFilter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039#include "flatten/Archive.h"
40#include "flatten/TableFlattener.h"
41#include "flatten/XmlFlattener.h"
Adam Lesinski06460ef2017-03-14 18:52:13 -070042#include "io/BigBufferInputStream.h"
Adam Lesinski5b54ca22017-08-02 14:57:43 -070043#include "io/FileInputStream.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080044#include "io/FileSystem.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070045#include "io/Util.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080046#include "io/ZipArchive.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070047#include "java/JavaClassGenerator.h"
48#include "java/ManifestClassGenerator.h"
49#include "java/ProguardRules.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050#include "link/Linkers.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051#include "link/ManifestFixer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080052#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053#include "link/TableMerger.h"
Adam Lesinskic744ae82017-05-17 19:28:38 -070054#include "link/XmlCompatVersioner.h"
Adam Lesinskid48944a2017-02-21 14:22:30 -080055#include "optimize/ResourceDeduper.h"
56#include "optimize/VersionCollapser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057#include "process/IResourceTableConsumer.h"
58#include "process/SymbolTable.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080059#include "proto/ProtoSerialize.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080060#include "split/TableSplitter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061#include "unflatten/BinaryResourceParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062#include "util/Files.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080063#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064
Adam Lesinski5b54ca22017-08-02 14:57:43 -070065using ::aapt::io::FileInputStream;
66using ::android::StringPiece;
67using ::android::base::StringPrintf;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070068
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069namespace aapt {
70
71struct LinkOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 std::string output_path;
73 std::string manifest_path;
74 std::vector<std::string> include_paths;
75 std::vector<std::string> overlay_files;
Adam Lesinskib39ad7c2017-03-13 11:40:48 -070076 std::vector<std::string> assets_dirs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 bool output_to_directory = false;
78 bool auto_add_overlay = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 // Java/Proguard options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 Maybe<std::string> generate_java_class_path;
82 Maybe<std::string> custom_java_package;
83 std::set<std::string> extra_java_packages;
Adam Lesinski418763f2017-04-11 17:36:53 -070084 Maybe<std::string> generate_text_symbols_path;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 Maybe<std::string> generate_proguard_rules_path;
86 Maybe<std::string> generate_main_dex_proguard_rules_path;
87 bool generate_non_final_ids = false;
88 std::vector<std::string> javadoc_annotations;
89 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070090
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 // Optimizations/features.
92 bool no_auto_version = false;
93 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +090094 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 bool no_resource_deduping = false;
96 bool no_xml_namespaces = false;
97 bool do_not_compress_anything = false;
98 std::unordered_set<std::string> extensions_to_not_compress;
99
100 // Static lib options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 bool no_static_lib_packages = false;
102
103 // AndroidManifest.xml massaging options.
104 ManifestFixerOptions manifest_fixer_options;
105
106 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700108
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800109 // Flattening options.
110 TableFlattenerOptions table_flattener_options;
111
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 TableSplitterOptions table_splitter_options;
114 std::vector<SplitConstraints> split_constraints;
115 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 std::unordered_map<ResourceName, ResourceId> stable_id_map;
119 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120};
121
Adam Lesinski64587af2016-02-18 18:33:06 -0800122class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 public:
Chris Warrington820d72a2017-04-27 15:27:01 +0100124 LinkContext(IDiagnostics* diagnostics)
125 : diagnostics_(diagnostics), name_mangler_({}), symbols_(&name_mangler_) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700126 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127
Adam Lesinskib522f042017-04-21 16:57:59 -0700128 PackageType GetPackageType() override {
129 return package_type_;
130 }
131
132 void SetPackageType(PackageType type) {
133 package_type_ = type;
134 }
135
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700136 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100137 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700138 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700140 NameMangler* GetNameMangler() override {
141 return &name_mangler_;
142 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
145 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 const std::string& GetCompilationPackage() override {
149 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800153 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800155
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700156 uint8_t GetPackageId() override {
157 return package_id_;
158 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700160 void SetPackageId(uint8_t id) {
161 package_id_ = id;
162 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800163
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700164 SymbolTable* GetExternalSymbols() override {
165 return &symbols_;
166 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800167
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700168 bool IsVerbose() override {
169 return verbose_;
170 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800171
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700172 void SetVerbose(bool val) {
173 verbose_ = val;
174 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800175
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700176 int GetMinSdkVersion() override {
177 return min_sdk_version_;
178 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700179
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700180 void SetMinSdkVersion(int minSdk) {
181 min_sdk_version_ = minSdk;
182 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 DISALLOW_COPY_AND_ASSIGN(LinkContext);
186
Adam Lesinskib522f042017-04-21 16:57:59 -0700187 PackageType package_type_ = PackageType::kApp;
Chris Warrington820d72a2017-04-27 15:27:01 +0100188 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 NameMangler name_mangler_;
190 std::string compilation_package_;
191 uint8_t package_id_ = 0x0;
192 SymbolTable symbols_;
193 bool verbose_ = false;
194 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700195};
196
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700197// A custom delegate that generates compatible pre-O IDs for use with feature splits.
198// Feature splits use package IDs > 7f, which in Java (since Java doesn't have unsigned ints)
199// is interpreted as a negative number. Some verification was wrongly assuming negative values
200// were invalid.
201//
202// This delegate will attempt to masquerade any '@id/' references with ID 0xPPTTEEEE,
203// where PP > 7f, as 0x7fPPEEEE. Any potential overlapping is verified and an error occurs if such
204// an overlap exists.
205class FeatureSplitSymbolTableDelegate : public DefaultSymbolTableDelegate {
206 public:
207 FeatureSplitSymbolTableDelegate(IAaptContext* context) : context_(context) {
208 }
209
210 virtual ~FeatureSplitSymbolTableDelegate() = default;
211
212 virtual std::unique_ptr<SymbolTable::Symbol> FindByName(
213 const ResourceName& name,
214 const std::vector<std::unique_ptr<ISymbolSource>>& sources) override {
215 std::unique_ptr<SymbolTable::Symbol> symbol =
216 DefaultSymbolTableDelegate::FindByName(name, sources);
217 if (symbol == nullptr) {
218 return {};
219 }
220
221 // Check to see if this is an 'id' with the target package.
222 if (name.type == ResourceType::kId && symbol->id) {
223 ResourceId* id = &symbol->id.value();
224 if (id->package_id() > kAppPackageId) {
225 // Rewrite the resource ID to be compatible pre-O.
226 ResourceId rewritten_id(kAppPackageId, id->package_id(), id->entry_id());
227
228 // Check that this doesn't overlap another resource.
229 if (DefaultSymbolTableDelegate::FindById(rewritten_id, sources) != nullptr) {
230 // The ID overlaps, so log a message (since this is a weird failure) and fail.
231 context_->GetDiagnostics()->Error(DiagMessage() << "Failed to rewrite " << name
232 << " for pre-O feature split support");
233 return {};
234 }
235
236 if (context_->IsVerbose()) {
237 context_->GetDiagnostics()->Note(DiagMessage() << "rewriting " << name << " (" << *id
238 << ") -> (" << rewritten_id << ")");
239 }
240
241 *id = rewritten_id;
242 }
243 }
244 return symbol;
245 }
246
247 private:
248 DISALLOW_COPY_AND_ASSIGN(FeatureSplitSymbolTableDelegate);
249
250 IAaptContext* context_;
251};
252
Adam Lesinskic744ae82017-05-17 19:28:38 -0700253static bool FlattenXml(IAaptContext* context, xml::XmlResource* xml_res, const StringPiece& path,
Adam Lesinski9c402502017-10-13 12:40:37 -0700254 bool keep_raw_values, bool utf16, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 BigBuffer buffer(1024);
256 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 options.keep_raw_values = keep_raw_values;
Adam Lesinski9c402502017-10-13 12:40:37 -0700258 options.use_utf16 = utf16;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800261 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 }
263
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 if (context->IsVerbose()) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700265 context->GetDiagnostics()->Note(DiagMessage(path) << "writing to archive (keep_raw_values="
266 << (keep_raw_values ? "true" : "false")
267 << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 }
269
Adam Lesinski06460ef2017-03-14 18:52:13 -0700270 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700271 return io::CopyInputStreamToArchive(context, &input_stream, path.to_string(),
272 ArchiveEntry::kCompress, writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800273}
274
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700275static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source, const void* data,
276 size_t len, IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 pb::ResourceTable pb_table;
278 if (!pb_table.ParseFromArray(data, len)) {
279 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 return {};
281 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800282
Adam Lesinski06460ef2017-03-14 18:52:13 -0700283 std::unique_ptr<ResourceTable> table = DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 if (!table) {
285 return {};
286 }
287 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800288}
289
Adam Lesinski5b54ca22017-08-02 14:57:43 -0700290// Inflates an XML file from the source path.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700291static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path, IDiagnostics* diag) {
Adam Lesinski5b54ca22017-08-02 14:57:43 -0700292 FileInputStream fin(path);
293 if (fin.HadError()) {
294 diag->Error(DiagMessage(path) << "failed to load XML file: " << fin.GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 return {};
296 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800298}
299
Adam Lesinski355f2852016-02-13 20:26:45 -0800300struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 bool no_auto_version = false;
302 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900303 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 bool no_xml_namespaces = false;
305 bool keep_raw_values = false;
306 bool do_not_compress_anything = false;
307 bool update_proguard_spec = false;
308 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800309};
310
Adam Lesinskic744ae82017-05-17 19:28:38 -0700311// A sampling of public framework resource IDs.
312struct R {
313 struct attr {
314 enum : uint32_t {
315 paddingLeft = 0x010100d6u,
316 paddingRight = 0x010100d8u,
317 paddingHorizontal = 0x0101053du,
318
319 paddingTop = 0x010100d7u,
320 paddingBottom = 0x010100d9u,
321 paddingVertical = 0x0101053eu,
322
323 layout_marginLeft = 0x010100f7u,
324 layout_marginRight = 0x010100f9u,
325 layout_marginHorizontal = 0x0101053bu,
326
327 layout_marginTop = 0x010100f8u,
328 layout_marginBottom = 0x010100fau,
329 layout_marginVertical = 0x0101053cu,
330 };
331 };
332};
333
Adam Lesinski355f2852016-02-13 20:26:45 -0800334class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 public:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700336 ResourceFileFlattener(const ResourceFileFlattenerOptions& options, IAaptContext* context,
Adam Lesinskic744ae82017-05-17 19:28:38 -0700337 proguard::KeepSet* keep_set);
Adam Lesinski355f2852016-02-13 20:26:45 -0800338
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800340
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 private:
342 struct FileOperation {
343 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700344
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 // The entry this file came from.
Adam Lesinskibb94f322017-07-12 07:41:55 -0700346 ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700347
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 // The file to copy as-is.
Adam Lesinskibb94f322017-07-12 07:41:55 -0700349 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700350
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700352 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700353
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355 std::string dst_path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800357
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800359
Adam Lesinskic744ae82017-05-17 19:28:38 -0700360 std::vector<std::unique_ptr<xml::XmlResource>> LinkAndVersionXmlFile(ResourceTable* table,
361 FileOperation* file_op);
Adam Lesinski355f2852016-02-13 20:26:45 -0800362
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 ResourceFileFlattenerOptions options_;
364 IAaptContext* context_;
365 proguard::KeepSet* keep_set_;
Adam Lesinskic744ae82017-05-17 19:28:38 -0700366 XmlCompatVersioner::Rules rules_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800367};
368
Adam Lesinskic744ae82017-05-17 19:28:38 -0700369ResourceFileFlattener::ResourceFileFlattener(const ResourceFileFlattenerOptions& options,
370 IAaptContext* context, proguard::KeepSet* keep_set)
371 : options_(options), context_(context), keep_set_(keep_set) {
372 SymbolTable* symm = context_->GetExternalSymbols();
373
374 // Build up the rules for degrading newer attributes to older ones.
375 // NOTE(adamlesinski): These rules are hardcoded right now, but they should be
376 // generated from the attribute definitions themselves (b/62028956).
377 if (const SymbolTable::Symbol* s = symm->FindById(R::attr::paddingHorizontal)) {
378 std::vector<ReplacementAttr> replacements{
379 {"paddingLeft", R::attr::paddingLeft,
380 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
381 {"paddingRight", R::attr::paddingRight,
382 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
383 };
384 rules_[R::attr::paddingHorizontal] =
385 util::make_unique<DegradeToManyRule>(std::move(replacements));
386 }
387
388 if (const SymbolTable::Symbol* s = symm->FindById(R::attr::paddingVertical)) {
389 std::vector<ReplacementAttr> replacements{
390 {"paddingTop", R::attr::paddingTop,
391 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
392 {"paddingBottom", R::attr::paddingBottom,
393 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
394 };
395 rules_[R::attr::paddingVertical] =
396 util::make_unique<DegradeToManyRule>(std::move(replacements));
397 }
398
399 if (const SymbolTable::Symbol* s = symm->FindById(R::attr::layout_marginHorizontal)) {
400 std::vector<ReplacementAttr> replacements{
401 {"layout_marginLeft", R::attr::layout_marginLeft,
402 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
403 {"layout_marginRight", R::attr::layout_marginRight,
404 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
405 };
406 rules_[R::attr::layout_marginHorizontal] =
407 util::make_unique<DegradeToManyRule>(std::move(replacements));
408 }
409
410 if (const SymbolTable::Symbol* s = symm->FindById(R::attr::layout_marginVertical)) {
411 std::vector<ReplacementAttr> replacements{
412 {"layout_marginTop", R::attr::layout_marginTop,
413 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
414 {"layout_marginBottom", R::attr::layout_marginBottom,
415 Attribute(false, android::ResTable_map::TYPE_DIMENSION)},
416 };
417 rules_[R::attr::layout_marginVertical] =
418 util::make_unique<DegradeToManyRule>(std::move(replacements));
419 }
420}
421
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
423 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 return 0;
425 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800426
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 for (const std::string& extension : options_.extensions_to_not_compress) {
428 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800430 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431 }
432 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800433}
434
Adam Lesinskibb94f322017-07-12 07:41:55 -0700435static bool IsTransitionElement(const std::string& name) {
436 return name == "fade" || name == "changeBounds" || name == "slide" || name == "explode" ||
437 name == "changeImageTransform" || name == "changeTransform" ||
438 name == "changeClipBounds" || name == "autoTransition" || name == "recolor" ||
439 name == "changeScroll" || name == "transitionSet" || name == "transition" ||
440 name == "transitionManager";
441}
442
443static bool IsVectorElement(const std::string& name) {
444 return name == "vector" || name == "animated-vector" || name == "pathInterpolator" ||
ztenghuif49cce32017-10-13 15:56:08 -0700445 name == "objectAnimator" || name == "gradient";
Adam Lesinskibb94f322017-07-12 07:41:55 -0700446}
447
Adam Lesinskic744ae82017-05-17 19:28:38 -0700448template <typename T>
449std::vector<T> make_singleton_vec(T&& val) {
450 std::vector<T> vec;
451 vec.emplace_back(std::forward<T>(val));
452 return vec;
453}
454
455std::vector<std::unique_ptr<xml::XmlResource>> ResourceFileFlattener::LinkAndVersionXmlFile(
456 ResourceTable* table, FileOperation* file_op) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700459
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 if (context_->IsVerbose()) {
461 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700462 }
463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 XmlReferenceLinker xml_linker;
465 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700466 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 }
468
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700469 if (options_.update_proguard_spec && !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700470 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 }
472
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700473 if (options_.no_xml_namespaces) {
474 XmlNamespaceRemover namespace_remover;
475 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700476 return {};
Adam Lesinski355f2852016-02-13 20:26:45 -0800477 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700478 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800479
Adam Lesinskibb94f322017-07-12 07:41:55 -0700480 if (options_.no_auto_version) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700481 return make_singleton_vec(std::move(file_op->xml_to_flatten));
482 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700483
Adam Lesinskibb94f322017-07-12 07:41:55 -0700484 if (options_.no_version_vectors || options_.no_version_transitions) {
485 // Skip this if it is a vector or animated-vector.
Adam Lesinski5c33fb52017-08-09 10:54:23 -0700486 xml::Element* el = doc->root.get();
Adam Lesinskibb94f322017-07-12 07:41:55 -0700487 if (el && el->namespace_uri.empty()) {
488 if ((options_.no_version_vectors && IsVectorElement(el->name)) ||
489 (options_.no_version_transitions && IsTransitionElement(el->name))) {
490 return make_singleton_vec(std::move(file_op->xml_to_flatten));
491 }
492 }
493 }
494
Adam Lesinskic744ae82017-05-17 19:28:38 -0700495 const ConfigDescription& config = file_op->config;
496 ResourceEntry* entry = file_op->entry;
497
498 XmlCompatVersioner xml_compat_versioner(&rules_);
499 const util::Range<ApiVersion> api_range{config.sdkVersion,
500 FindNextApiVersionForConfig(entry, config)};
501 return xml_compat_versioner.Process(context_, doc, api_range);
Adam Lesinski355f2852016-02-13 20:26:45 -0800502}
503
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700504bool ResourceFileFlattener::Flatten(ResourceTable* table, IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 bool error = false;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700506 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation> config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800507
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 for (auto& pkg : table->packages) {
509 for (auto& type : pkg->types) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700510 // Sort by config and name, so that we get better locality in the zip file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 config_sorted_files.clear();
Adam Lesinskibb94f322017-07-12 07:41:55 -0700512 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800513
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 // Populate the queue with all files in the ResourceTable.
515 for (auto& entry : type->entries) {
Adam Lesinskibb94f322017-07-12 07:41:55 -0700516 for (auto& config_value : entry->values) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700517 // WARNING! Do not insert or remove any resources while executing in this scope. It will
518 // corrupt the iteration order.
519
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700520 FileReference* file_ref = ValueCast<FileReference>(config_value->value.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700522 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700524
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700525 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700529 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 }
531
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 FileOperation file_op;
533 file_op.entry = entry.get();
534 file_op.dst_path = *file_ref->path;
535 file_op.config = config_value->config;
Adam Lesinskic744ae82017-05-17 19:28:38 -0700536 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700538 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 if (type->type != ResourceType::kRaw &&
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700540 (util::EndsWith(src_path, ".xml.flat") || util::EndsWith(src_path, ".xml"))) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 << "failed to open file");
545 return false;
546 }
547
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700548 file_op.xml_to_flatten = xml::Inflate(data->data(), data->size(),
549 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 return false;
553 }
554
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 file_op.xml_to_flatten->file.config = config_value->config;
556 file_op.xml_to_flatten->file.source = file_ref->GetSource();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700557 file_op.xml_to_flatten->file.name = ResourceName(pkg->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 }
Adam Lesinskic744ae82017-05-17 19:28:38 -0700559
560 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
561 // else we end up copying the string in the std::make_pair() method,
562 // then creating a StringPiece from the copy, which would cause us
563 // to end up referencing garbage in the map.
564 const StringPiece entry_name(entry->name);
565 config_sorted_files[std::make_pair(config_value->config, entry_name)] =
566 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 }
568 }
569
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571 for (auto& map_entry : config_sorted_files) {
572 const ConfigDescription& config = map_entry.first.first;
Adam Lesinskic744ae82017-05-17 19:28:38 -0700573 FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 if (file_op.xml_to_flatten) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700576 std::vector<std::unique_ptr<xml::XmlResource>> versioned_docs =
577 LinkAndVersionXmlFile(table, &file_op);
Adam Lesinskide7b7422017-08-07 11:56:32 -0700578 if (versioned_docs.empty()) {
579 error = true;
580 continue;
581 }
582
Adam Lesinskic744ae82017-05-17 19:28:38 -0700583 for (std::unique_ptr<xml::XmlResource>& doc : versioned_docs) {
584 std::string dst_path = file_op.dst_path;
585 if (doc->file.config != file_op.config) {
586 // Only add the new versioned configurations.
587 if (context_->IsVerbose()) {
588 context_->GetDiagnostics()->Note(DiagMessage(doc->file.source)
589 << "auto-versioning resource from config '"
590 << config << "' -> '" << doc->file.config << "'");
591 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592
Adam Lesinskic744ae82017-05-17 19:28:38 -0700593 dst_path =
594 ResourceUtils::BuildResourceFileName(doc->file, context_->GetNameMangler());
595 bool result = table->AddFileReferenceAllowMangled(doc->file.name, doc->file.config,
596 doc->file.source, dst_path, nullptr,
597 context_->GetDiagnostics());
598 if (!result) {
599 return false;
600 }
601 }
602 error |= !FlattenXml(context_, doc.get(), dst_path, options_.keep_raw_values,
Adam Lesinski9c402502017-10-13 12:40:37 -0700603 false /*utf16*/, archive_writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 }
605 } else {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700606 error |= !io::CopyFileToArchive(context_, file_op.file_to_copy, file_op.dst_path,
607 GetCompressionFlags(file_op.dst_path), archive_writer);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700608 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700609 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700610 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700611 }
612 return !error;
613}
614
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700615static bool WriteStableIdMapToPath(IDiagnostics* diag,
616 const std::unordered_map<ResourceName, ResourceId>& id_map,
617 const std::string& id_map_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700620 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 return false;
622 }
623
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 const ResourceName& name = entry.first;
626 const ResourceId& id = entry.second;
627 fout << name << " = " << id << "\n";
628 }
629
630 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700631 diag->Error(DiagMessage(id_map_path) << "failed writing to file: "
632 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 return false;
634 }
635
636 return true;
637}
638
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700639static bool LoadStableIdMap(IDiagnostics* diag, const std::string& path,
640 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 std::string content;
Adam Lesinski2354b562017-05-26 16:31:38 -0700642 if (!android::base::ReadFileToString(path, &content, true /*follow_symlinks*/)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700643 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 return false;
645 }
646
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 out_id_map->clear();
648 size_t line_no = 0;
649 for (StringPiece line : util::Tokenize(content, '\n')) {
650 line_no++;
651 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 if (line.empty()) {
653 continue;
654 }
655
656 auto iter = std::find(line.begin(), line.end(), '=');
657 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700658 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 return false;
660 }
661
662 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700663 StringPiece res_name_str =
664 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
665 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700666 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource name '" << res_name_str
667 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 return false;
669 }
670
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700671 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
672 const size_t res_id_str_len = line.size() - res_id_start_idx;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700673 StringPiece res_id_str = util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
676 if (!maybe_id) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700677 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '" << res_id_str
678 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 return false;
680 }
681
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700682 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 }
684 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700685}
686
Adam Lesinskifb48d292015-11-07 15:52:13 -0800687class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 public:
689 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700690 : options_(options),
691 context_(context),
692 final_table_(),
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700693 file_collection_(util::make_unique<io::FileCollection>()) {
694 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700695
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700696 /**
697 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700700 bool LoadSymbolsFromIncludePaths() {
701 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700703 for (const std::string& path : options_.include_paths) {
704 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700705 context_->GetDiagnostics()->Note(DiagMessage(path) << "loading include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700706 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700707
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700708 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700709 std::string error_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800710 std::unique_ptr<ResourceTable> include_static = LoadStaticLibrary(path, &error_str);
711 if (include_static) {
Adam Lesinskib522f042017-04-21 16:57:59 -0700712 if (context_->GetPackageType() != PackageType::kStaticLib) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800713 // Can't include static libraries when not building a static library (they have no IDs
714 // assigned).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700715 context_->GetDiagnostics()->Error(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800716 DiagMessage(path) << "can't include static library when not building a static lib");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 return false;
718 }
719
720 // If we are using --no-static-lib-packages, we need to rename the
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800721 // package of this table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722 if (options_.no_static_lib_packages) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800723 // Since package names can differ, and multiple packages can exist in a ResourceTable,
724 // we place the requirement that all static libraries are built with the package
725 // ID 0x7f. So if one is not found, this is an error.
726 if (ResourceTablePackage* pkg = include_static->FindPackageById(kAppPackageId)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700727 pkg->name = context_->GetCompilationPackage();
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800728 } else {
729 context_->GetDiagnostics()->Error(DiagMessage(path)
730 << "no package with ID 0x7f found in static library");
731 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 }
733 }
734
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700735 context_->GetExternalSymbols()->AppendSource(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800736 util::make_unique<ResourceTableSymbolSource>(include_static.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700737
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800738 static_table_includes_.push_back(std::move(include_static));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700739
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700740 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700742 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 return false;
744 }
745
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700746 if (!asset_source->AddAssetPath(path)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800747 context_->GetDiagnostics()->Error(DiagMessage(path) << "failed to load include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 return false;
749 }
750 }
751
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800752 // Capture the shared libraries so that the final resource table can be properly flattened
753 // with support for shared libraries.
754 for (auto& entry : asset_source->GetAssignedPackageIds()) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800755 if (entry.first > kFrameworkPackageId && entry.first < kAppPackageId) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800756 final_table_.included_packages_[entry.first] = entry.second;
Adam Lesinski1210e8c2017-11-07 17:08:07 -0800757 } else if (entry.first == kAppPackageId) {
758 // Capture the included base feature package.
759 included_feature_base_ = entry.second;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800760 }
761 }
762
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700763 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700764 return true;
765 }
766
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800767 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800769 xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
770 if (manifest_el == nullptr) {
771 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700772 }
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800773
774 AppInfo app_info;
775
776 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
777 diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>");
778 return {};
779 }
780
781 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
782 if (!package_attr) {
783 diag->Error(DiagMessage(xml_res->file.source)
784 << "<manifest> must have a 'package' attribute");
785 return {};
786 }
787 app_info.package = package_attr->value;
788
789 if (xml::Attribute* version_code_attr =
790 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
791 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_attr->value);
792 if (!maybe_code) {
793 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
794 << "invalid android:versionCode '" << version_code_attr->value << "'");
795 return {};
796 }
797 app_info.version_code = maybe_code.value();
798 }
799
800 if (xml::Attribute* revision_code_attr =
801 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
802 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(revision_code_attr->value);
803 if (!maybe_code) {
804 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
805 << "invalid android:revisionCode '" << revision_code_attr->value << "'");
806 return {};
807 }
808 app_info.revision_code = maybe_code.value();
809 }
810
811 if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
812 if (!split_name_attr->value.empty()) {
813 app_info.split_name = split_name_attr->value;
814 }
815 }
816
817 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
818 if (xml::Attribute* min_sdk =
819 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700820 app_info.min_sdk_version = ResourceUtils::ParseSdkVersion(min_sdk->value);
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800821 }
822 }
823 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700824 }
825
826 /**
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800827 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700828 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700829 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700830 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700831 bool VerifyNoExternalPackages() {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700832 auto is_ext_package_func = [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700833 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
834 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700835 };
836
837 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700838 for (const auto& package : final_table_.packages) {
839 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840 // We have a package that is not related to the one we're building!
841 for (const auto& type : package->types) {
842 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700843 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700845 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700846 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700847 // for the 'android' package. This is due to legacy reasons.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700848 if (ValueCast<Id>(config_value->value.get()) && package->name == "android") {
849 context_->GetDiagnostics()->Warn(DiagMessage(config_value->value->GetSource())
850 << "generated id '" << res_name
851 << "' for external package '" << package->name
852 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700853 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700854 context_->GetDiagnostics()->Error(DiagMessage(config_value->value->GetSource())
855 << "defined resource '" << res_name
856 << "' for external package '" << package->name
857 << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700858 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700860 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 }
862 }
863 }
864 }
865
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700866 auto new_end_iter = std::remove_if(final_table_.packages.begin(), final_table_.packages.end(),
867 is_ext_package_func);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700868 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700869 return !error;
870 }
871
872 /**
873 * Returns true if no IDs have been set, false otherwise.
874 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700875 bool VerifyNoIdsSet() {
876 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700877 for (const auto& type : package->types) {
878 if (type->id) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800879 context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID "
880 << StringPrintf("%02x", type->id.value())
881 << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700883 }
884
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700885 for (const auto& entry : type->entries) {
886 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700887 ResourceNameRef res_name(package->name, type->type, entry->name);
888 context_->GetDiagnostics()->Error(
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800889 DiagMessage() << "entry " << res_name << " has ID "
890 << StringPrintf("%02x", entry->id.value()) << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700891 return false;
892 }
893 }
894 }
895 }
896 return true;
897 }
898
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700899 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
900 if (options_.output_to_directory) {
901 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700902 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700904 }
905 }
906
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700907 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700908 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800909 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700910 if (!flattener.Consume(context_, table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700911 context_->GetDiagnostics()->Error(DiagMessage() << "failed to flatten resource table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700912 return false;
913 }
914
Adam Lesinski06460ef2017-03-14 18:52:13 -0700915 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700916 return io::CopyInputStreamToArchive(context_, &input_stream, "resources.arsc",
917 ArchiveEntry::kAlign, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700918 }
919
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700920 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700921 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700922 return io::CopyProtoToArchive(context_, pb_table.get(), "resources.arsc.flat", 0, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700923 }
924
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700925 bool WriteJavaFile(ResourceTable* table, const StringPiece& package_name_to_generate,
Adam Lesinski418763f2017-04-11 17:36:53 -0700926 const StringPiece& out_package, const JavaClassGeneratorOptions& java_options,
927 const Maybe<std::string> out_text_symbols_path = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700928 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700929 return true;
930 }
931
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700932 std::string out_path = options_.generate_java_class_path.value();
933 file::AppendPath(&out_path, file::PackageToPath(out_package));
934 if (!file::mkdirs(out_path)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700935 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path
936 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700937 return false;
938 }
939
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700940 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700941
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700942 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700943 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700944 context_->GetDiagnostics()->Error(DiagMessage()
945 << "failed writing to '" << out_path
946 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700947 return false;
948 }
949
Adam Lesinski418763f2017-04-11 17:36:53 -0700950 std::unique_ptr<std::ofstream> fout_text;
951 if (out_text_symbols_path) {
952 fout_text =
953 util::make_unique<std::ofstream>(out_text_symbols_path.value(), std::ofstream::binary);
954 if (!*fout_text) {
955 context_->GetDiagnostics()->Error(
956 DiagMessage() << "failed writing to '" << out_text_symbols_path.value()
957 << "': " << android::base::SystemErrorCodeToString(errno));
958 return false;
959 }
960 }
961
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700962 JavaClassGenerator generator(context_, table, java_options);
Adam Lesinski418763f2017-04-11 17:36:53 -0700963 if (!generator.Generate(package_name_to_generate, out_package, &fout, fout_text.get())) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700964 context_->GetDiagnostics()->Error(DiagMessage(out_path) << generator.getError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700965 return false;
966 }
967
968 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700969 context_->GetDiagnostics()->Error(DiagMessage()
970 << "failed writing to '" << out_path
971 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700972 }
973 return true;
974 }
975
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700976 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
977 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700978 return true;
979 }
980
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700981 std::unique_ptr<ClassDefinition> manifest_class =
982 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700983
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 // Something bad happened, but we already logged it, so exit.
986 return false;
987 }
988
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700989 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700990 // Empty Manifest class, no need to generate it.
991 return true;
992 }
993
994 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700995 for (const std::string& annotation : options_.javadoc_annotations) {
996 std::string proper_annotation = "@";
997 proper_annotation += annotation;
998 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700999 }
1000
Adam Lesinski0d81f702017-06-27 15:51:09 -07001001 const std::string package_utf8 =
1002 options_.custom_java_package.value_or_default(context_->GetCompilationPackage());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001003
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001004 std::string out_path = options_.generate_java_class_path.value();
1005 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001006
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001007 if (!file::mkdirs(out_path)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001008 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path
1009 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010 return false;
1011 }
1012
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001013 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001014
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001015 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001016 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001017 context_->GetDiagnostics()->Error(DiagMessage()
1018 << "failed writing to '" << out_path
1019 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001020 return false;
1021 }
1022
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001023 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8, true, &fout)) {
1024 context_->GetDiagnostics()->Error(DiagMessage()
1025 << "failed writing to '" << out_path
1026 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001027 return false;
1028 }
1029 return true;
1030 }
1031
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001032 bool WriteProguardFile(const Maybe<std::string>& out, const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001033 if (!out) {
1034 return true;
1035 }
1036
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001037 const std::string& out_path = out.value();
1038 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001040 context_->GetDiagnostics()->Error(DiagMessage()
1041 << "failed to open '" << out_path
1042 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001043 return false;
1044 }
1045
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001046 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001047 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001048 context_->GetDiagnostics()->Error(DiagMessage()
1049 << "failed writing to '" << out_path
1050 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001051 return false;
1052 }
1053 return true;
1054 }
1055
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001056 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1057 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001058 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001059 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001060 if (!collection) {
1061 return {};
1062 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001063 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001064 }
1065
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001066 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001067 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001068 if (!file) {
1069 return {};
1070 }
1071
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001072 std::unique_ptr<io::IData> data = file->OpenAsData();
1073 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1074 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075 }
1076
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001077 bool MergeStaticLibrary(const std::string& input, bool override) {
1078 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001079 context_->GetDiagnostics()->Note(DiagMessage() << "merging static library " << input);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001080 }
1081
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001082 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001083 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001084 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001085 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001086 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001087 return false;
1088 }
1089
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001090 std::unique_ptr<ResourceTable> table = LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091 if (!table) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001092 context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001093 return false;
1094 }
1095
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001096 ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001097 if (!pkg) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001098 context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001099 return false;
1100 }
1101
1102 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001103 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001104 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001105 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001106
1107 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001108 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001110 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001111 }
1112
1113 pkg->name = "";
1114 if (override) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001115 result = table_merger_->MergeOverlay(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001116 } else {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001117 result = table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001118 }
1119
1120 } else {
1121 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001122 // preserved and resource names are mangled.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001123 result =
1124 table_merger_->MergeAndMangle(Source(input), pkg->name, table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001125 }
1126
1127 if (!result) {
1128 return false;
1129 }
1130
1131 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001132 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001133 return true;
1134 }
1135
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001136 bool MergeResourceTable(io::IFile* file, bool override) {
1137 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001138 context_->GetDiagnostics()->Note(DiagMessage() << "merging resource table "
1139 << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001140 }
1141
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001142 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001143 if (!data) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001144 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001145 return false;
1146 }
1147
1148 std::unique_ptr<ResourceTable> table =
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001149 LoadTableFromPb(file->GetSource(), data->data(), data->size(), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001150 if (!table) {
1151 return false;
1152 }
1153
1154 bool result = false;
1155 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001156 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001157 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001158 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001159 }
1160 return result;
1161 }
1162
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001163 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc, bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001164 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001165 context_->GetDiagnostics()->Note(DiagMessage() << "merging '" << file_desc->name
1166 << "' from compiled file "
1167 << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001168 }
1169
1170 bool result = false;
1171 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001172 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001173 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001174 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001175 }
1176
1177 if (!result) {
1178 return false;
1179 }
1180
1181 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001182 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1183 if (exported_symbol.name.package.empty()) {
1184 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001185 }
1186
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001187 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001188
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001189 Maybe<ResourceName> mangled_name =
1190 context_->GetNameMangler()->MangleName(exported_symbol.name);
1191 if (mangled_name) {
1192 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001193 }
1194
1195 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001196 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1197 bool result = final_table_.AddResourceAllowMangled(
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001198 res_name, ConfigDescription::DefaultConfig(), std::string(), std::move(id),
1199 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001200 if (!result) {
1201 return false;
1202 }
1203 }
1204 return true;
1205 }
1206
1207 /**
1208 * Takes a path to load as a ZIP file and merges the files within into the
1209 * master ResourceTable.
1210 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001211 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001212 *
1213 * An io::IFileCollection is created from the ZIP file and added to the set of
1214 * io::IFileCollections that are open.
1215 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001216 bool MergeArchive(const std::string& input, bool override) {
1217 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001218 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive " << input);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001219 }
1220
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001221 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001222 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001223 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001224 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001225 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001226 return false;
1227 }
1228
1229 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001230 for (auto iter = collection->Iterator(); iter->HasNext();) {
1231 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001232 error = true;
1233 }
1234 }
1235
1236 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001237 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001238 return !error;
1239 }
1240
1241 /**
1242 * Takes a path to load and merge into the master ResourceTable. If override
1243 * is true,
1244 * conflicting resources are allowed to override each other, in order of last
1245 * seen.
1246 *
1247 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1248 * as ZIP archive
1249 * and the files within are merged individually.
1250 *
1251 * Otherwise the files is processed on its own.
1252 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001253 bool MergePath(const std::string& path, bool override) {
1254 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1255 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1256 return MergeArchive(path, override);
1257 } else if (util::EndsWith(path, ".apk")) {
1258 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001259 }
1260
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001261 io::IFile* file = file_collection_->InsertFile(path);
1262 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001263 }
1264
1265 /**
1266 * Takes a file to load and merge into the master ResourceTable. If override
1267 * is true,
1268 * conflicting resources are allowed to override each other, in order of last
1269 * seen.
1270 *
1271 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1272 * merged into the
1273 * master ResourceTable. If the file ends with .flat, then it is treated like
1274 * a compiled file
1275 * and the header data is read and merged into the final ResourceTable.
1276 *
1277 * All other file types are ignored. This is because these files could be
1278 * coming from a zip,
1279 * where we could have other files like classes.dex.
1280 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001281 bool MergeFile(io::IFile* file, bool override) {
1282 const Source& src = file->GetSource();
1283 if (util::EndsWith(src.path, ".arsc.flat")) {
1284 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001285
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001286 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001287 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001288 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001289 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001290 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001291 return false;
1292 }
1293
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001294 CompiledFileInputStream input_stream(data->data(), data->size());
1295 uint32_t num_files = 0;
1296 if (!input_stream.ReadLittleEndian32(&num_files)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001297 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed read num files");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001298 return false;
1299 }
1300
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001301 for (uint32_t i = 0; i < num_files; i++) {
Adam Lesinskib7917212017-08-10 15:37:28 -07001302 pb::internal::CompiledFile compiled_file;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001303 if (!input_stream.ReadCompiledFile(&compiled_file)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001304 context_->GetDiagnostics()->Error(DiagMessage(src)
1305 << "failed to read compiled file header");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001307 }
1308
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001310 if (!input_stream.ReadDataMetaData(&offset, &len)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001311 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to read data meta data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001312 return false;
1313 }
1314
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001315 std::unique_ptr<ResourceFile> resource_file = DeserializeCompiledFileFromPb(
1316 compiled_file, file->GetSource(), context_->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001317 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001318 return false;
1319 }
1320
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001321 if (!MergeCompiledFile(file->CreateFileSegment(offset, len), resource_file.get(),
1322 override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001323 return false;
1324 }
1325 }
1326 return true;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001327 } else if (util::EndsWith(src.path, ".xml") || util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001328 // Since AAPT compiles these file types and appends .flat to them, seeing
1329 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001330 const StringPiece file_type = util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1331 context_->GetDiagnostics()->Error(DiagMessage(src) << "uncompiled " << file_type
1332 << " file passed as argument. Must be "
1333 "compiled first into .flat file.");
Adam Lesinski6a396c12016-10-20 14:38:23 -07001334 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001335 }
1336
1337 // Ignore non .flat files. This could be classes.dex or something else that
1338 // happens
1339 // to be in an archive.
1340 return true;
1341 }
1342
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001343 bool CopyAssetsDirsToApk(IArchiveWriter* writer) {
1344 std::map<std::string, std::unique_ptr<io::RegularFile>> merged_assets;
1345 for (const std::string& assets_dir : options_.assets_dirs) {
1346 Maybe<std::vector<std::string>> files =
1347 file::FindFiles(assets_dir, context_->GetDiagnostics(), nullptr);
1348 if (!files) {
1349 return false;
1350 }
1351
1352 for (const std::string& file : files.value()) {
1353 std::string full_key = "assets/" + file;
1354 std::string full_path = assets_dir;
1355 file::AppendPath(&full_path, file);
1356
1357 auto iter = merged_assets.find(full_key);
1358 if (iter == merged_assets.end()) {
1359 merged_assets.emplace(std::move(full_key),
1360 util::make_unique<io::RegularFile>(Source(std::move(full_path))));
1361 } else if (context_->IsVerbose()) {
1362 context_->GetDiagnostics()->Warn(DiagMessage(iter->second->GetSource())
1363 << "asset file overrides '" << full_path << "'");
1364 }
1365 }
1366 }
1367
1368 for (auto& entry : merged_assets) {
1369 uint32_t compression_flags = ArchiveEntry::kCompress;
1370 std::string extension = file::GetExtension(entry.first).to_string();
1371 if (options_.extensions_to_not_compress.count(extension) > 0) {
1372 compression_flags = 0u;
1373 }
1374
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001375 if (!io::CopyFileToArchive(context_, entry.second.get(), entry.first, compression_flags,
1376 writer)) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001377 return false;
1378 }
1379 }
1380 return true;
1381 }
1382
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001383 /**
1384 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001385 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001386 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001387 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set, xml::XmlResource* manifest,
1388 ResourceTable* table) {
Adam Lesinskib522f042017-04-21 16:57:59 -07001389 const bool keep_raw_values = context_->GetPackageType() == PackageType::kStaticLib;
Adam Lesinski9c402502017-10-13 12:40:37 -07001390 bool result = FlattenXml(context_, manifest, "AndroidManifest.xml", keep_raw_values,
1391 true /*utf16*/, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001392 if (!result) {
1393 return false;
1394 }
1395
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001396 ResourceFileFlattenerOptions file_flattener_options;
1397 file_flattener_options.keep_raw_values = keep_raw_values;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001398 file_flattener_options.do_not_compress_anything = options_.do_not_compress_anything;
1399 file_flattener_options.extensions_to_not_compress = options_.extensions_to_not_compress;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400 file_flattener_options.no_auto_version = options_.no_auto_version;
1401 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001402 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001403 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1404 file_flattener_options.update_proguard_spec =
1405 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001406
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001407 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001408
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001409 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001410 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001411 return false;
1412 }
1413
Adam Lesinski1210e8c2017-11-07 17:08:07 -08001414 // Hack to fix b/68820737.
1415 // We need to modify the ResourceTable's package name, but that should NOT affect
1416 // anything else being generated, which includes the Java classes.
1417 // If required, the package name is modifed before flattening, and then modified back
1418 // to its original name.
1419 ResourceTablePackage* package_to_rewrite = nullptr;
1420 if (context_->GetPackageId() > kAppPackageId &&
1421 included_feature_base_ == make_value(context_->GetCompilationPackage())) {
1422 // The base APK is included, and this is a feature split. If the base package is
1423 // the same as this package, then we are building an old style Android Instant Apps feature
1424 // split and must apply this workaround to avoid requiring namespaces support.
1425 package_to_rewrite = table->FindPackage(context_->GetCompilationPackage());
1426 if (package_to_rewrite != nullptr) {
1427 CHECK_EQ(1u, table->packages.size()) << "can't change name of package when > 1 package";
1428
1429 std::string new_package_name =
1430 StringPrintf("%s.%s", package_to_rewrite->name.c_str(),
1431 app_info_.split_name.value_or_default("feature").c_str());
1432
1433 if (context_->IsVerbose()) {
1434 context_->GetDiagnostics()->Note(
1435 DiagMessage() << "rewriting resource package name for feature split to '"
1436 << new_package_name << "'");
1437 }
1438 package_to_rewrite->name = new_package_name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439 }
1440 }
Adam Lesinski1210e8c2017-11-07 17:08:07 -08001441
1442 bool success;
1443 if (context_->GetPackageType() == PackageType::kStaticLib) {
1444 success = FlattenTableToPb(table, writer);
1445 } else {
1446 success = FlattenTable(table, writer);
1447 }
1448
1449 if (package_to_rewrite != nullptr) {
1450 // Change the name back.
1451 package_to_rewrite->name = context_->GetCompilationPackage();
1452 if (package_to_rewrite->id) {
1453 table->included_packages_.erase(package_to_rewrite->id.value());
1454 }
1455 }
1456
1457 if (!success) {
1458 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resource table");
1459 }
1460 return success;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001461 }
1462
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001463 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001464 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001465 std::unique_ptr<xml::XmlResource> manifest_xml =
1466 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1467 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001468 return 1;
1469 }
1470
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001471 // First extract the Package name without modifying it (via --rename-manifest-package).
1472 if (Maybe<AppInfo> maybe_app_info =
1473 ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001474 const AppInfo& app_info = maybe_app_info.value();
1475 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001476 }
1477
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001478 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1479 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001480 return 1;
1481 }
1482
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001483 Maybe<AppInfo> maybe_app_info =
1484 ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001485 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001486 return 1;
1487 }
1488
Adam Lesinski1210e8c2017-11-07 17:08:07 -08001489 app_info_ = maybe_app_info.value();
1490 context_->SetMinSdkVersion(app_info_.min_sdk_version.value_or_default(0));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001491
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001492 context_->SetNameManglerPolicy(NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001493
1494 // Override the package ID when it is "android".
1495 if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001496 context_->SetPackageId(0x01);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001497
1498 // Verify we're building a regular app.
Adam Lesinskib522f042017-04-21 16:57:59 -07001499 if (context_->GetPackageType() != PackageType::kApp) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001500 context_->GetDiagnostics()->Error(
1501 DiagMessage() << "package 'android' can only be built as a regular app");
1502 return 1;
1503 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001504 }
1505
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001506 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001507 return 1;
1508 }
1509
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001510 TableMergerOptions table_merger_options;
1511 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001512 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_, table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001513
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001514 if (context_->IsVerbose()) {
1515 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001516 << StringPrintf("linking package '%s' using package ID %02x",
1517 context_->GetCompilationPackage().data(),
1518 context_->GetPackageId()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001519 }
1520
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001521 for (const std::string& input : input_files) {
1522 if (!MergePath(input, false)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001523 context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing input");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001524 return 1;
1525 }
1526 }
1527
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001528 for (const std::string& input : options_.overlay_files) {
1529 if (!MergePath(input, true)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001530 context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing overlays");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001531 return 1;
1532 }
1533 }
1534
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001535 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001536 return 1;
1537 }
1538
Adam Lesinskib522f042017-04-21 16:57:59 -07001539 if (context_->GetPackageType() != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001540 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001541 if (!mover.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001542 context_->GetDiagnostics()->Error(DiagMessage() << "failed moving private attributes");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001543 return 1;
1544 }
1545
1546 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001547 IdAssigner id_assigner(&options_.stable_id_map);
1548 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001549 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001550 return 1;
1551 }
1552
1553 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001554 if (options_.resource_id_map_path) {
1555 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001556 for (auto& type : package->types) {
1557 for (auto& entry : type->entries) {
1558 ResourceName name(package->name, type->type, entry->name);
1559 // The IDs are guaranteed to exist.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001560 options_.stable_id_map[std::move(name)] =
1561 ResourceId(package->id.value(), type->id.value(), entry->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001562 }
1563 }
1564 }
1565
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001566 if (!WriteStableIdMapToPath(context_->GetDiagnostics(), options_.stable_id_map,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001567 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001568 return 1;
1569 }
1570 }
1571 } else {
1572 // Static libs are merged with other apps, and ID collisions are bad, so
1573 // verify that
1574 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001575 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001576 return 1;
1577 }
1578 }
1579
1580 // Add the names to mangle based on our source merge earlier.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001581 context_->SetNameManglerPolicy(
1582 NameManglerPolicy{context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001583
1584 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001585 context_->GetExternalSymbols()->PrependSource(
1586 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001587
Adam Lesinski1e4b0e52017-04-27 15:01:10 -07001588 // Workaround for pre-O runtime that would treat negative resource IDs
1589 // (any ID with a package ID > 7f) as invalid. Intercept any ID (PPTTEEEE) with PP > 0x7f
1590 // and type == 'id', and return the ID 0x7fPPEEEE. IDs don't need to be real resources, they
1591 // are just identifiers.
1592 if (context_->GetMinSdkVersion() < SDK_O && context_->GetPackageType() == PackageType::kApp) {
1593 if (context_->IsVerbose()) {
1594 context_->GetDiagnostics()->Note(DiagMessage()
1595 << "enabling pre-O feature split ID rewriting");
1596 }
1597 context_->GetExternalSymbols()->SetDelegate(
1598 util::make_unique<FeatureSplitSymbolTableDelegate>(context_));
1599 }
1600
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001601 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001602 if (!linker.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001603 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking references");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001604 return 1;
1605 }
1606
Adam Lesinskib522f042017-04-21 16:57:59 -07001607 if (context_->GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001608 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001609 context_->GetDiagnostics()->Warn(DiagMessage()
1610 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001611 }
1612 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001613 ProductFilter product_filter(options_.products);
1614 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001615 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001616 return 1;
1617 }
1618 }
1619
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001620 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001621 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001622 if (!versioner.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001623 context_->GetDiagnostics()->Error(DiagMessage() << "failed versioning styles");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001624 return 1;
1625 }
1626 }
1627
Adam Lesinskib522f042017-04-21 16:57:59 -07001628 if (context_->GetPackageType() != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001629 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001630 context_->GetDiagnostics()->Note(DiagMessage()
1631 << "collapsing resource versions for minimum SDK "
1632 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001633 }
1634
1635 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001636 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001637 return 1;
1638 }
1639 }
1640
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001641 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001642 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001643 if (!deduper.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001644 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001645 return 1;
1646 }
1647 }
1648
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001649 proguard::KeepSet proguard_keep_set;
1650 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001651
Adam Lesinskib522f042017-04-21 16:57:59 -07001652 if (context_->GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001653 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001654 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001655 context_->GetDiagnostics()->Warn(DiagMessage()
1656 << "can't strip resources when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001657 }
1658 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001659 // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or
1660 // equal to the minSdk.
1661 options_.split_constraints =
1662 AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001663
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001664 TableSplitter table_splitter(options_.split_constraints, options_.table_splitter_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001665 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001666 return 1;
1667 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001668 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001669
1670 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001671 auto path_iter = options_.split_paths.begin();
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001672 auto split_constraints_iter = options_.split_constraints.begin();
1673 for (std::unique_ptr<ResourceTable>& split_table : table_splitter.splits()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001674 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001675 context_->GetDiagnostics()->Note(DiagMessage(*path_iter)
1676 << "generating split with configurations '"
1677 << util::Joiner(split_constraints_iter->configs, ", ")
1678 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001679 }
1680
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001681 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(*path_iter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001682 if (!archive_writer) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001683 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001684 return 1;
1685 }
1686
1687 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001688 std::unique_ptr<xml::XmlResource> split_manifest =
Adam Lesinski1210e8c2017-11-07 17:08:07 -08001689 GenerateSplitManifest(app_info_, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001690
1691 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001692 if (!linker.Consume(context_, split_manifest.get())) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001693 context_->GetDiagnostics()->Error(DiagMessage()
1694 << "failed to create Split AndroidManifest.xml");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001695 return 1;
1696 }
1697
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001698 if (!WriteApk(archive_writer.get(), &proguard_keep_set, split_manifest.get(),
1699 split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001700 return 1;
1701 }
1702
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001703 ++path_iter;
1704 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001705 }
1706 }
1707
1708 // Start writing the base APK.
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001709 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(options_.output_path);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001710 if (!archive_writer) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001711 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001712 return 1;
1713 }
1714
1715 bool error = false;
1716 {
1717 // AndroidManifest.xml has no resource name, but the CallSite is built
1718 // from the name
1719 // (aka, which package the AndroidManifest.xml is coming from).
1720 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001721 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001722
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001723 XmlReferenceLinker manifest_linker;
1724 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1725 if (options_.generate_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001726 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1727 manifest_xml.get(), &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001728 error = true;
1729 }
1730
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001731 if (options_.generate_main_dex_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001732 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1733 manifest_xml.get(),
1734 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001735 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001736 }
1737
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001738 if (options_.generate_java_class_path) {
1739 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001740 error = true;
1741 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001742 }
1743
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001744 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001745 // PackageParser will fail if URIs are removed from
1746 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001747 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1748 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001749 error = true;
1750 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001751 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001752 } else {
1753 error = true;
1754 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001755 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001756
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001757 if (error) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001758 context_->GetDiagnostics()->Error(DiagMessage() << "failed processing manifest");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001759 return 1;
1760 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001761
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001762 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(), &final_table_)) {
1763 return 1;
1764 }
1765
1766 if (!CopyAssetsDirsToApk(archive_writer.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001767 return 1;
1768 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001769
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001770 if (options_.generate_java_class_path) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001771 // The set of packages whose R class to call in the main classes
1772 // onResourcesLoaded callback.
1773 std::vector<std::string> packages_to_callback;
1774
1775 JavaClassGeneratorOptions template_options;
1776 template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1777 template_options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001778
Adam Lesinskib522f042017-04-21 16:57:59 -07001779 if (context_->GetPackageType() == PackageType::kStaticLib ||
1780 options_.generate_non_final_ids) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001781 template_options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001782 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001783
Adam Lesinskib522f042017-04-21 16:57:59 -07001784 if (context_->GetPackageType() == PackageType::kSharedLib) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001785 template_options.use_final = false;
1786 template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{};
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001787 }
1788
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001789 const StringPiece actual_package = context_->GetCompilationPackage();
1790 StringPiece output_package = context_->GetCompilationPackage();
1791 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001792 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001793 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001794 }
1795
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001796 // Generate the private symbols if required.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001797 if (options_.private_symbols) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001798 packages_to_callback.push_back(options_.private_symbols.value());
1799
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001800 // If we defined a private symbols package, we only emit Public symbols
1801 // to the original package, and private and public symbols to the
1802 // private package.
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001803 JavaClassGeneratorOptions options = template_options;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001804 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001805 if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001806 options)) {
1807 return 1;
1808 }
1809 }
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001810
1811 // Generate all the symbols for all extra packages.
1812 for (const std::string& extra_package : options_.extra_java_packages) {
1813 packages_to_callback.push_back(extra_package);
1814
1815 JavaClassGeneratorOptions options = template_options;
1816 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1817 if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) {
1818 return 1;
1819 }
1820 }
1821
1822 // Generate the main public R class.
1823 JavaClassGeneratorOptions options = template_options;
1824
1825 // Only generate public symbols if we have a private package.
1826 if (options_.private_symbols) {
1827 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
1828 }
1829
1830 if (options.rewrite_callback_options) {
1831 options.rewrite_callback_options.value().packages_to_callback =
1832 std::move(packages_to_callback);
1833 }
1834
Adam Lesinski418763f2017-04-11 17:36:53 -07001835 if (!WriteJavaFile(&final_table_, actual_package, output_package, options,
1836 options_.generate_text_symbols_path)) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001837 return 1;
1838 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001839 }
1840
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001841 if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001842 return 1;
1843 }
1844
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001845 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1846 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001847 return 1;
1848 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001849 return 0;
1850 }
1851
1852 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001853 LinkOptions options_;
1854 LinkContext* context_;
1855 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001856
Adam Lesinski1210e8c2017-11-07 17:08:07 -08001857 AppInfo app_info_;
1858
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001859 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001860
1861 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001862 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001863
1864 // A vector of IFileCollections. This is mainly here to keep ownership of the
1865 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001866 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001867
1868 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001869 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001870 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001871
1872 // The set of shared libraries being used, mapping their assigned package ID to package name.
1873 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1210e8c2017-11-07 17:08:07 -08001874
1875 // The package name of the base application, if it is included.
1876 Maybe<std::string> included_feature_base_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001877};
1878
Chris Warrington820d72a2017-04-27 15:27:01 +01001879int Link(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
1880 LinkContext context(diagnostics);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001881 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001882 std::vector<std::string> overlay_arg_list;
1883 std::vector<std::string> extra_java_packages;
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001884 Maybe<std::string> package_id;
Adam Lesinski113ee092017-04-03 19:38:25 -07001885 std::vector<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001886 Maybe<std::string> preferred_density;
1887 Maybe<std::string> product_list;
1888 bool legacy_x_flag = false;
1889 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001890 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001891 bool shared_lib = false;
1892 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001893 Maybe<std::string> stable_id_file_path;
1894 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001895 Flags flags =
1896 Flags()
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001897 .RequiredFlag("-o", "Output path.", &options.output_path)
1898 .RequiredFlag("--manifest", "Path to the Android manifest to build.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001899 &options.manifest_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001900 .OptionalFlagList("-I", "Adds an Android APK to link against.", &options.include_paths)
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001901 .OptionalFlagList("-A",
1902 "An assets directory to include in the APK. These are unprocessed.",
1903 &options.assets_dirs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001904 .OptionalFlagList("-R",
1905 "Compilation unit to link, using `overlay` semantics.\n"
1906 "The last conflicting resource given takes precedence.",
1907 &overlay_arg_list)
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001908 .OptionalFlag("--package-id",
1909 "Specify the package ID to use for this app. Must be greater or equal to\n"
1910 "0x7f and can't be used with --static-lib or --shared-lib.",
1911 &package_id)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001912 .OptionalFlag("--java", "Directory in which to generate R.java.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001913 &options.generate_java_class_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001914 .OptionalFlag("--proguard", "Output file for generated Proguard rules.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001915 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001916 .OptionalFlag("--proguard-main-dex",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001917 "Output file for generated Proguard rules for the main dex.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001918 &options.generate_main_dex_proguard_rules_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001919 .OptionalSwitch("--no-auto-version",
1920 "Disables automatic style and layout SDK versioning.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001921 &options.no_auto_version)
1922 .OptionalSwitch("--no-version-vectors",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001923 "Disables automatic versioning of vector drawables. Use this only\n"
1924 "when building with vector drawable support library.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001925 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001926 .OptionalSwitch("--no-version-transitions",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001927 "Disables automatic versioning of transition resources. Use this only\n"
1928 "when building with transition support library.",
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001929 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001930 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001931 "Disables automatic deduping of resources with\n"
1932 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001933 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001934 .OptionalSwitch("--enable-sparse-encoding",
1935 "Enables encoding sparse entries using a binary search tree.\n"
1936 "This decreases APK size at the cost of resource retrieval performance.",
1937 &options.table_flattener_options.use_sparse_entries)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001938 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001939 &legacy_x_flag)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001940 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001941 &require_localization)
Adam Lesinski113ee092017-04-03 19:38:25 -07001942 .OptionalFlagList("-c",
Adam Lesinski418763f2017-04-11 17:36:53 -07001943 "Comma separated list of configurations to include. The default\n"
1944 "is all configurations.",
1945 &configs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001946 .OptionalFlag("--preferred-density",
1947 "Selects the closest matching density and strips out all others.",
1948 &preferred_density)
1949 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001950 .OptionalSwitch("--output-to-dir",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001951 "Outputs the APK contents to a directory specified by -o.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001952 &options.output_to_directory)
1953 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001954 "Removes XML namespace prefix and URI information from\n"
1955 "AndroidManifest.xml and XML binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001956 &options.no_xml_namespaces)
1957 .OptionalFlag("--min-sdk-version",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001958 "Default minimum SDK version to use for AndroidManifest.xml.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001959 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001960 .OptionalFlag("--target-sdk-version",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001961 "Default target SDK version to use for AndroidManifest.xml.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001962 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001963 .OptionalFlag("--version-code",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001964 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
1965 "present.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001966 &options.manifest_fixer_options.version_code_default)
1967 .OptionalFlag("--version-name",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001968 "Version name to inject into the AndroidManifest.xml if none is present.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001969 &options.manifest_fixer_options.version_name_default)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001970 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
1971 &shared_lib)
1972 .OptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001973 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001974 "Merge all library resources under the app's package.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001975 &options.no_static_lib_packages)
1976 .OptionalSwitch("--non-final-ids",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001977 "Generates R.java without the final modifier. This is implied when\n"
1978 "--static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001979 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001980 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001981 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001982 .OptionalFlag("--emit-ids",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001983 "Emit a file at the given path with a list of name to ID mappings,\n"
1984 "suitable for use with --stable-ids.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001985 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001986 .OptionalFlag("--private-symbols",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001987 "Package name to use when generating R.java for private symbols.\n"
1988 "If not specified, public and private symbols will use the application's\n"
1989 "package name.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001990 &options.private_symbols)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001991 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001992 &options.custom_java_package)
1993 .OptionalFlagList("--extra-packages",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001994 "Generate the same R.java but with different package names.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001995 &extra_java_packages)
1996 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001997 "Adds a JavaDoc annotation to all generated Java classes.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001998 &options.javadoc_annotations)
Adam Lesinski418763f2017-04-11 17:36:53 -07001999 .OptionalFlag("--output-text-symbols",
2000 "Generates a text file containing the resource symbols of the R class in\n"
2001 "the specified folder.",
2002 &options.generate_text_symbols_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002003 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002004 "Allows the addition of new resources in overlays without\n"
2005 "<add-resource> tags.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002006 &options.auto_add_overlay)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002007 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002008 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002009 .OptionalFlag("--rename-instrumentation-target-package",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002010 "Changes the name of the target package for instrumentation. Most useful\n"
2011 "when used in conjunction with --rename-manifest-package.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002012 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002013 .OptionalFlagList("-0", "File extensions not to compress.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002014 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002015 .OptionalFlagList("--split",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002016 "Split resources matching a set of configs out to a Split APK.\n"
Adam Lesinskidb091572017-04-13 12:48:56 -07002017 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
2018 "On Windows, use a semicolon ';' separator instead.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002019 &split_args)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002020 .OptionalSwitch("-v", "Enables verbose logging.", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002021
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002022 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002023 return 1;
2024 }
2025
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002026 // Expand all argument-files passed into the command line. These start with '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002027 std::vector<std::string> arg_list;
2028 for (const std::string& arg : flags.GetArgs()) {
2029 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002030 const std::string path = arg.substr(1, arg.size() - 1);
2031 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002032 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
2033 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002034 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002035 }
2036 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002037 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002038 }
2039 }
2040
2041 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002042 for (const std::string& arg : overlay_arg_list) {
2043 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002044 const std::string path = arg.substr(1, arg.size() - 1);
2045 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002046 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
2047 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002048 return 1;
2049 }
2050 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002051 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002052 }
2053 }
2054
2055 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002056 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002057 }
2058
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002059 if (shared_lib && static_lib) {
2060 context.GetDiagnostics()->Error(DiagMessage()
2061 << "only one of --shared-lib and --static-lib can be defined");
2062 return 1;
2063 }
2064
2065 if (shared_lib) {
Adam Lesinskib522f042017-04-21 16:57:59 -07002066 context.SetPackageType(PackageType::kSharedLib);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002067 context.SetPackageId(0x00);
2068 } else if (static_lib) {
Adam Lesinskib522f042017-04-21 16:57:59 -07002069 context.SetPackageType(PackageType::kStaticLib);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002070 context.SetPackageId(kAppPackageId);
2071 } else {
Adam Lesinskib522f042017-04-21 16:57:59 -07002072 context.SetPackageType(PackageType::kApp);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002073 context.SetPackageId(kAppPackageId);
2074 }
2075
2076 if (package_id) {
Adam Lesinskib522f042017-04-21 16:57:59 -07002077 if (context.GetPackageType() != PackageType::kApp) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002078 context.GetDiagnostics()->Error(
2079 DiagMessage() << "can't specify --package-id when not building a regular app");
2080 return 1;
2081 }
2082
2083 const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id.value());
2084 if (!maybe_package_id_int) {
2085 context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id.value()
2086 << "' is not a valid integer");
2087 return 1;
2088 }
2089
2090 const uint32_t package_id_int = maybe_package_id_int.value();
2091 if (package_id_int < kAppPackageId || package_id_int > std::numeric_limits<uint8_t>::max()) {
2092 context.GetDiagnostics()->Error(
2093 DiagMessage() << StringPrintf(
2094 "invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int));
2095 return 1;
2096 }
2097 context.SetPackageId(static_cast<uint8_t>(package_id_int));
2098 }
2099
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002100 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002101 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002102 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002103 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002104 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002105 }
2106 }
2107
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002108 if (product_list) {
2109 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002110 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002111 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002112 }
2113 }
2114 }
2115
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002116 std::unique_ptr<IConfigFilter> filter;
Mihai Nitaf4dacf22017-04-07 08:25:06 -07002117 if (!configs.empty()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002118 filter = ParseConfigFilterParameters(configs, context.GetDiagnostics());
2119 if (filter == nullptr) {
2120 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002121 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002122 options.table_splitter_options.config_filter = filter.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002123 }
2124
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002125 if (preferred_density) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002126 Maybe<uint16_t> density =
2127 ParseTargetDensityParameter(preferred_density.value(), context.GetDiagnostics());
2128 if (!density) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002129 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002130 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002131 options.table_splitter_options.preferred_densities.push_back(density.value());
2132 }
Adam Lesinskic51562c2016-04-28 11:12:38 -07002133
Adam Lesinskid0f492d2017-04-03 18:12:45 -07002134 // Parse the split parameters.
2135 for (const std::string& split_arg : split_args) {
2136 options.split_paths.push_back({});
2137 options.split_constraints.push_back({});
2138 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(), &options.split_paths.back(),
2139 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002140 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002141 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002142 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002143
Adam Lesinskib522f042017-04-21 16:57:59 -07002144 if (context.GetPackageType() != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002145 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2146 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002147 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002148 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002149 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002150
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002151 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002152 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002153 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2154 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2155 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2156 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2157
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002158 // Turn off auto versioning for static-libs.
Adam Lesinskib522f042017-04-21 16:57:59 -07002159 if (context.GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002160 options.no_auto_version = true;
2161 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002162 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002163 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002164
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002165 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002166 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002167}
2168
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002169} // namespace aapt