blob: 6e0809e6245057a507277111d15ce35e0a042376 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include <sys/stat.h>
18
19#include <fstream>
20#include <queue>
21#include <unordered_map>
22#include <vector>
23
24#include "android-base/errors.h"
25#include "android-base/file.h"
Adam Lesinskif34b6f42017-03-03 16:33:26 -080026#include "android-base/stringprintf.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080027#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028#include "google/protobuf/io/coded_stream.h"
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "AppInfo.h"
31#include "Debug.h"
32#include "Flags.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080033#include "Locale.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034#include "NameMangler.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080035#include "ResourceUtils.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070036#include "cmd/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037#include "compile/IdAssigner.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080038#include "filter/ConfigFilter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039#include "flatten/Archive.h"
40#include "flatten/TableFlattener.h"
41#include "flatten/XmlFlattener.h"
Adam Lesinski06460ef2017-03-14 18:52:13 -070042#include "io/BigBufferInputStream.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080043#include "io/FileSystem.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070044#include "io/Util.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080045#include "io/ZipArchive.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070046#include "java/JavaClassGenerator.h"
47#include "java/ManifestClassGenerator.h"
48#include "java/ProguardRules.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049#include "link/Linkers.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050#include "link/ManifestFixer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080051#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052#include "link/TableMerger.h"
Adam Lesinskid48944a2017-02-21 14:22:30 -080053#include "optimize/ResourceDeduper.h"
54#include "optimize/VersionCollapser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055#include "process/IResourceTableConsumer.h"
56#include "process/SymbolTable.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080057#include "proto/ProtoSerialize.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080058#include "split/TableSplitter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059#include "unflatten/BinaryResourceParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060#include "util/Files.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080061#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062
Adam Lesinskid5083f62017-01-16 15:07:21 -080063using android::StringPiece;
Adam Lesinskif34b6f42017-03-03 16:33:26 -080064using android::base::StringPrintf;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070065
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066namespace aapt {
67
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080068// The type of package to build.
69enum class PackageType {
70 kApp,
71 kSharedLib,
72 kStaticLib,
73};
74
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075struct LinkOptions {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080076 PackageType package_type = PackageType::kApp;
77
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 std::string output_path;
79 std::string manifest_path;
80 std::vector<std::string> include_paths;
81 std::vector<std::string> overlay_files;
Adam Lesinskib39ad7c2017-03-13 11:40:48 -070082 std::vector<std::string> assets_dirs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 bool output_to_directory = false;
84 bool auto_add_overlay = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 // Java/Proguard options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 Maybe<std::string> generate_java_class_path;
88 Maybe<std::string> custom_java_package;
89 std::set<std::string> extra_java_packages;
90 Maybe<std::string> generate_proguard_rules_path;
91 Maybe<std::string> generate_main_dex_proguard_rules_path;
92 bool generate_non_final_ids = false;
93 std::vector<std::string> javadoc_annotations;
94 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070095
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 // Optimizations/features.
97 bool no_auto_version = false;
98 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +090099 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 bool no_resource_deduping = false;
101 bool no_xml_namespaces = false;
102 bool do_not_compress_anything = false;
103 std::unordered_set<std::string> extensions_to_not_compress;
104
105 // Static lib options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 bool no_static_lib_packages = false;
107
108 // AndroidManifest.xml massaging options.
109 ManifestFixerOptions manifest_fixer_options;
110
111 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700113
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800114 // Flattening options.
115 TableFlattenerOptions table_flattener_options;
116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 TableSplitterOptions table_splitter_options;
119 std::vector<SplitConstraints> split_constraints;
120 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 std::unordered_map<ResourceName, ResourceId> stable_id_map;
124 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125};
126
Adam Lesinski64587af2016-02-18 18:33:06 -0800127class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 public:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700129 LinkContext() : name_mangler_({}), symbols_(&name_mangler_) {
130 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700132 IDiagnostics* GetDiagnostics() override {
133 return &diagnostics_;
134 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700136 NameMangler* GetNameMangler() override {
137 return &name_mangler_;
138 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
141 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 const std::string& GetCompilationPackage() override {
145 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800149 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800151
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700152 uint8_t GetPackageId() override {
153 return package_id_;
154 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700156 void SetPackageId(uint8_t id) {
157 package_id_ = id;
158 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800159
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700160 SymbolTable* GetExternalSymbols() override {
161 return &symbols_;
162 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800163
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700164 bool IsVerbose() override {
165 return verbose_;
166 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800167
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700168 void SetVerbose(bool val) {
169 verbose_ = val;
170 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800171
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700172 int GetMinSdkVersion() override {
173 return min_sdk_version_;
174 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700175
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700176 void SetMinSdkVersion(int minSdk) {
177 min_sdk_version_ = minSdk;
178 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700179
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 DISALLOW_COPY_AND_ASSIGN(LinkContext);
182
183 StdErrDiagnostics diagnostics_;
184 NameMangler name_mangler_;
185 std::string compilation_package_;
186 uint8_t package_id_ = 0x0;
187 SymbolTable symbols_;
188 bool verbose_ = false;
189 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190};
191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700193 Maybe<size_t> max_sdk_level, bool keep_raw_values, IArchiveWriter* writer,
194 IAaptContext* context) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 BigBuffer buffer(1024);
196 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 options.keep_raw_values = keep_raw_values;
198 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800201 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 }
203
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 DiagMessage msg;
206 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 if (max_sdk_level) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700208 msg << " maxSdkLevel=" << max_sdk_level.value() << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 }
212
Adam Lesinski06460ef2017-03-14 18:52:13 -0700213 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700214 return io::CopyInputStreamToArchive(context, &input_stream, path.to_string(),
215 ArchiveEntry::kCompress, writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800216}
217
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700218static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source, const void* data,
219 size_t len, IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700220 pb::ResourceTable pb_table;
221 if (!pb_table.ParseFromArray(data, len)) {
222 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 return {};
224 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800225
Adam Lesinski06460ef2017-03-14 18:52:13 -0700226 std::unique_ptr<ResourceTable> table = DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 if (!table) {
228 return {};
229 }
230 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800231}
232
233/**
234 * Inflates an XML file from the source path.
235 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700236static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 std::ifstream fin(path, std::ifstream::binary);
238 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 return {};
241 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800243}
244
Adam Lesinski355f2852016-02-13 20:26:45 -0800245struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 bool no_auto_version = false;
247 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900248 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 bool no_xml_namespaces = false;
250 bool keep_raw_values = false;
251 bool do_not_compress_anything = false;
252 bool update_proguard_spec = false;
253 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800254};
255
256class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 public:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700258 ResourceFileFlattener(const ResourceFileFlattenerOptions& options, IAaptContext* context,
259 proguard::KeepSet* keep_set)
260 : options_(options), context_(context), keep_set_(keep_set) {
261 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800262
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800264
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 private:
266 struct FileOperation {
267 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700268
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 // The entry this file came from.
270 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700271
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700274
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700277
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 std::string dst_path;
280 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800282
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
286 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800287
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 ResourceFileFlattenerOptions options_;
289 IAaptContext* context_;
290 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800291};
292
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
294 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 return 0;
296 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800297
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 for (const std::string& extension : options_.extensions_to_not_compress) {
299 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800301 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 }
303 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800304}
305
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900306static bool IsTransitionElement(const std::string& name) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700307 return name == "fade" || name == "changeBounds" || name == "slide" || name == "explode" ||
308 name == "changeImageTransform" || name == "changeTransform" ||
309 name == "changeClipBounds" || name == "autoTransition" || name == "recolor" ||
310 name == "changeScroll" || name == "transitionSet" || name == "transition" ||
311 name == "transitionManager";
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900312}
313
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700314bool ResourceFileFlattener::LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
315 std::queue<FileOperation>* out_file_op_queue) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700318
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 if (context_->IsVerbose()) {
320 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 }
322
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 XmlReferenceLinker xml_linker;
324 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700325 return false;
326 }
327
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700328 if (options_.update_proguard_spec && !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329 return false;
330 }
331
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 if (options_.no_xml_namespaces) {
333 XmlNamespaceRemover namespace_remover;
334 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800336 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800338
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 if (!options_.no_auto_version) {
340 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 xml::Element* el = xml::FindRootElement(doc);
343 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 if (el->name == "vector" || el->name == "animated-vector") {
345 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700348 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700350 }
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900351 if (options_.no_version_transitions) {
352 // Skip this if it is a transition resource.
353 xml::Element* el = xml::FindRootElement(doc);
354 if (el && el->namespace_uri.empty()) {
355 if (IsTransitionElement(el->name)) {
356 // We are NOT going to version this file.
357 file_op->skip_version = true;
358 return true;
359 }
360 }
361 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700362
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364
365 // Find the first SDK level used that is higher than this defined config and
366 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700367 const int min_sdk_version = context_->GetMinSdkVersion();
368 for (int sdk_level : xml_linker.sdk_levels()) {
369 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700370 if (!ShouldGenerateVersionedResource(file_op->entry, config, sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371 // If we shouldn't generate a versioned resource, stop checking.
372 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800373 }
374
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 ResourceFile versioned_file_desc = doc->file;
376 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700377
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 FileOperation new_file_op;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700379 new_file_op.xml_to_flatten =
380 util::make_unique<xml::XmlResource>(versioned_file_desc, doc->root->Clone());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 new_file_op.config = versioned_file_desc.config;
382 new_file_op.entry = file_op->entry;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700383 new_file_op.dst_path =
384 ResourceUtils::BuildResourceFileName(versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800385
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700387 context_->GetDiagnostics()->Note(DiagMessage(versioned_file_desc.source)
388 << "auto-versioning resource from config '" << config
389 << "' -> '" << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800390 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 bool added = table->AddFileReferenceAllowMangled(
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700393 versioned_file_desc.name, versioned_file_desc.config, versioned_file_desc.source,
394 new_file_op.dst_path, nullptr, context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395 if (!added) {
396 return false;
397 }
398
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 break;
401 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800402 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 }
404 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800405}
406
407/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408 * Do not insert or remove any resources while executing in this function. It
409 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800410 * corrupt the iteration order.
411 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700412bool ResourceFileFlattener::Flatten(ResourceTable* table, IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 bool error = false;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700414 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation> config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800415
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 for (auto& pkg : table->packages) {
417 for (auto& type : pkg->types) {
418 // Sort by config and name, so that we get better locality in the zip
419 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420 config_sorted_files.clear();
421 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800422
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 // Populate the queue with all files in the ResourceTable.
424 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 for (auto& config_value : entry->values) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700426 FileReference* file_ref = ValueCast<FileReference>(config_value->value.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700428 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700430
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700435 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 }
437
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438 FileOperation file_op;
439 file_op.entry = entry.get();
440 file_op.dst_path = *file_ref->path;
441 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 if (type->type != ResourceType::kRaw &&
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700445 (util::EndsWith(src_path, ".xml.flat") || util::EndsWith(src_path, ".xml"))) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 << "failed to open file");
450 return false;
451 }
452
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700453 file_op.xml_to_flatten = xml::Inflate(data->data(), data->size(),
454 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 return false;
458 }
459
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 file_op.xml_to_flatten->file.config = config_value->config;
461 file_op.xml_to_flatten->file.source = file_ref->GetSource();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700462 file_op.xml_to_flatten->file.name = ResourceName(pkg->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463
464 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468
469 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 // else we end up copying the string in the std::make_pair() method,
471 // then creating a StringPiece from the copy, which would cause us
472 // to end up referencing garbage in the map.
473 const StringPiece entry_name(entry->name);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700474 config_sorted_files[std::make_pair(config_value->config, entry_name)] =
475 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 }
477 }
478 }
479
480 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 for (; !file_operations.empty(); file_operations.pop()) {
482 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 error = true;
486 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700487 }
488
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
490 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 // creating a StringPiece from the copy, which would cause us to end up
492 // referencing garbage in the map.
493 const StringPiece entry_name(file_op.entry->name);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700494 config_sorted_files[std::make_pair(file_op.config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 }
496
497 if (error) {
498 return false;
499 }
500
501 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 for (auto& map_entry : config_sorted_files) {
503 const ConfigDescription& config = map_entry.first.first;
504 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 if (file_op.xml_to_flatten) {
507 Maybe<size_t> max_sdk_level;
508 if (!options_.no_auto_version && !file_op.skip_version) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700509 max_sdk_level = std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
510 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 }
512
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700513 bool result = FlattenXml(file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
514 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 if (!result) {
516 error = true;
517 }
518 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700519 bool result =
520 io::CopyFileToArchive(context_, file_op.file_to_copy, file_op.dst_path,
521 GetCompressionFlags(file_op.dst_path), archive_writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522 if (!result) {
523 error = true;
524 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700525 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700527 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528 }
529 return !error;
530}
531
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700532static bool WriteStableIdMapToPath(IDiagnostics* diag,
533 const std::unordered_map<ResourceName, ResourceId>& id_map,
534 const std::string& id_map_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700537 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 return false;
539 }
540
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 const ResourceName& name = entry.first;
543 const ResourceId& id = entry.second;
544 fout << name << " = " << id << "\n";
545 }
546
547 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700548 diag->Error(DiagMessage(id_map_path) << "failed writing to file: "
549 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 return false;
551 }
552
553 return true;
554}
555
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700556static bool LoadStableIdMap(IDiagnostics* diag, const std::string& path,
557 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 std::string content;
559 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 return false;
562 }
563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 out_id_map->clear();
565 size_t line_no = 0;
566 for (StringPiece line : util::Tokenize(content, '\n')) {
567 line_no++;
568 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 if (line.empty()) {
570 continue;
571 }
572
573 auto iter = std::find(line.begin(), line.end(), '=');
574 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 return false;
577 }
578
579 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 StringPiece res_name_str =
581 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
582 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700583 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource name '" << res_name_str
584 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 return false;
586 }
587
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
589 const size_t res_id_str_len = line.size() - res_id_start_idx;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700590 StringPiece res_id_str = util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
593 if (!maybe_id) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700594 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '" << res_id_str
595 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 return false;
597 }
598
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 }
601 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700602}
603
Adam Lesinskifb48d292015-11-07 15:52:13 -0800604class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 public:
606 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 : options_(options),
608 context_(context),
609 final_table_(),
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700610 file_collection_(util::make_unique<io::FileCollection>()) {
611 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700612
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 /**
614 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700615 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700617 bool LoadSymbolsFromIncludePaths() {
618 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700620 for (const std::string& path : options_.include_paths) {
621 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700622 context_->GetDiagnostics()->Note(DiagMessage(path) << "loading include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700624
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700626 std::string error_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800627 std::unique_ptr<ResourceTable> include_static = LoadStaticLibrary(path, &error_str);
628 if (include_static) {
629 if (options_.package_type != PackageType::kStaticLib) {
630 // Can't include static libraries when not building a static library (they have no IDs
631 // assigned).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 context_->GetDiagnostics()->Error(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800633 DiagMessage(path) << "can't include static library when not building a static lib");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700634 return false;
635 }
636
637 // If we are using --no-static-lib-packages, we need to rename the
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800638 // package of this table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700639 if (options_.no_static_lib_packages) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800640 // Since package names can differ, and multiple packages can exist in a ResourceTable,
641 // we place the requirement that all static libraries are built with the package
642 // ID 0x7f. So if one is not found, this is an error.
643 if (ResourceTablePackage* pkg = include_static->FindPackageById(kAppPackageId)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700644 pkg->name = context_->GetCompilationPackage();
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800645 } else {
646 context_->GetDiagnostics()->Error(DiagMessage(path)
647 << "no package with ID 0x7f found in static library");
648 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 }
650 }
651
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652 context_->GetExternalSymbols()->AppendSource(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800653 util::make_unique<ResourceTableSymbolSource>(include_static.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700654
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800655 static_table_includes_.push_back(std::move(include_static));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700659 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700660 return false;
661 }
662
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700663 if (!asset_source->AddAssetPath(path)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800664 context_->GetDiagnostics()->Error(DiagMessage(path) << "failed to load include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 return false;
666 }
667 }
668
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800669 // Capture the shared libraries so that the final resource table can be properly flattened
670 // with support for shared libraries.
671 for (auto& entry : asset_source->GetAssignedPackageIds()) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800672 if (entry.first > kFrameworkPackageId && entry.first < kAppPackageId) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800673 final_table_.included_packages_[entry.first] = entry.second;
674 }
675 }
676
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700677 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700678 return true;
679 }
680
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800681 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800683 xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
684 if (manifest_el == nullptr) {
685 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700686 }
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800687
688 AppInfo app_info;
689
690 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
691 diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>");
692 return {};
693 }
694
695 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
696 if (!package_attr) {
697 diag->Error(DiagMessage(xml_res->file.source)
698 << "<manifest> must have a 'package' attribute");
699 return {};
700 }
701 app_info.package = package_attr->value;
702
703 if (xml::Attribute* version_code_attr =
704 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
705 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_attr->value);
706 if (!maybe_code) {
707 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
708 << "invalid android:versionCode '" << version_code_attr->value << "'");
709 return {};
710 }
711 app_info.version_code = maybe_code.value();
712 }
713
714 if (xml::Attribute* revision_code_attr =
715 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
716 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(revision_code_attr->value);
717 if (!maybe_code) {
718 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
719 << "invalid android:revisionCode '" << revision_code_attr->value << "'");
720 return {};
721 }
722 app_info.revision_code = maybe_code.value();
723 }
724
725 if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
726 if (!split_name_attr->value.empty()) {
727 app_info.split_name = split_name_attr->value;
728 }
729 }
730
731 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
732 if (xml::Attribute* min_sdk =
733 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700734 app_info.min_sdk_version = ResourceUtils::ParseSdkVersion(min_sdk->value);
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800735 }
736 }
737 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 }
739
740 /**
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800741 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700743 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700744 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700745 bool VerifyNoExternalPackages() {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700746 auto is_ext_package_func = [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700747 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
748 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 };
750
751 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700752 for (const auto& package : final_table_.packages) {
753 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 // We have a package that is not related to the one we're building!
755 for (const auto& type : package->types) {
756 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700759 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 // for the 'android' package. This is due to legacy reasons.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700762 if (ValueCast<Id>(config_value->value.get()) && package->name == "android") {
763 context_->GetDiagnostics()->Warn(DiagMessage(config_value->value->GetSource())
764 << "generated id '" << res_name
765 << "' for external package '" << package->name
766 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700767 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700768 context_->GetDiagnostics()->Error(DiagMessage(config_value->value->GetSource())
769 << "defined resource '" << res_name
770 << "' for external package '" << package->name
771 << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700772 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700774 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775 }
776 }
777 }
778 }
779
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700780 auto new_end_iter = std::remove_if(final_table_.packages.begin(), final_table_.packages.end(),
781 is_ext_package_func);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700782 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700783 return !error;
784 }
785
786 /**
787 * Returns true if no IDs have been set, false otherwise.
788 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700789 bool VerifyNoIdsSet() {
790 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700791 for (const auto& type : package->types) {
792 if (type->id) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800793 context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID "
794 << StringPrintf("%02x", type->id.value())
795 << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700796 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700797 }
798
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700799 for (const auto& entry : type->entries) {
800 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700801 ResourceNameRef res_name(package->name, type->type, entry->name);
802 context_->GetDiagnostics()->Error(
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800803 DiagMessage() << "entry " << res_name << " has ID "
804 << StringPrintf("%02x", entry->id.value()) << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700805 return false;
806 }
807 }
808 }
809 }
810 return true;
811 }
812
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700813 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
814 if (options_.output_to_directory) {
815 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700816 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700817 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700818 }
819 }
820
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700821 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800823 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700824 if (!flattener.Consume(context_, table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700825 context_->GetDiagnostics()->Error(DiagMessage() << "failed to flatten resource table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700826 return false;
827 }
828
Adam Lesinski06460ef2017-03-14 18:52:13 -0700829 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700830 return io::CopyInputStreamToArchive(context_, &input_stream, "resources.arsc",
831 ArchiveEntry::kAlign, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700832 }
833
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700834 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700835 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700836 return io::CopyProtoToArchive(context_, pb_table.get(), "resources.arsc.flat", 0, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837 }
838
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700839 bool WriteJavaFile(ResourceTable* table, const StringPiece& package_name_to_generate,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700840 const StringPiece& out_package,
841 const JavaClassGeneratorOptions& java_options) {
842 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700843 return true;
844 }
845
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700846 std::string out_path = options_.generate_java_class_path.value();
847 file::AppendPath(&out_path, file::PackageToPath(out_package));
848 if (!file::mkdirs(out_path)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700849 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path
850 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700851 return false;
852 }
853
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700854 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700856 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700858 context_->GetDiagnostics()->Error(DiagMessage()
859 << "failed writing to '" << out_path
860 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 return false;
862 }
863
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700864 JavaClassGenerator generator(context_, table, java_options);
865 if (!generator.Generate(package_name_to_generate, out_package, &fout)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700866 context_->GetDiagnostics()->Error(DiagMessage(out_path) << generator.getError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700867 return false;
868 }
869
870 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700871 context_->GetDiagnostics()->Error(DiagMessage()
872 << "failed writing to '" << out_path
873 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 }
875 return true;
876 }
877
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700878 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
879 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 return true;
881 }
882
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700883 std::unique_ptr<ClassDefinition> manifest_class =
884 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700885
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700886 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700887 // Something bad happened, but we already logged it, so exit.
888 return false;
889 }
890
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700891 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700892 // Empty Manifest class, no need to generate it.
893 return true;
894 }
895
896 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700897 for (const std::string& annotation : options_.javadoc_annotations) {
898 std::string proper_annotation = "@";
899 proper_annotation += annotation;
900 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 }
902
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700904
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700905 std::string out_path = options_.generate_java_class_path.value();
906 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700908 if (!file::mkdirs(out_path)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700909 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path
910 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700911 return false;
912 }
913
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700914 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700915
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700916 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700917 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700918 context_->GetDiagnostics()->Error(DiagMessage()
919 << "failed writing to '" << out_path
920 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700921 return false;
922 }
923
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700924 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8, true, &fout)) {
925 context_->GetDiagnostics()->Error(DiagMessage()
926 << "failed writing to '" << out_path
927 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700928 return false;
929 }
930 return true;
931 }
932
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700933 bool WriteProguardFile(const Maybe<std::string>& out, const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700934 if (!out) {
935 return true;
936 }
937
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700938 const std::string& out_path = out.value();
939 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700940 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700941 context_->GetDiagnostics()->Error(DiagMessage()
942 << "failed to open '" << out_path
943 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700944 return false;
945 }
946
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700947 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700948 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700949 context_->GetDiagnostics()->Error(DiagMessage()
950 << "failed writing to '" << out_path
951 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700952 return false;
953 }
954 return true;
955 }
956
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700957 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
958 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700959 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700960 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700961 if (!collection) {
962 return {};
963 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700964 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700965 }
966
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700967 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700968 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700969 if (!file) {
970 return {};
971 }
972
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700973 std::unique_ptr<io::IData> data = file->OpenAsData();
974 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
975 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700976 }
977
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700978 bool MergeStaticLibrary(const std::string& input, bool override) {
979 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700980 context_->GetDiagnostics()->Note(DiagMessage() << "merging static library " << input);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700981 }
982
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700983 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700984 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700985 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700986 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700987 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700988 return false;
989 }
990
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800991 std::unique_ptr<ResourceTable> table = LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700992 if (!table) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800993 context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700994 return false;
995 }
996
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800997 ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700998 if (!pkg) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800999 context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001000 return false;
1001 }
1002
1003 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001004 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001005 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001006 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007
1008 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001009 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001011 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012 }
1013
1014 pkg->name = "";
1015 if (override) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001016 result = table_merger_->MergeOverlay(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001017 } else {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001018 result = table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001019 }
1020
1021 } else {
1022 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001023 // preserved and resource names are mangled.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001024 result =
1025 table_merger_->MergeAndMangle(Source(input), pkg->name, table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001026 }
1027
1028 if (!result) {
1029 return false;
1030 }
1031
1032 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001033 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001034 return true;
1035 }
1036
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001037 bool MergeResourceTable(io::IFile* file, bool override) {
1038 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001039 context_->GetDiagnostics()->Note(DiagMessage() << "merging resource table "
1040 << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001041 }
1042
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001043 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001044 if (!data) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001045 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001046 return false;
1047 }
1048
1049 std::unique_ptr<ResourceTable> table =
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001050 LoadTableFromPb(file->GetSource(), data->data(), data->size(), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001051 if (!table) {
1052 return false;
1053 }
1054
1055 bool result = false;
1056 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001057 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001058 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001059 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001060 }
1061 return result;
1062 }
1063
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001064 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc, bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001065 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001066 context_->GetDiagnostics()->Note(DiagMessage() << "merging '" << file_desc->name
1067 << "' from compiled file "
1068 << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001069 }
1070
1071 bool result = false;
1072 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001073 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001074 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001075 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001076 }
1077
1078 if (!result) {
1079 return false;
1080 }
1081
1082 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001083 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1084 if (exported_symbol.name.package.empty()) {
1085 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001086 }
1087
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001088 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001089
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001090 Maybe<ResourceName> mangled_name =
1091 context_->GetNameMangler()->MangleName(exported_symbol.name);
1092 if (mangled_name) {
1093 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001094 }
1095
1096 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001097 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1098 bool result = final_table_.AddResourceAllowMangled(
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001099 res_name, ConfigDescription::DefaultConfig(), std::string(), std::move(id),
1100 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001101 if (!result) {
1102 return false;
1103 }
1104 }
1105 return true;
1106 }
1107
1108 /**
1109 * Takes a path to load as a ZIP file and merges the files within into the
1110 * master ResourceTable.
1111 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001112 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001113 *
1114 * An io::IFileCollection is created from the ZIP file and added to the set of
1115 * io::IFileCollections that are open.
1116 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001117 bool MergeArchive(const std::string& input, bool override) {
1118 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001119 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive " << input);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001120 }
1121
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001122 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001123 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001124 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001125 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001126 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001127 return false;
1128 }
1129
1130 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001131 for (auto iter = collection->Iterator(); iter->HasNext();) {
1132 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001133 error = true;
1134 }
1135 }
1136
1137 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001138 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001139 return !error;
1140 }
1141
1142 /**
1143 * Takes a path to load and merge into the master ResourceTable. If override
1144 * is true,
1145 * conflicting resources are allowed to override each other, in order of last
1146 * seen.
1147 *
1148 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1149 * as ZIP archive
1150 * and the files within are merged individually.
1151 *
1152 * Otherwise the files is processed on its own.
1153 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001154 bool MergePath(const std::string& path, bool override) {
1155 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1156 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1157 return MergeArchive(path, override);
1158 } else if (util::EndsWith(path, ".apk")) {
1159 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001160 }
1161
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001162 io::IFile* file = file_collection_->InsertFile(path);
1163 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001164 }
1165
1166 /**
1167 * Takes a file to load and merge into the master ResourceTable. If override
1168 * is true,
1169 * conflicting resources are allowed to override each other, in order of last
1170 * seen.
1171 *
1172 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1173 * merged into the
1174 * master ResourceTable. If the file ends with .flat, then it is treated like
1175 * a compiled file
1176 * and the header data is read and merged into the final ResourceTable.
1177 *
1178 * All other file types are ignored. This is because these files could be
1179 * coming from a zip,
1180 * where we could have other files like classes.dex.
1181 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001182 bool MergeFile(io::IFile* file, bool override) {
1183 const Source& src = file->GetSource();
1184 if (util::EndsWith(src.path, ".arsc.flat")) {
1185 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001186
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001187 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001188 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001189 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001190 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001191 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001192 return false;
1193 }
1194
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001195 CompiledFileInputStream input_stream(data->data(), data->size());
1196 uint32_t num_files = 0;
1197 if (!input_stream.ReadLittleEndian32(&num_files)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001198 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed read num files");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001199 return false;
1200 }
1201
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001202 for (uint32_t i = 0; i < num_files; i++) {
1203 pb::CompiledFile compiled_file;
1204 if (!input_stream.ReadCompiledFile(&compiled_file)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001205 context_->GetDiagnostics()->Error(DiagMessage(src)
1206 << "failed to read compiled file header");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001207 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001208 }
1209
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001210 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001211 if (!input_stream.ReadDataMetaData(&offset, &len)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001212 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to read data meta data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001213 return false;
1214 }
1215
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001216 std::unique_ptr<ResourceFile> resource_file = DeserializeCompiledFileFromPb(
1217 compiled_file, file->GetSource(), context_->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001218 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001219 return false;
1220 }
1221
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001222 if (!MergeCompiledFile(file->CreateFileSegment(offset, len), resource_file.get(),
1223 override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001224 return false;
1225 }
1226 }
1227 return true;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001228 } else if (util::EndsWith(src.path, ".xml") || util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001229 // Since AAPT compiles these file types and appends .flat to them, seeing
1230 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001231 const StringPiece file_type = util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1232 context_->GetDiagnostics()->Error(DiagMessage(src) << "uncompiled " << file_type
1233 << " file passed as argument. Must be "
1234 "compiled first into .flat file.");
Adam Lesinski6a396c12016-10-20 14:38:23 -07001235 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001236 }
1237
1238 // Ignore non .flat files. This could be classes.dex or something else that
1239 // happens
1240 // to be in an archive.
1241 return true;
1242 }
1243
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001244 bool CopyAssetsDirsToApk(IArchiveWriter* writer) {
1245 std::map<std::string, std::unique_ptr<io::RegularFile>> merged_assets;
1246 for (const std::string& assets_dir : options_.assets_dirs) {
1247 Maybe<std::vector<std::string>> files =
1248 file::FindFiles(assets_dir, context_->GetDiagnostics(), nullptr);
1249 if (!files) {
1250 return false;
1251 }
1252
1253 for (const std::string& file : files.value()) {
1254 std::string full_key = "assets/" + file;
1255 std::string full_path = assets_dir;
1256 file::AppendPath(&full_path, file);
1257
1258 auto iter = merged_assets.find(full_key);
1259 if (iter == merged_assets.end()) {
1260 merged_assets.emplace(std::move(full_key),
1261 util::make_unique<io::RegularFile>(Source(std::move(full_path))));
1262 } else if (context_->IsVerbose()) {
1263 context_->GetDiagnostics()->Warn(DiagMessage(iter->second->GetSource())
1264 << "asset file overrides '" << full_path << "'");
1265 }
1266 }
1267 }
1268
1269 for (auto& entry : merged_assets) {
1270 uint32_t compression_flags = ArchiveEntry::kCompress;
1271 std::string extension = file::GetExtension(entry.first).to_string();
1272 if (options_.extensions_to_not_compress.count(extension) > 0) {
1273 compression_flags = 0u;
1274 }
1275
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001276 if (!io::CopyFileToArchive(context_, entry.second.get(), entry.first, compression_flags,
1277 writer)) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001278 return false;
1279 }
1280 }
1281 return true;
1282 }
1283
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001284 /**
1285 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001286 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001287 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001288 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set, xml::XmlResource* manifest,
1289 ResourceTable* table) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001290 const bool keep_raw_values = options_.package_type == PackageType::kStaticLib;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001291 bool result =
1292 FlattenXml(manifest, "AndroidManifest.xml", {}, keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001293 if (!result) {
1294 return false;
1295 }
1296
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001297 ResourceFileFlattenerOptions file_flattener_options;
1298 file_flattener_options.keep_raw_values = keep_raw_values;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001299 file_flattener_options.do_not_compress_anything = options_.do_not_compress_anything;
1300 file_flattener_options.extensions_to_not_compress = options_.extensions_to_not_compress;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001301 file_flattener_options.no_auto_version = options_.no_auto_version;
1302 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001303 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001304 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1305 file_flattener_options.update_proguard_spec =
1306 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001307
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001308 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001310 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001311 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001312 return false;
1313 }
1314
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001315 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001316 if (!FlattenTableToPb(table, writer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001317 return false;
1318 }
1319 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001320 if (!FlattenTable(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001321 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001322 return false;
1323 }
1324 }
1325 return true;
1326 }
1327
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001328 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001329 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001330 std::unique_ptr<xml::XmlResource> manifest_xml =
1331 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1332 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001333 return 1;
1334 }
1335
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001336 // First extract the Package name without modifying it (via --rename-manifest-package).
1337 if (Maybe<AppInfo> maybe_app_info =
1338 ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001339 const AppInfo& app_info = maybe_app_info.value();
1340 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001341 }
1342
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001343 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1344 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001345 return 1;
1346 }
1347
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001348 Maybe<AppInfo> maybe_app_info =
1349 ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001350 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001351 return 1;
1352 }
1353
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001354 const AppInfo& app_info = maybe_app_info.value();
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001355 context_->SetMinSdkVersion(app_info.min_sdk_version.value_or_default(0));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001356
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001357 context_->SetNameManglerPolicy(NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001358
1359 // Override the package ID when it is "android".
1360 if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001361 context_->SetPackageId(0x01);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001362
1363 // Verify we're building a regular app.
1364 if (options_.package_type != PackageType::kApp) {
1365 context_->GetDiagnostics()->Error(
1366 DiagMessage() << "package 'android' can only be built as a regular app");
1367 return 1;
1368 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001369 }
1370
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001371 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001372 return 1;
1373 }
1374
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001375 TableMergerOptions table_merger_options;
1376 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001377 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_, table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001379 if (context_->IsVerbose()) {
1380 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001381 << StringPrintf("linking package '%s' using package ID %02x",
1382 context_->GetCompilationPackage().data(),
1383 context_->GetPackageId()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001384 }
1385
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001386 for (const std::string& input : input_files) {
1387 if (!MergePath(input, false)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001388 context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing input");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001389 return 1;
1390 }
1391 }
1392
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001393 for (const std::string& input : options_.overlay_files) {
1394 if (!MergePath(input, true)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001395 context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing overlays");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001396 return 1;
1397 }
1398 }
1399
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001401 return 1;
1402 }
1403
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001404 if (options_.package_type != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001405 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001406 if (!mover.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001407 context_->GetDiagnostics()->Error(DiagMessage() << "failed moving private attributes");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001408 return 1;
1409 }
1410
1411 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001412 IdAssigner id_assigner(&options_.stable_id_map);
1413 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001414 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001415 return 1;
1416 }
1417
1418 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001419 if (options_.resource_id_map_path) {
1420 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001421 for (auto& type : package->types) {
1422 for (auto& entry : type->entries) {
1423 ResourceName name(package->name, type->type, entry->name);
1424 // The IDs are guaranteed to exist.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001425 options_.stable_id_map[std::move(name)] =
1426 ResourceId(package->id.value(), type->id.value(), entry->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001427 }
1428 }
1429 }
1430
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001431 if (!WriteStableIdMapToPath(context_->GetDiagnostics(), options_.stable_id_map,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001432 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001433 return 1;
1434 }
1435 }
1436 } else {
1437 // Static libs are merged with other apps, and ID collisions are bad, so
1438 // verify that
1439 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001440 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001441 return 1;
1442 }
1443 }
1444
1445 // Add the names to mangle based on our source merge earlier.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001446 context_->SetNameManglerPolicy(
1447 NameManglerPolicy{context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001448
1449 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001450 context_->GetExternalSymbols()->PrependSource(
1451 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001452
1453 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001454 if (!linker.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001455 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking references");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001456 return 1;
1457 }
1458
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001459 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001460 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001461 context_->GetDiagnostics()->Warn(DiagMessage()
1462 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001463 }
1464 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001465 ProductFilter product_filter(options_.products);
1466 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001467 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001468 return 1;
1469 }
1470 }
1471
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001472 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001473 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001474 if (!versioner.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001475 context_->GetDiagnostics()->Error(DiagMessage() << "failed versioning styles");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001476 return 1;
1477 }
1478 }
1479
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001480 if (options_.package_type != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001481 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001482 context_->GetDiagnostics()->Note(DiagMessage()
1483 << "collapsing resource versions for minimum SDK "
1484 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001485 }
1486
1487 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001488 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001489 return 1;
1490 }
1491 }
1492
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001493 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001494 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001495 if (!deduper.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001496 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001497 return 1;
1498 }
1499 }
1500
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001501 proguard::KeepSet proguard_keep_set;
1502 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001503
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001504 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001505 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001506 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001507 context_->GetDiagnostics()->Warn(DiagMessage()
1508 << "can't strip resources when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001509 }
1510 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001511 // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or
1512 // equal to the minSdk.
1513 options_.split_constraints =
1514 AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001515
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001516 TableSplitter table_splitter(options_.split_constraints, options_.table_splitter_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001517 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001518 return 1;
1519 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001520 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001521
1522 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001523 auto path_iter = options_.split_paths.begin();
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001524 auto split_constraints_iter = options_.split_constraints.begin();
1525 for (std::unique_ptr<ResourceTable>& split_table : table_splitter.splits()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001526 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001527 context_->GetDiagnostics()->Note(DiagMessage(*path_iter)
1528 << "generating split with configurations '"
1529 << util::Joiner(split_constraints_iter->configs, ", ")
1530 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001531 }
1532
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001533 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(*path_iter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001534 if (!archive_writer) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001535 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001536 return 1;
1537 }
1538
1539 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001540 std::unique_ptr<xml::XmlResource> split_manifest =
1541 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001542
1543 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001544 if (!linker.Consume(context_, split_manifest.get())) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001545 context_->GetDiagnostics()->Error(DiagMessage()
1546 << "failed to create Split AndroidManifest.xml");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001547 return 1;
1548 }
1549
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001550 if (!WriteApk(archive_writer.get(), &proguard_keep_set, split_manifest.get(),
1551 split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001552 return 1;
1553 }
1554
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001555 ++path_iter;
1556 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001557 }
1558 }
1559
1560 // Start writing the base APK.
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001561 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(options_.output_path);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001562 if (!archive_writer) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001563 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001564 return 1;
1565 }
1566
1567 bool error = false;
1568 {
1569 // AndroidManifest.xml has no resource name, but the CallSite is built
1570 // from the name
1571 // (aka, which package the AndroidManifest.xml is coming from).
1572 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001573 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001574
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001575 XmlReferenceLinker manifest_linker;
1576 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1577 if (options_.generate_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001578 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1579 manifest_xml.get(), &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001580 error = true;
1581 }
1582
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001583 if (options_.generate_main_dex_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001584 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1585 manifest_xml.get(),
1586 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001587 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001588 }
1589
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001590 if (options_.generate_java_class_path) {
1591 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001592 error = true;
1593 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001594 }
1595
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001597 // PackageParser will fail if URIs are removed from
1598 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001599 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1600 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001601 error = true;
1602 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001603 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001604 } else {
1605 error = true;
1606 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001607 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001608
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001609 if (error) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001610 context_->GetDiagnostics()->Error(DiagMessage() << "failed processing manifest");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001611 return 1;
1612 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001613
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001614 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(), &final_table_)) {
1615 return 1;
1616 }
1617
1618 if (!CopyAssetsDirsToApk(archive_writer.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001619 return 1;
1620 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001621
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001622 if (options_.generate_java_class_path) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001623 // The set of packages whose R class to call in the main classes
1624 // onResourcesLoaded callback.
1625 std::vector<std::string> packages_to_callback;
1626
1627 JavaClassGeneratorOptions template_options;
1628 template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1629 template_options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001630
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001631 if (options_.package_type == PackageType::kStaticLib || options_.generate_non_final_ids) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001632 template_options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001633 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001634
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001635 if (options_.package_type == PackageType::kSharedLib) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001636 template_options.use_final = false;
1637 template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{};
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001638 }
1639
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001640 const StringPiece actual_package = context_->GetCompilationPackage();
1641 StringPiece output_package = context_->GetCompilationPackage();
1642 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001643 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001644 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001645 }
1646
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001647 // Generate the private symbols if required.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001648 if (options_.private_symbols) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001649 packages_to_callback.push_back(options_.private_symbols.value());
1650
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001651 // If we defined a private symbols package, we only emit Public symbols
1652 // to the original package, and private and public symbols to the
1653 // private package.
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001654 JavaClassGeneratorOptions options = template_options;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001655 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001656 if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001657 options)) {
1658 return 1;
1659 }
1660 }
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001661
1662 // Generate all the symbols for all extra packages.
1663 for (const std::string& extra_package : options_.extra_java_packages) {
1664 packages_to_callback.push_back(extra_package);
1665
1666 JavaClassGeneratorOptions options = template_options;
1667 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1668 if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) {
1669 return 1;
1670 }
1671 }
1672
1673 // Generate the main public R class.
1674 JavaClassGeneratorOptions options = template_options;
1675
1676 // Only generate public symbols if we have a private package.
1677 if (options_.private_symbols) {
1678 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
1679 }
1680
1681 if (options.rewrite_callback_options) {
1682 options.rewrite_callback_options.value().packages_to_callback =
1683 std::move(packages_to_callback);
1684 }
1685
1686 if (!WriteJavaFile(&final_table_, actual_package, output_package, options)) {
1687 return 1;
1688 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001689 }
1690
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001691 if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001692 return 1;
1693 }
1694
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001695 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1696 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001697 return 1;
1698 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001699 return 0;
1700 }
1701
1702 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001703 LinkOptions options_;
1704 LinkContext* context_;
1705 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001706
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001707 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001708
1709 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001710 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001711
1712 // A vector of IFileCollections. This is mainly here to keep ownership of the
1713 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001714 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001715
1716 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001717 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001718 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001719
1720 // The set of shared libraries being used, mapping their assigned package ID to package name.
1721 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001722};
1723
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001724int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001725 LinkContext context;
1726 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001727 std::vector<std::string> overlay_arg_list;
1728 std::vector<std::string> extra_java_packages;
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001729 Maybe<std::string> package_id;
Adam Lesinski113ee092017-04-03 19:38:25 -07001730 std::vector<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001731 Maybe<std::string> preferred_density;
1732 Maybe<std::string> product_list;
1733 bool legacy_x_flag = false;
1734 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001735 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001736 bool shared_lib = false;
1737 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001738 Maybe<std::string> stable_id_file_path;
1739 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001740 Flags flags =
1741 Flags()
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001742 .RequiredFlag("-o", "Output path.", &options.output_path)
1743 .RequiredFlag("--manifest", "Path to the Android manifest to build.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001744 &options.manifest_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001745 .OptionalFlagList("-I", "Adds an Android APK to link against.", &options.include_paths)
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001746 .OptionalFlagList("-A",
1747 "An assets directory to include in the APK. These are unprocessed.",
1748 &options.assets_dirs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001749 .OptionalFlagList("-R",
1750 "Compilation unit to link, using `overlay` semantics.\n"
1751 "The last conflicting resource given takes precedence.",
1752 &overlay_arg_list)
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001753 .OptionalFlag("--package-id",
1754 "Specify the package ID to use for this app. Must be greater or equal to\n"
1755 "0x7f and can't be used with --static-lib or --shared-lib.",
1756 &package_id)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001757 .OptionalFlag("--java", "Directory in which to generate R.java.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001758 &options.generate_java_class_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001759 .OptionalFlag("--proguard", "Output file for generated Proguard rules.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001760 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001761 .OptionalFlag("--proguard-main-dex",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001762 "Output file for generated Proguard rules for the main dex.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001763 &options.generate_main_dex_proguard_rules_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001764 .OptionalSwitch("--no-auto-version",
1765 "Disables automatic style and layout SDK versioning.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001766 &options.no_auto_version)
1767 .OptionalSwitch("--no-version-vectors",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001768 "Disables automatic versioning of vector drawables. Use this only\n"
1769 "when building with vector drawable support library.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001770 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001771 .OptionalSwitch("--no-version-transitions",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001772 "Disables automatic versioning of transition resources. Use this only\n"
1773 "when building with transition support library.",
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001774 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001775 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001776 "Disables automatic deduping of resources with\n"
1777 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001778 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001779 .OptionalSwitch("--enable-sparse-encoding",
1780 "Enables encoding sparse entries using a binary search tree.\n"
1781 "This decreases APK size at the cost of resource retrieval performance.",
1782 &options.table_flattener_options.use_sparse_entries)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001783 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001784 &legacy_x_flag)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001785 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001786 &require_localization)
Adam Lesinski113ee092017-04-03 19:38:25 -07001787 .OptionalFlagList("-c",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001788 "Comma separated list of configurations to include. The default\n"
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001789 "is all configurations.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001790 &configs)
1791 .OptionalFlag("--preferred-density",
1792 "Selects the closest matching density and strips out all others.",
1793 &preferred_density)
1794 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001795 .OptionalSwitch("--output-to-dir",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001796 "Outputs the APK contents to a directory specified by -o.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001797 &options.output_to_directory)
1798 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001799 "Removes XML namespace prefix and URI information from\n"
1800 "AndroidManifest.xml and XML binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001801 &options.no_xml_namespaces)
1802 .OptionalFlag("--min-sdk-version",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001803 "Default minimum SDK version to use for AndroidManifest.xml.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001804 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001805 .OptionalFlag("--target-sdk-version",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001806 "Default target SDK version to use for AndroidManifest.xml.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001807 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001808 .OptionalFlag("--version-code",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001809 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
1810 "present.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001811 &options.manifest_fixer_options.version_code_default)
1812 .OptionalFlag("--version-name",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001813 "Version name to inject into the AndroidManifest.xml if none is present.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001814 &options.manifest_fixer_options.version_name_default)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001815 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
1816 &shared_lib)
1817 .OptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001818 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001819 "Merge all library resources under the app's package.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001820 &options.no_static_lib_packages)
1821 .OptionalSwitch("--non-final-ids",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001822 "Generates R.java without the final modifier. This is implied when\n"
1823 "--static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001824 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001825 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001826 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001827 .OptionalFlag("--emit-ids",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001828 "Emit a file at the given path with a list of name to ID mappings,\n"
1829 "suitable for use with --stable-ids.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001830 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001831 .OptionalFlag("--private-symbols",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001832 "Package name to use when generating R.java for private symbols.\n"
1833 "If not specified, public and private symbols will use the application's\n"
1834 "package name.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001835 &options.private_symbols)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001836 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001837 &options.custom_java_package)
1838 .OptionalFlagList("--extra-packages",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001839 "Generate the same R.java but with different package names.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001840 &extra_java_packages)
1841 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001842 "Adds a JavaDoc annotation to all generated Java classes.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001843 &options.javadoc_annotations)
1844 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001845 "Allows the addition of new resources in overlays without\n"
1846 "<add-resource> tags.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001847 &options.auto_add_overlay)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001848 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001849 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001850 .OptionalFlag("--rename-instrumentation-target-package",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001851 "Changes the name of the target package for instrumentation. Most useful\n"
1852 "when used in conjunction with --rename-manifest-package.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001853 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001854 .OptionalFlagList("-0", "File extensions not to compress.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001855 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001856 .OptionalFlagList("--split",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001857 "Split resources matching a set of configs out to a Split APK.\n"
1858 "Syntax: path/to/output.apk:<config>[,<config>[...]].",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001859 &split_args)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001860 .OptionalSwitch("-v", "Enables verbose logging.", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001861
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001862 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001863 return 1;
1864 }
1865
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001866 // Expand all argument-files passed into the command line. These start with '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001867 std::vector<std::string> arg_list;
1868 for (const std::string& arg : flags.GetArgs()) {
1869 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001870 const std::string path = arg.substr(1, arg.size() - 1);
1871 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001872 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
1873 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001874 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001875 }
1876 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001877 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001878 }
1879 }
1880
1881 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001882 for (const std::string& arg : overlay_arg_list) {
1883 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001884 const std::string path = arg.substr(1, arg.size() - 1);
1885 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001886 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
1887 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001888 return 1;
1889 }
1890 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001891 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001892 }
1893 }
1894
1895 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001896 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001897 }
1898
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001899 if (shared_lib && static_lib) {
1900 context.GetDiagnostics()->Error(DiagMessage()
1901 << "only one of --shared-lib and --static-lib can be defined");
1902 return 1;
1903 }
1904
1905 if (shared_lib) {
1906 options.package_type = PackageType::kSharedLib;
1907 context.SetPackageId(0x00);
1908 } else if (static_lib) {
1909 options.package_type = PackageType::kStaticLib;
1910 context.SetPackageId(kAppPackageId);
1911 } else {
1912 options.package_type = PackageType::kApp;
1913 context.SetPackageId(kAppPackageId);
1914 }
1915
1916 if (package_id) {
1917 if (options.package_type != PackageType::kApp) {
1918 context.GetDiagnostics()->Error(
1919 DiagMessage() << "can't specify --package-id when not building a regular app");
1920 return 1;
1921 }
1922
1923 const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id.value());
1924 if (!maybe_package_id_int) {
1925 context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id.value()
1926 << "' is not a valid integer");
1927 return 1;
1928 }
1929
1930 const uint32_t package_id_int = maybe_package_id_int.value();
1931 if (package_id_int < kAppPackageId || package_id_int > std::numeric_limits<uint8_t>::max()) {
1932 context.GetDiagnostics()->Error(
1933 DiagMessage() << StringPrintf(
1934 "invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int));
1935 return 1;
1936 }
1937 context.SetPackageId(static_cast<uint8_t>(package_id_int));
1938 }
1939
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001940 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001941 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001942 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001943 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001944 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001945 }
1946 }
1947
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001948 if (product_list) {
1949 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001950 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001951 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001952 }
1953 }
1954 }
1955
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001956 std::unique_ptr<IConfigFilter> filter;
Mihai Nitaf4dacf22017-04-07 08:25:06 -07001957 if (!configs.empty()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001958 filter = ParseConfigFilterParameters(configs, context.GetDiagnostics());
1959 if (filter == nullptr) {
1960 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001961 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001962 options.table_splitter_options.config_filter = filter.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001963 }
1964
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001965 if (preferred_density) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001966 Maybe<uint16_t> density =
1967 ParseTargetDensityParameter(preferred_density.value(), context.GetDiagnostics());
1968 if (!density) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001969 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07001970 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001971 options.table_splitter_options.preferred_densities.push_back(density.value());
1972 }
Adam Lesinskic51562c2016-04-28 11:12:38 -07001973
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001974 // Parse the split parameters.
1975 for (const std::string& split_arg : split_args) {
1976 options.split_paths.push_back({});
1977 options.split_constraints.push_back({});
1978 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(), &options.split_paths.back(),
1979 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001980 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07001981 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001982 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07001983
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001984 if (options.package_type != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001985 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
1986 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001987 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08001988 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001989 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001990
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001991 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001992 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001993 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
1994 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
1995 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
1996 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
1997
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001998 // Turn off auto versioning for static-libs.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001999 if (options.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002000 options.no_auto_version = true;
2001 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002002 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002003 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002004
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002005 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002006 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002007}
2008
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002009} // namespace aapt