blob: 10421115b62929abce0dbd3bcc021b437b10db9d [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 Lesinskice5e56e2016-10-21 17:56:45 -0700754 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 IDiagnostics* diag) {
756 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 if (xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get())) {
758 AppInfo app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700759
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760 if (!manifest_el->namespace_uri.empty() ||
761 manifest_el->name != "manifest") {
762 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700763 << "root tag must be <manifest>");
764 return {};
765 }
766
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700767 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
768 if (!package_attr) {
769 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700770 << "<manifest> must have a 'package' attribute");
771 return {};
772 }
773
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700774 app_info.package = package_attr->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776 if (xml::Attribute* version_code_attr =
777 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
778 Maybe<uint32_t> maybe_code =
779 ResourceUtils::ParseInt(version_code_attr->value);
780 if (!maybe_code) {
781 diag->Error(DiagMessage(xml_res->file.source.WithLine(
782 manifest_el->line_number))
783 << "invalid android:versionCode '"
784 << version_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700785 return {};
786 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700787 app_info.version_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700788 }
789
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700790 if (xml::Attribute* revision_code_attr =
791 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
792 Maybe<uint32_t> maybe_code =
793 ResourceUtils::ParseInt(revision_code_attr->value);
794 if (!maybe_code) {
795 diag->Error(DiagMessage(xml_res->file.source.WithLine(
796 manifest_el->line_number))
797 << "invalid android:revisionCode '"
798 << revision_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700799 return {};
800 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700801 app_info.revision_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700802 }
803
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700804 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
805 if (xml::Attribute* min_sdk = uses_sdk_el->FindAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806 xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700807 app_info.min_sdk_version = min_sdk->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700808 }
809 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700810 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811 }
812 return {};
813 }
814
815 /**
816 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it
817 * linked.
818 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700819 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700820 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700821 bool VerifyNoExternalPackages() {
822 auto is_ext_package_func =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700823 [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700824 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
825 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700826 };
827
828 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700829 for (const auto& package : final_table_.packages) {
830 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700831 // We have a package that is not related to the one we're building!
832 for (const auto& type : package->types) {
833 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700834 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700835
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700836 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700838 // for the 'android' package. This is due to legacy reasons.
839 if (ValueCast<Id>(config_value->value.get()) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840 package->name == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700841 context_->GetDiagnostics()->Warn(
842 DiagMessage(config_value->value->GetSource())
843 << "generated id '" << res_name
844 << "' for external package '" << package->name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700845 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700846 context_->GetDiagnostics()->Error(
847 DiagMessage(config_value->value->GetSource())
848 << "defined resource '" << res_name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700849 << "' for external package '" << package->name << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700850 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700851 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700852 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700853 }
854 }
855 }
856 }
857
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700858 auto new_end_iter =
859 std::remove_if(final_table_.packages.begin(),
860 final_table_.packages.end(), is_ext_package_func);
861 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700862 return !error;
863 }
864
865 /**
866 * Returns true if no IDs have been set, false otherwise.
867 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700868 bool VerifyNoIdsSet() {
869 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700870 for (const auto& type : package->types) {
871 if (type->id) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800872 context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID "
873 << StringPrintf("%02x", type->id.value())
874 << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700876 }
877
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 for (const auto& entry : type->entries) {
879 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700880 ResourceNameRef res_name(package->name, type->type, entry->name);
881 context_->GetDiagnostics()->Error(
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800882 DiagMessage() << "entry " << res_name << " has ID "
883 << StringPrintf("%02x", entry->id.value()) << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700884 return false;
885 }
886 }
887 }
888 }
889 return true;
890 }
891
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700892 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
893 if (options_.output_to_directory) {
894 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700895 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700896 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700897 }
898 }
899
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800902 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 if (!flattener.Consume(context_, table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700904 return false;
905 }
906
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700907 if (writer->StartEntry("resources.arsc", ArchiveEntry::kAlign)) {
908 if (writer->WriteEntry(buffer)) {
909 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700910 return true;
911 }
912 }
913 }
914
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700915 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700916 DiagMessage() << "failed to write resources.arsc to archive");
917 return false;
918 }
919
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700920 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700921 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700922 if (!writer->StartEntry("resources.arsc.flat", 0)) {
923 context_->GetDiagnostics()->Error(DiagMessage() << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700924 return false;
925 }
926
927 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700928 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700929 {
930 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700931 // ZeroCopyOutputStream interface.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700932 CopyingOutputStreamAdaptor adaptor(writer);
933
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700934 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
935 if (!pb_table->SerializeToZeroCopyStream(&adaptor)) {
936 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700937 return false;
938 }
939 }
940
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700941 if (!writer->FinishEntry()) {
942 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700943 << "failed to finish entry");
944 return false;
945 }
946 return true;
947 }
948
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700949 bool WriteJavaFile(ResourceTable* table,
950 const StringPiece& package_name_to_generate,
951 const StringPiece& out_package,
952 const JavaClassGeneratorOptions& java_options) {
953 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700954 return true;
955 }
956
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700957 std::string out_path = options_.generate_java_class_path.value();
958 file::AppendPath(&out_path, file::PackageToPath(out_package));
959 if (!file::mkdirs(out_path)) {
960 context_->GetDiagnostics()->Error(
961 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700962 return false;
963 }
964
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700965 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700966
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700967 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700968 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700969 context_->GetDiagnostics()->Error(
970 DiagMessage() << "failed writing to '" << out_path << "': "
971 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700972 return false;
973 }
974
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700975 JavaClassGenerator generator(context_, table, java_options);
976 if (!generator.Generate(package_name_to_generate, out_package, &fout)) {
977 context_->GetDiagnostics()->Error(DiagMessage(out_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700978 << generator.getError());
979 return false;
980 }
981
982 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700983 context_->GetDiagnostics()->Error(
984 DiagMessage() << "failed writing to '" << out_path << "': "
985 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700986 }
987 return true;
988 }
989
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700990 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
991 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700992 return true;
993 }
994
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700995 std::unique_ptr<ClassDefinition> manifest_class =
996 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700997
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700998 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700999 // Something bad happened, but we already logged it, so exit.
1000 return false;
1001 }
1002
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001003 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001004 // Empty Manifest class, no need to generate it.
1005 return true;
1006 }
1007
1008 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001009 for (const std::string& annotation : options_.javadoc_annotations) {
1010 std::string proper_annotation = "@";
1011 proper_annotation += annotation;
1012 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001013 }
1014
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001015 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001016
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017 std::string out_path = options_.generate_java_class_path.value();
1018 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001019
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001020 if (!file::mkdirs(out_path)) {
1021 context_->GetDiagnostics()->Error(
1022 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001023 return false;
1024 }
1025
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001026 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001027
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001028 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001029 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001030 context_->GetDiagnostics()->Error(
1031 DiagMessage() << "failed writing to '" << out_path << "': "
1032 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001033 return false;
1034 }
1035
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001036 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8,
1037 true, &fout)) {
1038 context_->GetDiagnostics()->Error(
1039 DiagMessage() << "failed writing to '" << out_path << "': "
1040 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001041 return false;
1042 }
1043 return true;
1044 }
1045
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001046 bool WriteProguardFile(const Maybe<std::string>& out,
1047 const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001048 if (!out) {
1049 return true;
1050 }
1051
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001052 const std::string& out_path = out.value();
1053 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001054 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001055 context_->GetDiagnostics()->Error(
1056 DiagMessage() << "failed to open '" << out_path << "': "
1057 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001058 return false;
1059 }
1060
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001061 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001062 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001063 context_->GetDiagnostics()->Error(
1064 DiagMessage() << "failed writing to '" << out_path << "': "
1065 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001066 return false;
1067 }
1068 return true;
1069 }
1070
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001071 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1072 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001073 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001074 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075 if (!collection) {
1076 return {};
1077 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001078 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001079 }
1080
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001081 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001082 io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001083 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001084 if (!file) {
1085 return {};
1086 }
1087
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001088 std::unique_ptr<io::IData> data = file->OpenAsData();
1089 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1090 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091 }
1092
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001093 bool MergeStaticLibrary(const std::string& input, bool override) {
1094 if (context_->IsVerbose()) {
1095 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001096 << "merging static library " << input);
1097 }
1098
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001099 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001100 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001101 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001102 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001103 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001104 return false;
1105 }
1106
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001107 std::unique_ptr<ResourceTable> table = LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001108 if (!table) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001109 context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001110 return false;
1111 }
1112
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001113 ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001114 if (!pkg) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001115 context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001116 return false;
1117 }
1118
1119 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001120 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001121 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001122 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001123
1124 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001125 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001126 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001127 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001128 }
1129
1130 pkg->name = "";
1131 if (override) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001132 result = table_merger_->MergeOverlay(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001133 } else {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001134 result = table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001135 }
1136
1137 } else {
1138 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001139 // preserved and resource names are mangled.
1140 result = table_merger_->MergeAndMangle(Source(input), pkg->name,
1141 table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001142 }
1143
1144 if (!result) {
1145 return false;
1146 }
1147
1148 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001149 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001150 return true;
1151 }
1152
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001153 bool MergeResourceTable(io::IFile* file, bool override) {
1154 if (context_->IsVerbose()) {
1155 context_->GetDiagnostics()->Note(
1156 DiagMessage() << "merging resource table " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001157 }
1158
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001159 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001160 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001161 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001162 << "failed to open file");
1163 return false;
1164 }
1165
1166 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001167 LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1168 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001169 if (!table) {
1170 return false;
1171 }
1172
1173 bool result = false;
1174 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001175 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001176 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001177 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001178 }
1179 return result;
1180 }
1181
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001182 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001183 bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001184 if (context_->IsVerbose()) {
1185 context_->GetDiagnostics()->Note(
1186 DiagMessage() << "merging '" << file_desc->name
1187 << "' from compiled file " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001188 }
1189
1190 bool result = false;
1191 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001192 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001193 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001194 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001195 }
1196
1197 if (!result) {
1198 return false;
1199 }
1200
1201 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001202 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1203 if (exported_symbol.name.package.empty()) {
1204 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001205 }
1206
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001207 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001208
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001209 Maybe<ResourceName> mangled_name =
1210 context_->GetNameMangler()->MangleName(exported_symbol.name);
1211 if (mangled_name) {
1212 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001213 }
1214
1215 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001216 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1217 bool result = final_table_.AddResourceAllowMangled(
1218 res_name, ConfigDescription::DefaultConfig(), std::string(),
1219 std::move(id), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001220 if (!result) {
1221 return false;
1222 }
1223 }
1224 return true;
1225 }
1226
1227 /**
1228 * Takes a path to load as a ZIP file and merges the files within into the
1229 * master ResourceTable.
1230 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001231 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001232 *
1233 * An io::IFileCollection is created from the ZIP file and added to the set of
1234 * io::IFileCollections that are open.
1235 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001236 bool MergeArchive(const std::string& input, bool override) {
1237 if (context_->IsVerbose()) {
1238 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001239 << input);
1240 }
1241
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001242 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001243 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001244 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001245 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001246 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001247 return false;
1248 }
1249
1250 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001251 for (auto iter = collection->Iterator(); iter->HasNext();) {
1252 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001253 error = true;
1254 }
1255 }
1256
1257 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001258 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001259 return !error;
1260 }
1261
1262 /**
1263 * Takes a path to load and merge into the master ResourceTable. If override
1264 * is true,
1265 * conflicting resources are allowed to override each other, in order of last
1266 * seen.
1267 *
1268 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1269 * as ZIP archive
1270 * and the files within are merged individually.
1271 *
1272 * Otherwise the files is processed on its own.
1273 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001274 bool MergePath(const std::string& path, bool override) {
1275 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1276 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1277 return MergeArchive(path, override);
1278 } else if (util::EndsWith(path, ".apk")) {
1279 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001280 }
1281
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001282 io::IFile* file = file_collection_->InsertFile(path);
1283 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001284 }
1285
1286 /**
1287 * Takes a file to load and merge into the master ResourceTable. If override
1288 * is true,
1289 * conflicting resources are allowed to override each other, in order of last
1290 * seen.
1291 *
1292 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1293 * merged into the
1294 * master ResourceTable. If the file ends with .flat, then it is treated like
1295 * a compiled file
1296 * and the header data is read and merged into the final ResourceTable.
1297 *
1298 * All other file types are ignored. This is because these files could be
1299 * coming from a zip,
1300 * where we could have other files like classes.dex.
1301 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001302 bool MergeFile(io::IFile* file, bool override) {
1303 const Source& src = file->GetSource();
1304 if (util::EndsWith(src.path, ".arsc.flat")) {
1305 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001307 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001308 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001309 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001310 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001311 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001312 return false;
1313 }
1314
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001315 CompiledFileInputStream input_stream(data->data(), data->size());
1316 uint32_t num_files = 0;
1317 if (!input_stream.ReadLittleEndian32(&num_files)) {
1318 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001319 << "failed read num files");
1320 return false;
1321 }
1322
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001323 for (uint32_t i = 0; i < num_files; i++) {
1324 pb::CompiledFile compiled_file;
1325 if (!input_stream.ReadCompiledFile(&compiled_file)) {
1326 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001327 DiagMessage(src) << "failed to read compiled file header");
1328 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001329 }
1330
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001331 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001332 if (!input_stream.ReadDataMetaData(&offset, &len)) {
1333 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001334 << "failed to read data meta data");
1335 return false;
1336 }
1337
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001338 std::unique_ptr<ResourceFile> resource_file =
1339 DeserializeCompiledFileFromPb(compiled_file, file->GetSource(),
1340 context_->GetDiagnostics());
1341 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001342 return false;
1343 }
1344
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001345 if (!MergeCompiledFile(file->CreateFileSegment(offset, len),
1346 resource_file.get(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001347 return false;
1348 }
1349 }
1350 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001351 } else if (util::EndsWith(src.path, ".xml") ||
1352 util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001353 // Since AAPT compiles these file types and appends .flat to them, seeing
1354 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001355 const StringPiece file_type =
1356 util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1357 context_->GetDiagnostics()->Error(DiagMessage(src)
1358 << "uncompiled " << file_type
Adam Lesinski6a396c12016-10-20 14:38:23 -07001359 << " file passed as argument. Must be "
1360 "compiled first into .flat file.");
1361 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001362 }
1363
1364 // Ignore non .flat files. This could be classes.dex or something else that
1365 // happens
1366 // to be in an archive.
1367 return true;
1368 }
1369
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001370 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(
1371 const AppInfo& app_info, const SplitConstraints& constraints) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001372 std::unique_ptr<xml::XmlResource> doc =
1373 util::make_unique<xml::XmlResource>();
1374
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001375 std::unique_ptr<xml::Namespace> namespace_android =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001376 util::make_unique<xml::Namespace>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001377 namespace_android->namespace_uri = xml::kSchemaAndroid;
1378 namespace_android->namespace_prefix = "android";
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001379
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001380 std::unique_ptr<xml::Element> manifest_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001381 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001382 manifest_el->name = "manifest";
1383 manifest_el->attributes.push_back(
1384 xml::Attribute{"", "package", app_info.package});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001385
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001386 if (app_info.version_code) {
1387 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001388 xml::Attribute{xml::kSchemaAndroid, "versionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001389 std::to_string(app_info.version_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001390 }
1391
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001392 if (app_info.revision_code) {
1393 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001394 xml::Attribute{xml::kSchemaAndroid, "revisionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001395 std::to_string(app_info.revision_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001396 }
1397
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001398 std::stringstream split_name;
1399 split_name << "config." << util::Joiner(constraints.configs, "_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001400
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001401 manifest_el->attributes.push_back(
1402 xml::Attribute{"", "split", split_name.str()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001404 std::unique_ptr<xml::Element> application_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001405 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001406 application_el->name = "application";
1407 application_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001408 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false"});
1409
Adam Lesinskie343eb12016-10-27 16:31:58 -07001410 manifest_el->AppendChild(std::move(application_el));
1411 namespace_android->AppendChild(std::move(manifest_el));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001412 doc->root = std::move(namespace_android);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001413 return doc;
1414 }
1415
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001416 bool CopyAssetsDirsToApk(IArchiveWriter* writer) {
1417 std::map<std::string, std::unique_ptr<io::RegularFile>> merged_assets;
1418 for (const std::string& assets_dir : options_.assets_dirs) {
1419 Maybe<std::vector<std::string>> files =
1420 file::FindFiles(assets_dir, context_->GetDiagnostics(), nullptr);
1421 if (!files) {
1422 return false;
1423 }
1424
1425 for (const std::string& file : files.value()) {
1426 std::string full_key = "assets/" + file;
1427 std::string full_path = assets_dir;
1428 file::AppendPath(&full_path, file);
1429
1430 auto iter = merged_assets.find(full_key);
1431 if (iter == merged_assets.end()) {
1432 merged_assets.emplace(std::move(full_key),
1433 util::make_unique<io::RegularFile>(Source(std::move(full_path))));
1434 } else if (context_->IsVerbose()) {
1435 context_->GetDiagnostics()->Warn(DiagMessage(iter->second->GetSource())
1436 << "asset file overrides '" << full_path << "'");
1437 }
1438 }
1439 }
1440
1441 for (auto& entry : merged_assets) {
1442 uint32_t compression_flags = ArchiveEntry::kCompress;
1443 std::string extension = file::GetExtension(entry.first).to_string();
1444 if (options_.extensions_to_not_compress.count(extension) > 0) {
1445 compression_flags = 0u;
1446 }
1447
1448 if (!CopyFileToArchive(entry.second.get(), entry.first, compression_flags, writer,
1449 context_)) {
1450 return false;
1451 }
1452 }
1453 return true;
1454 }
1455
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001456 /**
1457 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001458 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001459 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001460 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001461 xml::XmlResource* manifest, ResourceTable* table) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001462 const bool keep_raw_values = options_.package_type == PackageType::kStaticLib;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001463 bool result = FlattenXml(manifest, "AndroidManifest.xml", {},
1464 keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001465 if (!result) {
1466 return false;
1467 }
1468
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001469 ResourceFileFlattenerOptions file_flattener_options;
1470 file_flattener_options.keep_raw_values = keep_raw_values;
1471 file_flattener_options.do_not_compress_anything =
1472 options_.do_not_compress_anything;
1473 file_flattener_options.extensions_to_not_compress =
1474 options_.extensions_to_not_compress;
1475 file_flattener_options.no_auto_version = options_.no_auto_version;
1476 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001477 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001478 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1479 file_flattener_options.update_proguard_spec =
1480 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001481
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001482 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001483
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001484 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001485 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001486 return false;
1487 }
1488
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001489 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001490 if (!FlattenTableToPb(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001491 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001492 return false;
1493 }
1494 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001495 if (!FlattenTable(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001496 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001497 return false;
1498 }
1499 }
1500 return true;
1501 }
1502
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001503 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001504 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001505 std::unique_ptr<xml::XmlResource> manifest_xml =
1506 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1507 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001508 return 1;
1509 }
1510
1511 // First extract the Package name without modifying it (via
1512 // --rename-manifest-package).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001513 if (Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1514 manifest_xml.get(), context_->GetDiagnostics())) {
1515 const AppInfo& app_info = maybe_app_info.value();
1516 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001517 }
1518
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001519 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1520 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001521 return 1;
1522 }
1523
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001524 Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1525 manifest_xml.get(), context_->GetDiagnostics());
1526 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001527 return 1;
1528 }
1529
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001530 const AppInfo& app_info = maybe_app_info.value();
1531 if (app_info.min_sdk_version) {
1532 if (Maybe<int> maybe_min_sdk_version = ResourceUtils::ParseSdkVersion(
1533 app_info.min_sdk_version.value())) {
1534 context_->SetMinSdkVersion(maybe_min_sdk_version.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 }
1536 }
1537
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001538 context_->SetNameManglerPolicy(
1539 NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001540
1541 // Override the package ID when it is "android".
1542 if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001543 context_->SetPackageId(0x01);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001544
1545 // Verify we're building a regular app.
1546 if (options_.package_type != PackageType::kApp) {
1547 context_->GetDiagnostics()->Error(
1548 DiagMessage() << "package 'android' can only be built as a regular app");
1549 return 1;
1550 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001551 }
1552
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001553 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001554 return 1;
1555 }
1556
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001557 TableMergerOptions table_merger_options;
1558 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
1559 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_,
1560 table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001561
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001562 if (context_->IsVerbose()) {
1563 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001564 << StringPrintf("linking package '%s' using package ID %02x",
1565 context_->GetCompilationPackage().data(),
1566 context_->GetPackageId()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001567 }
1568
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001569 for (const std::string& input : input_files) {
1570 if (!MergePath(input, false)) {
1571 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001572 << "failed parsing input");
1573 return 1;
1574 }
1575 }
1576
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001577 for (const std::string& input : options_.overlay_files) {
1578 if (!MergePath(input, true)) {
1579 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001580 << "failed parsing overlays");
1581 return 1;
1582 }
1583 }
1584
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001585 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001586 return 1;
1587 }
1588
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001589 if (options_.package_type != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001590 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001591 if (!mover.Consume(context_, &final_table_)) {
1592 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001593 DiagMessage() << "failed moving private attributes");
1594 return 1;
1595 }
1596
1597 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001598 IdAssigner id_assigner(&options_.stable_id_map);
1599 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001600 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001601 return 1;
1602 }
1603
1604 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001605 if (options_.resource_id_map_path) {
1606 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001607 for (auto& type : package->types) {
1608 for (auto& entry : type->entries) {
1609 ResourceName name(package->name, type->type, entry->name);
1610 // The IDs are guaranteed to exist.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001611 options_.stable_id_map[std::move(name)] = ResourceId(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001612 package->id.value(), type->id.value(), entry->id.value());
1613 }
1614 }
1615 }
1616
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001617 if (!WriteStableIdMapToPath(context_->GetDiagnostics(),
1618 options_.stable_id_map,
1619 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001620 return 1;
1621 }
1622 }
1623 } else {
1624 // Static libs are merged with other apps, and ID collisions are bad, so
1625 // verify that
1626 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001627 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001628 return 1;
1629 }
1630 }
1631
1632 // Add the names to mangle based on our source merge earlier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001633 context_->SetNameManglerPolicy(NameManglerPolicy{
1634 context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001635
1636 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001637 context_->GetExternalSymbols()->PrependSource(
1638 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001639
1640 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001641 if (!linker.Consume(context_, &final_table_)) {
1642 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001643 << "failed linking references");
1644 return 1;
1645 }
1646
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001647 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001648 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001649 context_->GetDiagnostics()->Warn(DiagMessage()
1650 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001651 }
1652 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001653 ProductFilter product_filter(options_.products);
1654 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001655 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001656 return 1;
1657 }
1658 }
1659
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001660 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001661 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001662 if (!versioner.Consume(context_, &final_table_)) {
1663 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001664 << "failed versioning styles");
1665 return 1;
1666 }
1667 }
1668
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001669 if (options_.package_type != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001670 if (context_->IsVerbose()) {
1671 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001672 DiagMessage() << "collapsing resource versions for minimum SDK "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001673 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001674 }
1675
1676 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001677 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001678 return 1;
1679 }
1680 }
1681
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001682 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001683 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001684 if (!deduper.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001685 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001686 return 1;
1687 }
1688 }
1689
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001690 proguard::KeepSet proguard_keep_set;
1691 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001692
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001693 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001694 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001695 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001696 context_->GetDiagnostics()->Warn(DiagMessage()
1697 << "can't strip resources when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001698 }
1699 } else {
1700 // Adjust the SplitConstraints so that their SDK version is stripped if it
1701 // is less
1702 // than or equal to the minSdk. Otherwise the resources that have had
1703 // their SDK version
1704 // stripped due to minSdk won't ever match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001705 std::vector<SplitConstraints> adjusted_constraints_list;
1706 adjusted_constraints_list.reserve(options_.split_constraints.size());
1707 for (const SplitConstraints& constraints : options_.split_constraints) {
1708 SplitConstraints adjusted_constraints;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001709 for (const ConfigDescription& config : constraints.configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001710 if (config.sdkVersion <= context_->GetMinSdkVersion()) {
1711 adjusted_constraints.configs.insert(config.CopyWithoutSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001712 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001713 adjusted_constraints.configs.insert(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001714 }
1715 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001716 adjusted_constraints_list.push_back(std::move(adjusted_constraints));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001717 }
1718
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001719 TableSplitter table_splitter(adjusted_constraints_list,
1720 options_.table_splitter_options);
1721 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001722 return 1;
1723 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001724 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001725
1726 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001727 auto path_iter = options_.split_paths.begin();
1728 auto split_constraints_iter = adjusted_constraints_list.begin();
1729 for (std::unique_ptr<ResourceTable>& split_table :
1730 table_splitter.splits()) {
1731 if (context_->IsVerbose()) {
1732 context_->GetDiagnostics()->Note(
1733 DiagMessage(*path_iter)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001734 << "generating split with configurations '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001735 << util::Joiner(split_constraints_iter->configs, ", ") << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001736 }
1737
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001738 std::unique_ptr<IArchiveWriter> archive_writer =
1739 MakeArchiveWriter(*path_iter);
1740 if (!archive_writer) {
1741 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001742 << "failed to create archive");
1743 return 1;
1744 }
1745
1746 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001747 std::unique_ptr<xml::XmlResource> split_manifest =
1748 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001749
1750 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001751 if (!linker.Consume(context_, split_manifest.get())) {
1752 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001753 DiagMessage() << "failed to create Split AndroidManifest.xml");
1754 return 1;
1755 }
1756
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001757 if (!WriteApk(archive_writer.get(), &proguard_keep_set,
1758 split_manifest.get(), split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001759 return 1;
1760 }
1761
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001762 ++path_iter;
1763 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001764 }
1765 }
1766
1767 // Start writing the base APK.
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001768 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(options_.output_path);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001769 if (!archive_writer) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001770 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001771 return 1;
1772 }
1773
1774 bool error = false;
1775 {
1776 // AndroidManifest.xml has no resource name, but the CallSite is built
1777 // from the name
1778 // (aka, which package the AndroidManifest.xml is coming from).
1779 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001780 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001781
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001782 XmlReferenceLinker manifest_linker;
1783 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1784 if (options_.generate_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001785 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1786 manifest_xml.get(), &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001787 error = true;
1788 }
1789
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001790 if (options_.generate_main_dex_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001791 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1792 manifest_xml.get(),
1793 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001794 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001795 }
1796
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001797 if (options_.generate_java_class_path) {
1798 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001799 error = true;
1800 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001801 }
1802
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001803 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001804 // PackageParser will fail if URIs are removed from
1805 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001806 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1807 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001808 error = true;
1809 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001810 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001811 } else {
1812 error = true;
1813 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001814 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001815
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001816 if (error) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001817 context_->GetDiagnostics()->Error(DiagMessage() << "failed processing manifest");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001818 return 1;
1819 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001820
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001821 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(), &final_table_)) {
1822 return 1;
1823 }
1824
1825 if (!CopyAssetsDirsToApk(archive_writer.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001826 return 1;
1827 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001828
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001829 if (options_.generate_java_class_path) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001830 // The set of packages whose R class to call in the main classes
1831 // onResourcesLoaded callback.
1832 std::vector<std::string> packages_to_callback;
1833
1834 JavaClassGeneratorOptions template_options;
1835 template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1836 template_options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001837
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001838 if (options_.package_type == PackageType::kStaticLib || options_.generate_non_final_ids) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001839 template_options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001840 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001841
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001842 if (options_.package_type == PackageType::kSharedLib) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001843 template_options.use_final = false;
1844 template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{};
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001845 }
1846
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001847 const StringPiece actual_package = context_->GetCompilationPackage();
1848 StringPiece output_package = context_->GetCompilationPackage();
1849 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001850 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001851 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001852 }
1853
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001854 // Generate the private symbols if required.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001855 if (options_.private_symbols) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001856 packages_to_callback.push_back(options_.private_symbols.value());
1857
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001858 // If we defined a private symbols package, we only emit Public symbols
1859 // to the original package, and private and public symbols to the
1860 // private package.
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001861 JavaClassGeneratorOptions options = template_options;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001862 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001863 if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001864 options)) {
1865 return 1;
1866 }
1867 }
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001868
1869 // Generate all the symbols for all extra packages.
1870 for (const std::string& extra_package : options_.extra_java_packages) {
1871 packages_to_callback.push_back(extra_package);
1872
1873 JavaClassGeneratorOptions options = template_options;
1874 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1875 if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) {
1876 return 1;
1877 }
1878 }
1879
1880 // Generate the main public R class.
1881 JavaClassGeneratorOptions options = template_options;
1882
1883 // Only generate public symbols if we have a private package.
1884 if (options_.private_symbols) {
1885 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
1886 }
1887
1888 if (options.rewrite_callback_options) {
1889 options.rewrite_callback_options.value().packages_to_callback =
1890 std::move(packages_to_callback);
1891 }
1892
1893 if (!WriteJavaFile(&final_table_, actual_package, output_package, options)) {
1894 return 1;
1895 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001896 }
1897
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001898 if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001899 return 1;
1900 }
1901
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001902 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1903 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001904 return 1;
1905 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001906 return 0;
1907 }
1908
1909 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001910 LinkOptions options_;
1911 LinkContext* context_;
1912 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001913
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001914 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001915
1916 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001917 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001918
1919 // A vector of IFileCollections. This is mainly here to keep ownership of the
1920 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001921 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001922
1923 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001924 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001925 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001926
1927 // The set of shared libraries being used, mapping their assigned package ID to package name.
1928 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001929};
1930
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001931int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001932 LinkContext context;
1933 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001934 std::vector<std::string> overlay_arg_list;
1935 std::vector<std::string> extra_java_packages;
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001936 Maybe<std::string> package_id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001937 Maybe<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001938 Maybe<std::string> preferred_density;
1939 Maybe<std::string> product_list;
1940 bool legacy_x_flag = false;
1941 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001942 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001943 bool shared_lib = false;
1944 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001945 Maybe<std::string> stable_id_file_path;
1946 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001947 Flags flags =
1948 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001949 .RequiredFlag("-o", "Output path", &options.output_path)
1950 .RequiredFlag("--manifest", "Path to the Android manifest to build",
1951 &options.manifest_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001952 .OptionalFlagList("-I", "Adds an Android APK to link against", &options.include_paths)
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001953 .OptionalFlagList("-A",
1954 "An assets directory to include in the APK. These are unprocessed.",
1955 &options.assets_dirs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001956 .OptionalFlagList("-R",
1957 "Compilation unit to link, using `overlay` semantics.\n"
1958 "The last conflicting resource given takes precedence.",
1959 &overlay_arg_list)
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001960 .OptionalFlag("--package-id",
1961 "Specify the package ID to use for this app. Must be greater or equal to\n"
1962 "0x7f and can't be used with --static-lib or --shared-lib.",
1963 &package_id)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001964 .OptionalFlag("--java", "Directory in which to generate R.java",
1965 &options.generate_java_class_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001966 .OptionalFlag("--proguard", "Output file for generated Proguard rules",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001967 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001968 .OptionalFlag("--proguard-main-dex",
1969 "Output file for generated Proguard rules for the main dex",
1970 &options.generate_main_dex_proguard_rules_path)
1971 .OptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001972 &options.no_auto_version)
1973 .OptionalSwitch("--no-version-vectors",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001974 "Disables automatic versioning of vector drawables. "
1975 "Use this only\n"
1976 "when building with vector drawable support library",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001977 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001978 .OptionalSwitch("--no-version-transitions",
1979 "Disables automatic versioning of transition resources. "
1980 "Use this only\n"
1981 "when building with transition support library",
1982 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001983 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001984 "Disables automatic deduping of resources with\n"
1985 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001986 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001987 .OptionalSwitch("--enable-sparse-encoding",
1988 "Enables encoding sparse entries using a binary search tree.\n"
1989 "This decreases APK size at the cost of resource retrieval performance.",
1990 &options.table_flattener_options.use_sparse_entries)
1991 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01",
1992 &legacy_x_flag)
1993 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001994 &require_localization)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001995 .OptionalFlag("-c",
1996 "Comma separated list of configurations to include. The default\n"
1997 "is all configurations",
1998 &configs)
1999 .OptionalFlag("--preferred-density",
2000 "Selects the closest matching density and strips out all others.",
2001 &preferred_density)
2002 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002003 .OptionalSwitch("--output-to-dir",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002004 "Outputs the APK contents to a directory specified "
2005 "by -o",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002006 &options.output_to_directory)
2007 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002008 "Removes XML namespace prefix and URI "
2009 "information from AndroidManifest.xml\nand XML "
2010 "binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002011 &options.no_xml_namespaces)
2012 .OptionalFlag("--min-sdk-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002013 "Default minimum SDK version to use for "
2014 "AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002015 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002016 .OptionalFlag("--target-sdk-version",
2017 "Default target SDK version to use for "
2018 "AndroidManifest.xml",
2019 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002020 .OptionalFlag("--version-code",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002021 "Version code (integer) to inject into the "
2022 "AndroidManifest.xml if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002023 &options.manifest_fixer_options.version_code_default)
2024 .OptionalFlag("--version-name",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002025 "Version name to inject into the AndroidManifest.xml "
2026 "if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002027 &options.manifest_fixer_options.version_name_default)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002028 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library", &shared_lib)
2029 .OptionalSwitch("--static-lib", "Generate a static Android library", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002030 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002031 "Merge all library resources under the app's package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002032 &options.no_static_lib_packages)
2033 .OptionalSwitch("--non-final-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002034 "Generates R.java without the final modifier.\n"
2035 "This is implied when --static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002036 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002037 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002038 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002039 .OptionalFlag("--emit-ids",
2040 "Emit a file at the given path with a list of name to ID\n"
2041 "mappings, suitable for use with --stable-ids.",
2042 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002043 .OptionalFlag("--private-symbols",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002044 "Package name to use when generating R.java for "
2045 "private symbols.\n"
2046 "If not specified, public and private symbols will use "
2047 "the application's "
2048 "package name",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002049 &options.private_symbols)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002050 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002051 &options.custom_java_package)
2052 .OptionalFlagList("--extra-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002053 "Generate the same R.java but with different "
2054 "package names",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002055 &extra_java_packages)
2056 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002057 "Adds a JavaDoc annotation to all "
Adam Lesinskid0f116b2016-07-08 15:00:32 -07002058 "generated Java classes",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002059 &options.javadoc_annotations)
2060 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002061 "Allows the addition of new resources in "
2062 "overlays without <add-resource> tags",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002063 &options.auto_add_overlay)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002064 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002065 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002066 .OptionalFlag("--rename-instrumentation-target-package",
2067 "Changes the name of the target package for instrumentation. "
2068 "Most useful "
2069 "when used\nin conjunction with --rename-manifest-package",
2070 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002071 .OptionalFlagList("-0", "File extensions not to compress",
2072 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002073 .OptionalFlagList("--split",
2074 "Split resources matching a set of configs out to a "
2075 "Split APK.\nSyntax: path/to/output.apk:<config>[,<config>[...]]",
2076 &split_args)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002077 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002078
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002079 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002080 return 1;
2081 }
2082
2083 // Expand all argument-files passed into the command line. These start with
2084 // '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002085 std::vector<std::string> arg_list;
2086 for (const std::string& arg : flags.GetArgs()) {
2087 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002088 const std::string path = arg.substr(1, arg.size() - 1);
2089 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002090 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
2091 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002092 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002093 }
2094 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002095 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002096 }
2097 }
2098
2099 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002100 for (const std::string& arg : overlay_arg_list) {
2101 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002102 const std::string path = arg.substr(1, arg.size() - 1);
2103 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002104 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
2105 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002106 return 1;
2107 }
2108 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002109 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002110 }
2111 }
2112
2113 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002114 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002115 }
2116
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002117 if (shared_lib && static_lib) {
2118 context.GetDiagnostics()->Error(DiagMessage()
2119 << "only one of --shared-lib and --static-lib can be defined");
2120 return 1;
2121 }
2122
2123 if (shared_lib) {
2124 options.package_type = PackageType::kSharedLib;
2125 context.SetPackageId(0x00);
2126 } else if (static_lib) {
2127 options.package_type = PackageType::kStaticLib;
2128 context.SetPackageId(kAppPackageId);
2129 } else {
2130 options.package_type = PackageType::kApp;
2131 context.SetPackageId(kAppPackageId);
2132 }
2133
2134 if (package_id) {
2135 if (options.package_type != PackageType::kApp) {
2136 context.GetDiagnostics()->Error(
2137 DiagMessage() << "can't specify --package-id when not building a regular app");
2138 return 1;
2139 }
2140
2141 const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id.value());
2142 if (!maybe_package_id_int) {
2143 context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id.value()
2144 << "' is not a valid integer");
2145 return 1;
2146 }
2147
2148 const uint32_t package_id_int = maybe_package_id_int.value();
2149 if (package_id_int < kAppPackageId || package_id_int > std::numeric_limits<uint8_t>::max()) {
2150 context.GetDiagnostics()->Error(
2151 DiagMessage() << StringPrintf(
2152 "invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int));
2153 return 1;
2154 }
2155 context.SetPackageId(static_cast<uint8_t>(package_id_int));
2156 }
2157
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002158 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002159 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002160 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002161 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002162 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002163 }
2164 }
2165
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002166 if (product_list) {
2167 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002168 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002169 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002170 }
2171 }
2172 }
2173
2174 AxisConfigFilter filter;
2175 if (configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002176 for (const StringPiece& config_str : util::Tokenize(configs.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002177 ConfigDescription config;
2178 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002179 if (lv.InitFromFilterString(config_str)) {
2180 lv.WriteTo(&config);
2181 } else if (!ConfigDescription::Parse(config_str, &config)) {
2182 context.GetDiagnostics()->Error(DiagMessage() << "invalid config '"
2183 << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002184 << "' for -c option");
2185 return 1;
2186 }
2187
2188 if (config.density != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002189 context.GetDiagnostics()->Warn(DiagMessage() << "ignoring density '"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002190 << config
2191 << "' for -c option");
2192 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002193 filter.AddConfig(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002194 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002195 }
2196
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002197 options.table_splitter_options.config_filter = &filter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002198 }
2199
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002200 if (preferred_density) {
2201 ConfigDescription preferred_density_config;
2202 if (!ConfigDescription::Parse(preferred_density.value(),
2203 &preferred_density_config)) {
2204 context.GetDiagnostics()->Error(
2205 DiagMessage() << "invalid density '" << preferred_density.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002206 << "' for --preferred-density option");
2207 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002208 }
2209
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002210 // Clear the version that can be automatically added.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002211 preferred_density_config.sdkVersion = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002212
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002213 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002214 ConfigDescription::CONFIG_DENSITY) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002215 context.GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002216 DiagMessage() << "invalid preferred density '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002217 << preferred_density.value() << "'. "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002218 << "Preferred density must only be a density value");
2219 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002220 }
Pierre Lecesne672384b2017-02-06 10:29:02 +00002221 options.table_splitter_options.preferred_densities.push_back(preferred_density_config.density);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002222 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002223
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002224 if (options.package_type != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002225 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2226 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002227 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002228 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002229 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002230
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002231 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002232 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002233 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2234 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2235 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2236 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2237
2238 // Parse the split parameters.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002239 for (const std::string& split_arg : split_args) {
2240 options.split_paths.push_back({});
2241 options.split_constraints.push_back({});
2242 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(),
2243 &options.split_paths.back(),
2244 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002245 return 1;
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002246 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002247 }
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002248
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002249 // Turn off auto versioning for static-libs.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002250 if (options.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002251 options.no_auto_version = true;
2252 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002253 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002254 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002255
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002256 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002257 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002258}
2259
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002260} // namespace aapt