blob: c8f02171bfd3112df1e053fc52fbebd92a63cdb5 [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 Lesinski1ab598f2015-08-14 14:26:04 -070036#include "compile/IdAssigner.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080037#include "filter/ConfigFilter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038#include "flatten/Archive.h"
39#include "flatten/TableFlattener.h"
40#include "flatten/XmlFlattener.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080041#include "io/FileSystem.h"
42#include "io/ZipArchive.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070043#include "java/JavaClassGenerator.h"
44#include "java/ManifestClassGenerator.h"
45#include "java/ProguardRules.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046#include "link/Linkers.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047#include "link/ManifestFixer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080048#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049#include "link/TableMerger.h"
Adam Lesinskid48944a2017-02-21 14:22:30 -080050#include "optimize/ResourceDeduper.h"
51#include "optimize/VersionCollapser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052#include "process/IResourceTableConsumer.h"
53#include "process/SymbolTable.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080054#include "proto/ProtoSerialize.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080055#include "split/TableSplitter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056#include "unflatten/BinaryResourceParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057#include "util/Files.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080058#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059
Adam Lesinskid5083f62017-01-16 15:07:21 -080060using android::StringPiece;
Adam Lesinskif34b6f42017-03-03 16:33:26 -080061using android::base::StringPrintf;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070063
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064namespace aapt {
65
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080066// The type of package to build.
67enum class PackageType {
68 kApp,
69 kSharedLib,
70 kStaticLib,
71};
72
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073struct LinkOptions {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080074 PackageType package_type = PackageType::kApp;
75
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 std::string output_path;
77 std::string manifest_path;
78 std::vector<std::string> include_paths;
79 std::vector<std::string> overlay_files;
80 bool output_to_directory = false;
81 bool auto_add_overlay = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 // Java/Proguard options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 Maybe<std::string> generate_java_class_path;
85 Maybe<std::string> custom_java_package;
86 std::set<std::string> extra_java_packages;
87 Maybe<std::string> generate_proguard_rules_path;
88 Maybe<std::string> generate_main_dex_proguard_rules_path;
89 bool generate_non_final_ids = false;
90 std::vector<std::string> javadoc_annotations;
91 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070092
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 // Optimizations/features.
94 bool no_auto_version = false;
95 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +090096 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 bool no_resource_deduping = false;
98 bool no_xml_namespaces = false;
99 bool do_not_compress_anything = false;
100 std::unordered_set<std::string> extensions_to_not_compress;
101
102 // Static lib options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 bool no_static_lib_packages = false;
104
105 // AndroidManifest.xml massaging options.
106 ManifestFixerOptions manifest_fixer_options;
107
108 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700110
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800111 // Flattening options.
112 TableFlattenerOptions table_flattener_options;
113
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 TableSplitterOptions table_splitter_options;
116 std::vector<SplitConstraints> split_constraints;
117 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 std::unordered_map<ResourceName, ResourceId> stable_id_map;
121 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122};
123
Adam Lesinski64587af2016-02-18 18:33:06 -0800124class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 public:
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800126 LinkContext() : name_mangler_({}), symbols_(&name_mangler_) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 NameMangler* GetNameMangler() override { return &name_mangler_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
133 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 const std::string& GetCompilationPackage() override {
137 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800141 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 uint8_t GetPackageId() override { return package_id_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 void SetPackageId(uint8_t id) { package_id_ = id; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 SymbolTable* GetExternalSymbols() override { return &symbols_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 bool IsVerbose() override { return verbose_; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 int GetMinSdkVersion() override { return min_sdk_version_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 void SetMinSdkVersion(int minSdk) { min_sdk_version_ = minSdk; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700157
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 DISALLOW_COPY_AND_ASSIGN(LinkContext);
160
161 StdErrDiagnostics diagnostics_;
162 NameMangler name_mangler_;
163 std::string compilation_package_;
164 uint8_t package_id_ = 0x0;
165 SymbolTable symbols_;
166 bool verbose_ = false;
167 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168};
169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170static bool CopyFileToArchive(io::IFile* file, const std::string& out_path,
171 uint32_t compression_flags,
172 IArchiveWriter* writer, IAaptContext* context) {
173 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 context->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 << "failed to open file");
Adam Lesinski355f2852016-02-13 20:26:45 -0800177 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 }
179
180 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data->data());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 const size_t buffer_size = data->size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 if (context->IsVerbose()) {
184 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 << " to archive");
186 }
187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 if (writer->StartEntry(out_path, compression_flags)) {
189 if (writer->WriteEntry(buffer, buffer_size)) {
190 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 return true;
192 }
193 }
194 }
195
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 context->GetDiagnostics()->Error(DiagMessage() << "failed to write file "
197 << out_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800199}
200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
202 Maybe<size_t> max_sdk_level, bool keep_raw_values,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 IArchiveWriter* writer, IAaptContext* context) {
204 BigBuffer buffer(1024);
205 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 options.keep_raw_values = keep_raw_values;
207 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800210 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 }
212
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 DiagMessage msg;
215 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 if (max_sdk_level) {
217 msg << " maxSdkLevel=" << max_sdk_level.value()
218 << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700220 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 }
222
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 if (writer->StartEntry(path, ArchiveEntry::kCompress)) {
224 if (writer->WriteEntry(buffer)) {
225 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 return true;
227 }
228 }
229 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 << " to archive");
232 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800233}
234
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 const void* data,
237 size_t len,
Adam Lesinski355f2852016-02-13 20:26:45 -0800238 IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 pb::ResourceTable pb_table;
240 if (!pb_table.ParseFromArray(data, len)) {
241 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 return {};
243 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800244
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 if (!table) {
248 return {};
249 }
250 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800251}
252
253/**
254 * Inflates an XML file from the source path.
255 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 IDiagnostics* diag) {
258 std::ifstream fin(path, std::ifstream::binary);
259 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 return {};
262 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800264}
265
Adam Lesinski355f2852016-02-13 20:26:45 -0800266struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 bool no_auto_version = false;
268 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900269 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 bool no_xml_namespaces = false;
271 bool keep_raw_values = false;
272 bool do_not_compress_anything = false;
273 bool update_proguard_spec = false;
274 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800275};
276
277class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 public:
279 ResourceFileFlattener(const ResourceFileFlattenerOptions& options,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 IAaptContext* context, proguard::KeepSet* keep_set)
281 : options_(options), context_(context), keep_set_(keep_set) {}
Adam Lesinski355f2852016-02-13 20:26:45 -0800282
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800284
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 private:
286 struct FileOperation {
287 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700288
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 // The entry this file came from.
290 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700291
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700294
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700297
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 std::string dst_path;
300 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800302
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800304
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
306 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800307
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 ResourceFileFlattenerOptions options_;
309 IAaptContext* context_;
310 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800311};
312
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
314 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 return 0;
316 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800317
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 for (const std::string& extension : options_.extensions_to_not_compress) {
319 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800321 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 }
323 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800324}
325
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900326static bool IsTransitionElement(const std::string& name) {
327 return
328 name == "fade" ||
329 name == "changeBounds" ||
330 name == "slide" ||
331 name == "explode" ||
332 name == "changeImageTransform" ||
333 name == "changeTransform" ||
334 name == "changeClipBounds" ||
335 name == "autoTransition" ||
336 name == "recolor" ||
337 name == "changeScroll" ||
338 name == "transitionSet" ||
339 name == "transition" ||
340 name == "transitionManager";
341}
342
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343bool ResourceFileFlattener::LinkAndVersionXmlFile(
344 ResourceTable* table, FileOperation* file_op,
345 std::queue<FileOperation>* out_file_op_queue) {
346 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700348
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 if (context_->IsVerbose()) {
350 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 }
352
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 XmlReferenceLinker xml_linker;
354 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 return false;
356 }
357
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 if (options_.update_proguard_spec &&
359 !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 return false;
361 }
362
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 if (options_.no_xml_namespaces) {
364 XmlNamespaceRemover namespace_remover;
365 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800367 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800369
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 if (!options_.no_auto_version) {
371 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373 xml::Element* el = xml::FindRootElement(doc);
374 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 if (el->name == "vector" || el->name == "animated-vector") {
376 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700379 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700381 }
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900382 if (options_.no_version_transitions) {
383 // Skip this if it is a transition resource.
384 xml::Element* el = xml::FindRootElement(doc);
385 if (el && el->namespace_uri.empty()) {
386 if (IsTransitionElement(el->name)) {
387 // We are NOT going to version this file.
388 file_op->skip_version = true;
389 return true;
390 }
391 }
392 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700393
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395
396 // Find the first SDK level used that is higher than this defined config and
397 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398 const int min_sdk_version = context_->GetMinSdkVersion();
399 for (int sdk_level : xml_linker.sdk_levels()) {
400 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
401 if (!ShouldGenerateVersionedResource(file_op->entry, config,
402 sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 // If we shouldn't generate a versioned resource, stop checking.
404 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800405 }
406
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 ResourceFile versioned_file_desc = doc->file;
408 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700409
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 FileOperation new_file_op;
411 new_file_op.xml_to_flatten = util::make_unique<xml::XmlResource>(
412 versioned_file_desc, doc->root->Clone());
413 new_file_op.config = versioned_file_desc.config;
414 new_file_op.entry = file_op->entry;
415 new_file_op.dst_path = ResourceUtils::BuildResourceFileName(
416 versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 if (context_->IsVerbose()) {
419 context_->GetDiagnostics()->Note(
420 DiagMessage(versioned_file_desc.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 << "auto-versioning resource from config '" << config << "' -> '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800423 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 bool added = table->AddFileReferenceAllowMangled(
426 versioned_file_desc.name, versioned_file_desc.config,
427 versioned_file_desc.source, new_file_op.dst_path, nullptr,
428 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 if (!added) {
430 return false;
431 }
432
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 break;
435 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800436 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437 }
438 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800439}
440
441/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 * Do not insert or remove any resources while executing in this function. It
443 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800444 * corrupt the iteration order.
445 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446bool ResourceFileFlattener::Flatten(ResourceTable* table,
447 IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 bool error = false;
449 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800451
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700452 for (auto& pkg : table->packages) {
453 for (auto& type : pkg->types) {
454 // Sort by config and name, so that we get better locality in the zip
455 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 config_sorted_files.clear();
457 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800458
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 // Populate the queue with all files in the ResourceTable.
460 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 for (auto& config_value : entry->values) {
462 FileReference* file_ref =
463 ValueCast<FileReference>(config_value->value.get());
464 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700465 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700467
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700472 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 }
474
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 FileOperation file_op;
476 file_op.entry = entry.get();
477 file_op.dst_path = *file_ref->path;
478 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 if (type->type != ResourceType::kRaw &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 (util::EndsWith(src_path, ".xml.flat") ||
483 util::EndsWith(src_path, ".xml"))) {
484 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 << "failed to open file");
488 return false;
489 }
490
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 file_op.xml_to_flatten =
492 xml::Inflate(data->data(), data->size(),
493 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 return false;
497 }
498
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499 file_op.xml_to_flatten->file.config = config_value->config;
500 file_op.xml_to_flatten->file.source = file_ref->GetSource();
501 file_op.xml_to_flatten->file.name =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 ResourceName(pkg->name, type->type, entry->name);
503
504 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508
509 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700510 // else we end up copying the string in the std::make_pair() method,
511 // then creating a StringPiece from the copy, which would cause us
512 // to end up referencing garbage in the map.
513 const StringPiece entry_name(entry->name);
514 config_sorted_files[std::make_pair(
515 config_value->config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 }
517 }
518 }
519
520 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521 for (; !file_operations.empty(); file_operations.pop()) {
522 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 error = true;
526 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700527 }
528
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700529 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
530 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 // creating a StringPiece from the copy, which would cause us to end up
532 // referencing garbage in the map.
533 const StringPiece entry_name(file_op.entry->name);
534 config_sorted_files[std::make_pair(file_op.config, entry_name)] =
535 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 }
537
538 if (error) {
539 return false;
540 }
541
542 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 for (auto& map_entry : config_sorted_files) {
544 const ConfigDescription& config = map_entry.first.first;
545 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 if (file_op.xml_to_flatten) {
548 Maybe<size_t> max_sdk_level;
549 if (!options_.no_auto_version && !file_op.skip_version) {
550 max_sdk_level =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 }
554
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 bool result = FlattenXml(
556 file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
557 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 if (!result) {
559 error = true;
560 }
561 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 bool result = CopyFileToArchive(
563 file_op.file_to_copy, file_op.dst_path,
564 GetCompressionFlags(file_op.dst_path), archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 if (!result) {
566 error = true;
567 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700568 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700570 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 }
572 return !error;
573}
574
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575static bool WriteStableIdMapToPath(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 IDiagnostics* diag,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 const std::unordered_map<ResourceName, ResourceId>& id_map,
578 const std::string& id_map_path) {
579 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 return false;
583 }
584
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586 const ResourceName& name = entry.first;
587 const ResourceId& id = entry.second;
588 fout << name << " = " << id << "\n";
589 }
590
591 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 diag->Error(DiagMessage(id_map_path)
593 << "failed writing to file: "
594 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 return false;
596 }
597
598 return true;
599}
600
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601static bool LoadStableIdMap(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 IDiagnostics* diag, const std::string& path,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700603 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 std::string content;
605 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 return false;
608 }
609
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700610 out_id_map->clear();
611 size_t line_no = 0;
612 for (StringPiece line : util::Tokenize(content, '\n')) {
613 line_no++;
614 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700615 if (line.empty()) {
616 continue;
617 }
618
619 auto iter = std::find(line.begin(), line.end(), '=');
620 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622 return false;
623 }
624
625 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700626 StringPiece res_name_str =
627 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
628 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
629 diag->Error(DiagMessage(Source(path, line_no))
630 << "invalid resource name '" << res_name_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631 return false;
632 }
633
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
635 const size_t res_id_str_len = line.size() - res_id_start_idx;
636 StringPiece res_id_str =
637 util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700639 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
640 if (!maybe_id) {
641 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '"
642 << res_id_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 return false;
644 }
645
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700646 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647 }
648 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700649}
650
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700651static bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag,
652 std::string* out_path,
653 SplitConstraints* out_split) {
654 std::vector<std::string> parts = util::Split(arg, ':');
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655 if (parts.size() != 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700656 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
657 diag->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 DiagMessage()
659 << "should be --split path/to/output.apk:<config>[,<config>...]");
660 return false;
661 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700662 *out_path = parts[0];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 std::vector<ConfigDescription> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 configs.push_back({});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700666 if (!ConfigDescription::Parse(config_str, &configs.back())) {
667 diag->Error(DiagMessage() << "invalid config '" << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 << "' in split parameter '" << arg << "'");
669 return false;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700670 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700672 out_split->configs.insert(configs.begin(), configs.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673 return true;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700674}
675
Adam Lesinskifb48d292015-11-07 15:52:13 -0800676class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677 public:
678 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 : options_(options),
680 context_(context),
681 final_table_(),
682 file_collection_(util::make_unique<io::FileCollection>()) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700683
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 /**
685 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700686 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688 bool LoadSymbolsFromIncludePaths() {
689 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700691 for (const std::string& path : options_.include_paths) {
692 if (context_->IsVerbose()) {
693 context_->GetDiagnostics()->Note(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 << "loading include path");
695 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700696
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 std::string error_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800699 std::unique_ptr<ResourceTable> include_static = LoadStaticLibrary(path, &error_str);
700 if (include_static) {
701 if (options_.package_type != PackageType::kStaticLib) {
702 // Can't include static libraries when not building a static library (they have no IDs
703 // assigned).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 context_->GetDiagnostics()->Error(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800705 DiagMessage(path) << "can't include static library when not building a static lib");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700706 return false;
707 }
708
709 // If we are using --no-static-lib-packages, we need to rename the
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800710 // package of this table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700711 if (options_.no_static_lib_packages) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800712 // Since package names can differ, and multiple packages can exist in a ResourceTable,
713 // we place the requirement that all static libraries are built with the package
714 // ID 0x7f. So if one is not found, this is an error.
715 if (ResourceTablePackage* pkg = include_static->FindPackageById(kAppPackageId)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 pkg->name = context_->GetCompilationPackage();
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800717 } else {
718 context_->GetDiagnostics()->Error(DiagMessage(path)
719 << "no package with ID 0x7f found in static library");
720 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 }
722 }
723
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700724 context_->GetExternalSymbols()->AppendSource(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800725 util::make_unique<ResourceTableSymbolSource>(include_static.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800727 static_table_includes_.push_back(std::move(include_static));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700728
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700729 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700731 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 return false;
733 }
734
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700735 if (!asset_source->AddAssetPath(path)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800736 context_->GetDiagnostics()->Error(DiagMessage(path) << "failed to load include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700737 return false;
738 }
739 }
740
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800741 // Capture the shared libraries so that the final resource table can be properly flattened
742 // with support for shared libraries.
743 for (auto& entry : asset_source->GetAssignedPackageIds()) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800744 if (entry.first > kFrameworkPackageId && entry.first < kAppPackageId) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800745 final_table_.included_packages_[entry.first] = entry.second;
746 }
747 }
748
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700749 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 return true;
751 }
752
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700753 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 IDiagnostics* diag) {
755 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700756 if (xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get())) {
757 AppInfo app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700759 if (!manifest_el->namespace_uri.empty() ||
760 manifest_el->name != "manifest") {
761 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 << "root tag must be <manifest>");
763 return {};
764 }
765
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
767 if (!package_attr) {
768 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 << "<manifest> must have a 'package' attribute");
770 return {};
771 }
772
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700773 app_info.package = package_attr->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700774
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700775 if (xml::Attribute* version_code_attr =
776 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
777 Maybe<uint32_t> maybe_code =
778 ResourceUtils::ParseInt(version_code_attr->value);
779 if (!maybe_code) {
780 diag->Error(DiagMessage(xml_res->file.source.WithLine(
781 manifest_el->line_number))
782 << "invalid android:versionCode '"
783 << version_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700784 return {};
785 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786 app_info.version_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700787 }
788
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700789 if (xml::Attribute* revision_code_attr =
790 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
791 Maybe<uint32_t> maybe_code =
792 ResourceUtils::ParseInt(revision_code_attr->value);
793 if (!maybe_code) {
794 diag->Error(DiagMessage(xml_res->file.source.WithLine(
795 manifest_el->line_number))
796 << "invalid android:revisionCode '"
797 << revision_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700798 return {};
799 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700800 app_info.revision_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700801 }
802
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700803 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
804 if (xml::Attribute* min_sdk = uses_sdk_el->FindAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700805 xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700806 app_info.min_sdk_version = min_sdk->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700807 }
808 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700809 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700810 }
811 return {};
812 }
813
814 /**
815 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it
816 * linked.
817 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700818 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700819 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700820 bool VerifyNoExternalPackages() {
821 auto is_ext_package_func =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700823 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
824 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700825 };
826
827 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700828 for (const auto& package : final_table_.packages) {
829 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700830 // We have a package that is not related to the one we're building!
831 for (const auto& type : package->types) {
832 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700833 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700834
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700835 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700836 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700837 // for the 'android' package. This is due to legacy reasons.
838 if (ValueCast<Id>(config_value->value.get()) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839 package->name == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700840 context_->GetDiagnostics()->Warn(
841 DiagMessage(config_value->value->GetSource())
842 << "generated id '" << res_name
843 << "' for external package '" << package->name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700845 context_->GetDiagnostics()->Error(
846 DiagMessage(config_value->value->GetSource())
847 << "defined resource '" << res_name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 << "' for external package '" << package->name << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700849 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700850 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700851 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700852 }
853 }
854 }
855 }
856
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857 auto new_end_iter =
858 std::remove_if(final_table_.packages.begin(),
859 final_table_.packages.end(), is_ext_package_func);
860 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 return !error;
862 }
863
864 /**
865 * Returns true if no IDs have been set, false otherwise.
866 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700867 bool VerifyNoIdsSet() {
868 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700869 for (const auto& type : package->types) {
870 if (type->id) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800871 context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID "
872 << StringPrintf("%02x", type->id.value())
873 << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700875 }
876
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700877 for (const auto& entry : type->entries) {
878 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700879 ResourceNameRef res_name(package->name, type->type, entry->name);
880 context_->GetDiagnostics()->Error(
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800881 DiagMessage() << "entry " << res_name << " has ID "
882 << StringPrintf("%02x", entry->id.value()) << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700883 return false;
884 }
885 }
886 }
887 }
888 return true;
889 }
890
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700891 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
892 if (options_.output_to_directory) {
893 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700894 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700895 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700896 }
897 }
898
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700899 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700900 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800901 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700902 if (!flattener.Consume(context_, table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700903 return false;
904 }
905
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700906 if (writer->StartEntry("resources.arsc", ArchiveEntry::kAlign)) {
907 if (writer->WriteEntry(buffer)) {
908 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700909 return true;
910 }
911 }
912 }
913
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700914 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700915 DiagMessage() << "failed to write resources.arsc to archive");
916 return false;
917 }
918
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700919 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700920 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700921 if (!writer->StartEntry("resources.arsc.flat", 0)) {
922 context_->GetDiagnostics()->Error(DiagMessage() << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700923 return false;
924 }
925
926 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700927 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700928 {
929 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700930 // ZeroCopyOutputStream interface.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700931 CopyingOutputStreamAdaptor adaptor(writer);
932
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700933 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
934 if (!pb_table->SerializeToZeroCopyStream(&adaptor)) {
935 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700936 return false;
937 }
938 }
939
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700940 if (!writer->FinishEntry()) {
941 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700942 << "failed to finish entry");
943 return false;
944 }
945 return true;
946 }
947
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700948 bool WriteJavaFile(ResourceTable* table,
949 const StringPiece& package_name_to_generate,
950 const StringPiece& out_package,
951 const JavaClassGeneratorOptions& java_options) {
952 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953 return true;
954 }
955
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700956 std::string out_path = options_.generate_java_class_path.value();
957 file::AppendPath(&out_path, file::PackageToPath(out_package));
958 if (!file::mkdirs(out_path)) {
959 context_->GetDiagnostics()->Error(
960 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700961 return false;
962 }
963
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700964 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700965
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700966 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700967 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700968 context_->GetDiagnostics()->Error(
969 DiagMessage() << "failed writing to '" << out_path << "': "
970 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700971 return false;
972 }
973
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700974 JavaClassGenerator generator(context_, table, java_options);
975 if (!generator.Generate(package_name_to_generate, out_package, &fout)) {
976 context_->GetDiagnostics()->Error(DiagMessage(out_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 << generator.getError());
978 return false;
979 }
980
981 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700982 context_->GetDiagnostics()->Error(
983 DiagMessage() << "failed writing to '" << out_path << "': "
984 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 }
986 return true;
987 }
988
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700989 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
990 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700991 return true;
992 }
993
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700994 std::unique_ptr<ClassDefinition> manifest_class =
995 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700996
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700997 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700998 // Something bad happened, but we already logged it, so exit.
999 return false;
1000 }
1001
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001002 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001003 // Empty Manifest class, no need to generate it.
1004 return true;
1005 }
1006
1007 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001008 for (const std::string& annotation : options_.javadoc_annotations) {
1009 std::string proper_annotation = "@";
1010 proper_annotation += annotation;
1011 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012 }
1013
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001014 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001015
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001016 std::string out_path = options_.generate_java_class_path.value();
1017 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001018
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001019 if (!file::mkdirs(out_path)) {
1020 context_->GetDiagnostics()->Error(
1021 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001022 return false;
1023 }
1024
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001025 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001026
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001028 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001029 context_->GetDiagnostics()->Error(
1030 DiagMessage() << "failed writing to '" << out_path << "': "
1031 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001032 return false;
1033 }
1034
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001035 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8,
1036 true, &fout)) {
1037 context_->GetDiagnostics()->Error(
1038 DiagMessage() << "failed writing to '" << out_path << "': "
1039 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001040 return false;
1041 }
1042 return true;
1043 }
1044
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001045 bool WriteProguardFile(const Maybe<std::string>& out,
1046 const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001047 if (!out) {
1048 return true;
1049 }
1050
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001051 const std::string& out_path = out.value();
1052 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001053 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001054 context_->GetDiagnostics()->Error(
1055 DiagMessage() << "failed to open '" << out_path << "': "
1056 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001057 return false;
1058 }
1059
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001060 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001061 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001062 context_->GetDiagnostics()->Error(
1063 DiagMessage() << "failed writing to '" << out_path << "': "
1064 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001065 return false;
1066 }
1067 return true;
1068 }
1069
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001070 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1071 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001072 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001073 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001074 if (!collection) {
1075 return {};
1076 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001077 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001078 }
1079
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001080 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001081 io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001082 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001083 if (!file) {
1084 return {};
1085 }
1086
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001087 std::unique_ptr<io::IData> data = file->OpenAsData();
1088 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1089 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001090 }
1091
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001092 bool MergeStaticLibrary(const std::string& input, bool override) {
1093 if (context_->IsVerbose()) {
1094 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001095 << "merging static library " << input);
1096 }
1097
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001098 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001099 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001100 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001101 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001102 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001103 return false;
1104 }
1105
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001106 std::unique_ptr<ResourceTable> table = LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001107 if (!table) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001108 context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 return false;
1110 }
1111
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001112 ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001113 if (!pkg) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001114 context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001115 return false;
1116 }
1117
1118 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001119 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001120 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001121 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001122
1123 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001124 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001125 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001126 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001127 }
1128
1129 pkg->name = "";
1130 if (override) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001131 result = table_merger_->MergeOverlay(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001132 } else {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001133 result = table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001134 }
1135
1136 } else {
1137 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001138 // preserved and resource names are mangled.
1139 result = table_merger_->MergeAndMangle(Source(input), pkg->name,
1140 table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001141 }
1142
1143 if (!result) {
1144 return false;
1145 }
1146
1147 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001148 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001149 return true;
1150 }
1151
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001152 bool MergeResourceTable(io::IFile* file, bool override) {
1153 if (context_->IsVerbose()) {
1154 context_->GetDiagnostics()->Note(
1155 DiagMessage() << "merging resource table " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001156 }
1157
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001158 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001159 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001160 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001161 << "failed to open file");
1162 return false;
1163 }
1164
1165 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001166 LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1167 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001168 if (!table) {
1169 return false;
1170 }
1171
1172 bool result = false;
1173 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001174 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001175 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001176 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001177 }
1178 return result;
1179 }
1180
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001181 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001182 bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001183 if (context_->IsVerbose()) {
1184 context_->GetDiagnostics()->Note(
1185 DiagMessage() << "merging '" << file_desc->name
1186 << "' from compiled file " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001187 }
1188
1189 bool result = false;
1190 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001191 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001192 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001193 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001194 }
1195
1196 if (!result) {
1197 return false;
1198 }
1199
1200 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001201 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1202 if (exported_symbol.name.package.empty()) {
1203 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001204 }
1205
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001206 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001207
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001208 Maybe<ResourceName> mangled_name =
1209 context_->GetNameMangler()->MangleName(exported_symbol.name);
1210 if (mangled_name) {
1211 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001212 }
1213
1214 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001215 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1216 bool result = final_table_.AddResourceAllowMangled(
1217 res_name, ConfigDescription::DefaultConfig(), std::string(),
1218 std::move(id), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001219 if (!result) {
1220 return false;
1221 }
1222 }
1223 return true;
1224 }
1225
1226 /**
1227 * Takes a path to load as a ZIP file and merges the files within into the
1228 * master ResourceTable.
1229 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001230 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001231 *
1232 * An io::IFileCollection is created from the ZIP file and added to the set of
1233 * io::IFileCollections that are open.
1234 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001235 bool MergeArchive(const std::string& input, bool override) {
1236 if (context_->IsVerbose()) {
1237 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001238 << input);
1239 }
1240
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001241 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001242 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001243 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001244 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001245 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001246 return false;
1247 }
1248
1249 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001250 for (auto iter = collection->Iterator(); iter->HasNext();) {
1251 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001252 error = true;
1253 }
1254 }
1255
1256 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001257 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001258 return !error;
1259 }
1260
1261 /**
1262 * Takes a path to load and merge into the master ResourceTable. If override
1263 * is true,
1264 * conflicting resources are allowed to override each other, in order of last
1265 * seen.
1266 *
1267 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1268 * as ZIP archive
1269 * and the files within are merged individually.
1270 *
1271 * Otherwise the files is processed on its own.
1272 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001273 bool MergePath(const std::string& path, bool override) {
1274 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1275 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1276 return MergeArchive(path, override);
1277 } else if (util::EndsWith(path, ".apk")) {
1278 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001279 }
1280
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001281 io::IFile* file = file_collection_->InsertFile(path);
1282 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001283 }
1284
1285 /**
1286 * Takes a file to load and merge into the master ResourceTable. If override
1287 * is true,
1288 * conflicting resources are allowed to override each other, in order of last
1289 * seen.
1290 *
1291 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1292 * merged into the
1293 * master ResourceTable. If the file ends with .flat, then it is treated like
1294 * a compiled file
1295 * and the header data is read and merged into the final ResourceTable.
1296 *
1297 * All other file types are ignored. This is because these files could be
1298 * coming from a zip,
1299 * where we could have other files like classes.dex.
1300 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001301 bool MergeFile(io::IFile* file, bool override) {
1302 const Source& src = file->GetSource();
1303 if (util::EndsWith(src.path, ".arsc.flat")) {
1304 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001305
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001306 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001307 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001310 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001311 return false;
1312 }
1313
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001314 CompiledFileInputStream input_stream(data->data(), data->size());
1315 uint32_t num_files = 0;
1316 if (!input_stream.ReadLittleEndian32(&num_files)) {
1317 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001318 << "failed read num files");
1319 return false;
1320 }
1321
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001322 for (uint32_t i = 0; i < num_files; i++) {
1323 pb::CompiledFile compiled_file;
1324 if (!input_stream.ReadCompiledFile(&compiled_file)) {
1325 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001326 DiagMessage(src) << "failed to read compiled file header");
1327 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001328 }
1329
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001330 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001331 if (!input_stream.ReadDataMetaData(&offset, &len)) {
1332 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001333 << "failed to read data meta data");
1334 return false;
1335 }
1336
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001337 std::unique_ptr<ResourceFile> resource_file =
1338 DeserializeCompiledFileFromPb(compiled_file, file->GetSource(),
1339 context_->GetDiagnostics());
1340 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001341 return false;
1342 }
1343
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001344 if (!MergeCompiledFile(file->CreateFileSegment(offset, len),
1345 resource_file.get(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001346 return false;
1347 }
1348 }
1349 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001350 } else if (util::EndsWith(src.path, ".xml") ||
1351 util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001352 // Since AAPT compiles these file types and appends .flat to them, seeing
1353 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001354 const StringPiece file_type =
1355 util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1356 context_->GetDiagnostics()->Error(DiagMessage(src)
1357 << "uncompiled " << file_type
Adam Lesinski6a396c12016-10-20 14:38:23 -07001358 << " file passed as argument. Must be "
1359 "compiled first into .flat file.");
1360 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001361 }
1362
1363 // Ignore non .flat files. This could be classes.dex or something else that
1364 // happens
1365 // to be in an archive.
1366 return true;
1367 }
1368
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001369 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(
1370 const AppInfo& app_info, const SplitConstraints& constraints) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001371 std::unique_ptr<xml::XmlResource> doc =
1372 util::make_unique<xml::XmlResource>();
1373
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001374 std::unique_ptr<xml::Namespace> namespace_android =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001375 util::make_unique<xml::Namespace>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001376 namespace_android->namespace_uri = xml::kSchemaAndroid;
1377 namespace_android->namespace_prefix = "android";
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001379 std::unique_ptr<xml::Element> manifest_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001380 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001381 manifest_el->name = "manifest";
1382 manifest_el->attributes.push_back(
1383 xml::Attribute{"", "package", app_info.package});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001384
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001385 if (app_info.version_code) {
1386 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001387 xml::Attribute{xml::kSchemaAndroid, "versionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001388 std::to_string(app_info.version_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001389 }
1390
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001391 if (app_info.revision_code) {
1392 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001393 xml::Attribute{xml::kSchemaAndroid, "revisionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001394 std::to_string(app_info.revision_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001395 }
1396
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001397 std::stringstream split_name;
1398 split_name << "config." << util::Joiner(constraints.configs, "_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001399
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400 manifest_el->attributes.push_back(
1401 xml::Attribute{"", "split", split_name.str()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001402
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001403 std::unique_ptr<xml::Element> application_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001404 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001405 application_el->name = "application";
1406 application_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001407 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false"});
1408
Adam Lesinskie343eb12016-10-27 16:31:58 -07001409 manifest_el->AppendChild(std::move(application_el));
1410 namespace_android->AppendChild(std::move(manifest_el));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001411 doc->root = std::move(namespace_android);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001412 return doc;
1413 }
1414
1415 /**
1416 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001417 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001418 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001419 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001420 xml::XmlResource* manifest, ResourceTable* table) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001421 const bool keep_raw_values = options_.package_type == PackageType::kStaticLib;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001422 bool result = FlattenXml(manifest, "AndroidManifest.xml", {},
1423 keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001424 if (!result) {
1425 return false;
1426 }
1427
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001428 ResourceFileFlattenerOptions file_flattener_options;
1429 file_flattener_options.keep_raw_values = keep_raw_values;
1430 file_flattener_options.do_not_compress_anything =
1431 options_.do_not_compress_anything;
1432 file_flattener_options.extensions_to_not_compress =
1433 options_.extensions_to_not_compress;
1434 file_flattener_options.no_auto_version = options_.no_auto_version;
1435 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001436 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001437 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1438 file_flattener_options.update_proguard_spec =
1439 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001440
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001441 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001442
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001443 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001444 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001445 return false;
1446 }
1447
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001448 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001449 if (!FlattenTableToPb(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001450 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001451 return false;
1452 }
1453 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001454 if (!FlattenTable(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001455 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001456 return false;
1457 }
1458 }
1459 return true;
1460 }
1461
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001462 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001463 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001464 std::unique_ptr<xml::XmlResource> manifest_xml =
1465 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1466 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001467 return 1;
1468 }
1469
1470 // First extract the Package name without modifying it (via
1471 // --rename-manifest-package).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001472 if (Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1473 manifest_xml.get(), context_->GetDiagnostics())) {
1474 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 Lesinskice5e56e2016-10-21 17:56:45 -07001483 Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1484 manifest_xml.get(), context_->GetDiagnostics());
1485 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001486 return 1;
1487 }
1488
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001489 const AppInfo& app_info = maybe_app_info.value();
1490 if (app_info.min_sdk_version) {
1491 if (Maybe<int> maybe_min_sdk_version = ResourceUtils::ParseSdkVersion(
1492 app_info.min_sdk_version.value())) {
1493 context_->SetMinSdkVersion(maybe_min_sdk_version.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001494 }
1495 }
1496
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001497 context_->SetNameManglerPolicy(
1498 NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001499
1500 // Override the package ID when it is "android".
1501 if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001502 context_->SetPackageId(0x01);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001503
1504 // Verify we're building a regular app.
1505 if (options_.package_type != PackageType::kApp) {
1506 context_->GetDiagnostics()->Error(
1507 DiagMessage() << "package 'android' can only be built as a regular app");
1508 return 1;
1509 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001510 }
1511
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001512 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001513 return 1;
1514 }
1515
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001516 TableMergerOptions table_merger_options;
1517 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
1518 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_,
1519 table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001520
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001521 if (context_->IsVerbose()) {
1522 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001523 << StringPrintf("linking package '%s' using package ID %02x",
1524 context_->GetCompilationPackage().data(),
1525 context_->GetPackageId()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001526 }
1527
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001528 for (const std::string& input : input_files) {
1529 if (!MergePath(input, false)) {
1530 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001531 << "failed parsing input");
1532 return 1;
1533 }
1534 }
1535
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001536 for (const std::string& input : options_.overlay_files) {
1537 if (!MergePath(input, true)) {
1538 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001539 << "failed parsing overlays");
1540 return 1;
1541 }
1542 }
1543
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001544 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001545 return 1;
1546 }
1547
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001548 if (options_.package_type != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001549 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001550 if (!mover.Consume(context_, &final_table_)) {
1551 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001552 DiagMessage() << "failed moving private attributes");
1553 return 1;
1554 }
1555
1556 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001557 IdAssigner id_assigner(&options_.stable_id_map);
1558 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001559 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001560 return 1;
1561 }
1562
1563 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001564 if (options_.resource_id_map_path) {
1565 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001566 for (auto& type : package->types) {
1567 for (auto& entry : type->entries) {
1568 ResourceName name(package->name, type->type, entry->name);
1569 // The IDs are guaranteed to exist.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001570 options_.stable_id_map[std::move(name)] = ResourceId(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001571 package->id.value(), type->id.value(), entry->id.value());
1572 }
1573 }
1574 }
1575
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001576 if (!WriteStableIdMapToPath(context_->GetDiagnostics(),
1577 options_.stable_id_map,
1578 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001579 return 1;
1580 }
1581 }
1582 } else {
1583 // Static libs are merged with other apps, and ID collisions are bad, so
1584 // verify that
1585 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001586 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001587 return 1;
1588 }
1589 }
1590
1591 // Add the names to mangle based on our source merge earlier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001592 context_->SetNameManglerPolicy(NameManglerPolicy{
1593 context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001594
1595 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 context_->GetExternalSymbols()->PrependSource(
1597 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001598
1599 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001600 if (!linker.Consume(context_, &final_table_)) {
1601 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001602 << "failed linking references");
1603 return 1;
1604 }
1605
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001606 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001607 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001608 context_->GetDiagnostics()->Warn(DiagMessage()
1609 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001610 }
1611 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001612 ProductFilter product_filter(options_.products);
1613 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001614 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001615 return 1;
1616 }
1617 }
1618
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001619 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001620 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001621 if (!versioner.Consume(context_, &final_table_)) {
1622 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001623 << "failed versioning styles");
1624 return 1;
1625 }
1626 }
1627
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001628 if (options_.package_type != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001629 if (context_->IsVerbose()) {
1630 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001631 DiagMessage() << "collapsing resource versions for minimum SDK "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001632 << 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 Lesinskiceb9b2f2017-02-16 12:05:42 -08001652 if (options_.package_type == 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 {
1659 // Adjust the SplitConstraints so that their SDK version is stripped if it
1660 // is less
1661 // than or equal to the minSdk. Otherwise the resources that have had
1662 // their SDK version
1663 // stripped due to minSdk won't ever match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001664 std::vector<SplitConstraints> adjusted_constraints_list;
1665 adjusted_constraints_list.reserve(options_.split_constraints.size());
1666 for (const SplitConstraints& constraints : options_.split_constraints) {
1667 SplitConstraints adjusted_constraints;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001668 for (const ConfigDescription& config : constraints.configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001669 if (config.sdkVersion <= context_->GetMinSdkVersion()) {
1670 adjusted_constraints.configs.insert(config.CopyWithoutSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001671 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001672 adjusted_constraints.configs.insert(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001673 }
1674 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001675 adjusted_constraints_list.push_back(std::move(adjusted_constraints));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001676 }
1677
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001678 TableSplitter table_splitter(adjusted_constraints_list,
1679 options_.table_splitter_options);
1680 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001681 return 1;
1682 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001683 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001684
1685 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001686 auto path_iter = options_.split_paths.begin();
1687 auto split_constraints_iter = adjusted_constraints_list.begin();
1688 for (std::unique_ptr<ResourceTable>& split_table :
1689 table_splitter.splits()) {
1690 if (context_->IsVerbose()) {
1691 context_->GetDiagnostics()->Note(
1692 DiagMessage(*path_iter)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001693 << "generating split with configurations '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001694 << util::Joiner(split_constraints_iter->configs, ", ") << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001695 }
1696
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001697 std::unique_ptr<IArchiveWriter> archive_writer =
1698 MakeArchiveWriter(*path_iter);
1699 if (!archive_writer) {
1700 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001701 << "failed to create archive");
1702 return 1;
1703 }
1704
1705 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001706 std::unique_ptr<xml::XmlResource> split_manifest =
1707 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001708
1709 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001710 if (!linker.Consume(context_, split_manifest.get())) {
1711 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001712 DiagMessage() << "failed to create Split AndroidManifest.xml");
1713 return 1;
1714 }
1715
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001716 if (!WriteApk(archive_writer.get(), &proguard_keep_set,
1717 split_manifest.get(), split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001718 return 1;
1719 }
1720
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001721 ++path_iter;
1722 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001723 }
1724 }
1725
1726 // Start writing the base APK.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001727 std::unique_ptr<IArchiveWriter> archive_writer =
1728 MakeArchiveWriter(options_.output_path);
1729 if (!archive_writer) {
1730 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001731 << "failed to create archive");
1732 return 1;
1733 }
1734
1735 bool error = false;
1736 {
1737 // AndroidManifest.xml has no resource name, but the CallSite is built
1738 // from the name
1739 // (aka, which package the AndroidManifest.xml is coming from).
1740 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001741 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001742
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001743 XmlReferenceLinker manifest_linker;
1744 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1745 if (options_.generate_proguard_rules_path &&
1746 !proguard::CollectProguardRulesForManifest(
1747 Source(options_.manifest_path), manifest_xml.get(),
1748 &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001749 error = true;
1750 }
1751
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001752 if (options_.generate_main_dex_proguard_rules_path &&
1753 !proguard::CollectProguardRulesForManifest(
1754 Source(options_.manifest_path), manifest_xml.get(),
1755 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001756 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001757 }
1758
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001759 if (options_.generate_java_class_path) {
1760 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001761 error = true;
1762 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001763 }
1764
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001765 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001766 // PackageParser will fail if URIs are removed from
1767 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001768 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1769 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001770 error = true;
1771 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001772 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001773 } else {
1774 error = true;
1775 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001776 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001777
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001778 if (error) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001779 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001780 << "failed processing manifest");
1781 return 1;
1782 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001783
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001784 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(),
1785 &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001786 return 1;
1787 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001788
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001789 if (options_.generate_java_class_path) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001790 // The set of packages whose R class to call in the main classes
1791 // onResourcesLoaded callback.
1792 std::vector<std::string> packages_to_callback;
1793
1794 JavaClassGeneratorOptions template_options;
1795 template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1796 template_options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001797
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001798 if (options_.package_type == PackageType::kStaticLib || options_.generate_non_final_ids) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001799 template_options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001800 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001801
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001802 if (options_.package_type == PackageType::kSharedLib) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001803 template_options.use_final = false;
1804 template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{};
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001805 }
1806
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001807 const StringPiece actual_package = context_->GetCompilationPackage();
1808 StringPiece output_package = context_->GetCompilationPackage();
1809 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001810 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001811 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001812 }
1813
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001814 // Generate the private symbols if required.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001815 if (options_.private_symbols) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001816 packages_to_callback.push_back(options_.private_symbols.value());
1817
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001818 // If we defined a private symbols package, we only emit Public symbols
1819 // to the original package, and private and public symbols to the
1820 // private package.
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001821 JavaClassGeneratorOptions options = template_options;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001822 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001823 if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001824 options)) {
1825 return 1;
1826 }
1827 }
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001828
1829 // Generate all the symbols for all extra packages.
1830 for (const std::string& extra_package : options_.extra_java_packages) {
1831 packages_to_callback.push_back(extra_package);
1832
1833 JavaClassGeneratorOptions options = template_options;
1834 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1835 if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) {
1836 return 1;
1837 }
1838 }
1839
1840 // Generate the main public R class.
1841 JavaClassGeneratorOptions options = template_options;
1842
1843 // Only generate public symbols if we have a private package.
1844 if (options_.private_symbols) {
1845 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
1846 }
1847
1848 if (options.rewrite_callback_options) {
1849 options.rewrite_callback_options.value().packages_to_callback =
1850 std::move(packages_to_callback);
1851 }
1852
1853 if (!WriteJavaFile(&final_table_, actual_package, output_package, options)) {
1854 return 1;
1855 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001856 }
1857
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001858 if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001859 return 1;
1860 }
1861
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001862 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1863 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001864 return 1;
1865 }
1866
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001867 if (context_->IsVerbose()) {
1868 DebugPrintTableOptions debug_print_table_options;
1869 debug_print_table_options.show_sources = true;
1870 Debug::PrintTable(&final_table_, debug_print_table_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001871 }
1872 return 0;
1873 }
1874
1875 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001876 LinkOptions options_;
1877 LinkContext* context_;
1878 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001879
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001880 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001881
1882 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001883 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001884
1885 // A vector of IFileCollections. This is mainly here to keep ownership of the
1886 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001887 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001888
1889 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001890 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001891 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001892
1893 // The set of shared libraries being used, mapping their assigned package ID to package name.
1894 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001895};
1896
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001897int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001898 LinkContext context;
1899 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001900 std::vector<std::string> overlay_arg_list;
1901 std::vector<std::string> extra_java_packages;
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001902 Maybe<std::string> package_id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001903 Maybe<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001904 Maybe<std::string> preferred_density;
1905 Maybe<std::string> product_list;
1906 bool legacy_x_flag = false;
1907 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001908 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001909 bool shared_lib = false;
1910 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001911 Maybe<std::string> stable_id_file_path;
1912 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001913 Flags flags =
1914 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001915 .RequiredFlag("-o", "Output path", &options.output_path)
1916 .RequiredFlag("--manifest", "Path to the Android manifest to build",
1917 &options.manifest_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001918 .OptionalFlagList("-I", "Adds an Android APK to link against", &options.include_paths)
1919 .OptionalFlagList("-R",
1920 "Compilation unit to link, using `overlay` semantics.\n"
1921 "The last conflicting resource given takes precedence.",
1922 &overlay_arg_list)
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001923 .OptionalFlag("--package-id",
1924 "Specify the package ID to use for this app. Must be greater or equal to\n"
1925 "0x7f and can't be used with --static-lib or --shared-lib.",
1926 &package_id)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001927 .OptionalFlag("--java", "Directory in which to generate R.java",
1928 &options.generate_java_class_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001929 .OptionalFlag("--proguard", "Output file for generated Proguard rules",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001930 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001931 .OptionalFlag("--proguard-main-dex",
1932 "Output file for generated Proguard rules for the main dex",
1933 &options.generate_main_dex_proguard_rules_path)
1934 .OptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001935 &options.no_auto_version)
1936 .OptionalSwitch("--no-version-vectors",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001937 "Disables automatic versioning of vector drawables. "
1938 "Use this only\n"
1939 "when building with vector drawable support library",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001940 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001941 .OptionalSwitch("--no-version-transitions",
1942 "Disables automatic versioning of transition resources. "
1943 "Use this only\n"
1944 "when building with transition support library",
1945 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001946 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001947 "Disables automatic deduping of resources with\n"
1948 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001949 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001950 .OptionalSwitch("--enable-sparse-encoding",
1951 "Enables encoding sparse entries using a binary search tree.\n"
1952 "This decreases APK size at the cost of resource retrieval performance.",
1953 &options.table_flattener_options.use_sparse_entries)
1954 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01",
1955 &legacy_x_flag)
1956 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001957 &require_localization)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001958 .OptionalFlag("-c",
1959 "Comma separated list of configurations to include. The default\n"
1960 "is all configurations",
1961 &configs)
1962 .OptionalFlag("--preferred-density",
1963 "Selects the closest matching density and strips out all others.",
1964 &preferred_density)
1965 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001966 .OptionalSwitch("--output-to-dir",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001967 "Outputs the APK contents to a directory specified "
1968 "by -o",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001969 &options.output_to_directory)
1970 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001971 "Removes XML namespace prefix and URI "
1972 "information from AndroidManifest.xml\nand XML "
1973 "binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001974 &options.no_xml_namespaces)
1975 .OptionalFlag("--min-sdk-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001976 "Default minimum SDK version to use for "
1977 "AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001978 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001979 .OptionalFlag("--target-sdk-version",
1980 "Default target SDK version to use for "
1981 "AndroidManifest.xml",
1982 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001983 .OptionalFlag("--version-code",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001984 "Version code (integer) to inject into the "
1985 "AndroidManifest.xml if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001986 &options.manifest_fixer_options.version_code_default)
1987 .OptionalFlag("--version-name",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001988 "Version name to inject into the AndroidManifest.xml "
1989 "if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001990 &options.manifest_fixer_options.version_name_default)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001991 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library", &shared_lib)
1992 .OptionalSwitch("--static-lib", "Generate a static Android library", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001993 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001994 "Merge all library resources under the app's package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001995 &options.no_static_lib_packages)
1996 .OptionalSwitch("--non-final-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001997 "Generates R.java without the final modifier.\n"
1998 "This is implied when --static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001999 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002000 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002001 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002002 .OptionalFlag("--emit-ids",
2003 "Emit a file at the given path with a list of name to ID\n"
2004 "mappings, suitable for use with --stable-ids.",
2005 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002006 .OptionalFlag("--private-symbols",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002007 "Package name to use when generating R.java for "
2008 "private symbols.\n"
2009 "If not specified, public and private symbols will use "
2010 "the application's "
2011 "package name",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002012 &options.private_symbols)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002013 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002014 &options.custom_java_package)
2015 .OptionalFlagList("--extra-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002016 "Generate the same R.java but with different "
2017 "package names",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002018 &extra_java_packages)
2019 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002020 "Adds a JavaDoc annotation to all "
Adam Lesinskid0f116b2016-07-08 15:00:32 -07002021 "generated Java classes",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002022 &options.javadoc_annotations)
2023 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002024 "Allows the addition of new resources in "
2025 "overlays without <add-resource> tags",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002026 &options.auto_add_overlay)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002027 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002028 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002029 .OptionalFlag("--rename-instrumentation-target-package",
2030 "Changes the name of the target package for instrumentation. "
2031 "Most useful "
2032 "when used\nin conjunction with --rename-manifest-package",
2033 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002034 .OptionalFlagList("-0", "File extensions not to compress",
2035 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002036 .OptionalFlagList("--split",
2037 "Split resources matching a set of configs out to a "
2038 "Split APK.\nSyntax: path/to/output.apk:<config>[,<config>[...]]",
2039 &split_args)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002040 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002041
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002042 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002043 return 1;
2044 }
2045
2046 // Expand all argument-files passed into the command line. These start with
2047 // '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002048 std::vector<std::string> arg_list;
2049 for (const std::string& arg : flags.GetArgs()) {
2050 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002051 const std::string path = arg.substr(1, arg.size() - 1);
2052 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002053 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
2054 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002055 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002056 }
2057 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002058 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002059 }
2060 }
2061
2062 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002063 for (const std::string& arg : overlay_arg_list) {
2064 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002065 const std::string path = arg.substr(1, arg.size() - 1);
2066 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002067 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
2068 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002069 return 1;
2070 }
2071 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002072 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002073 }
2074 }
2075
2076 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002077 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002078 }
2079
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002080 if (shared_lib && static_lib) {
2081 context.GetDiagnostics()->Error(DiagMessage()
2082 << "only one of --shared-lib and --static-lib can be defined");
2083 return 1;
2084 }
2085
2086 if (shared_lib) {
2087 options.package_type = PackageType::kSharedLib;
2088 context.SetPackageId(0x00);
2089 } else if (static_lib) {
2090 options.package_type = PackageType::kStaticLib;
2091 context.SetPackageId(kAppPackageId);
2092 } else {
2093 options.package_type = PackageType::kApp;
2094 context.SetPackageId(kAppPackageId);
2095 }
2096
2097 if (package_id) {
2098 if (options.package_type != PackageType::kApp) {
2099 context.GetDiagnostics()->Error(
2100 DiagMessage() << "can't specify --package-id when not building a regular app");
2101 return 1;
2102 }
2103
2104 const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id.value());
2105 if (!maybe_package_id_int) {
2106 context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id.value()
2107 << "' is not a valid integer");
2108 return 1;
2109 }
2110
2111 const uint32_t package_id_int = maybe_package_id_int.value();
2112 if (package_id_int < kAppPackageId || package_id_int > std::numeric_limits<uint8_t>::max()) {
2113 context.GetDiagnostics()->Error(
2114 DiagMessage() << StringPrintf(
2115 "invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int));
2116 return 1;
2117 }
2118 context.SetPackageId(static_cast<uint8_t>(package_id_int));
2119 }
2120
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002121 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002122 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002123 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002124 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002125 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002126 }
2127 }
2128
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002129 if (product_list) {
2130 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002131 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002132 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002133 }
2134 }
2135 }
2136
2137 AxisConfigFilter filter;
2138 if (configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002139 for (const StringPiece& config_str : util::Tokenize(configs.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002140 ConfigDescription config;
2141 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002142 if (lv.InitFromFilterString(config_str)) {
2143 lv.WriteTo(&config);
2144 } else if (!ConfigDescription::Parse(config_str, &config)) {
2145 context.GetDiagnostics()->Error(DiagMessage() << "invalid config '"
2146 << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002147 << "' for -c option");
2148 return 1;
2149 }
2150
2151 if (config.density != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002152 context.GetDiagnostics()->Warn(DiagMessage() << "ignoring density '"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002153 << config
2154 << "' for -c option");
2155 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002156 filter.AddConfig(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002157 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002158 }
2159
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002160 options.table_splitter_options.config_filter = &filter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002161 }
2162
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002163 if (preferred_density) {
2164 ConfigDescription preferred_density_config;
2165 if (!ConfigDescription::Parse(preferred_density.value(),
2166 &preferred_density_config)) {
2167 context.GetDiagnostics()->Error(
2168 DiagMessage() << "invalid density '" << preferred_density.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002169 << "' for --preferred-density option");
2170 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002171 }
2172
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002173 // Clear the version that can be automatically added.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002174 preferred_density_config.sdkVersion = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002175
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002176 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002177 ConfigDescription::CONFIG_DENSITY) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002178 context.GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002179 DiagMessage() << "invalid preferred density '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002180 << preferred_density.value() << "'. "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002181 << "Preferred density must only be a density value");
2182 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002183 }
Pierre Lecesne672384b2017-02-06 10:29:02 +00002184 options.table_splitter_options.preferred_densities.push_back(preferred_density_config.density);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002185 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002186
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002187 if (options.package_type != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002188 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2189 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002190 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002191 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002192 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002193
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002194 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002195 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002196 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2197 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2198 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2199 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2200
2201 // Parse the split parameters.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002202 for (const std::string& split_arg : split_args) {
2203 options.split_paths.push_back({});
2204 options.split_constraints.push_back({});
2205 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(),
2206 &options.split_paths.back(),
2207 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002208 return 1;
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002209 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002210 }
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002211
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002212 // Turn off auto versioning for static-libs.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002213 if (options.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002214 options.no_auto_version = true;
2215 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002216 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002217 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002218
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002219 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002220 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002221}
2222
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002223} // namespace aapt