blob: 1b4d5bb4f6e1eec00704ad41f44d8b5de3951ae1 [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;
Adam Lesinskib39ad7c2017-03-13 11:40:48 -070080 std::vector<std::string> assets_dirs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 bool output_to_directory = false;
82 bool auto_add_overlay = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 // Java/Proguard options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 Maybe<std::string> generate_java_class_path;
86 Maybe<std::string> custom_java_package;
87 std::set<std::string> extra_java_packages;
88 Maybe<std::string> generate_proguard_rules_path;
89 Maybe<std::string> generate_main_dex_proguard_rules_path;
90 bool generate_non_final_ids = false;
91 std::vector<std::string> javadoc_annotations;
92 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070093
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 // Optimizations/features.
95 bool no_auto_version = false;
96 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +090097 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 bool no_resource_deduping = false;
99 bool no_xml_namespaces = false;
100 bool do_not_compress_anything = false;
101 std::unordered_set<std::string> extensions_to_not_compress;
102
103 // Static lib options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 bool no_static_lib_packages = false;
105
106 // AndroidManifest.xml massaging options.
107 ManifestFixerOptions manifest_fixer_options;
108
109 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700111
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800112 // Flattening options.
113 TableFlattenerOptions table_flattener_options;
114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 TableSplitterOptions table_splitter_options;
117 std::vector<SplitConstraints> split_constraints;
118 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700119
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 std::unordered_map<ResourceName, ResourceId> stable_id_map;
122 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123};
124
Adam Lesinski64587af2016-02-18 18:33:06 -0800125class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 public:
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800127 LinkContext() : name_mangler_({}), symbols_(&name_mangler_) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700130
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 NameMangler* GetNameMangler() override { return &name_mangler_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
134 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 const std::string& GetCompilationPackage() override {
138 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800142 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 uint8_t GetPackageId() override { return package_id_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 void SetPackageId(uint8_t id) { package_id_ = id; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800148
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 SymbolTable* GetExternalSymbols() override { return &symbols_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800150
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 bool IsVerbose() override { return verbose_; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 int GetMinSdkVersion() override { return min_sdk_version_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 void SetMinSdkVersion(int minSdk) { min_sdk_version_ = minSdk; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700158
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 DISALLOW_COPY_AND_ASSIGN(LinkContext);
161
162 StdErrDiagnostics diagnostics_;
163 NameMangler name_mangler_;
164 std::string compilation_package_;
165 uint8_t package_id_ = 0x0;
166 SymbolTable symbols_;
167 bool verbose_ = false;
168 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169};
170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171static bool CopyFileToArchive(io::IFile* file, const std::string& out_path,
172 uint32_t compression_flags,
173 IArchiveWriter* writer, IAaptContext* context) {
174 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 context->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 << "failed to open file");
Adam Lesinski355f2852016-02-13 20:26:45 -0800178 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 }
180
181 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data->data());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 const size_t buffer_size = data->size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 if (context->IsVerbose()) {
185 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 << " to archive");
187 }
188
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 if (writer->StartEntry(out_path, compression_flags)) {
190 if (writer->WriteEntry(buffer, buffer_size)) {
191 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 return true;
193 }
194 }
195 }
196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 context->GetDiagnostics()->Error(DiagMessage() << "failed to write file "
198 << out_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800200}
201
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
203 Maybe<size_t> max_sdk_level, bool keep_raw_values,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 IArchiveWriter* writer, IAaptContext* context) {
205 BigBuffer buffer(1024);
206 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 options.keep_raw_values = keep_raw_values;
208 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800211 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 }
213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 DiagMessage msg;
216 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 if (max_sdk_level) {
218 msg << " maxSdkLevel=" << max_sdk_level.value()
219 << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 }
223
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 if (writer->StartEntry(path, ArchiveEntry::kCompress)) {
225 if (writer->WriteEntry(buffer)) {
226 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 return true;
228 }
229 }
230 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 << " to archive");
233 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800234}
235
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 const void* data,
238 size_t len,
Adam Lesinski355f2852016-02-13 20:26:45 -0800239 IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 pb::ResourceTable pb_table;
241 if (!pb_table.ParseFromArray(data, len)) {
242 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 return {};
244 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800245
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 if (!table) {
249 return {};
250 }
251 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800252}
253
254/**
255 * Inflates an XML file from the source path.
256 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 IDiagnostics* diag) {
259 std::ifstream fin(path, std::ifstream::binary);
260 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 return {};
263 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800265}
266
Adam Lesinski355f2852016-02-13 20:26:45 -0800267struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 bool no_auto_version = false;
269 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900270 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 bool no_xml_namespaces = false;
272 bool keep_raw_values = false;
273 bool do_not_compress_anything = false;
274 bool update_proguard_spec = false;
275 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800276};
277
278class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 public:
280 ResourceFileFlattener(const ResourceFileFlattenerOptions& options,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 IAaptContext* context, proguard::KeepSet* keep_set)
282 : options_(options), context_(context), keep_set_(keep_set) {}
Adam Lesinski355f2852016-02-13 20:26:45 -0800283
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800285
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 private:
287 struct FileOperation {
288 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700289
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 // The entry this file came from.
291 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700292
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700295
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700298
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 std::string dst_path;
301 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800303
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800305
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
307 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 ResourceFileFlattenerOptions options_;
310 IAaptContext* context_;
311 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800312};
313
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
315 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 return 0;
317 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800318
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 for (const std::string& extension : options_.extensions_to_not_compress) {
320 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800322 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 }
324 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800325}
326
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900327static bool IsTransitionElement(const std::string& name) {
328 return
329 name == "fade" ||
330 name == "changeBounds" ||
331 name == "slide" ||
332 name == "explode" ||
333 name == "changeImageTransform" ||
334 name == "changeTransform" ||
335 name == "changeClipBounds" ||
336 name == "autoTransition" ||
337 name == "recolor" ||
338 name == "changeScroll" ||
339 name == "transitionSet" ||
340 name == "transition" ||
341 name == "transitionManager";
342}
343
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344bool ResourceFileFlattener::LinkAndVersionXmlFile(
345 ResourceTable* table, FileOperation* file_op,
346 std::queue<FileOperation>* out_file_op_queue) {
347 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700349
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 if (context_->IsVerbose()) {
351 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 }
353
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354 XmlReferenceLinker xml_linker;
355 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 return false;
357 }
358
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 if (options_.update_proguard_spec &&
360 !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 return false;
362 }
363
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 if (options_.no_xml_namespaces) {
365 XmlNamespaceRemover namespace_remover;
366 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800368 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800370
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 if (!options_.no_auto_version) {
372 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 xml::Element* el = xml::FindRootElement(doc);
375 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 if (el->name == "vector" || el->name == "animated-vector") {
377 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700379 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700380 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700382 }
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900383 if (options_.no_version_transitions) {
384 // Skip this if it is a transition resource.
385 xml::Element* el = xml::FindRootElement(doc);
386 if (el && el->namespace_uri.empty()) {
387 if (IsTransitionElement(el->name)) {
388 // We are NOT going to version this file.
389 file_op->skip_version = true;
390 return true;
391 }
392 }
393 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700394
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396
397 // Find the first SDK level used that is higher than this defined config and
398 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 const int min_sdk_version = context_->GetMinSdkVersion();
400 for (int sdk_level : xml_linker.sdk_levels()) {
401 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
402 if (!ShouldGenerateVersionedResource(file_op->entry, config,
403 sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 // If we shouldn't generate a versioned resource, stop checking.
405 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800406 }
407
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 ResourceFile versioned_file_desc = doc->file;
409 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700410
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 FileOperation new_file_op;
412 new_file_op.xml_to_flatten = util::make_unique<xml::XmlResource>(
413 versioned_file_desc, doc->root->Clone());
414 new_file_op.config = versioned_file_desc.config;
415 new_file_op.entry = file_op->entry;
416 new_file_op.dst_path = ResourceUtils::BuildResourceFileName(
417 versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800418
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 if (context_->IsVerbose()) {
420 context_->GetDiagnostics()->Note(
421 DiagMessage(versioned_file_desc.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 << "auto-versioning resource from config '" << config << "' -> '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800424 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426 bool added = table->AddFileReferenceAllowMangled(
427 versioned_file_desc.name, versioned_file_desc.config,
428 versioned_file_desc.source, new_file_op.dst_path, nullptr,
429 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 if (!added) {
431 return false;
432 }
433
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 break;
436 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800437 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 }
439 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800440}
441
442/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 * Do not insert or remove any resources while executing in this function. It
444 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800445 * corrupt the iteration order.
446 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447bool ResourceFileFlattener::Flatten(ResourceTable* table,
448 IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 bool error = false;
450 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451 config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800452
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 for (auto& pkg : table->packages) {
454 for (auto& type : pkg->types) {
455 // Sort by config and name, so that we get better locality in the zip
456 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 config_sorted_files.clear();
458 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800459
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 // Populate the queue with all files in the ResourceTable.
461 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700462 for (auto& config_value : entry->values) {
463 FileReference* file_ref =
464 ValueCast<FileReference>(config_value->value.get());
465 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700466 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700468
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700473 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 }
475
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 FileOperation file_op;
477 file_op.entry = entry.get();
478 file_op.dst_path = *file_ref->path;
479 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 if (type->type != ResourceType::kRaw &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 (util::EndsWith(src_path, ".xml.flat") ||
484 util::EndsWith(src_path, ".xml"))) {
485 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 << "failed to open file");
489 return false;
490 }
491
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 file_op.xml_to_flatten =
493 xml::Inflate(data->data(), data->size(),
494 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 return false;
498 }
499
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700500 file_op.xml_to_flatten->file.config = config_value->config;
501 file_op.xml_to_flatten->file.source = file_ref->GetSource();
502 file_op.xml_to_flatten->file.name =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 ResourceName(pkg->name, type->type, entry->name);
504
505 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700508 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509
510 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 // else we end up copying the string in the std::make_pair() method,
512 // then creating a StringPiece from the copy, which would cause us
513 // to end up referencing garbage in the map.
514 const StringPiece entry_name(entry->name);
515 config_sorted_files[std::make_pair(
516 config_value->config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 }
518 }
519 }
520
521 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 for (; !file_operations.empty(); file_operations.pop()) {
523 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700525 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 error = true;
527 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700528 }
529
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
531 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 // creating a StringPiece from the copy, which would cause us to end up
533 // referencing garbage in the map.
534 const StringPiece entry_name(file_op.entry->name);
535 config_sorted_files[std::make_pair(file_op.config, entry_name)] =
536 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537 }
538
539 if (error) {
540 return false;
541 }
542
543 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544 for (auto& map_entry : config_sorted_files) {
545 const ConfigDescription& config = map_entry.first.first;
546 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 if (file_op.xml_to_flatten) {
549 Maybe<size_t> max_sdk_level;
550 if (!options_.no_auto_version && !file_op.skip_version) {
551 max_sdk_level =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700553 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 }
555
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556 bool result = FlattenXml(
557 file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
558 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 if (!result) {
560 error = true;
561 }
562 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700563 bool result = CopyFileToArchive(
564 file_op.file_to_copy, file_op.dst_path,
565 GetCompressionFlags(file_op.dst_path), archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 if (!result) {
567 error = true;
568 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700569 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700571 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 }
573 return !error;
574}
575
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576static bool WriteStableIdMapToPath(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700577 IDiagnostics* diag,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700578 const std::unordered_map<ResourceName, ResourceId>& id_map,
579 const std::string& id_map_path) {
580 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700583 return false;
584 }
585
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 const ResourceName& name = entry.first;
588 const ResourceId& id = entry.second;
589 fout << name << " = " << id << "\n";
590 }
591
592 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 diag->Error(DiagMessage(id_map_path)
594 << "failed writing to file: "
595 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 return false;
597 }
598
599 return true;
600}
601
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602static bool LoadStableIdMap(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 IDiagnostics* diag, const std::string& path,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700604 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 std::string content;
606 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 return false;
609 }
610
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700611 out_id_map->clear();
612 size_t line_no = 0;
613 for (StringPiece line : util::Tokenize(content, '\n')) {
614 line_no++;
615 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 if (line.empty()) {
617 continue;
618 }
619
620 auto iter = std::find(line.begin(), line.end(), '=');
621 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 return false;
624 }
625
626 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 StringPiece res_name_str =
628 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
629 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
630 diag->Error(DiagMessage(Source(path, line_no))
631 << "invalid resource name '" << res_name_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700632 return false;
633 }
634
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700635 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
636 const size_t res_id_str_len = line.size() - res_id_start_idx;
637 StringPiece res_id_str =
638 util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700639
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700640 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
641 if (!maybe_id) {
642 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '"
643 << res_id_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 return false;
645 }
646
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 }
649 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700650}
651
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652static bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag,
653 std::string* out_path,
654 SplitConstraints* out_split) {
655 std::vector<std::string> parts = util::Split(arg, ':');
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 if (parts.size() != 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
658 diag->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 DiagMessage()
660 << "should be --split path/to/output.apk:<config>[,<config>...]");
661 return false;
662 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700663 *out_path = parts[0];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 std::vector<ConfigDescription> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 configs.push_back({});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700667 if (!ConfigDescription::Parse(config_str, &configs.back())) {
668 diag->Error(DiagMessage() << "invalid config '" << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 << "' in split parameter '" << arg << "'");
670 return false;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700671 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 out_split->configs.insert(configs.begin(), configs.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674 return true;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700675}
676
Adam Lesinskifb48d292015-11-07 15:52:13 -0800677class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700678 public:
679 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700680 : options_(options),
681 context_(context),
682 final_table_(),
683 file_collection_(util::make_unique<io::FileCollection>()) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700684
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 /**
686 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700687 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 bool LoadSymbolsFromIncludePaths() {
690 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700692 for (const std::string& path : options_.include_paths) {
693 if (context_->IsVerbose()) {
694 context_->GetDiagnostics()->Note(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 << "loading include path");
696 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700697
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700698 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700699 std::string error_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800700 std::unique_ptr<ResourceTable> include_static = LoadStaticLibrary(path, &error_str);
701 if (include_static) {
702 if (options_.package_type != PackageType::kStaticLib) {
703 // Can't include static libraries when not building a static library (they have no IDs
704 // assigned).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 context_->GetDiagnostics()->Error(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800706 DiagMessage(path) << "can't include static library when not building a static lib");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 return false;
708 }
709
710 // If we are using --no-static-lib-packages, we need to rename the
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800711 // package of this table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 if (options_.no_static_lib_packages) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800713 // Since package names can differ, and multiple packages can exist in a ResourceTable,
714 // we place the requirement that all static libraries are built with the package
715 // ID 0x7f. So if one is not found, this is an error.
716 if (ResourceTablePackage* pkg = include_static->FindPackageById(kAppPackageId)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 pkg->name = context_->GetCompilationPackage();
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800718 } else {
719 context_->GetDiagnostics()->Error(DiagMessage(path)
720 << "no package with ID 0x7f found in static library");
721 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 }
723 }
724
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700725 context_->GetExternalSymbols()->AppendSource(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800726 util::make_unique<ResourceTableSymbolSource>(include_static.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800728 static_table_includes_.push_back(std::move(include_static));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700732 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 return false;
734 }
735
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736 if (!asset_source->AddAssetPath(path)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800737 context_->GetDiagnostics()->Error(DiagMessage(path) << "failed to load include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 return false;
739 }
740 }
741
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800742 // Capture the shared libraries so that the final resource table can be properly flattened
743 // with support for shared libraries.
744 for (auto& entry : asset_source->GetAssignedPackageIds()) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800745 if (entry.first > kFrameworkPackageId && entry.first < kAppPackageId) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800746 final_table_.included_packages_[entry.first] = entry.second;
747 }
748 }
749
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 return true;
752 }
753
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800754 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800756 xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
757 if (manifest_el == nullptr) {
758 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700759 }
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800760
761 AppInfo app_info;
762
763 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
764 diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>");
765 return {};
766 }
767
768 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
769 if (!package_attr) {
770 diag->Error(DiagMessage(xml_res->file.source)
771 << "<manifest> must have a 'package' attribute");
772 return {};
773 }
774 app_info.package = package_attr->value;
775
776 if (xml::Attribute* version_code_attr =
777 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
778 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_attr->value);
779 if (!maybe_code) {
780 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
781 << "invalid android:versionCode '" << version_code_attr->value << "'");
782 return {};
783 }
784 app_info.version_code = maybe_code.value();
785 }
786
787 if (xml::Attribute* revision_code_attr =
788 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
789 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(revision_code_attr->value);
790 if (!maybe_code) {
791 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
792 << "invalid android:revisionCode '" << revision_code_attr->value << "'");
793 return {};
794 }
795 app_info.revision_code = maybe_code.value();
796 }
797
798 if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
799 if (!split_name_attr->value.empty()) {
800 app_info.split_name = split_name_attr->value;
801 }
802 }
803
804 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
805 if (xml::Attribute* min_sdk =
806 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
807 app_info.min_sdk_version = min_sdk->value;
808 }
809 }
810 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811 }
812
813 /**
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800814 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700815 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700816 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700818 bool VerifyNoExternalPackages() {
819 auto is_ext_package_func =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700820 [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700821 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
822 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700823 };
824
825 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700826 for (const auto& package : final_table_.packages) {
827 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700828 // We have a package that is not related to the one we're building!
829 for (const auto& type : package->types) {
830 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700831 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700832
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700833 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700834 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700835 // for the 'android' package. This is due to legacy reasons.
836 if (ValueCast<Id>(config_value->value.get()) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837 package->name == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700838 context_->GetDiagnostics()->Warn(
839 DiagMessage(config_value->value->GetSource())
840 << "generated id '" << res_name
841 << "' for external package '" << package->name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700842 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700843 context_->GetDiagnostics()->Error(
844 DiagMessage(config_value->value->GetSource())
845 << "defined resource '" << res_name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700846 << "' for external package '" << package->name << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700847 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700849 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700850 }
851 }
852 }
853 }
854
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700855 auto new_end_iter =
856 std::remove_if(final_table_.packages.begin(),
857 final_table_.packages.end(), is_ext_package_func);
858 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 return !error;
860 }
861
862 /**
863 * Returns true if no IDs have been set, false otherwise.
864 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700865 bool VerifyNoIdsSet() {
866 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700867 for (const auto& type : package->types) {
868 if (type->id) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800869 context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID "
870 << StringPrintf("%02x", type->id.value())
871 << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700872 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700873 }
874
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875 for (const auto& entry : type->entries) {
876 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700877 ResourceNameRef res_name(package->name, type->type, entry->name);
878 context_->GetDiagnostics()->Error(
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800879 DiagMessage() << "entry " << res_name << " has ID "
880 << StringPrintf("%02x", entry->id.value()) << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700881 return false;
882 }
883 }
884 }
885 }
886 return true;
887 }
888
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700889 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
890 if (options_.output_to_directory) {
891 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700892 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700893 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700894 }
895 }
896
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700897 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700898 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800899 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 if (!flattener.Consume(context_, table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 return false;
902 }
903
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700904 if (writer->StartEntry("resources.arsc", ArchiveEntry::kAlign)) {
905 if (writer->WriteEntry(buffer)) {
906 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 return true;
908 }
909 }
910 }
911
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700912 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700913 DiagMessage() << "failed to write resources.arsc to archive");
914 return false;
915 }
916
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700917 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700918 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700919 if (!writer->StartEntry("resources.arsc.flat", 0)) {
920 context_->GetDiagnostics()->Error(DiagMessage() << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700921 return false;
922 }
923
924 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700925 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700926 {
927 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700928 // ZeroCopyOutputStream interface.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700929 CopyingOutputStreamAdaptor adaptor(writer);
930
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700931 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
932 if (!pb_table->SerializeToZeroCopyStream(&adaptor)) {
933 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700934 return false;
935 }
936 }
937
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700938 if (!writer->FinishEntry()) {
939 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700940 << "failed to finish entry");
941 return false;
942 }
943 return true;
944 }
945
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700946 bool WriteJavaFile(ResourceTable* table,
947 const StringPiece& package_name_to_generate,
948 const StringPiece& out_package,
949 const JavaClassGeneratorOptions& java_options) {
950 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700951 return true;
952 }
953
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700954 std::string out_path = options_.generate_java_class_path.value();
955 file::AppendPath(&out_path, file::PackageToPath(out_package));
956 if (!file::mkdirs(out_path)) {
957 context_->GetDiagnostics()->Error(
958 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700959 return false;
960 }
961
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700962 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700963
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700964 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700965 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700966 context_->GetDiagnostics()->Error(
967 DiagMessage() << "failed writing to '" << out_path << "': "
968 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700969 return false;
970 }
971
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700972 JavaClassGenerator generator(context_, table, java_options);
973 if (!generator.Generate(package_name_to_generate, out_package, &fout)) {
974 context_->GetDiagnostics()->Error(DiagMessage(out_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700975 << generator.getError());
976 return false;
977 }
978
979 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700980 context_->GetDiagnostics()->Error(
981 DiagMessage() << "failed writing to '" << out_path << "': "
982 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700983 }
984 return true;
985 }
986
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700987 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
988 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700989 return true;
990 }
991
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700992 std::unique_ptr<ClassDefinition> manifest_class =
993 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700994
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700995 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700996 // Something bad happened, but we already logged it, so exit.
997 return false;
998 }
999
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001000 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001001 // Empty Manifest class, no need to generate it.
1002 return true;
1003 }
1004
1005 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001006 for (const std::string& annotation : options_.javadoc_annotations) {
1007 std::string proper_annotation = "@";
1008 proper_annotation += annotation;
1009 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010 }
1011
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001012 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001013
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001014 std::string out_path = options_.generate_java_class_path.value();
1015 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001016
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017 if (!file::mkdirs(out_path)) {
1018 context_->GetDiagnostics()->Error(
1019 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001020 return false;
1021 }
1022
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001023 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001024
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001025 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001026 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 context_->GetDiagnostics()->Error(
1028 DiagMessage() << "failed writing to '" << out_path << "': "
1029 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001030 return false;
1031 }
1032
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001033 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8,
1034 true, &fout)) {
1035 context_->GetDiagnostics()->Error(
1036 DiagMessage() << "failed writing to '" << out_path << "': "
1037 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001038 return false;
1039 }
1040 return true;
1041 }
1042
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001043 bool WriteProguardFile(const Maybe<std::string>& out,
1044 const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001045 if (!out) {
1046 return true;
1047 }
1048
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001049 const std::string& out_path = out.value();
1050 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001051 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001052 context_->GetDiagnostics()->Error(
1053 DiagMessage() << "failed to open '" << out_path << "': "
1054 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001055 return false;
1056 }
1057
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001058 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001059 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001060 context_->GetDiagnostics()->Error(
1061 DiagMessage() << "failed writing to '" << out_path << "': "
1062 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001063 return false;
1064 }
1065 return true;
1066 }
1067
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001068 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1069 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001070 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001071 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001072 if (!collection) {
1073 return {};
1074 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001075 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001076 }
1077
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001078 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001079 io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001080 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001081 if (!file) {
1082 return {};
1083 }
1084
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001085 std::unique_ptr<io::IData> data = file->OpenAsData();
1086 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1087 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001088 }
1089
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001090 bool MergeStaticLibrary(const std::string& input, bool override) {
1091 if (context_->IsVerbose()) {
1092 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001093 << "merging static library " << input);
1094 }
1095
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001096 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001097 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001098 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001099 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001100 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001101 return false;
1102 }
1103
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001104 std::unique_ptr<ResourceTable> table = LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001105 if (!table) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001106 context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001107 return false;
1108 }
1109
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001110 ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001111 if (!pkg) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001112 context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001113 return false;
1114 }
1115
1116 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001117 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001118 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001119 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001120
1121 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001122 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001123 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001124 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001125 }
1126
1127 pkg->name = "";
1128 if (override) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001129 result = table_merger_->MergeOverlay(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001130 } else {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001131 result = table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001132 }
1133
1134 } else {
1135 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001136 // preserved and resource names are mangled.
1137 result = table_merger_->MergeAndMangle(Source(input), pkg->name,
1138 table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001139 }
1140
1141 if (!result) {
1142 return false;
1143 }
1144
1145 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001146 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001147 return true;
1148 }
1149
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001150 bool MergeResourceTable(io::IFile* file, bool override) {
1151 if (context_->IsVerbose()) {
1152 context_->GetDiagnostics()->Note(
1153 DiagMessage() << "merging resource table " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001154 }
1155
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001156 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001157 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001158 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001159 << "failed to open file");
1160 return false;
1161 }
1162
1163 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001164 LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1165 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001166 if (!table) {
1167 return false;
1168 }
1169
1170 bool result = false;
1171 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001172 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001173 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001174 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001175 }
1176 return result;
1177 }
1178
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001179 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001180 bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001181 if (context_->IsVerbose()) {
1182 context_->GetDiagnostics()->Note(
1183 DiagMessage() << "merging '" << file_desc->name
1184 << "' from compiled file " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001185 }
1186
1187 bool result = false;
1188 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001189 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001190 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001191 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001192 }
1193
1194 if (!result) {
1195 return false;
1196 }
1197
1198 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001199 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1200 if (exported_symbol.name.package.empty()) {
1201 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001202 }
1203
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001204 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001205
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001206 Maybe<ResourceName> mangled_name =
1207 context_->GetNameMangler()->MangleName(exported_symbol.name);
1208 if (mangled_name) {
1209 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001210 }
1211
1212 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001213 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1214 bool result = final_table_.AddResourceAllowMangled(
1215 res_name, ConfigDescription::DefaultConfig(), std::string(),
1216 std::move(id), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001217 if (!result) {
1218 return false;
1219 }
1220 }
1221 return true;
1222 }
1223
1224 /**
1225 * Takes a path to load as a ZIP file and merges the files within into the
1226 * master ResourceTable.
1227 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001228 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001229 *
1230 * An io::IFileCollection is created from the ZIP file and added to the set of
1231 * io::IFileCollections that are open.
1232 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001233 bool MergeArchive(const std::string& input, bool override) {
1234 if (context_->IsVerbose()) {
1235 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001236 << input);
1237 }
1238
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001239 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001240 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001241 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001242 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001243 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001244 return false;
1245 }
1246
1247 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001248 for (auto iter = collection->Iterator(); iter->HasNext();) {
1249 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001250 error = true;
1251 }
1252 }
1253
1254 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001255 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001256 return !error;
1257 }
1258
1259 /**
1260 * Takes a path to load and merge into the master ResourceTable. If override
1261 * is true,
1262 * conflicting resources are allowed to override each other, in order of last
1263 * seen.
1264 *
1265 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1266 * as ZIP archive
1267 * and the files within are merged individually.
1268 *
1269 * Otherwise the files is processed on its own.
1270 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001271 bool MergePath(const std::string& path, bool override) {
1272 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1273 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1274 return MergeArchive(path, override);
1275 } else if (util::EndsWith(path, ".apk")) {
1276 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001277 }
1278
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001279 io::IFile* file = file_collection_->InsertFile(path);
1280 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001281 }
1282
1283 /**
1284 * Takes a file to load and merge into the master ResourceTable. If override
1285 * is true,
1286 * conflicting resources are allowed to override each other, in order of last
1287 * seen.
1288 *
1289 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1290 * merged into the
1291 * master ResourceTable. If the file ends with .flat, then it is treated like
1292 * a compiled file
1293 * and the header data is read and merged into the final ResourceTable.
1294 *
1295 * All other file types are ignored. This is because these files could be
1296 * coming from a zip,
1297 * where we could have other files like classes.dex.
1298 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001299 bool MergeFile(io::IFile* file, bool override) {
1300 const Source& src = file->GetSource();
1301 if (util::EndsWith(src.path, ".arsc.flat")) {
1302 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001303
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001304 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001305 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001306 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001307 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309 return false;
1310 }
1311
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001312 CompiledFileInputStream input_stream(data->data(), data->size());
1313 uint32_t num_files = 0;
1314 if (!input_stream.ReadLittleEndian32(&num_files)) {
1315 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001316 << "failed read num files");
1317 return false;
1318 }
1319
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001320 for (uint32_t i = 0; i < num_files; i++) {
1321 pb::CompiledFile compiled_file;
1322 if (!input_stream.ReadCompiledFile(&compiled_file)) {
1323 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001324 DiagMessage(src) << "failed to read compiled file header");
1325 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001326 }
1327
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001328 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001329 if (!input_stream.ReadDataMetaData(&offset, &len)) {
1330 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001331 << "failed to read data meta data");
1332 return false;
1333 }
1334
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001335 std::unique_ptr<ResourceFile> resource_file =
1336 DeserializeCompiledFileFromPb(compiled_file, file->GetSource(),
1337 context_->GetDiagnostics());
1338 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001339 return false;
1340 }
1341
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001342 if (!MergeCompiledFile(file->CreateFileSegment(offset, len),
1343 resource_file.get(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001344 return false;
1345 }
1346 }
1347 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001348 } else if (util::EndsWith(src.path, ".xml") ||
1349 util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001350 // Since AAPT compiles these file types and appends .flat to them, seeing
1351 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001352 const StringPiece file_type =
1353 util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1354 context_->GetDiagnostics()->Error(DiagMessage(src)
1355 << "uncompiled " << file_type
Adam Lesinski6a396c12016-10-20 14:38:23 -07001356 << " file passed as argument. Must be "
1357 "compiled first into .flat file.");
1358 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001359 }
1360
1361 // Ignore non .flat files. This could be classes.dex or something else that
1362 // happens
1363 // to be in an archive.
1364 return true;
1365 }
1366
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001367 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
1368 const SplitConstraints& constraints) {
1369 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001370
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001371 std::unique_ptr<xml::Namespace> namespace_android = util::make_unique<xml::Namespace>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001372 namespace_android->namespace_uri = xml::kSchemaAndroid;
1373 namespace_android->namespace_prefix = "android";
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001374
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001375 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001376 manifest_el->name = "manifest";
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001377 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001379 if (app_info.version_code) {
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001380 manifest_el->attributes.push_back(xml::Attribute{
1381 xml::kSchemaAndroid, "versionCode", std::to_string(app_info.version_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001382 }
1383
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001384 if (app_info.revision_code) {
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001385 manifest_el->attributes.push_back(xml::Attribute{
1386 xml::kSchemaAndroid, "revisionCode", std::to_string(app_info.revision_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001387 }
1388
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001389 std::stringstream split_name;
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001390 if (app_info.split_name) {
1391 split_name << app_info.split_name.value() << ".";
1392 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001393 split_name << "config." << util::Joiner(constraints.configs, "_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001394
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001395 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001396
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001397 if (app_info.split_name) {
1398 manifest_el->attributes.push_back(
1399 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
1400 }
1401
1402 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001403 application_el->name = "application";
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001404 application_el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "hasCode", "false"});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001405
Adam Lesinskie343eb12016-10-27 16:31:58 -07001406 manifest_el->AppendChild(std::move(application_el));
1407 namespace_android->AppendChild(std::move(manifest_el));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001408 doc->root = std::move(namespace_android);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001409 return doc;
1410 }
1411
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001412 bool CopyAssetsDirsToApk(IArchiveWriter* writer) {
1413 std::map<std::string, std::unique_ptr<io::RegularFile>> merged_assets;
1414 for (const std::string& assets_dir : options_.assets_dirs) {
1415 Maybe<std::vector<std::string>> files =
1416 file::FindFiles(assets_dir, context_->GetDiagnostics(), nullptr);
1417 if (!files) {
1418 return false;
1419 }
1420
1421 for (const std::string& file : files.value()) {
1422 std::string full_key = "assets/" + file;
1423 std::string full_path = assets_dir;
1424 file::AppendPath(&full_path, file);
1425
1426 auto iter = merged_assets.find(full_key);
1427 if (iter == merged_assets.end()) {
1428 merged_assets.emplace(std::move(full_key),
1429 util::make_unique<io::RegularFile>(Source(std::move(full_path))));
1430 } else if (context_->IsVerbose()) {
1431 context_->GetDiagnostics()->Warn(DiagMessage(iter->second->GetSource())
1432 << "asset file overrides '" << full_path << "'");
1433 }
1434 }
1435 }
1436
1437 for (auto& entry : merged_assets) {
1438 uint32_t compression_flags = ArchiveEntry::kCompress;
1439 std::string extension = file::GetExtension(entry.first).to_string();
1440 if (options_.extensions_to_not_compress.count(extension) > 0) {
1441 compression_flags = 0u;
1442 }
1443
1444 if (!CopyFileToArchive(entry.second.get(), entry.first, compression_flags, writer,
1445 context_)) {
1446 return false;
1447 }
1448 }
1449 return true;
1450 }
1451
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001452 /**
1453 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001454 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001455 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001456 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001457 xml::XmlResource* manifest, ResourceTable* table) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001458 const bool keep_raw_values = options_.package_type == PackageType::kStaticLib;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001459 bool result = FlattenXml(manifest, "AndroidManifest.xml", {},
1460 keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001461 if (!result) {
1462 return false;
1463 }
1464
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001465 ResourceFileFlattenerOptions file_flattener_options;
1466 file_flattener_options.keep_raw_values = keep_raw_values;
1467 file_flattener_options.do_not_compress_anything =
1468 options_.do_not_compress_anything;
1469 file_flattener_options.extensions_to_not_compress =
1470 options_.extensions_to_not_compress;
1471 file_flattener_options.no_auto_version = options_.no_auto_version;
1472 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001473 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001474 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1475 file_flattener_options.update_proguard_spec =
1476 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001477
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001478 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001479
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001480 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001481 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001482 return false;
1483 }
1484
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001485 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001486 if (!FlattenTableToPb(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001487 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001488 return false;
1489 }
1490 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001491 if (!FlattenTable(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001492 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001493 return false;
1494 }
1495 }
1496 return true;
1497 }
1498
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001499 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001500 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001501 std::unique_ptr<xml::XmlResource> manifest_xml =
1502 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1503 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001504 return 1;
1505 }
1506
1507 // First extract the Package name without modifying it (via
1508 // --rename-manifest-package).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001509 if (Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1510 manifest_xml.get(), context_->GetDiagnostics())) {
1511 const AppInfo& app_info = maybe_app_info.value();
1512 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001513 }
1514
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001515 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1516 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001517 return 1;
1518 }
1519
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001520 Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1521 manifest_xml.get(), context_->GetDiagnostics());
1522 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001523 return 1;
1524 }
1525
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001526 const AppInfo& app_info = maybe_app_info.value();
1527 if (app_info.min_sdk_version) {
1528 if (Maybe<int> maybe_min_sdk_version = ResourceUtils::ParseSdkVersion(
1529 app_info.min_sdk_version.value())) {
1530 context_->SetMinSdkVersion(maybe_min_sdk_version.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001531 }
1532 }
1533
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001534 context_->SetNameManglerPolicy(
1535 NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001536
1537 // Override the package ID when it is "android".
1538 if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001539 context_->SetPackageId(0x01);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001540
1541 // Verify we're building a regular app.
1542 if (options_.package_type != PackageType::kApp) {
1543 context_->GetDiagnostics()->Error(
1544 DiagMessage() << "package 'android' can only be built as a regular app");
1545 return 1;
1546 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001547 }
1548
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001549 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001550 return 1;
1551 }
1552
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001553 TableMergerOptions table_merger_options;
1554 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
1555 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_,
1556 table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001557
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001558 if (context_->IsVerbose()) {
1559 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001560 << StringPrintf("linking package '%s' using package ID %02x",
1561 context_->GetCompilationPackage().data(),
1562 context_->GetPackageId()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001563 }
1564
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001565 for (const std::string& input : input_files) {
1566 if (!MergePath(input, false)) {
1567 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001568 << "failed parsing input");
1569 return 1;
1570 }
1571 }
1572
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001573 for (const std::string& input : options_.overlay_files) {
1574 if (!MergePath(input, true)) {
1575 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001576 << "failed parsing overlays");
1577 return 1;
1578 }
1579 }
1580
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001581 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001582 return 1;
1583 }
1584
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001585 if (options_.package_type != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001586 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001587 if (!mover.Consume(context_, &final_table_)) {
1588 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001589 DiagMessage() << "failed moving private attributes");
1590 return 1;
1591 }
1592
1593 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001594 IdAssigner id_assigner(&options_.stable_id_map);
1595 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001596 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001597 return 1;
1598 }
1599
1600 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001601 if (options_.resource_id_map_path) {
1602 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001603 for (auto& type : package->types) {
1604 for (auto& entry : type->entries) {
1605 ResourceName name(package->name, type->type, entry->name);
1606 // The IDs are guaranteed to exist.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001607 options_.stable_id_map[std::move(name)] = ResourceId(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001608 package->id.value(), type->id.value(), entry->id.value());
1609 }
1610 }
1611 }
1612
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001613 if (!WriteStableIdMapToPath(context_->GetDiagnostics(),
1614 options_.stable_id_map,
1615 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001616 return 1;
1617 }
1618 }
1619 } else {
1620 // Static libs are merged with other apps, and ID collisions are bad, so
1621 // verify that
1622 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001623 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001624 return 1;
1625 }
1626 }
1627
1628 // Add the names to mangle based on our source merge earlier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001629 context_->SetNameManglerPolicy(NameManglerPolicy{
1630 context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001631
1632 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001633 context_->GetExternalSymbols()->PrependSource(
1634 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001635
1636 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001637 if (!linker.Consume(context_, &final_table_)) {
1638 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001639 << "failed linking references");
1640 return 1;
1641 }
1642
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001643 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001644 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001645 context_->GetDiagnostics()->Warn(DiagMessage()
1646 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001647 }
1648 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001649 ProductFilter product_filter(options_.products);
1650 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001651 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001652 return 1;
1653 }
1654 }
1655
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001656 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001657 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001658 if (!versioner.Consume(context_, &final_table_)) {
1659 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001660 << "failed versioning styles");
1661 return 1;
1662 }
1663 }
1664
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001665 if (options_.package_type != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001666 if (context_->IsVerbose()) {
1667 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001668 DiagMessage() << "collapsing resource versions for minimum SDK "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001669 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001670 }
1671
1672 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001673 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001674 return 1;
1675 }
1676 }
1677
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001678 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001679 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001680 if (!deduper.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001681 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001682 return 1;
1683 }
1684 }
1685
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001686 proguard::KeepSet proguard_keep_set;
1687 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001688
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001689 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001690 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001691 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001692 context_->GetDiagnostics()->Warn(DiagMessage()
1693 << "can't strip resources when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001694 }
1695 } else {
1696 // Adjust the SplitConstraints so that their SDK version is stripped if it
1697 // is less
1698 // than or equal to the minSdk. Otherwise the resources that have had
1699 // their SDK version
1700 // stripped due to minSdk won't ever match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001701 std::vector<SplitConstraints> adjusted_constraints_list;
1702 adjusted_constraints_list.reserve(options_.split_constraints.size());
1703 for (const SplitConstraints& constraints : options_.split_constraints) {
1704 SplitConstraints adjusted_constraints;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001705 for (const ConfigDescription& config : constraints.configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001706 if (config.sdkVersion <= context_->GetMinSdkVersion()) {
1707 adjusted_constraints.configs.insert(config.CopyWithoutSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001708 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001709 adjusted_constraints.configs.insert(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001710 }
1711 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001712 adjusted_constraints_list.push_back(std::move(adjusted_constraints));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001713 }
1714
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001715 TableSplitter table_splitter(adjusted_constraints_list,
1716 options_.table_splitter_options);
1717 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001718 return 1;
1719 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001720 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001721
1722 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001723 auto path_iter = options_.split_paths.begin();
1724 auto split_constraints_iter = adjusted_constraints_list.begin();
1725 for (std::unique_ptr<ResourceTable>& split_table :
1726 table_splitter.splits()) {
1727 if (context_->IsVerbose()) {
1728 context_->GetDiagnostics()->Note(
1729 DiagMessage(*path_iter)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001730 << "generating split with configurations '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001731 << util::Joiner(split_constraints_iter->configs, ", ") << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001732 }
1733
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001734 std::unique_ptr<IArchiveWriter> archive_writer =
1735 MakeArchiveWriter(*path_iter);
1736 if (!archive_writer) {
1737 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001738 << "failed to create archive");
1739 return 1;
1740 }
1741
1742 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001743 std::unique_ptr<xml::XmlResource> split_manifest =
1744 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001745
1746 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001747 if (!linker.Consume(context_, split_manifest.get())) {
1748 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001749 DiagMessage() << "failed to create Split AndroidManifest.xml");
1750 return 1;
1751 }
1752
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001753 if (!WriteApk(archive_writer.get(), &proguard_keep_set,
1754 split_manifest.get(), split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001755 return 1;
1756 }
1757
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001758 ++path_iter;
1759 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001760 }
1761 }
1762
1763 // Start writing the base APK.
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001764 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(options_.output_path);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001765 if (!archive_writer) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001766 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001767 return 1;
1768 }
1769
1770 bool error = false;
1771 {
1772 // AndroidManifest.xml has no resource name, but the CallSite is built
1773 // from the name
1774 // (aka, which package the AndroidManifest.xml is coming from).
1775 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001776 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001777
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001778 XmlReferenceLinker manifest_linker;
1779 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1780 if (options_.generate_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001781 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1782 manifest_xml.get(), &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001783 error = true;
1784 }
1785
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001786 if (options_.generate_main_dex_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001787 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1788 manifest_xml.get(),
1789 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001790 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001791 }
1792
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001793 if (options_.generate_java_class_path) {
1794 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001795 error = true;
1796 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001797 }
1798
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001799 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001800 // PackageParser will fail if URIs are removed from
1801 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001802 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1803 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001804 error = true;
1805 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001806 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001807 } else {
1808 error = true;
1809 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001810 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001811
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001812 if (error) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001813 context_->GetDiagnostics()->Error(DiagMessage() << "failed processing manifest");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001814 return 1;
1815 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001816
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001817 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(), &final_table_)) {
1818 return 1;
1819 }
1820
1821 if (!CopyAssetsDirsToApk(archive_writer.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001822 return 1;
1823 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001824
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001825 if (options_.generate_java_class_path) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001826 // The set of packages whose R class to call in the main classes
1827 // onResourcesLoaded callback.
1828 std::vector<std::string> packages_to_callback;
1829
1830 JavaClassGeneratorOptions template_options;
1831 template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1832 template_options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001833
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001834 if (options_.package_type == PackageType::kStaticLib || options_.generate_non_final_ids) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001835 template_options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001836 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001837
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001838 if (options_.package_type == PackageType::kSharedLib) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001839 template_options.use_final = false;
1840 template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{};
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001841 }
1842
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001843 const StringPiece actual_package = context_->GetCompilationPackage();
1844 StringPiece output_package = context_->GetCompilationPackage();
1845 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001846 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001847 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001848 }
1849
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001850 // Generate the private symbols if required.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001851 if (options_.private_symbols) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001852 packages_to_callback.push_back(options_.private_symbols.value());
1853
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001854 // If we defined a private symbols package, we only emit Public symbols
1855 // to the original package, and private and public symbols to the
1856 // private package.
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001857 JavaClassGeneratorOptions options = template_options;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001858 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001859 if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001860 options)) {
1861 return 1;
1862 }
1863 }
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001864
1865 // Generate all the symbols for all extra packages.
1866 for (const std::string& extra_package : options_.extra_java_packages) {
1867 packages_to_callback.push_back(extra_package);
1868
1869 JavaClassGeneratorOptions options = template_options;
1870 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1871 if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) {
1872 return 1;
1873 }
1874 }
1875
1876 // Generate the main public R class.
1877 JavaClassGeneratorOptions options = template_options;
1878
1879 // Only generate public symbols if we have a private package.
1880 if (options_.private_symbols) {
1881 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
1882 }
1883
1884 if (options.rewrite_callback_options) {
1885 options.rewrite_callback_options.value().packages_to_callback =
1886 std::move(packages_to_callback);
1887 }
1888
1889 if (!WriteJavaFile(&final_table_, actual_package, output_package, options)) {
1890 return 1;
1891 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001892 }
1893
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001894 if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001895 return 1;
1896 }
1897
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001898 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1899 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001900 return 1;
1901 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001902 return 0;
1903 }
1904
1905 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001906 LinkOptions options_;
1907 LinkContext* context_;
1908 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001909
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001910 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001911
1912 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001913 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001914
1915 // A vector of IFileCollections. This is mainly here to keep ownership of the
1916 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001917 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001918
1919 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001920 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001921 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001922
1923 // The set of shared libraries being used, mapping their assigned package ID to package name.
1924 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001925};
1926
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001927int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001928 LinkContext context;
1929 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001930 std::vector<std::string> overlay_arg_list;
1931 std::vector<std::string> extra_java_packages;
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001932 Maybe<std::string> package_id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001933 Maybe<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001934 Maybe<std::string> preferred_density;
1935 Maybe<std::string> product_list;
1936 bool legacy_x_flag = false;
1937 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001938 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001939 bool shared_lib = false;
1940 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001941 Maybe<std::string> stable_id_file_path;
1942 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001943 Flags flags =
1944 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001945 .RequiredFlag("-o", "Output path", &options.output_path)
1946 .RequiredFlag("--manifest", "Path to the Android manifest to build",
1947 &options.manifest_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001948 .OptionalFlagList("-I", "Adds an Android APK to link against", &options.include_paths)
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001949 .OptionalFlagList("-A",
1950 "An assets directory to include in the APK. These are unprocessed.",
1951 &options.assets_dirs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001952 .OptionalFlagList("-R",
1953 "Compilation unit to link, using `overlay` semantics.\n"
1954 "The last conflicting resource given takes precedence.",
1955 &overlay_arg_list)
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001956 .OptionalFlag("--package-id",
1957 "Specify the package ID to use for this app. Must be greater or equal to\n"
1958 "0x7f and can't be used with --static-lib or --shared-lib.",
1959 &package_id)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001960 .OptionalFlag("--java", "Directory in which to generate R.java",
1961 &options.generate_java_class_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001962 .OptionalFlag("--proguard", "Output file for generated Proguard rules",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001963 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001964 .OptionalFlag("--proguard-main-dex",
1965 "Output file for generated Proguard rules for the main dex",
1966 &options.generate_main_dex_proguard_rules_path)
1967 .OptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001968 &options.no_auto_version)
1969 .OptionalSwitch("--no-version-vectors",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001970 "Disables automatic versioning of vector drawables. "
1971 "Use this only\n"
1972 "when building with vector drawable support library",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001973 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001974 .OptionalSwitch("--no-version-transitions",
1975 "Disables automatic versioning of transition resources. "
1976 "Use this only\n"
1977 "when building with transition support library",
1978 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001979 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001980 "Disables automatic deduping of resources with\n"
1981 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001982 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001983 .OptionalSwitch("--enable-sparse-encoding",
1984 "Enables encoding sparse entries using a binary search tree.\n"
1985 "This decreases APK size at the cost of resource retrieval performance.",
1986 &options.table_flattener_options.use_sparse_entries)
1987 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01",
1988 &legacy_x_flag)
1989 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001990 &require_localization)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001991 .OptionalFlag("-c",
1992 "Comma separated list of configurations to include. The default\n"
1993 "is all configurations",
1994 &configs)
1995 .OptionalFlag("--preferred-density",
1996 "Selects the closest matching density and strips out all others.",
1997 &preferred_density)
1998 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001999 .OptionalSwitch("--output-to-dir",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002000 "Outputs the APK contents to a directory specified "
2001 "by -o",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002002 &options.output_to_directory)
2003 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002004 "Removes XML namespace prefix and URI "
2005 "information from AndroidManifest.xml\nand XML "
2006 "binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002007 &options.no_xml_namespaces)
2008 .OptionalFlag("--min-sdk-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002009 "Default minimum SDK version to use for "
2010 "AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002011 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002012 .OptionalFlag("--target-sdk-version",
2013 "Default target SDK version to use for "
2014 "AndroidManifest.xml",
2015 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002016 .OptionalFlag("--version-code",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002017 "Version code (integer) to inject into the "
2018 "AndroidManifest.xml if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002019 &options.manifest_fixer_options.version_code_default)
2020 .OptionalFlag("--version-name",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002021 "Version name to inject into the AndroidManifest.xml "
2022 "if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002023 &options.manifest_fixer_options.version_name_default)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002024 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library", &shared_lib)
2025 .OptionalSwitch("--static-lib", "Generate a static Android library", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002026 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002027 "Merge all library resources under the app's package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002028 &options.no_static_lib_packages)
2029 .OptionalSwitch("--non-final-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002030 "Generates R.java without the final modifier.\n"
2031 "This is implied when --static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002032 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002033 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002034 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002035 .OptionalFlag("--emit-ids",
2036 "Emit a file at the given path with a list of name to ID\n"
2037 "mappings, suitable for use with --stable-ids.",
2038 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002039 .OptionalFlag("--private-symbols",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002040 "Package name to use when generating R.java for "
2041 "private symbols.\n"
2042 "If not specified, public and private symbols will use "
2043 "the application's "
2044 "package name",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002045 &options.private_symbols)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002046 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002047 &options.custom_java_package)
2048 .OptionalFlagList("--extra-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002049 "Generate the same R.java but with different "
2050 "package names",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002051 &extra_java_packages)
2052 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002053 "Adds a JavaDoc annotation to all "
Adam Lesinskid0f116b2016-07-08 15:00:32 -07002054 "generated Java classes",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002055 &options.javadoc_annotations)
2056 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002057 "Allows the addition of new resources in "
2058 "overlays without <add-resource> tags",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002059 &options.auto_add_overlay)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002060 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002061 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002062 .OptionalFlag("--rename-instrumentation-target-package",
2063 "Changes the name of the target package for instrumentation. "
2064 "Most useful "
2065 "when used\nin conjunction with --rename-manifest-package",
2066 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002067 .OptionalFlagList("-0", "File extensions not to compress",
2068 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002069 .OptionalFlagList("--split",
2070 "Split resources matching a set of configs out to a "
2071 "Split APK.\nSyntax: path/to/output.apk:<config>[,<config>[...]]",
2072 &split_args)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002073 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002074
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002075 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002076 return 1;
2077 }
2078
2079 // Expand all argument-files passed into the command line. These start with
2080 // '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002081 std::vector<std::string> arg_list;
2082 for (const std::string& arg : flags.GetArgs()) {
2083 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002084 const std::string path = arg.substr(1, arg.size() - 1);
2085 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002086 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
2087 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002088 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002089 }
2090 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002091 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002092 }
2093 }
2094
2095 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002096 for (const std::string& arg : overlay_arg_list) {
2097 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002098 const std::string path = arg.substr(1, arg.size() - 1);
2099 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002100 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
2101 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002102 return 1;
2103 }
2104 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002105 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002106 }
2107 }
2108
2109 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002110 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002111 }
2112
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002113 if (shared_lib && static_lib) {
2114 context.GetDiagnostics()->Error(DiagMessage()
2115 << "only one of --shared-lib and --static-lib can be defined");
2116 return 1;
2117 }
2118
2119 if (shared_lib) {
2120 options.package_type = PackageType::kSharedLib;
2121 context.SetPackageId(0x00);
2122 } else if (static_lib) {
2123 options.package_type = PackageType::kStaticLib;
2124 context.SetPackageId(kAppPackageId);
2125 } else {
2126 options.package_type = PackageType::kApp;
2127 context.SetPackageId(kAppPackageId);
2128 }
2129
2130 if (package_id) {
2131 if (options.package_type != PackageType::kApp) {
2132 context.GetDiagnostics()->Error(
2133 DiagMessage() << "can't specify --package-id when not building a regular app");
2134 return 1;
2135 }
2136
2137 const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id.value());
2138 if (!maybe_package_id_int) {
2139 context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id.value()
2140 << "' is not a valid integer");
2141 return 1;
2142 }
2143
2144 const uint32_t package_id_int = maybe_package_id_int.value();
2145 if (package_id_int < kAppPackageId || package_id_int > std::numeric_limits<uint8_t>::max()) {
2146 context.GetDiagnostics()->Error(
2147 DiagMessage() << StringPrintf(
2148 "invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int));
2149 return 1;
2150 }
2151 context.SetPackageId(static_cast<uint8_t>(package_id_int));
2152 }
2153
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002154 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002155 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002156 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002157 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002158 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002159 }
2160 }
2161
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002162 if (product_list) {
2163 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002164 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002165 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002166 }
2167 }
2168 }
2169
2170 AxisConfigFilter filter;
2171 if (configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002172 for (const StringPiece& config_str : util::Tokenize(configs.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002173 ConfigDescription config;
2174 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002175 if (lv.InitFromFilterString(config_str)) {
2176 lv.WriteTo(&config);
2177 } else if (!ConfigDescription::Parse(config_str, &config)) {
2178 context.GetDiagnostics()->Error(DiagMessage() << "invalid config '"
2179 << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002180 << "' for -c option");
2181 return 1;
2182 }
2183
2184 if (config.density != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002185 context.GetDiagnostics()->Warn(DiagMessage() << "ignoring density '"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002186 << config
2187 << "' for -c option");
2188 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002189 filter.AddConfig(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002190 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002191 }
2192
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002193 options.table_splitter_options.config_filter = &filter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002194 }
2195
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002196 if (preferred_density) {
2197 ConfigDescription preferred_density_config;
2198 if (!ConfigDescription::Parse(preferred_density.value(),
2199 &preferred_density_config)) {
2200 context.GetDiagnostics()->Error(
2201 DiagMessage() << "invalid density '" << preferred_density.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002202 << "' for --preferred-density option");
2203 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002204 }
2205
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002206 // Clear the version that can be automatically added.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002207 preferred_density_config.sdkVersion = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002208
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002209 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002210 ConfigDescription::CONFIG_DENSITY) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002211 context.GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002212 DiagMessage() << "invalid preferred density '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002213 << preferred_density.value() << "'. "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002214 << "Preferred density must only be a density value");
2215 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002216 }
Pierre Lecesne672384b2017-02-06 10:29:02 +00002217 options.table_splitter_options.preferred_densities.push_back(preferred_density_config.density);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002218 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002219
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002220 if (options.package_type != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002221 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2222 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002223 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002224 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002225 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002226
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002227 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002228 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002229 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2230 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2231 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2232 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2233
2234 // Parse the split parameters.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002235 for (const std::string& split_arg : split_args) {
2236 options.split_paths.push_back({});
2237 options.split_constraints.push_back({});
2238 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(),
2239 &options.split_paths.back(),
2240 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002241 return 1;
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002242 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002243 }
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002244
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002245 // Turn off auto versioning for static-libs.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002246 if (options.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002247 options.no_auto_version = true;
2248 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002249 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002250 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002251
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002252 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002253 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002254}
2255
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002256} // namespace aapt