blob: 93748d358d7a7fe455f61eeca67d3d724aba037c [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
68struct LinkOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 std::string output_path;
70 std::string manifest_path;
71 std::vector<std::string> include_paths;
72 std::vector<std::string> overlay_files;
Adam Lesinskib39ad7c2017-03-13 11:40:48 -070073 std::vector<std::string> assets_dirs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 bool output_to_directory = false;
75 bool auto_add_overlay = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 // Java/Proguard options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 Maybe<std::string> generate_java_class_path;
79 Maybe<std::string> custom_java_package;
80 std::set<std::string> extra_java_packages;
Adam Lesinski418763f2017-04-11 17:36:53 -070081 Maybe<std::string> generate_text_symbols_path;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 Maybe<std::string> generate_proguard_rules_path;
83 Maybe<std::string> generate_main_dex_proguard_rules_path;
84 bool generate_non_final_ids = false;
85 std::vector<std::string> javadoc_annotations;
86 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 // Optimizations/features.
89 bool no_auto_version = false;
90 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +090091 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 bool no_resource_deduping = false;
93 bool no_xml_namespaces = false;
94 bool do_not_compress_anything = false;
95 std::unordered_set<std::string> extensions_to_not_compress;
96
97 // Static lib options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 bool no_static_lib_packages = false;
99
100 // AndroidManifest.xml massaging options.
101 ManifestFixerOptions manifest_fixer_options;
102
103 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700105
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800106 // Flattening options.
107 TableFlattenerOptions table_flattener_options;
108
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 TableSplitterOptions table_splitter_options;
111 std::vector<SplitConstraints> split_constraints;
112 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700113
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 std::unordered_map<ResourceName, ResourceId> stable_id_map;
116 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117};
118
Adam Lesinski64587af2016-02-18 18:33:06 -0800119class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 public:
Chris Warrington820d72a2017-04-27 15:27:01 +0100121 LinkContext(IDiagnostics* diagnostics)
122 : diagnostics_(diagnostics), name_mangler_({}), symbols_(&name_mangler_) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700123 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124
Adam Lesinskib522f042017-04-21 16:57:59 -0700125 PackageType GetPackageType() override {
126 return package_type_;
127 }
128
129 void SetPackageType(PackageType type) {
130 package_type_ = type;
131 }
132
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700133 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100134 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700135 }
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
Adam Lesinskib522f042017-04-21 16:57:59 -0700184 PackageType package_type_ = PackageType::kApp;
Chris Warrington820d72a2017-04-27 15:27:01 +0100185 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 NameMangler name_mangler_;
187 std::string compilation_package_;
188 uint8_t package_id_ = 0x0;
189 SymbolTable symbols_;
190 bool verbose_ = false;
191 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700192};
193
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700195 Maybe<size_t> max_sdk_level, bool keep_raw_values, IArchiveWriter* writer,
196 IAaptContext* context) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 BigBuffer buffer(1024);
198 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 options.keep_raw_values = keep_raw_values;
200 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800203 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 }
205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 DiagMessage msg;
208 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 if (max_sdk_level) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700210 msg << " maxSdkLevel=" << max_sdk_level.value() << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 }
214
Adam Lesinski06460ef2017-03-14 18:52:13 -0700215 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700216 return io::CopyInputStreamToArchive(context, &input_stream, path.to_string(),
217 ArchiveEntry::kCompress, writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800218}
219
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700220static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source, const void* data,
221 size_t len, IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222 pb::ResourceTable pb_table;
223 if (!pb_table.ParseFromArray(data, len)) {
224 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 return {};
226 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800227
Adam Lesinski06460ef2017-03-14 18:52:13 -0700228 std::unique_ptr<ResourceTable> table = DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 if (!table) {
230 return {};
231 }
232 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800233}
234
235/**
236 * Inflates an XML file from the source path.
237 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700238static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239 std::ifstream fin(path, std::ifstream::binary);
240 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 return {};
243 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800245}
246
Adam Lesinski355f2852016-02-13 20:26:45 -0800247struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 bool no_auto_version = false;
249 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900250 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 bool no_xml_namespaces = false;
252 bool keep_raw_values = false;
253 bool do_not_compress_anything = false;
254 bool update_proguard_spec = false;
255 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800256};
257
258class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 public:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700260 ResourceFileFlattener(const ResourceFileFlattenerOptions& options, IAaptContext* context,
261 proguard::KeepSet* keep_set)
262 : options_(options), context_(context), keep_set_(keep_set) {
263 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800266
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 private:
268 struct FileOperation {
269 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700270
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 // The entry this file came from.
272 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700273
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700276
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700279
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 std::string dst_path;
282 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800286
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
288 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 ResourceFileFlattenerOptions options_;
291 IAaptContext* context_;
292 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800293};
294
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
296 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 return 0;
298 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800299
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 for (const std::string& extension : options_.extensions_to_not_compress) {
301 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800303 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 }
305 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800306}
307
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900308static bool IsTransitionElement(const std::string& name) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700309 return name == "fade" || name == "changeBounds" || name == "slide" || name == "explode" ||
310 name == "changeImageTransform" || name == "changeTransform" ||
311 name == "changeClipBounds" || name == "autoTransition" || name == "recolor" ||
312 name == "changeScroll" || name == "transitionSet" || name == "transition" ||
313 name == "transitionManager";
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900314}
315
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700316bool ResourceFileFlattener::LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
317 std::queue<FileOperation>* out_file_op_queue) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700319 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700320
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 if (context_->IsVerbose()) {
322 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 }
324
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 XmlReferenceLinker xml_linker;
326 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 return false;
328 }
329
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700330 if (options_.update_proguard_spec && !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331 return false;
332 }
333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 if (options_.no_xml_namespaces) {
335 XmlNamespaceRemover namespace_remover;
336 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800338 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800340
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341 if (!options_.no_auto_version) {
342 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 xml::Element* el = xml::FindRootElement(doc);
345 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700346 if (el->name == "vector" || el->name == "animated-vector") {
347 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700350 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700352 }
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900353 if (options_.no_version_transitions) {
354 // Skip this if it is a transition resource.
355 xml::Element* el = xml::FindRootElement(doc);
356 if (el && el->namespace_uri.empty()) {
357 if (IsTransitionElement(el->name)) {
358 // We are NOT going to version this file.
359 file_op->skip_version = true;
360 return true;
361 }
362 }
363 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700364
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366
367 // Find the first SDK level used that is higher than this defined config and
368 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369 const int min_sdk_version = context_->GetMinSdkVersion();
370 for (int sdk_level : xml_linker.sdk_levels()) {
371 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700372 if (!ShouldGenerateVersionedResource(file_op->entry, config, sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 // If we shouldn't generate a versioned resource, stop checking.
374 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800375 }
376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 ResourceFile versioned_file_desc = doc->file;
378 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700379
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 FileOperation new_file_op;
Adam Lesinskiea134e02017-04-13 12:55:19 -0700381 new_file_op.xml_to_flatten = util::make_unique<xml::XmlResource>(
382 versioned_file_desc, StringPool{}, doc->root->Clone());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700383 new_file_op.config = versioned_file_desc.config;
384 new_file_op.entry = file_op->entry;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700385 new_file_op.dst_path =
386 ResourceUtils::BuildResourceFileName(versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800387
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700389 context_->GetDiagnostics()->Note(DiagMessage(versioned_file_desc.source)
390 << "auto-versioning resource from config '" << config
391 << "' -> '" << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800392 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 bool added = table->AddFileReferenceAllowMangled(
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700395 versioned_file_desc.name, versioned_file_desc.config, versioned_file_desc.source,
396 new_file_op.dst_path, nullptr, context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 if (!added) {
398 return false;
399 }
400
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 break;
403 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800404 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 }
406 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800407}
408
409/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 * Do not insert or remove any resources while executing in this function. It
411 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800412 * corrupt the iteration order.
413 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700414bool ResourceFileFlattener::Flatten(ResourceTable* table, IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 bool error = false;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700416 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation> config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800417
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 for (auto& pkg : table->packages) {
419 for (auto& type : pkg->types) {
420 // Sort by config and name, so that we get better locality in the zip
421 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 config_sorted_files.clear();
423 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800424
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 // Populate the queue with all files in the ResourceTable.
426 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 for (auto& config_value : entry->values) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700428 FileReference* file_ref = ValueCast<FileReference>(config_value->value.get());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700430 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700432
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700435 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700437 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 }
439
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 FileOperation file_op;
441 file_op.entry = entry.get();
442 file_op.dst_path = *file_ref->path;
443 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 if (type->type != ResourceType::kRaw &&
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700447 (util::EndsWith(src_path, ".xml.flat") || util::EndsWith(src_path, ".xml"))) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 << "failed to open file");
452 return false;
453 }
454
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700455 file_op.xml_to_flatten = xml::Inflate(data->data(), data->size(),
456 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 return false;
460 }
461
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700462 file_op.xml_to_flatten->file.config = config_value->config;
463 file_op.xml_to_flatten->file.source = file_ref->GetSource();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700464 file_op.xml_to_flatten->file.name = ResourceName(pkg->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465
466 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470
471 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700472 // else we end up copying the string in the std::make_pair() method,
473 // then creating a StringPiece from the copy, which would cause us
474 // to end up referencing garbage in the map.
475 const StringPiece entry_name(entry->name);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700476 config_sorted_files[std::make_pair(config_value->config, entry_name)] =
477 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700478 }
479 }
480 }
481
482 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 for (; !file_operations.empty(); file_operations.pop()) {
484 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 error = true;
488 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700489 }
490
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
492 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 // creating a StringPiece from the copy, which would cause us to end up
494 // referencing garbage in the map.
495 const StringPiece entry_name(file_op.entry->name);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700496 config_sorted_files[std::make_pair(file_op.config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 }
498
499 if (error) {
500 return false;
501 }
502
503 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504 for (auto& map_entry : config_sorted_files) {
505 const ConfigDescription& config = map_entry.first.first;
506 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700508 if (file_op.xml_to_flatten) {
509 Maybe<size_t> max_sdk_level;
510 if (!options_.no_auto_version && !file_op.skip_version) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700511 max_sdk_level = std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
512 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700513 }
514
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700515 bool result = FlattenXml(file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
516 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 if (!result) {
518 error = true;
519 }
520 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700521 bool result =
522 io::CopyFileToArchive(context_, file_op.file_to_copy, file_op.dst_path,
523 GetCompressionFlags(file_op.dst_path), archive_writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 if (!result) {
525 error = true;
526 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700527 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700529 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 }
531 return !error;
532}
533
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700534static bool WriteStableIdMapToPath(IDiagnostics* diag,
535 const std::unordered_map<ResourceName, ResourceId>& id_map,
536 const std::string& id_map_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700537 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700539 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 return false;
541 }
542
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 const ResourceName& name = entry.first;
545 const ResourceId& id = entry.second;
546 fout << name << " = " << id << "\n";
547 }
548
549 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700550 diag->Error(DiagMessage(id_map_path) << "failed writing to file: "
551 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 return false;
553 }
554
555 return true;
556}
557
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700558static bool LoadStableIdMap(IDiagnostics* diag, const std::string& path,
559 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 std::string content;
561 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 return false;
564 }
565
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 out_id_map->clear();
567 size_t line_no = 0;
568 for (StringPiece line : util::Tokenize(content, '\n')) {
569 line_no++;
570 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 if (line.empty()) {
572 continue;
573 }
574
575 auto iter = std::find(line.begin(), line.end(), '=');
576 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 return false;
579 }
580
581 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582 StringPiece res_name_str =
583 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
584 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700585 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource name '" << res_name_str
586 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 return false;
588 }
589
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700590 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
591 const size_t res_id_str_len = line.size() - res_id_start_idx;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700592 StringPiece res_id_str = util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
595 if (!maybe_id) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700596 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '" << res_id_str
597 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 return false;
599 }
600
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 }
603 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700604}
605
Adam Lesinskifb48d292015-11-07 15:52:13 -0800606class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 public:
608 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 : options_(options),
610 context_(context),
611 final_table_(),
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700612 file_collection_(util::make_unique<io::FileCollection>()) {
613 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700614
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700615 /**
616 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700617 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 bool LoadSymbolsFromIncludePaths() {
620 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622 for (const std::string& path : options_.include_paths) {
623 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700624 context_->GetDiagnostics()->Note(DiagMessage(path) << "loading include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700626
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700628 std::string error_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800629 std::unique_ptr<ResourceTable> include_static = LoadStaticLibrary(path, &error_str);
630 if (include_static) {
Adam Lesinskib522f042017-04-21 16:57:59 -0700631 if (context_->GetPackageType() != PackageType::kStaticLib) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800632 // Can't include static libraries when not building a static library (they have no IDs
633 // assigned).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 context_->GetDiagnostics()->Error(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800635 DiagMessage(path) << "can't include static library when not building a static lib");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 return false;
637 }
638
639 // If we are using --no-static-lib-packages, we need to rename the
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800640 // package of this table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700641 if (options_.no_static_lib_packages) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800642 // Since package names can differ, and multiple packages can exist in a ResourceTable,
643 // we place the requirement that all static libraries are built with the package
644 // ID 0x7f. So if one is not found, this is an error.
645 if (ResourceTablePackage* pkg = include_static->FindPackageById(kAppPackageId)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700646 pkg->name = context_->GetCompilationPackage();
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800647 } else {
648 context_->GetDiagnostics()->Error(DiagMessage(path)
649 << "no package with ID 0x7f found in static library");
650 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 }
652 }
653
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700654 context_->GetExternalSymbols()->AppendSource(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800655 util::make_unique<ResourceTableSymbolSource>(include_static.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800657 static_table_includes_.push_back(std::move(include_static));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700659 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700660 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700661 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700662 return false;
663 }
664
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 if (!asset_source->AddAssetPath(path)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800666 context_->GetDiagnostics()->Error(DiagMessage(path) << "failed to load include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 return false;
668 }
669 }
670
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800671 // Capture the shared libraries so that the final resource table can be properly flattened
672 // with support for shared libraries.
673 for (auto& entry : asset_source->GetAssignedPackageIds()) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800674 if (entry.first > kFrameworkPackageId && entry.first < kAppPackageId) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800675 final_table_.included_packages_[entry.first] = entry.second;
676 }
677 }
678
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 return true;
681 }
682
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800683 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800685 xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
686 if (manifest_el == nullptr) {
687 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 }
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800689
690 AppInfo app_info;
691
692 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
693 diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>");
694 return {};
695 }
696
697 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
698 if (!package_attr) {
699 diag->Error(DiagMessage(xml_res->file.source)
700 << "<manifest> must have a 'package' attribute");
701 return {};
702 }
703 app_info.package = package_attr->value;
704
705 if (xml::Attribute* version_code_attr =
706 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
707 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_attr->value);
708 if (!maybe_code) {
709 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
710 << "invalid android:versionCode '" << version_code_attr->value << "'");
711 return {};
712 }
713 app_info.version_code = maybe_code.value();
714 }
715
716 if (xml::Attribute* revision_code_attr =
717 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
718 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(revision_code_attr->value);
719 if (!maybe_code) {
720 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
721 << "invalid android:revisionCode '" << revision_code_attr->value << "'");
722 return {};
723 }
724 app_info.revision_code = maybe_code.value();
725 }
726
727 if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
728 if (!split_name_attr->value.empty()) {
729 app_info.split_name = split_name_attr->value;
730 }
731 }
732
733 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
734 if (xml::Attribute* min_sdk =
735 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700736 app_info.min_sdk_version = ResourceUtils::ParseSdkVersion(min_sdk->value);
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800737 }
738 }
739 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 }
741
742 /**
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800743 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700744 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700745 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700746 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700747 bool VerifyNoExternalPackages() {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700748 auto is_ext_package_func = [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700749 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
750 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 };
752
753 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700754 for (const auto& package : final_table_.packages) {
755 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700756 // We have a package that is not related to the one we're building!
757 for (const auto& type : package->types) {
758 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700759 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700763 // for the 'android' package. This is due to legacy reasons.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700764 if (ValueCast<Id>(config_value->value.get()) && package->name == "android") {
765 context_->GetDiagnostics()->Warn(DiagMessage(config_value->value->GetSource())
766 << "generated id '" << res_name
767 << "' for external package '" << package->name
768 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700770 context_->GetDiagnostics()->Error(DiagMessage(config_value->value->GetSource())
771 << "defined resource '" << res_name
772 << "' for external package '" << package->name
773 << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700774 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700776 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700777 }
778 }
779 }
780 }
781
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700782 auto new_end_iter = std::remove_if(final_table_.packages.begin(), final_table_.packages.end(),
783 is_ext_package_func);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700784 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700785 return !error;
786 }
787
788 /**
789 * Returns true if no IDs have been set, false otherwise.
790 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700791 bool VerifyNoIdsSet() {
792 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700793 for (const auto& type : package->types) {
794 if (type->id) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800795 context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID "
796 << StringPrintf("%02x", type->id.value())
797 << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700798 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700799 }
800
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700801 for (const auto& entry : type->entries) {
802 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700803 ResourceNameRef res_name(package->name, type->type, entry->name);
804 context_->GetDiagnostics()->Error(
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800805 DiagMessage() << "entry " << res_name << " has ID "
806 << StringPrintf("%02x", entry->id.value()) << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700807 return false;
808 }
809 }
810 }
811 }
812 return true;
813 }
814
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700815 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
816 if (options_.output_to_directory) {
817 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700818 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700819 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700820 }
821 }
822
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700823 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700824 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800825 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700826 if (!flattener.Consume(context_, table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700827 context_->GetDiagnostics()->Error(DiagMessage() << "failed to flatten resource table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700828 return false;
829 }
830
Adam Lesinski06460ef2017-03-14 18:52:13 -0700831 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700832 return io::CopyInputStreamToArchive(context_, &input_stream, "resources.arsc",
833 ArchiveEntry::kAlign, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700834 }
835
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700836 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700837 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700838 return io::CopyProtoToArchive(context_, pb_table.get(), "resources.arsc.flat", 0, writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839 }
840
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700841 bool WriteJavaFile(ResourceTable* table, const StringPiece& package_name_to_generate,
Adam Lesinski418763f2017-04-11 17:36:53 -0700842 const StringPiece& out_package, const JavaClassGeneratorOptions& java_options,
843 const Maybe<std::string> out_text_symbols_path = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700844 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700845 return true;
846 }
847
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700848 std::string out_path = options_.generate_java_class_path.value();
849 file::AppendPath(&out_path, file::PackageToPath(out_package));
850 if (!file::mkdirs(out_path)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700851 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path
852 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700853 return false;
854 }
855
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700856 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700858 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700860 context_->GetDiagnostics()->Error(DiagMessage()
861 << "failed writing to '" << out_path
862 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700863 return false;
864 }
865
Adam Lesinski418763f2017-04-11 17:36:53 -0700866 std::unique_ptr<std::ofstream> fout_text;
867 if (out_text_symbols_path) {
868 fout_text =
869 util::make_unique<std::ofstream>(out_text_symbols_path.value(), std::ofstream::binary);
870 if (!*fout_text) {
871 context_->GetDiagnostics()->Error(
872 DiagMessage() << "failed writing to '" << out_text_symbols_path.value()
873 << "': " << android::base::SystemErrorCodeToString(errno));
874 return false;
875 }
876 }
877
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700878 JavaClassGenerator generator(context_, table, java_options);
Adam Lesinski418763f2017-04-11 17:36:53 -0700879 if (!generator.Generate(package_name_to_generate, out_package, &fout, fout_text.get())) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700880 context_->GetDiagnostics()->Error(DiagMessage(out_path) << generator.getError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700881 return false;
882 }
883
884 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700885 context_->GetDiagnostics()->Error(DiagMessage()
886 << "failed writing to '" << out_path
887 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888 }
889 return true;
890 }
891
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700892 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
893 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700894 return true;
895 }
896
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700897 std::unique_ptr<ClassDefinition> manifest_class =
898 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700899
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 // Something bad happened, but we already logged it, so exit.
902 return false;
903 }
904
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700905 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700906 // Empty Manifest class, no need to generate it.
907 return true;
908 }
909
910 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700911 for (const std::string& annotation : options_.javadoc_annotations) {
912 std::string proper_annotation = "@";
913 proper_annotation += annotation;
914 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700915 }
916
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700917 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700918
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700919 std::string out_path = options_.generate_java_class_path.value();
920 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700921
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700922 if (!file::mkdirs(out_path)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700923 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path
924 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700925 return false;
926 }
927
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700928 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700929
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700930 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700931 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700932 context_->GetDiagnostics()->Error(DiagMessage()
933 << "failed writing to '" << out_path
934 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700935 return false;
936 }
937
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700938 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8, true, &fout)) {
939 context_->GetDiagnostics()->Error(DiagMessage()
940 << "failed writing to '" << out_path
941 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700942 return false;
943 }
944 return true;
945 }
946
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700947 bool WriteProguardFile(const Maybe<std::string>& out, const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700948 if (!out) {
949 return true;
950 }
951
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700952 const std::string& out_path = out.value();
953 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700954 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700955 context_->GetDiagnostics()->Error(DiagMessage()
956 << "failed to open '" << out_path
957 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700958 return false;
959 }
960
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700961 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700962 if (!fout) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700963 context_->GetDiagnostics()->Error(DiagMessage()
964 << "failed writing to '" << out_path
965 << "': " << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700966 return false;
967 }
968 return true;
969 }
970
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700971 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
972 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700973 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700974 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700975 if (!collection) {
976 return {};
977 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700978 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700979 }
980
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700981 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700982 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700983 if (!file) {
984 return {};
985 }
986
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700987 std::unique_ptr<io::IData> data = file->OpenAsData();
988 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
989 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700990 }
991
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700992 bool MergeStaticLibrary(const std::string& input, bool override) {
993 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700994 context_->GetDiagnostics()->Note(DiagMessage() << "merging static library " << input);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700995 }
996
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700997 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700998 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700999 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001000 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001001 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001002 return false;
1003 }
1004
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001005 std::unique_ptr<ResourceTable> table = LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001006 if (!table) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001007 context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001008 return false;
1009 }
1010
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001011 ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012 if (!pkg) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001013 context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001014 return false;
1015 }
1016
1017 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001018 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001019 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001020 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001021
1022 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001023 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001024 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001025 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001026 }
1027
1028 pkg->name = "";
1029 if (override) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001030 result = table_merger_->MergeOverlay(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001031 } else {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001032 result = table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001033 }
1034
1035 } else {
1036 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001037 // preserved and resource names are mangled.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001038 result =
1039 table_merger_->MergeAndMangle(Source(input), pkg->name, table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001040 }
1041
1042 if (!result) {
1043 return false;
1044 }
1045
1046 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001047 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001048 return true;
1049 }
1050
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001051 bool MergeResourceTable(io::IFile* file, bool override) {
1052 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001053 context_->GetDiagnostics()->Note(DiagMessage() << "merging resource table "
1054 << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001055 }
1056
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001057 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001058 if (!data) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001059 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001060 return false;
1061 }
1062
1063 std::unique_ptr<ResourceTable> table =
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001064 LoadTableFromPb(file->GetSource(), data->data(), data->size(), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001065 if (!table) {
1066 return false;
1067 }
1068
1069 bool result = false;
1070 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001071 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001072 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001073 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001074 }
1075 return result;
1076 }
1077
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001078 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc, bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001079 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001080 context_->GetDiagnostics()->Note(DiagMessage() << "merging '" << file_desc->name
1081 << "' from compiled file "
1082 << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001083 }
1084
1085 bool result = false;
1086 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001087 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001088 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001089 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001090 }
1091
1092 if (!result) {
1093 return false;
1094 }
1095
1096 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001097 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1098 if (exported_symbol.name.package.empty()) {
1099 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001100 }
1101
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001102 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001103
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001104 Maybe<ResourceName> mangled_name =
1105 context_->GetNameMangler()->MangleName(exported_symbol.name);
1106 if (mangled_name) {
1107 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001108 }
1109
1110 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001111 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1112 bool result = final_table_.AddResourceAllowMangled(
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001113 res_name, ConfigDescription::DefaultConfig(), std::string(), std::move(id),
1114 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001115 if (!result) {
1116 return false;
1117 }
1118 }
1119 return true;
1120 }
1121
1122 /**
1123 * Takes a path to load as a ZIP file and merges the files within into the
1124 * master ResourceTable.
1125 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001126 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001127 *
1128 * An io::IFileCollection is created from the ZIP file and added to the set of
1129 * io::IFileCollections that are open.
1130 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001131 bool MergeArchive(const std::string& input, bool override) {
1132 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001133 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive " << input);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001134 }
1135
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001136 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001137 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001138 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001139 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001140 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001141 return false;
1142 }
1143
1144 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001145 for (auto iter = collection->Iterator(); iter->HasNext();) {
1146 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001147 error = true;
1148 }
1149 }
1150
1151 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001152 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001153 return !error;
1154 }
1155
1156 /**
1157 * Takes a path to load and merge into the master ResourceTable. If override
1158 * is true,
1159 * conflicting resources are allowed to override each other, in order of last
1160 * seen.
1161 *
1162 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1163 * as ZIP archive
1164 * and the files within are merged individually.
1165 *
1166 * Otherwise the files is processed on its own.
1167 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001168 bool MergePath(const std::string& path, bool override) {
1169 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1170 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1171 return MergeArchive(path, override);
1172 } else if (util::EndsWith(path, ".apk")) {
1173 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001174 }
1175
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001176 io::IFile* file = file_collection_->InsertFile(path);
1177 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001178 }
1179
1180 /**
1181 * Takes a file to load and merge into the master ResourceTable. If override
1182 * is true,
1183 * conflicting resources are allowed to override each other, in order of last
1184 * seen.
1185 *
1186 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1187 * merged into the
1188 * master ResourceTable. If the file ends with .flat, then it is treated like
1189 * a compiled file
1190 * and the header data is read and merged into the final ResourceTable.
1191 *
1192 * All other file types are ignored. This is because these files could be
1193 * coming from a zip,
1194 * where we could have other files like classes.dex.
1195 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001196 bool MergeFile(io::IFile* file, bool override) {
1197 const Source& src = file->GetSource();
1198 if (util::EndsWith(src.path, ".arsc.flat")) {
1199 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001200
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001201 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001202 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001203 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001204 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001205 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001206 return false;
1207 }
1208
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001209 CompiledFileInputStream input_stream(data->data(), data->size());
1210 uint32_t num_files = 0;
1211 if (!input_stream.ReadLittleEndian32(&num_files)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001212 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed read num files");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001213 return false;
1214 }
1215
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001216 for (uint32_t i = 0; i < num_files; i++) {
1217 pb::CompiledFile compiled_file;
1218 if (!input_stream.ReadCompiledFile(&compiled_file)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001219 context_->GetDiagnostics()->Error(DiagMessage(src)
1220 << "failed to read compiled file header");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001221 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001222 }
1223
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001224 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001225 if (!input_stream.ReadDataMetaData(&offset, &len)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001226 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to read data meta data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001227 return false;
1228 }
1229
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001230 std::unique_ptr<ResourceFile> resource_file = DeserializeCompiledFileFromPb(
1231 compiled_file, file->GetSource(), context_->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001232 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001233 return false;
1234 }
1235
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001236 if (!MergeCompiledFile(file->CreateFileSegment(offset, len), resource_file.get(),
1237 override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001238 return false;
1239 }
1240 }
1241 return true;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001242 } else if (util::EndsWith(src.path, ".xml") || util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001243 // Since AAPT compiles these file types and appends .flat to them, seeing
1244 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001245 const StringPiece file_type = util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1246 context_->GetDiagnostics()->Error(DiagMessage(src) << "uncompiled " << file_type
1247 << " file passed as argument. Must be "
1248 "compiled first into .flat file.");
Adam Lesinski6a396c12016-10-20 14:38:23 -07001249 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001250 }
1251
1252 // Ignore non .flat files. This could be classes.dex or something else that
1253 // happens
1254 // to be in an archive.
1255 return true;
1256 }
1257
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001258 bool CopyAssetsDirsToApk(IArchiveWriter* writer) {
1259 std::map<std::string, std::unique_ptr<io::RegularFile>> merged_assets;
1260 for (const std::string& assets_dir : options_.assets_dirs) {
1261 Maybe<std::vector<std::string>> files =
1262 file::FindFiles(assets_dir, context_->GetDiagnostics(), nullptr);
1263 if (!files) {
1264 return false;
1265 }
1266
1267 for (const std::string& file : files.value()) {
1268 std::string full_key = "assets/" + file;
1269 std::string full_path = assets_dir;
1270 file::AppendPath(&full_path, file);
1271
1272 auto iter = merged_assets.find(full_key);
1273 if (iter == merged_assets.end()) {
1274 merged_assets.emplace(std::move(full_key),
1275 util::make_unique<io::RegularFile>(Source(std::move(full_path))));
1276 } else if (context_->IsVerbose()) {
1277 context_->GetDiagnostics()->Warn(DiagMessage(iter->second->GetSource())
1278 << "asset file overrides '" << full_path << "'");
1279 }
1280 }
1281 }
1282
1283 for (auto& entry : merged_assets) {
1284 uint32_t compression_flags = ArchiveEntry::kCompress;
1285 std::string extension = file::GetExtension(entry.first).to_string();
1286 if (options_.extensions_to_not_compress.count(extension) > 0) {
1287 compression_flags = 0u;
1288 }
1289
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001290 if (!io::CopyFileToArchive(context_, entry.second.get(), entry.first, compression_flags,
1291 writer)) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001292 return false;
1293 }
1294 }
1295 return true;
1296 }
1297
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001298 /**
1299 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001300 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001301 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001302 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set, xml::XmlResource* manifest,
1303 ResourceTable* table) {
Adam Lesinskib522f042017-04-21 16:57:59 -07001304 const bool keep_raw_values = context_->GetPackageType() == PackageType::kStaticLib;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001305 bool result =
1306 FlattenXml(manifest, "AndroidManifest.xml", {}, keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001307 if (!result) {
1308 return false;
1309 }
1310
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001311 ResourceFileFlattenerOptions file_flattener_options;
1312 file_flattener_options.keep_raw_values = keep_raw_values;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001313 file_flattener_options.do_not_compress_anything = options_.do_not_compress_anything;
1314 file_flattener_options.extensions_to_not_compress = options_.extensions_to_not_compress;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001315 file_flattener_options.no_auto_version = options_.no_auto_version;
1316 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001317 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001318 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1319 file_flattener_options.update_proguard_spec =
1320 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001321
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001322 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001323
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001324 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001325 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001326 return false;
1327 }
1328
Adam Lesinskib522f042017-04-21 16:57:59 -07001329 if (context_->GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001330 if (!FlattenTableToPb(table, writer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001331 return false;
1332 }
1333 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001334 if (!FlattenTable(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001335 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001336 return false;
1337 }
1338 }
1339 return true;
1340 }
1341
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001342 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001343 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001344 std::unique_ptr<xml::XmlResource> manifest_xml =
1345 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1346 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001347 return 1;
1348 }
1349
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001350 // First extract the Package name without modifying it (via --rename-manifest-package).
1351 if (Maybe<AppInfo> maybe_app_info =
1352 ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001353 const AppInfo& app_info = maybe_app_info.value();
1354 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001355 }
1356
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001357 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1358 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001359 return 1;
1360 }
1361
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001362 Maybe<AppInfo> maybe_app_info =
1363 ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001364 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001365 return 1;
1366 }
1367
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001368 const AppInfo& app_info = maybe_app_info.value();
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001369 context_->SetMinSdkVersion(app_info.min_sdk_version.value_or_default(0));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001370
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001371 context_->SetNameManglerPolicy(NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001372
1373 // Override the package ID when it is "android".
1374 if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001375 context_->SetPackageId(0x01);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001376
1377 // Verify we're building a regular app.
Adam Lesinskib522f042017-04-21 16:57:59 -07001378 if (context_->GetPackageType() != PackageType::kApp) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001379 context_->GetDiagnostics()->Error(
1380 DiagMessage() << "package 'android' can only be built as a regular app");
1381 return 1;
1382 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001383 }
1384
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001385 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001386 return 1;
1387 }
1388
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001389 TableMergerOptions table_merger_options;
1390 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001391 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_, table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001392
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001393 if (context_->IsVerbose()) {
1394 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001395 << StringPrintf("linking package '%s' using package ID %02x",
1396 context_->GetCompilationPackage().data(),
1397 context_->GetPackageId()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001398 }
1399
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400 for (const std::string& input : input_files) {
1401 if (!MergePath(input, false)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001402 context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing input");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403 return 1;
1404 }
1405 }
1406
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001407 for (const std::string& input : options_.overlay_files) {
1408 if (!MergePath(input, true)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001409 context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing overlays");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001410 return 1;
1411 }
1412 }
1413
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001414 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001415 return 1;
1416 }
1417
Adam Lesinskib522f042017-04-21 16:57:59 -07001418 if (context_->GetPackageType() != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001419 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001420 if (!mover.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001421 context_->GetDiagnostics()->Error(DiagMessage() << "failed moving private attributes");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001422 return 1;
1423 }
1424
1425 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001426 IdAssigner id_assigner(&options_.stable_id_map);
1427 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001428 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001429 return 1;
1430 }
1431
1432 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001433 if (options_.resource_id_map_path) {
1434 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001435 for (auto& type : package->types) {
1436 for (auto& entry : type->entries) {
1437 ResourceName name(package->name, type->type, entry->name);
1438 // The IDs are guaranteed to exist.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001439 options_.stable_id_map[std::move(name)] =
1440 ResourceId(package->id.value(), type->id.value(), entry->id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001441 }
1442 }
1443 }
1444
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001445 if (!WriteStableIdMapToPath(context_->GetDiagnostics(), options_.stable_id_map,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001446 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001447 return 1;
1448 }
1449 }
1450 } else {
1451 // Static libs are merged with other apps, and ID collisions are bad, so
1452 // verify that
1453 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001454 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001455 return 1;
1456 }
1457 }
1458
1459 // Add the names to mangle based on our source merge earlier.
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001460 context_->SetNameManglerPolicy(
1461 NameManglerPolicy{context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001462
1463 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001464 context_->GetExternalSymbols()->PrependSource(
1465 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001466
1467 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001468 if (!linker.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001469 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking references");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001470 return 1;
1471 }
1472
Adam Lesinskib522f042017-04-21 16:57:59 -07001473 if (context_->GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001474 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001475 context_->GetDiagnostics()->Warn(DiagMessage()
1476 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001477 }
1478 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001479 ProductFilter product_filter(options_.products);
1480 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001481 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001482 return 1;
1483 }
1484 }
1485
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001486 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001487 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001488 if (!versioner.Consume(context_, &final_table_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001489 context_->GetDiagnostics()->Error(DiagMessage() << "failed versioning styles");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001490 return 1;
1491 }
1492 }
1493
Adam Lesinskib522f042017-04-21 16:57:59 -07001494 if (context_->GetPackageType() != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001495 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001496 context_->GetDiagnostics()->Note(DiagMessage()
1497 << "collapsing resource versions for minimum SDK "
1498 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001499 }
1500
1501 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001502 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001503 return 1;
1504 }
1505 }
1506
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001507 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001508 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001509 if (!deduper.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001510 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001511 return 1;
1512 }
1513 }
1514
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001515 proguard::KeepSet proguard_keep_set;
1516 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001517
Adam Lesinskib522f042017-04-21 16:57:59 -07001518 if (context_->GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001519 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001520 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001521 context_->GetDiagnostics()->Warn(DiagMessage()
1522 << "can't strip resources when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001523 }
1524 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001525 // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or
1526 // equal to the minSdk.
1527 options_.split_constraints =
1528 AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001529
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001530 TableSplitter table_splitter(options_.split_constraints, options_.table_splitter_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001531 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001532 return 1;
1533 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001534 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535
1536 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001537 auto path_iter = options_.split_paths.begin();
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001538 auto split_constraints_iter = options_.split_constraints.begin();
1539 for (std::unique_ptr<ResourceTable>& split_table : table_splitter.splits()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001540 if (context_->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001541 context_->GetDiagnostics()->Note(DiagMessage(*path_iter)
1542 << "generating split with configurations '"
1543 << util::Joiner(split_constraints_iter->configs, ", ")
1544 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001545 }
1546
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001547 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(*path_iter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001548 if (!archive_writer) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001549 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001550 return 1;
1551 }
1552
1553 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001554 std::unique_ptr<xml::XmlResource> split_manifest =
1555 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001556
1557 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001558 if (!linker.Consume(context_, split_manifest.get())) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001559 context_->GetDiagnostics()->Error(DiagMessage()
1560 << "failed to create Split AndroidManifest.xml");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001561 return 1;
1562 }
1563
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001564 if (!WriteApk(archive_writer.get(), &proguard_keep_set, split_manifest.get(),
1565 split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001566 return 1;
1567 }
1568
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001569 ++path_iter;
1570 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001571 }
1572 }
1573
1574 // Start writing the base APK.
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001575 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(options_.output_path);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001576 if (!archive_writer) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001577 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001578 return 1;
1579 }
1580
1581 bool error = false;
1582 {
1583 // AndroidManifest.xml has no resource name, but the CallSite is built
1584 // from the name
1585 // (aka, which package the AndroidManifest.xml is coming from).
1586 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001587 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001588
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001589 XmlReferenceLinker manifest_linker;
1590 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1591 if (options_.generate_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001592 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1593 manifest_xml.get(), &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001594 error = true;
1595 }
1596
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001597 if (options_.generate_main_dex_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001598 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1599 manifest_xml.get(),
1600 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001601 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001602 }
1603
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001604 if (options_.generate_java_class_path) {
1605 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001606 error = true;
1607 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001608 }
1609
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001610 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001611 // PackageParser will fail if URIs are removed from
1612 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001613 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1614 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001615 error = true;
1616 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001617 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001618 } else {
1619 error = true;
1620 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001621 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001622
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001623 if (error) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001624 context_->GetDiagnostics()->Error(DiagMessage() << "failed processing manifest");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001625 return 1;
1626 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001627
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001628 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(), &final_table_)) {
1629 return 1;
1630 }
1631
1632 if (!CopyAssetsDirsToApk(archive_writer.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001633 return 1;
1634 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001635
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001636 if (options_.generate_java_class_path) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001637 // The set of packages whose R class to call in the main classes
1638 // onResourcesLoaded callback.
1639 std::vector<std::string> packages_to_callback;
1640
1641 JavaClassGeneratorOptions template_options;
1642 template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1643 template_options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001644
Adam Lesinskib522f042017-04-21 16:57:59 -07001645 if (context_->GetPackageType() == PackageType::kStaticLib ||
1646 options_.generate_non_final_ids) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001647 template_options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001648 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001649
Adam Lesinskib522f042017-04-21 16:57:59 -07001650 if (context_->GetPackageType() == PackageType::kSharedLib) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001651 template_options.use_final = false;
1652 template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{};
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001653 }
1654
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001655 const StringPiece actual_package = context_->GetCompilationPackage();
1656 StringPiece output_package = context_->GetCompilationPackage();
1657 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001658 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001659 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001660 }
1661
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001662 // Generate the private symbols if required.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001663 if (options_.private_symbols) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001664 packages_to_callback.push_back(options_.private_symbols.value());
1665
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001666 // If we defined a private symbols package, we only emit Public symbols
1667 // to the original package, and private and public symbols to the
1668 // private package.
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001669 JavaClassGeneratorOptions options = template_options;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001670 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001671 if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001672 options)) {
1673 return 1;
1674 }
1675 }
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001676
1677 // Generate all the symbols for all extra packages.
1678 for (const std::string& extra_package : options_.extra_java_packages) {
1679 packages_to_callback.push_back(extra_package);
1680
1681 JavaClassGeneratorOptions options = template_options;
1682 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1683 if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) {
1684 return 1;
1685 }
1686 }
1687
1688 // Generate the main public R class.
1689 JavaClassGeneratorOptions options = template_options;
1690
1691 // Only generate public symbols if we have a private package.
1692 if (options_.private_symbols) {
1693 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
1694 }
1695
1696 if (options.rewrite_callback_options) {
1697 options.rewrite_callback_options.value().packages_to_callback =
1698 std::move(packages_to_callback);
1699 }
1700
Adam Lesinski418763f2017-04-11 17:36:53 -07001701 if (!WriteJavaFile(&final_table_, actual_package, output_package, options,
1702 options_.generate_text_symbols_path)) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001703 return 1;
1704 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001705 }
1706
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001707 if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001708 return 1;
1709 }
1710
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001711 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1712 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001713 return 1;
1714 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001715 return 0;
1716 }
1717
1718 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001719 LinkOptions options_;
1720 LinkContext* context_;
1721 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001722
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001723 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001724
1725 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001726 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001727
1728 // A vector of IFileCollections. This is mainly here to keep ownership of the
1729 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001730 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001731
1732 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001733 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001734 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001735
1736 // The set of shared libraries being used, mapping their assigned package ID to package name.
1737 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001738};
1739
Chris Warrington820d72a2017-04-27 15:27:01 +01001740int Link(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
1741 LinkContext context(diagnostics);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001742 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001743 std::vector<std::string> overlay_arg_list;
1744 std::vector<std::string> extra_java_packages;
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001745 Maybe<std::string> package_id;
Adam Lesinski113ee092017-04-03 19:38:25 -07001746 std::vector<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001747 Maybe<std::string> preferred_density;
1748 Maybe<std::string> product_list;
1749 bool legacy_x_flag = false;
1750 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001751 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001752 bool shared_lib = false;
1753 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001754 Maybe<std::string> stable_id_file_path;
1755 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001756 Flags flags =
1757 Flags()
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001758 .RequiredFlag("-o", "Output path.", &options.output_path)
1759 .RequiredFlag("--manifest", "Path to the Android manifest to build.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001760 &options.manifest_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001761 .OptionalFlagList("-I", "Adds an Android APK to link against.", &options.include_paths)
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001762 .OptionalFlagList("-A",
1763 "An assets directory to include in the APK. These are unprocessed.",
1764 &options.assets_dirs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001765 .OptionalFlagList("-R",
1766 "Compilation unit to link, using `overlay` semantics.\n"
1767 "The last conflicting resource given takes precedence.",
1768 &overlay_arg_list)
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001769 .OptionalFlag("--package-id",
1770 "Specify the package ID to use for this app. Must be greater or equal to\n"
1771 "0x7f and can't be used with --static-lib or --shared-lib.",
1772 &package_id)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001773 .OptionalFlag("--java", "Directory in which to generate R.java.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001774 &options.generate_java_class_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001775 .OptionalFlag("--proguard", "Output file for generated Proguard rules.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001776 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001777 .OptionalFlag("--proguard-main-dex",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001778 "Output file for generated Proguard rules for the main dex.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001779 &options.generate_main_dex_proguard_rules_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001780 .OptionalSwitch("--no-auto-version",
1781 "Disables automatic style and layout SDK versioning.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001782 &options.no_auto_version)
1783 .OptionalSwitch("--no-version-vectors",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001784 "Disables automatic versioning of vector drawables. Use this only\n"
1785 "when building with vector drawable support library.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001786 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001787 .OptionalSwitch("--no-version-transitions",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001788 "Disables automatic versioning of transition resources. Use this only\n"
1789 "when building with transition support library.",
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001790 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001791 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001792 "Disables automatic deduping of resources with\n"
1793 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001794 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001795 .OptionalSwitch("--enable-sparse-encoding",
1796 "Enables encoding sparse entries using a binary search tree.\n"
1797 "This decreases APK size at the cost of resource retrieval performance.",
1798 &options.table_flattener_options.use_sparse_entries)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001799 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001800 &legacy_x_flag)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001801 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001802 &require_localization)
Adam Lesinski113ee092017-04-03 19:38:25 -07001803 .OptionalFlagList("-c",
Adam Lesinski418763f2017-04-11 17:36:53 -07001804 "Comma separated list of configurations to include. The default\n"
1805 "is all configurations.",
1806 &configs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001807 .OptionalFlag("--preferred-density",
1808 "Selects the closest matching density and strips out all others.",
1809 &preferred_density)
1810 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001811 .OptionalSwitch("--output-to-dir",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001812 "Outputs the APK contents to a directory specified by -o.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001813 &options.output_to_directory)
1814 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001815 "Removes XML namespace prefix and URI information from\n"
1816 "AndroidManifest.xml and XML binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001817 &options.no_xml_namespaces)
1818 .OptionalFlag("--min-sdk-version",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001819 "Default minimum SDK version to use for AndroidManifest.xml.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001820 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001821 .OptionalFlag("--target-sdk-version",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001822 "Default target SDK version to use for AndroidManifest.xml.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001823 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001824 .OptionalFlag("--version-code",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001825 "Version code (integer) to inject into the AndroidManifest.xml if none is\n"
1826 "present.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001827 &options.manifest_fixer_options.version_code_default)
1828 .OptionalFlag("--version-name",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001829 "Version name to inject into the AndroidManifest.xml if none is present.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001830 &options.manifest_fixer_options.version_name_default)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001831 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library.",
1832 &shared_lib)
1833 .OptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001834 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001835 "Merge all library resources under the app's package.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001836 &options.no_static_lib_packages)
1837 .OptionalSwitch("--non-final-ids",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001838 "Generates R.java without the final modifier. This is implied when\n"
1839 "--static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001840 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001841 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001842 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001843 .OptionalFlag("--emit-ids",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001844 "Emit a file at the given path with a list of name to ID mappings,\n"
1845 "suitable for use with --stable-ids.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001846 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001847 .OptionalFlag("--private-symbols",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001848 "Package name to use when generating R.java for private symbols.\n"
1849 "If not specified, public and private symbols will use the application's\n"
1850 "package name.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001851 &options.private_symbols)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001852 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001853 &options.custom_java_package)
1854 .OptionalFlagList("--extra-packages",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001855 "Generate the same R.java but with different package names.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001856 &extra_java_packages)
1857 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001858 "Adds a JavaDoc annotation to all generated Java classes.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001859 &options.javadoc_annotations)
Adam Lesinski418763f2017-04-11 17:36:53 -07001860 .OptionalFlag("--output-text-symbols",
1861 "Generates a text file containing the resource symbols of the R class in\n"
1862 "the specified folder.",
1863 &options.generate_text_symbols_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001864 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001865 "Allows the addition of new resources in overlays without\n"
1866 "<add-resource> tags.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001867 &options.auto_add_overlay)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001868 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001869 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001870 .OptionalFlag("--rename-instrumentation-target-package",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001871 "Changes the name of the target package for instrumentation. Most useful\n"
1872 "when used in conjunction with --rename-manifest-package.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001873 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001874 .OptionalFlagList("-0", "File extensions not to compress.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001875 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001876 .OptionalFlagList("--split",
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001877 "Split resources matching a set of configs out to a Split APK.\n"
Adam Lesinskidb091572017-04-13 12:48:56 -07001878 "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
1879 "On Windows, use a semicolon ';' separator instead.",
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001880 &split_args)
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001881 .OptionalSwitch("-v", "Enables verbose logging.", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001882
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001883 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001884 return 1;
1885 }
1886
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001887 // Expand all argument-files passed into the command line. These start with '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001888 std::vector<std::string> arg_list;
1889 for (const std::string& arg : flags.GetArgs()) {
1890 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001891 const std::string path = arg.substr(1, arg.size() - 1);
1892 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001893 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
1894 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001895 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001896 }
1897 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001898 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001899 }
1900 }
1901
1902 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001903 for (const std::string& arg : overlay_arg_list) {
1904 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001905 const std::string path = arg.substr(1, arg.size() - 1);
1906 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001907 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
1908 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001909 return 1;
1910 }
1911 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001912 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001913 }
1914 }
1915
1916 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001917 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001918 }
1919
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001920 if (shared_lib && static_lib) {
1921 context.GetDiagnostics()->Error(DiagMessage()
1922 << "only one of --shared-lib and --static-lib can be defined");
1923 return 1;
1924 }
1925
1926 if (shared_lib) {
Adam Lesinskib522f042017-04-21 16:57:59 -07001927 context.SetPackageType(PackageType::kSharedLib);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001928 context.SetPackageId(0x00);
1929 } else if (static_lib) {
Adam Lesinskib522f042017-04-21 16:57:59 -07001930 context.SetPackageType(PackageType::kStaticLib);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001931 context.SetPackageId(kAppPackageId);
1932 } else {
Adam Lesinskib522f042017-04-21 16:57:59 -07001933 context.SetPackageType(PackageType::kApp);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001934 context.SetPackageId(kAppPackageId);
1935 }
1936
1937 if (package_id) {
Adam Lesinskib522f042017-04-21 16:57:59 -07001938 if (context.GetPackageType() != PackageType::kApp) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001939 context.GetDiagnostics()->Error(
1940 DiagMessage() << "can't specify --package-id when not building a regular app");
1941 return 1;
1942 }
1943
1944 const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id.value());
1945 if (!maybe_package_id_int) {
1946 context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id.value()
1947 << "' is not a valid integer");
1948 return 1;
1949 }
1950
1951 const uint32_t package_id_int = maybe_package_id_int.value();
1952 if (package_id_int < kAppPackageId || package_id_int > std::numeric_limits<uint8_t>::max()) {
1953 context.GetDiagnostics()->Error(
1954 DiagMessage() << StringPrintf(
1955 "invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int));
1956 return 1;
1957 }
1958 context.SetPackageId(static_cast<uint8_t>(package_id_int));
1959 }
1960
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001961 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001962 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001963 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001964 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001965 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001966 }
1967 }
1968
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001969 if (product_list) {
1970 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001971 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001972 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001973 }
1974 }
1975 }
1976
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001977 std::unique_ptr<IConfigFilter> filter;
Mihai Nitaf4dacf22017-04-07 08:25:06 -07001978 if (!configs.empty()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001979 filter = ParseConfigFilterParameters(configs, context.GetDiagnostics());
1980 if (filter == nullptr) {
1981 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001982 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001983 options.table_splitter_options.config_filter = filter.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001984 }
1985
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001986 if (preferred_density) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001987 Maybe<uint16_t> density =
1988 ParseTargetDensityParameter(preferred_density.value(), context.GetDiagnostics());
1989 if (!density) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001990 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07001991 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001992 options.table_splitter_options.preferred_densities.push_back(density.value());
1993 }
Adam Lesinskic51562c2016-04-28 11:12:38 -07001994
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001995 // Parse the split parameters.
1996 for (const std::string& split_arg : split_args) {
1997 options.split_paths.push_back({});
1998 options.split_constraints.push_back({});
1999 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(), &options.split_paths.back(),
2000 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002001 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002002 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002003 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002004
Adam Lesinskib522f042017-04-21 16:57:59 -07002005 if (context.GetPackageType() != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002006 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2007 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002008 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002009 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002010 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002011
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002012 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002013 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002014 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2015 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2016 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2017 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2018
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002019 // Turn off auto versioning for static-libs.
Adam Lesinskib522f042017-04-21 16:57:59 -07002020 if (context.GetPackageType() == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002021 options.no_auto_version = true;
2022 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002023 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002024 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002025
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002026 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002027 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002028}
2029
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002030} // namespace aapt