blob: 3d76439d7a727ead192181437f1d1f340ca0d618 [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;
Adam Lesinski418763f2017-04-11 17:36:53 -070090 Maybe<std::string> generate_text_symbols_path;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 Maybe<std::string> generate_proguard_rules_path;
92 Maybe<std::string> generate_main_dex_proguard_rules_path;
93 bool generate_non_final_ids = false;
94 std::vector<std::string> javadoc_annotations;
95 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070096
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 // Optimizations/features.
98 bool no_auto_version = false;
99 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900100 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 bool no_resource_deduping = false;
102 bool no_xml_namespaces = false;
103 bool do_not_compress_anything = false;
104 std::unordered_set<std::string> extensions_to_not_compress;
105
106 // Static lib options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 bool no_static_lib_packages = false;
108
109 // AndroidManifest.xml massaging options.
110 ManifestFixerOptions manifest_fixer_options;
111
112 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700114
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800115 // Flattening options.
116 TableFlattenerOptions table_flattener_options;
117
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 TableSplitterOptions table_splitter_options;
120 std::vector<SplitConstraints> split_constraints;
121 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700122
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 std::unordered_map<ResourceName, ResourceId> stable_id_map;
125 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126};
127
Adam Lesinski64587af2016-02-18 18:33:06 -0800128class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 public:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700130 LinkContext() : name_mangler_({}), symbols_(&name_mangler_) {
131 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700133 IDiagnostics* GetDiagnostics() override {
134 return &diagnostics_;
135 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700137 NameMangler* GetNameMangler() override {
138 return &name_mangler_;
139 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
142 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 const std::string& GetCompilationPackage() override {
146 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700148
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800150 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800152
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700153 uint8_t GetPackageId() override {
154 return package_id_;
155 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700157 void SetPackageId(uint8_t id) {
158 package_id_ = id;
159 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800160
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700161 SymbolTable* GetExternalSymbols() override {
162 return &symbols_;
163 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800164
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700165 bool IsVerbose() override {
166 return verbose_;
167 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800168
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700169 void SetVerbose(bool val) {
170 verbose_ = val;
171 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800172
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700173 int GetMinSdkVersion() override {
174 return min_sdk_version_;
175 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700176
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700177 void SetMinSdkVersion(int minSdk) {
178 min_sdk_version_ = minSdk;
179 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700180
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 DISALLOW_COPY_AND_ASSIGN(LinkContext);
183
184 StdErrDiagnostics diagnostics_;
185 NameMangler name_mangler_;
186 std::string compilation_package_;
187 uint8_t package_id_ = 0x0;
188 SymbolTable symbols_;
189 bool verbose_ = false;
190 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700191};
192
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700194 Maybe<size_t> max_sdk_level, bool keep_raw_values, IArchiveWriter* writer,
195 IAaptContext* context) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 BigBuffer buffer(1024);
197 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 options.keep_raw_values = keep_raw_values;
199 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800202 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 }
204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 DiagMessage msg;
207 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 if (max_sdk_level) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700209 msg << " maxSdkLevel=" << max_sdk_level.value() << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 }
213
Adam Lesinski06460ef2017-03-14 18:52:13 -0700214 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700215 return io::CopyInputStreamToArchive(context, &input_stream, path.to_string(),
216 ArchiveEntry::kCompress, writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800217}
218
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700219static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source, const void* data,
220 size_t len, IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 pb::ResourceTable pb_table;
222 if (!pb_table.ParseFromArray(data, len)) {
223 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 return {};
225 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800226
Adam Lesinski06460ef2017-03-14 18:52:13 -0700227 std::unique_ptr<ResourceTable> table = DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 if (!table) {
229 return {};
230 }
231 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800232}
233
234/**
235 * Inflates an XML file from the source path.
236 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700237static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 std::ifstream fin(path, std::ifstream::binary);
239 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 return {};
242 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800244}
245
Adam Lesinski355f2852016-02-13 20:26:45 -0800246struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 bool no_auto_version = false;
248 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900249 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 bool no_xml_namespaces = false;
251 bool keep_raw_values = false;
252 bool do_not_compress_anything = false;
253 bool update_proguard_spec = false;
254 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800255};
256
257class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 public:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700259 ResourceFileFlattener(const ResourceFileFlattenerOptions& options, IAaptContext* context,
260 proguard::KeepSet* keep_set)
261 : options_(options), context_(context), keep_set_(keep_set) {
262 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800263
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800265
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 private:
267 struct FileOperation {
268 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700269
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 // The entry this file came from.
271 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700272
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700275
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700278
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 std::string dst_path;
281 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800283
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
287 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800288
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 ResourceFileFlattenerOptions options_;
290 IAaptContext* context_;
291 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800292};
293
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
295 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 return 0;
297 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800298
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 for (const std::string& extension : options_.extensions_to_not_compress) {
300 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800302 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 }
304 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800305}
306
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900307static bool IsTransitionElement(const std::string& name) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700308 return name == "fade" || name == "changeBounds" || name == "slide" || name == "explode" ||
309 name == "changeImageTransform" || name == "changeTransform" ||
310 name == "changeClipBounds" || name == "autoTransition" || name == "recolor" ||
311 name == "changeScroll" || name == "transitionSet" || name == "transition" ||
312 name == "transitionManager";
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900313}
314
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700315bool ResourceFileFlattener::LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
316 std::queue<FileOperation>* out_file_op_queue) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700319
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 if (context_->IsVerbose()) {
321 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 }
323
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 XmlReferenceLinker xml_linker;
325 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 return false;
327 }
328
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700329 if (options_.update_proguard_spec && !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 return false;
331 }
332
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333 if (options_.no_xml_namespaces) {
334 XmlNamespaceRemover namespace_remover;
335 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800337 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800339
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 if (!options_.no_auto_version) {
341 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 xml::Element* el = xml::FindRootElement(doc);
344 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 if (el->name == "vector" || el->name == "animated-vector") {
346 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700347 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700349 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700351 }
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900352 if (options_.no_version_transitions) {
353 // Skip this if it is a transition resource.
354 xml::Element* el = xml::FindRootElement(doc);
355 if (el && el->namespace_uri.empty()) {
356 if (IsTransitionElement(el->name)) {
357 // We are NOT going to version this file.
358 file_op->skip_version = true;
359 return true;
360 }
361 }
362 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700363
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365
366 // Find the first SDK level used that is higher than this defined config and
367 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 const int min_sdk_version = context_->GetMinSdkVersion();
369 for (int sdk_level : xml_linker.sdk_levels()) {
370 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700371 if (!ShouldGenerateVersionedResource(file_op->entry, config, sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 // If we shouldn't generate a versioned resource, stop checking.
373 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800374 }
375
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376 ResourceFile versioned_file_desc = doc->file;
377 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700378
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 FileOperation new_file_op;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700380 new_file_op.xml_to_flatten =
381 util::make_unique<xml::XmlResource>(versioned_file_desc, doc->root->Clone());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 new_file_op.config = versioned_file_desc.config;
383 new_file_op.entry = file_op->entry;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700384 new_file_op.dst_path =
385 ResourceUtils::BuildResourceFileName(versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800386
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700388 context_->GetDiagnostics()->Note(DiagMessage(versioned_file_desc.source)
389 << "auto-versioning resource from config '" << config
390 << "' -> '" << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800391 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 bool added = table->AddFileReferenceAllowMangled(
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700394 versioned_file_desc.name, versioned_file_desc.config, versioned_file_desc.source,
395 new_file_op.dst_path, nullptr, context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 if (!added) {
397 return false;
398 }
399
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 break;
402 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800403 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 }
405 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800406}
407
408/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 * Do not insert or remove any resources while executing in this function. It
410 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800411 * corrupt the iteration order.
412 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700413bool ResourceFileFlattener::Flatten(ResourceTable* table, IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 bool error = false;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700415 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation> config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800416
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 for (auto& pkg : table->packages) {
418 for (auto& type : pkg->types) {
419 // Sort by config and name, so that we get better locality in the zip
420 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 config_sorted_files.clear();
422 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800423
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 // Populate the queue with all files in the ResourceTable.
425 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426 for (auto& config_value : entry->values) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700427 FileReference* file_ref = ValueCast<FileReference>(config_value->value.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700429 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700431
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700436 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437 }
438
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 FileOperation file_op;
440 file_op.entry = entry.get();
441 file_op.dst_path = *file_ref->path;
442 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700445 if (type->type != ResourceType::kRaw &&
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700446 (util::EndsWith(src_path, ".xml.flat") || util::EndsWith(src_path, ".xml"))) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 << "failed to open file");
451 return false;
452 }
453
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700454 file_op.xml_to_flatten = xml::Inflate(data->data(), data->size(),
455 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 return false;
459 }
460
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 file_op.xml_to_flatten->file.config = config_value->config;
462 file_op.xml_to_flatten->file.source = file_ref->GetSource();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700463 file_op.xml_to_flatten->file.name = ResourceName(pkg->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464
465 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700466 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469
470 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 // else we end up copying the string in the std::make_pair() method,
472 // then creating a StringPiece from the copy, which would cause us
473 // to end up referencing garbage in the map.
474 const StringPiece entry_name(entry->name);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700475 config_sorted_files[std::make_pair(config_value->config, entry_name)] =
476 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 }
478 }
479 }
480
481 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 for (; !file_operations.empty(); file_operations.pop()) {
483 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700485 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 error = true;
487 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700488 }
489
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
491 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 // creating a StringPiece from the copy, which would cause us to end up
493 // referencing garbage in the map.
494 const StringPiece entry_name(file_op.entry->name);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700495 config_sorted_files[std::make_pair(file_op.config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 }
497
498 if (error) {
499 return false;
500 }
501
502 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700503 for (auto& map_entry : config_sorted_files) {
504 const ConfigDescription& config = map_entry.first.first;
505 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 if (file_op.xml_to_flatten) {
508 Maybe<size_t> max_sdk_level;
509 if (!options_.no_auto_version && !file_op.skip_version) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700510 max_sdk_level = std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
511 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 }
513
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700514 bool result = FlattenXml(file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
515 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 if (!result) {
517 error = true;
518 }
519 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700520 bool result =
521 io::CopyFileToArchive(context_, file_op.file_to_copy, file_op.dst_path,
522 GetCompressionFlags(file_op.dst_path), archive_writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 if (!result) {
524 error = true;
525 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700526 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700528 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700529 }
530 return !error;
531}
532
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700533static bool WriteStableIdMapToPath(IDiagnostics* diag,
534 const std::unordered_map<ResourceName, ResourceId>& id_map,
535 const std::string& id_map_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700536 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700538 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 return false;
540 }
541
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 const ResourceName& name = entry.first;
544 const ResourceId& id = entry.second;
545 fout << name << " = " << id << "\n";
546 }
547
548 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700549 diag->Error(DiagMessage(id_map_path) << "failed writing to file: "
550 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 return false;
552 }
553
554 return true;
555}
556
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700557static bool LoadStableIdMap(IDiagnostics* diag, const std::string& path,
558 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 std::string content;
560 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700562 return false;
563 }
564
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 out_id_map->clear();
566 size_t line_no = 0;
567 for (StringPiece line : util::Tokenize(content, '\n')) {
568 line_no++;
569 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 if (line.empty()) {
571 continue;
572 }
573
574 auto iter = std::find(line.begin(), line.end(), '=');
575 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700577 return false;
578 }
579
580 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 StringPiece res_name_str =
582 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
583 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700584 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource name '" << res_name_str
585 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586 return false;
587 }
588
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
590 const size_t res_id_str_len = line.size() - res_id_start_idx;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700591 StringPiece res_id_str = util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
594 if (!maybe_id) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700595 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '" << res_id_str
596 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 return false;
598 }
599
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700600 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 }
602 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700603}
604
Adam Lesinskifb48d292015-11-07 15:52:13 -0800605class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606 public:
607 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700608 : options_(options),
609 context_(context),
610 final_table_(),
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700611 file_collection_(util::make_unique<io::FileCollection>()) {
612 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700613
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700614 /**
615 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700616 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 bool LoadSymbolsFromIncludePaths() {
619 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 for (const std::string& path : options_.include_paths) {
622 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700623 context_->GetDiagnostics()->Note(DiagMessage(path) << "loading include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700624 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700625
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 std::string error_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800628 std::unique_ptr<ResourceTable> include_static = LoadStaticLibrary(path, &error_str);
629 if (include_static) {
630 if (options_.package_type != PackageType::kStaticLib) {
631 // Can't include static libraries when not building a static library (they have no IDs
632 // assigned).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 context_->GetDiagnostics()->Error(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800634 DiagMessage(path) << "can't include static library when not building a static lib");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 return false;
636 }
637
638 // If we are using --no-static-lib-packages, we need to rename the
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800639 // package of this table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700640 if (options_.no_static_lib_packages) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800641 // Since package names can differ, and multiple packages can exist in a ResourceTable,
642 // we place the requirement that all static libraries are built with the package
643 // ID 0x7f. So if one is not found, this is an error.
644 if (ResourceTablePackage* pkg = include_static->FindPackageById(kAppPackageId)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 pkg->name = context_->GetCompilationPackage();
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800646 } else {
647 context_->GetDiagnostics()->Error(DiagMessage(path)
648 << "no package with ID 0x7f found in static library");
649 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700650 }
651 }
652
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700653 context_->GetExternalSymbols()->AppendSource(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800654 util::make_unique<ResourceTableSymbolSource>(include_static.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800656 static_table_includes_.push_back(std::move(include_static));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700658 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 return false;
662 }
663
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 if (!asset_source->AddAssetPath(path)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800665 context_->GetDiagnostics()->Error(DiagMessage(path) << "failed to load include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 return false;
667 }
668 }
669
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800670 // Capture the shared libraries so that the final resource table can be properly flattened
671 // with support for shared libraries.
672 for (auto& entry : asset_source->GetAssignedPackageIds()) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800673 if (entry.first > kFrameworkPackageId && entry.first < kAppPackageId) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800674 final_table_.included_packages_[entry.first] = entry.second;
675 }
676 }
677
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 return true;
680 }
681
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800682 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800684 xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
685 if (manifest_el == nullptr) {
686 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 }
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800688
689 AppInfo app_info;
690
691 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
692 diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>");
693 return {};
694 }
695
696 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
697 if (!package_attr) {
698 diag->Error(DiagMessage(xml_res->file.source)
699 << "<manifest> must have a 'package' attribute");
700 return {};
701 }
702 app_info.package = package_attr->value;
703
704 if (xml::Attribute* version_code_attr =
705 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
706 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_attr->value);
707 if (!maybe_code) {
708 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
709 << "invalid android:versionCode '" << version_code_attr->value << "'");
710 return {};
711 }
712 app_info.version_code = maybe_code.value();
713 }
714
715 if (xml::Attribute* revision_code_attr =
716 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
717 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(revision_code_attr->value);
718 if (!maybe_code) {
719 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
720 << "invalid android:revisionCode '" << revision_code_attr->value << "'");
721 return {};
722 }
723 app_info.revision_code = maybe_code.value();
724 }
725
726 if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
727 if (!split_name_attr->value.empty()) {
728 app_info.split_name = split_name_attr->value;
729 }
730 }
731
732 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
733 if (xml::Attribute* min_sdk =
734 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700735 app_info.min_sdk_version = ResourceUtils::ParseSdkVersion(min_sdk->value);
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800736 }
737 }
738 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700739 }
740
741 /**
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800742 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700744 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700745 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700746 bool VerifyNoExternalPackages() {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700747 auto is_ext_package_func = [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
749 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 };
751
752 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700753 for (const auto& package : final_table_.packages) {
754 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 // We have a package that is not related to the one we're building!
756 for (const auto& type : package->types) {
757 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700758 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700759
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700762 // for the 'android' package. This is due to legacy reasons.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700763 if (ValueCast<Id>(config_value->value.get()) && package->name == "android") {
764 context_->GetDiagnostics()->Warn(DiagMessage(config_value->value->GetSource())
765 << "generated id '" << res_name
766 << "' for external package '" << package->name
767 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700769 context_->GetDiagnostics()->Error(DiagMessage(config_value->value->GetSource())
770 << "defined resource '" << res_name
771 << "' for external package '" << package->name
772 << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700773 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700774 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700775 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700776 }
777 }
778 }
779 }
780
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700781 auto new_end_iter = std::remove_if(final_table_.packages.begin(), final_table_.packages.end(),
782 is_ext_package_func);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700783 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700784 return !error;
785 }
786
787 /**
788 * Returns true if no IDs have been set, false otherwise.
789 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700790 bool VerifyNoIdsSet() {
791 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700792 for (const auto& type : package->types) {
793 if (type->id) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800794 context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID "
795 << StringPrintf("%02x", type->id.value())
796 << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700797 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700798 }
799
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700800 for (const auto& entry : type->entries) {
801 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700802 ResourceNameRef res_name(package->name, type->type, entry->name);
803 context_->GetDiagnostics()->Error(
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800804 DiagMessage() << "entry " << res_name << " has ID "
805 << StringPrintf("%02x", entry->id.value()) << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806 return false;
807 }
808 }
809 }
810 }
811 return true;
812 }
813
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700814 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
815 if (options_.output_to_directory) {
816 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700818 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700819 }
820 }
821
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700822 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700823 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800824 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700825 if (!flattener.Consume(context_, table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700826 context_->GetDiagnostics()->Error(DiagMessage() << "failed to flatten resource table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 return false;
828 }
829
Adam Lesinski06460ef2017-03-14 18:52:13 -0700830 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700831 return io::CopyInputStreamToArchive(context_, &input_stream, "resources.arsc",
832 ArchiveEntry::kAlign, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 }
834
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700835 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700836 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700837 return io::CopyProtoToArchive(context_, pb_table.get(), "resources.arsc.flat", 0, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838 }
839
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700840 bool WriteJavaFile(ResourceTable* table, const StringPiece& package_name_to_generate,
Adam Lesinski418763f2017-04-11 17:36:53 -0700841 const StringPiece& out_package, const JavaClassGeneratorOptions& java_options,
842 const Maybe<std::string> out_text_symbols_path = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700843 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 return true;
845 }
846
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700847 std::string out_path = options_.generate_java_class_path.value();
848 file::AppendPath(&out_path, file::PackageToPath(out_package));
849 if (!file::mkdirs(out_path)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700850 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path
851 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700852 return false;
853 }
854
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700855 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700856
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700858 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700859 context_->GetDiagnostics()->Error(DiagMessage()
860 << "failed writing to '" << out_path
861 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700862 return false;
863 }
864
Adam Lesinski418763f2017-04-11 17:36:53 -0700865 std::unique_ptr<std::ofstream> fout_text;
866 if (out_text_symbols_path) {
867 fout_text =
868 util::make_unique<std::ofstream>(out_text_symbols_path.value(), std::ofstream::binary);
869 if (!*fout_text) {
870 context_->GetDiagnostics()->Error(
871 DiagMessage() << "failed writing to '" << out_text_symbols_path.value()
872 << "': " << android::base::SystemErrorCodeToString(errno));
873 return false;
874 }
875 }
876
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700877 JavaClassGenerator generator(context_, table, java_options);
Adam Lesinski418763f2017-04-11 17:36:53 -0700878 if (!generator.Generate(package_name_to_generate, out_package, &fout, fout_text.get())) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700879 context_->GetDiagnostics()->Error(DiagMessage(out_path) << generator.getError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 return false;
881 }
882
883 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700884 context_->GetDiagnostics()->Error(DiagMessage()
885 << "failed writing to '" << out_path
886 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700887 }
888 return true;
889 }
890
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700891 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
892 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700893 return true;
894 }
895
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700896 std::unique_ptr<ClassDefinition> manifest_class =
897 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700898
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700899 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700900 // Something bad happened, but we already logged it, so exit.
901 return false;
902 }
903
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700904 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700905 // Empty Manifest class, no need to generate it.
906 return true;
907 }
908
909 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700910 for (const std::string& annotation : options_.javadoc_annotations) {
911 std::string proper_annotation = "@";
912 proper_annotation += annotation;
913 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700914 }
915
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700916 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700917
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700918 std::string out_path = options_.generate_java_class_path.value();
919 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700920
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700921 if (!file::mkdirs(out_path)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700922 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path
923 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700924 return false;
925 }
926
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700927 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700928
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700929 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700930 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700931 context_->GetDiagnostics()->Error(DiagMessage()
932 << "failed writing to '" << out_path
933 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700934 return false;
935 }
936
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700937 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8, true, &fout)) {
938 context_->GetDiagnostics()->Error(DiagMessage()
939 << "failed writing to '" << out_path
940 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700941 return false;
942 }
943 return true;
944 }
945
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700946 bool WriteProguardFile(const Maybe<std::string>& out, const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700947 if (!out) {
948 return true;
949 }
950
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700951 const std::string& out_path = out.value();
952 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700954 context_->GetDiagnostics()->Error(DiagMessage()
955 << "failed to open '" << out_path
956 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700957 return false;
958 }
959
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700960 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700961 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700962 context_->GetDiagnostics()->Error(DiagMessage()
963 << "failed writing to '" << out_path
964 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700965 return false;
966 }
967 return true;
968 }
969
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700970 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
971 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700972 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700973 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700974 if (!collection) {
975 return {};
976 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700977 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700978 }
979
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700980 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700981 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700982 if (!file) {
983 return {};
984 }
985
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700986 std::unique_ptr<io::IData> data = file->OpenAsData();
987 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
988 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700989 }
990
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700991 bool MergeStaticLibrary(const std::string& input, bool override) {
992 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700993 context_->GetDiagnostics()->Note(DiagMessage() << "merging static library " << input);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700994 }
995
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700996 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700997 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700998 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700999 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001000 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001001 return false;
1002 }
1003
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001004 std::unique_ptr<ResourceTable> table = LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001005 if (!table) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001006 context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007 return false;
1008 }
1009
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001010 ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001011 if (!pkg) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001012 context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001013 return false;
1014 }
1015
1016 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001018 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001019 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001020
1021 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001022 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001023 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001024 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001025 }
1026
1027 pkg->name = "";
1028 if (override) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001029 result = table_merger_->MergeOverlay(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001030 } else {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001031 result = table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001032 }
1033
1034 } else {
1035 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001036 // preserved and resource names are mangled.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001037 result =
1038 table_merger_->MergeAndMangle(Source(input), pkg->name, table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039 }
1040
1041 if (!result) {
1042 return false;
1043 }
1044
1045 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001046 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001047 return true;
1048 }
1049
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001050 bool MergeResourceTable(io::IFile* file, bool override) {
1051 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001052 context_->GetDiagnostics()->Note(DiagMessage() << "merging resource table "
1053 << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001054 }
1055
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001056 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001057 if (!data) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001058 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001059 return false;
1060 }
1061
1062 std::unique_ptr<ResourceTable> table =
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001063 LoadTableFromPb(file->GetSource(), data->data(), data->size(), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001064 if (!table) {
1065 return false;
1066 }
1067
1068 bool result = false;
1069 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001070 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001071 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001072 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001073 }
1074 return result;
1075 }
1076
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001077 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc, bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001078 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001079 context_->GetDiagnostics()->Note(DiagMessage() << "merging '" << file_desc->name
1080 << "' from compiled file "
1081 << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001082 }
1083
1084 bool result = false;
1085 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001086 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001087 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001088 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001089 }
1090
1091 if (!result) {
1092 return false;
1093 }
1094
1095 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001096 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1097 if (exported_symbol.name.package.empty()) {
1098 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001099 }
1100
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001101 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001102
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001103 Maybe<ResourceName> mangled_name =
1104 context_->GetNameMangler()->MangleName(exported_symbol.name);
1105 if (mangled_name) {
1106 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001107 }
1108
1109 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001110 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1111 bool result = final_table_.AddResourceAllowMangled(
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001112 res_name, ConfigDescription::DefaultConfig(), std::string(), std::move(id),
1113 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001114 if (!result) {
1115 return false;
1116 }
1117 }
1118 return true;
1119 }
1120
1121 /**
1122 * Takes a path to load as a ZIP file and merges the files within into the
1123 * master ResourceTable.
1124 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001125 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001126 *
1127 * An io::IFileCollection is created from the ZIP file and added to the set of
1128 * io::IFileCollections that are open.
1129 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001130 bool MergeArchive(const std::string& input, bool override) {
1131 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001132 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive " << input);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001133 }
1134
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001135 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001136 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001137 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001138 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001139 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001140 return false;
1141 }
1142
1143 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001144 for (auto iter = collection->Iterator(); iter->HasNext();) {
1145 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001146 error = true;
1147 }
1148 }
1149
1150 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001151 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001152 return !error;
1153 }
1154
1155 /**
1156 * Takes a path to load and merge into the master ResourceTable. If override
1157 * is true,
1158 * conflicting resources are allowed to override each other, in order of last
1159 * seen.
1160 *
1161 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1162 * as ZIP archive
1163 * and the files within are merged individually.
1164 *
1165 * Otherwise the files is processed on its own.
1166 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001167 bool MergePath(const std::string& path, bool override) {
1168 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1169 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1170 return MergeArchive(path, override);
1171 } else if (util::EndsWith(path, ".apk")) {
1172 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001173 }
1174
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001175 io::IFile* file = file_collection_->InsertFile(path);
1176 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001177 }
1178
1179 /**
1180 * Takes a file to load and merge into the master ResourceTable. If override
1181 * is true,
1182 * conflicting resources are allowed to override each other, in order of last
1183 * seen.
1184 *
1185 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1186 * merged into the
1187 * master ResourceTable. If the file ends with .flat, then it is treated like
1188 * a compiled file
1189 * and the header data is read and merged into the final ResourceTable.
1190 *
1191 * All other file types are ignored. This is because these files could be
1192 * coming from a zip,
1193 * where we could have other files like classes.dex.
1194 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001195 bool MergeFile(io::IFile* file, bool override) {
1196 const Source& src = file->GetSource();
1197 if (util::EndsWith(src.path, ".arsc.flat")) {
1198 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001199
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001200 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001201 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001202 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001203 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001204 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001205 return false;
1206 }
1207
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001208 CompiledFileInputStream input_stream(data->data(), data->size());
1209 uint32_t num_files = 0;
1210 if (!input_stream.ReadLittleEndian32(&num_files)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001211 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed read num files");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001212 return false;
1213 }
1214
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001215 for (uint32_t i = 0; i < num_files; i++) {
1216 pb::CompiledFile compiled_file;
1217 if (!input_stream.ReadCompiledFile(&compiled_file)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001218 context_->GetDiagnostics()->Error(DiagMessage(src)
1219 << "failed to read compiled file header");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001220 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001221 }
1222
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001223 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001224 if (!input_stream.ReadDataMetaData(&offset, &len)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001225 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to read data meta data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001226 return false;
1227 }
1228
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001229 std::unique_ptr<ResourceFile> resource_file = DeserializeCompiledFileFromPb(
1230 compiled_file, file->GetSource(), context_->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001231 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001232 return false;
1233 }
1234
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001235 if (!MergeCompiledFile(file->CreateFileSegment(offset, len), resource_file.get(),
1236 override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001237 return false;
1238 }
1239 }
1240 return true;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001241 } else if (util::EndsWith(src.path, ".xml") || util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001242 // Since AAPT compiles these file types and appends .flat to them, seeing
1243 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001244 const StringPiece file_type = util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1245 context_->GetDiagnostics()->Error(DiagMessage(src) << "uncompiled " << file_type
1246 << " file passed as argument. Must be "
1247 "compiled first into .flat file.");
Adam Lesinski6a396c12016-10-20 14:38:23 -07001248 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001249 }
1250
1251 // Ignore non .flat files. This could be classes.dex or something else that
1252 // happens
1253 // to be in an archive.
1254 return true;
1255 }
1256
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001257 bool CopyAssetsDirsToApk(IArchiveWriter* writer) {
1258 std::map<std::string, std::unique_ptr<io::RegularFile>> merged_assets;
1259 for (const std::string& assets_dir : options_.assets_dirs) {
1260 Maybe<std::vector<std::string>> files =
1261 file::FindFiles(assets_dir, context_->GetDiagnostics(), nullptr);
1262 if (!files) {
1263 return false;
1264 }
1265
1266 for (const std::string& file : files.value()) {
1267 std::string full_key = "assets/" + file;
1268 std::string full_path = assets_dir;
1269 file::AppendPath(&full_path, file);
1270
1271 auto iter = merged_assets.find(full_key);
1272 if (iter == merged_assets.end()) {
1273 merged_assets.emplace(std::move(full_key),
1274 util::make_unique<io::RegularFile>(Source(std::move(full_path))));
1275 } else if (context_->IsVerbose()) {
1276 context_->GetDiagnostics()->Warn(DiagMessage(iter->second->GetSource())
1277 << "asset file overrides '" << full_path << "'");
1278 }
1279 }
1280 }
1281
1282 for (auto& entry : merged_assets) {
1283 uint32_t compression_flags = ArchiveEntry::kCompress;
1284 std::string extension = file::GetExtension(entry.first).to_string();
1285 if (options_.extensions_to_not_compress.count(extension) > 0) {
1286 compression_flags = 0u;
1287 }
1288
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001289 if (!io::CopyFileToArchive(context_, entry.second.get(), entry.first, compression_flags,
1290 writer)) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001291 return false;
1292 }
1293 }
1294 return true;
1295 }
1296
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001297 /**
1298 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001299 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001300 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001301 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set, xml::XmlResource* manifest,
1302 ResourceTable* table) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001303 const bool keep_raw_values = options_.package_type == PackageType::kStaticLib;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001304 bool result =
1305 FlattenXml(manifest, "AndroidManifest.xml", {}, keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306 if (!result) {
1307 return false;
1308 }
1309
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001310 ResourceFileFlattenerOptions file_flattener_options;
1311 file_flattener_options.keep_raw_values = keep_raw_values;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001312 file_flattener_options.do_not_compress_anything = options_.do_not_compress_anything;
1313 file_flattener_options.extensions_to_not_compress = options_.extensions_to_not_compress;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001314 file_flattener_options.no_auto_version = options_.no_auto_version;
1315 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001316 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001317 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1318 file_flattener_options.update_proguard_spec =
1319 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001320
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001321 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001322
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001323 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001324 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001325 return false;
1326 }
1327
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001328 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001329 if (!FlattenTableToPb(table, writer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001330 return false;
1331 }
1332 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001333 if (!FlattenTable(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001334 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001335 return false;
1336 }
1337 }
1338 return true;
1339 }
1340
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001341 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001342 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001343 std::unique_ptr<xml::XmlResource> manifest_xml =
1344 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1345 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001346 return 1;
1347 }
1348
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001349 // First extract the Package name without modifying it (via --rename-manifest-package).
1350 if (Maybe<AppInfo> maybe_app_info =
1351 ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001352 const AppInfo& app_info = maybe_app_info.value();
1353 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001354 }
1355
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001356 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1357 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001358 return 1;
1359 }
1360
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001361 Maybe<AppInfo> maybe_app_info =
1362 ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001363 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001364 return 1;
1365 }
1366
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001367 const AppInfo& app_info = maybe_app_info.value();
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001368 context_->SetMinSdkVersion(app_info.min_sdk_version.value_or_default(0));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001369
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001370 context_->SetNameManglerPolicy(NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001371
1372 // Override the package ID when it is "android".
1373 if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001374 context_->SetPackageId(0x01);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001375
1376 // Verify we're building a regular app.
1377 if (options_.package_type != PackageType::kApp) {
1378 context_->GetDiagnostics()->Error(
1379 DiagMessage() << "package 'android' can only be built as a regular app");
1380 return 1;
1381 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001382 }
1383
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001384 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001385 return 1;
1386 }
1387
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001388 TableMergerOptions table_merger_options;
1389 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001390 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_, table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001391
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001392 if (context_->IsVerbose()) {
1393 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001394 << StringPrintf("linking package '%s' using package ID %02x",
1395 context_->GetCompilationPackage().data(),
1396 context_->GetPackageId()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001397 }
1398
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001399 for (const std::string& input : input_files) {
1400 if (!MergePath(input, false)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001401 context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing input");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001402 return 1;
1403 }
1404 }
1405
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001406 for (const std::string& input : options_.overlay_files) {
1407 if (!MergePath(input, true)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001408 context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing overlays");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001409 return 1;
1410 }
1411 }
1412
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001413 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001414 return 1;
1415 }
1416
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001417 if (options_.package_type != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001418 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001419 if (!mover.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001420 context_->GetDiagnostics()->Error(DiagMessage() << "failed moving private attributes");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001421 return 1;
1422 }
1423
1424 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001425 IdAssigner id_assigner(&options_.stable_id_map);
1426 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001427 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001428 return 1;
1429 }
1430
1431 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001432 if (options_.resource_id_map_path) {
1433 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001434 for (auto& type : package->types) {
1435 for (auto& entry : type->entries) {
1436 ResourceName name(package->name, type->type, entry->name);
1437 // The IDs are guaranteed to exist.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001438 options_.stable_id_map[std::move(name)] =
1439 ResourceId(package->id.value(), type->id.value(), entry->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001440 }
1441 }
1442 }
1443
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001444 if (!WriteStableIdMapToPath(context_->GetDiagnostics(), options_.stable_id_map,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001445 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001446 return 1;
1447 }
1448 }
1449 } else {
1450 // Static libs are merged with other apps, and ID collisions are bad, so
1451 // verify that
1452 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001453 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001454 return 1;
1455 }
1456 }
1457
1458 // Add the names to mangle based on our source merge earlier.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001459 context_->SetNameManglerPolicy(
1460 NameManglerPolicy{context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001461
1462 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001463 context_->GetExternalSymbols()->PrependSource(
1464 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001465
1466 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001467 if (!linker.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001468 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking references");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001469 return 1;
1470 }
1471
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001472 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001473 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001474 context_->GetDiagnostics()->Warn(DiagMessage()
1475 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001476 }
1477 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001478 ProductFilter product_filter(options_.products);
1479 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001480 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001481 return 1;
1482 }
1483 }
1484
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001485 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001486 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001487 if (!versioner.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001488 context_->GetDiagnostics()->Error(DiagMessage() << "failed versioning styles");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001489 return 1;
1490 }
1491 }
1492
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001493 if (options_.package_type != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001494 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001495 context_->GetDiagnostics()->Note(DiagMessage()
1496 << "collapsing resource versions for minimum SDK "
1497 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001498 }
1499
1500 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001501 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001502 return 1;
1503 }
1504 }
1505
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001506 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001507 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001508 if (!deduper.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001509 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001510 return 1;
1511 }
1512 }
1513
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001514 proguard::KeepSet proguard_keep_set;
1515 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001516
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001517 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001518 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001519 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001520 context_->GetDiagnostics()->Warn(DiagMessage()
1521 << "can't strip resources when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001522 }
1523 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001524 // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or
1525 // equal to the minSdk.
1526 options_.split_constraints =
1527 AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001528
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001529 TableSplitter table_splitter(options_.split_constraints, options_.table_splitter_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001530 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001531 return 1;
1532 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001533 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001534
1535 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001536 auto path_iter = options_.split_paths.begin();
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001537 auto split_constraints_iter = options_.split_constraints.begin();
1538 for (std::unique_ptr<ResourceTable>& split_table : table_splitter.splits()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001539 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001540 context_->GetDiagnostics()->Note(DiagMessage(*path_iter)
1541 << "generating split with configurations '"
1542 << util::Joiner(split_constraints_iter->configs, ", ")
1543 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001544 }
1545
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001546 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(*path_iter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001547 if (!archive_writer) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001548 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001549 return 1;
1550 }
1551
1552 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001553 std::unique_ptr<xml::XmlResource> split_manifest =
1554 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001555
1556 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001557 if (!linker.Consume(context_, split_manifest.get())) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001558 context_->GetDiagnostics()->Error(DiagMessage()
1559 << "failed to create Split AndroidManifest.xml");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001560 return 1;
1561 }
1562
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001563 if (!WriteApk(archive_writer.get(), &proguard_keep_set, split_manifest.get(),
1564 split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001565 return 1;
1566 }
1567
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001568 ++path_iter;
1569 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001570 }
1571 }
1572
1573 // Start writing the base APK.
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001574 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(options_.output_path);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001575 if (!archive_writer) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001576 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001577 return 1;
1578 }
1579
1580 bool error = false;
1581 {
1582 // AndroidManifest.xml has no resource name, but the CallSite is built
1583 // from the name
1584 // (aka, which package the AndroidManifest.xml is coming from).
1585 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001586 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001587
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001588 XmlReferenceLinker manifest_linker;
1589 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1590 if (options_.generate_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001591 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1592 manifest_xml.get(), &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001593 error = true;
1594 }
1595
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 if (options_.generate_main_dex_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001597 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1598 manifest_xml.get(),
1599 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001600 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001601 }
1602
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001603 if (options_.generate_java_class_path) {
1604 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001605 error = true;
1606 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001607 }
1608
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001609 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001610 // PackageParser will fail if URIs are removed from
1611 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001612 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1613 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001614 error = true;
1615 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001616 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001617 } else {
1618 error = true;
1619 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001620 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001621
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001622 if (error) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001623 context_->GetDiagnostics()->Error(DiagMessage() << "failed processing manifest");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001624 return 1;
1625 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001626
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001627 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(), &final_table_)) {
1628 return 1;
1629 }
1630
1631 if (!CopyAssetsDirsToApk(archive_writer.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001632 return 1;
1633 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001634
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001635 if (options_.generate_java_class_path) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001636 // The set of packages whose R class to call in the main classes
1637 // onResourcesLoaded callback.
1638 std::vector<std::string> packages_to_callback;
1639
1640 JavaClassGeneratorOptions template_options;
1641 template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1642 template_options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001643
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001644 if (options_.package_type == PackageType::kStaticLib || options_.generate_non_final_ids) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001645 template_options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001646 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001647
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001648 if (options_.package_type == PackageType::kSharedLib) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001649 template_options.use_final = false;
1650 template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{};
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001651 }
1652
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001653 const StringPiece actual_package = context_->GetCompilationPackage();
1654 StringPiece output_package = context_->GetCompilationPackage();
1655 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001656 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001657 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001658 }
1659
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001660 // Generate the private symbols if required.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001661 if (options_.private_symbols) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001662 packages_to_callback.push_back(options_.private_symbols.value());
1663
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001664 // If we defined a private symbols package, we only emit Public symbols
1665 // to the original package, and private and public symbols to the
1666 // private package.
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001667 JavaClassGeneratorOptions options = template_options;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001668 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001669 if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001670 options)) {
1671 return 1;
1672 }
1673 }
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001674
1675 // Generate all the symbols for all extra packages.
1676 for (const std::string& extra_package : options_.extra_java_packages) {
1677 packages_to_callback.push_back(extra_package);
1678
1679 JavaClassGeneratorOptions options = template_options;
1680 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1681 if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) {
1682 return 1;
1683 }
1684 }
1685
1686 // Generate the main public R class.
1687 JavaClassGeneratorOptions options = template_options;
1688
1689 // Only generate public symbols if we have a private package.
1690 if (options_.private_symbols) {
1691 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
1692 }
1693
1694 if (options.rewrite_callback_options) {
1695 options.rewrite_callback_options.value().packages_to_callback =
1696 std::move(packages_to_callback);
1697 }
1698
Adam Lesinski418763f2017-04-11 17:36:53 -07001699 if (!WriteJavaFile(&final_table_, actual_package, output_package, options,
1700 options_.generate_text_symbols_path)) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001701 return 1;
1702 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001703 }
1704
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001705 if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001706 return 1;
1707 }
1708
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001709 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1710 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001711 return 1;
1712 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001713 return 0;
1714 }
1715
1716 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001717 LinkOptions options_;
1718 LinkContext* context_;
1719 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001720
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001721 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001722
1723 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001724 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001725
1726 // A vector of IFileCollections. This is mainly here to keep ownership of the
1727 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001728 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001729
1730 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001731 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001732 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001733
1734 // The set of shared libraries being used, mapping their assigned package ID to package name.
1735 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001736};
1737
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001738int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001739 LinkContext context;
1740 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001741 std::vector<std::string> overlay_arg_list;
1742 std::vector<std::string> extra_java_packages;
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001743 Maybe<std::string> package_id;
Adam Lesinski113ee092017-04-03 19:38:25 -07001744 std::vector<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001745 Maybe<std::string> preferred_density;
1746 Maybe<std::string> product_list;
1747 bool legacy_x_flag = false;
1748 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001749 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001750 bool shared_lib = false;
1751 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001752 Maybe<std::string> stable_id_file_path;
1753 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001754 Flags flags =
1755 Flags()
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001756 .RequiredFlag("-o", "Output path.", &options.output_path)
1757 .RequiredFlag("--manifest", "Path to the Android manifest to build.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001758 &options.manifest_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001759 .OptionalFlagList("-I", "Adds an Android APK to link against.", &options.include_paths)
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001760 .OptionalFlagList("-A",
1761 "An assets directory to include in the APK. These are unprocessed.",
1762 &options.assets_dirs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001763 .OptionalFlagList("-R",
1764 "Compilation unit to link, using `overlay` semantics.\n"
1765 "The last conflicting resource given takes precedence.",
1766 &overlay_arg_list)
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001767 .OptionalFlag("--package-id",
1768 "Specify the package ID to use for this app. Must be greater or equal to\n"
1769 "0x7f and can't be used with --static-lib or --shared-lib.",
1770 &package_id)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001771 .OptionalFlag("--java", "Directory in which to generate R.java.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001772 &options.generate_java_class_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001773 .OptionalFlag("--proguard", "Output file for generated Proguard rules.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001774 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001775 .OptionalFlag("--proguard-main-dex",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001776 "Output file for generated Proguard rules for the main dex.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001777 &options.generate_main_dex_proguard_rules_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001778 .OptionalSwitch("--no-auto-version",
1779 "Disables automatic style and layout SDK versioning.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001780 &options.no_auto_version)
1781 .OptionalSwitch("--no-version-vectors",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001782 "Disables automatic versioning of vector drawables. Use this only\n"
1783 "when building with vector drawable support library.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001784 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001785 .OptionalSwitch("--no-version-transitions",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001786 "Disables automatic versioning of transition resources. Use this only\n"
1787 "when building with transition support library.",
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001788 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001789 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001790 "Disables automatic deduping of resources with\n"
1791 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001792 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001793 .OptionalSwitch("--enable-sparse-encoding",
1794 "Enables encoding sparse entries using a binary search tree.\n"
1795 "This decreases APK size at the cost of resource retrieval performance.",
1796 &options.table_flattener_options.use_sparse_entries)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001797 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001798 &legacy_x_flag)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001799 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001800 &require_localization)
Adam Lesinski113ee092017-04-03 19:38:25 -07001801 .OptionalFlagList("-c",
Adam Lesinski418763f2017-04-11 17:36:53 -07001802 "Comma separated list of configurations to include. The default\n"
1803 "is all configurations.",
1804 &configs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001805 .OptionalFlag("--preferred-density",
1806 "Selects the closest matching density and strips out all others.",
1807 &preferred_density)
1808 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001809 .OptionalSwitch("--output-to-dir",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001810 "Outputs the APK contents to a directory specified by -o.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001811 &options.output_to_directory)
1812 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001813 "Removes XML namespace prefix and URI information from\n"
1814 "AndroidManifest.xml and XML binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001815 &options.no_xml_namespaces)
1816 .OptionalFlag("--min-sdk-version",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001817 "Default minimum SDK version to use for AndroidManifest.xml.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001818 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001819 .OptionalFlag("--target-sdk-version",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001820 "Default target SDK version to use for AndroidManifest.xml.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001821 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001822 .OptionalFlag("--version-code",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001823 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
1824 "present.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001825 &options.manifest_fixer_options.version_code_default)
1826 .OptionalFlag("--version-name",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001827 "Version name to inject into the AndroidManifest.xml if none is present.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001828 &options.manifest_fixer_options.version_name_default)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001829 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
1830 &shared_lib)
1831 .OptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001832 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001833 "Merge all library resources under the app's package.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001834 &options.no_static_lib_packages)
1835 .OptionalSwitch("--non-final-ids",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001836 "Generates R.java without the final modifier. This is implied when\n"
1837 "--static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001838 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001839 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001840 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001841 .OptionalFlag("--emit-ids",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001842 "Emit a file at the given path with a list of name to ID mappings,\n"
1843 "suitable for use with --stable-ids.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001844 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001845 .OptionalFlag("--private-symbols",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001846 "Package name to use when generating R.java for private symbols.\n"
1847 "If not specified, public and private symbols will use the application's\n"
1848 "package name.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001849 &options.private_symbols)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001850 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001851 &options.custom_java_package)
1852 .OptionalFlagList("--extra-packages",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001853 "Generate the same R.java but with different package names.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001854 &extra_java_packages)
1855 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001856 "Adds a JavaDoc annotation to all generated Java classes.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001857 &options.javadoc_annotations)
Adam Lesinski418763f2017-04-11 17:36:53 -07001858 .OptionalFlag("--output-text-symbols",
1859 "Generates a text file containing the resource symbols of the R class in\n"
1860 "the specified folder.",
1861 &options.generate_text_symbols_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001862 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001863 "Allows the addition of new resources in overlays without\n"
1864 "<add-resource> tags.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001865 &options.auto_add_overlay)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001866 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001867 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001868 .OptionalFlag("--rename-instrumentation-target-package",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001869 "Changes the name of the target package for instrumentation. Most useful\n"
1870 "when used in conjunction with --rename-manifest-package.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001871 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001872 .OptionalFlagList("-0", "File extensions not to compress.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001873 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001874 .OptionalFlagList("--split",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001875 "Split resources matching a set of configs out to a Split APK.\n"
1876 "Syntax: path/to/output.apk:<config>[,<config>[...]].",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001877 &split_args)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001878 .OptionalSwitch("-v", "Enables verbose logging.", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001879
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001880 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001881 return 1;
1882 }
1883
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001884 // Expand all argument-files passed into the command line. These start with '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001885 std::vector<std::string> arg_list;
1886 for (const std::string& arg : flags.GetArgs()) {
1887 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001888 const std::string path = arg.substr(1, arg.size() - 1);
1889 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001890 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
1891 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001892 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001893 }
1894 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001895 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001896 }
1897 }
1898
1899 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001900 for (const std::string& arg : overlay_arg_list) {
1901 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001902 const std::string path = arg.substr(1, arg.size() - 1);
1903 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001904 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
1905 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001906 return 1;
1907 }
1908 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001909 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001910 }
1911 }
1912
1913 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001914 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001915 }
1916
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001917 if (shared_lib && static_lib) {
1918 context.GetDiagnostics()->Error(DiagMessage()
1919 << "only one of --shared-lib and --static-lib can be defined");
1920 return 1;
1921 }
1922
1923 if (shared_lib) {
1924 options.package_type = PackageType::kSharedLib;
1925 context.SetPackageId(0x00);
1926 } else if (static_lib) {
1927 options.package_type = PackageType::kStaticLib;
1928 context.SetPackageId(kAppPackageId);
1929 } else {
1930 options.package_type = PackageType::kApp;
1931 context.SetPackageId(kAppPackageId);
1932 }
1933
1934 if (package_id) {
1935 if (options.package_type != PackageType::kApp) {
1936 context.GetDiagnostics()->Error(
1937 DiagMessage() << "can't specify --package-id when not building a regular app");
1938 return 1;
1939 }
1940
1941 const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id.value());
1942 if (!maybe_package_id_int) {
1943 context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id.value()
1944 << "' is not a valid integer");
1945 return 1;
1946 }
1947
1948 const uint32_t package_id_int = maybe_package_id_int.value();
1949 if (package_id_int < kAppPackageId || package_id_int > std::numeric_limits<uint8_t>::max()) {
1950 context.GetDiagnostics()->Error(
1951 DiagMessage() << StringPrintf(
1952 "invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int));
1953 return 1;
1954 }
1955 context.SetPackageId(static_cast<uint8_t>(package_id_int));
1956 }
1957
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001958 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001959 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001960 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001961 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001962 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001963 }
1964 }
1965
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001966 if (product_list) {
1967 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001968 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001969 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001970 }
1971 }
1972 }
1973
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001974 std::unique_ptr<IConfigFilter> filter;
Mihai Nitaf4dacf22017-04-07 08:25:06 -07001975 if (!configs.empty()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001976 filter = ParseConfigFilterParameters(configs, context.GetDiagnostics());
1977 if (filter == nullptr) {
1978 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001979 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001980 options.table_splitter_options.config_filter = filter.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001981 }
1982
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001983 if (preferred_density) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001984 Maybe<uint16_t> density =
1985 ParseTargetDensityParameter(preferred_density.value(), context.GetDiagnostics());
1986 if (!density) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001987 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07001988 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001989 options.table_splitter_options.preferred_densities.push_back(density.value());
1990 }
Adam Lesinskic51562c2016-04-28 11:12:38 -07001991
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001992 // Parse the split parameters.
1993 for (const std::string& split_arg : split_args) {
1994 options.split_paths.push_back({});
1995 options.split_constraints.push_back({});
1996 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(), &options.split_paths.back(),
1997 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001998 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07001999 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002000 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002001
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002002 if (options.package_type != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002003 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2004 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002005 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002006 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002007 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002008
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002009 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002010 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002011 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2012 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2013 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2014 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2015
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002016 // Turn off auto versioning for static-libs.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002017 if (options.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002018 options.no_auto_version = true;
2019 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002020 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002021 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002022
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002023 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002024 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002025}
2026
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002027} // namespace aapt