blob: f07e20bbc78a04bed70025c32293859bf8adf841 [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 Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "google/protobuf/io/coded_stream.h"
28
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "AppInfo.h"
30#include "Debug.h"
31#include "Flags.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080032#include "Locale.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033#include "NameMangler.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080034#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035#include "compile/IdAssigner.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080036#include "filter/ConfigFilter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037#include "flatten/Archive.h"
38#include "flatten/TableFlattener.h"
39#include "flatten/XmlFlattener.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080040#include "io/FileSystem.h"
41#include "io/ZipArchive.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070042#include "java/JavaClassGenerator.h"
43#include "java/ManifestClassGenerator.h"
44#include "java/ProguardRules.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045#include "link/Linkers.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046#include "link/ManifestFixer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080047#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048#include "link/TableMerger.h"
49#include "process/IResourceTableConsumer.h"
50#include "process/SymbolTable.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080051#include "proto/ProtoSerialize.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080052#include "split/TableSplitter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053#include "unflatten/BinaryResourceParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054#include "util/Files.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080055#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056
Adam Lesinskid5083f62017-01-16 15:07:21 -080057using android::StringPiece;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070059
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060namespace aapt {
61
62struct LinkOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 std::string output_path;
64 std::string manifest_path;
65 std::vector<std::string> include_paths;
66 std::vector<std::string> overlay_files;
67 bool output_to_directory = false;
68 bool auto_add_overlay = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 // Java/Proguard options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 Maybe<std::string> generate_java_class_path;
72 Maybe<std::string> custom_java_package;
73 std::set<std::string> extra_java_packages;
74 Maybe<std::string> generate_proguard_rules_path;
75 Maybe<std::string> generate_main_dex_proguard_rules_path;
76 bool generate_non_final_ids = false;
77 std::vector<std::string> javadoc_annotations;
78 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070079
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 // Optimizations/features.
81 bool no_auto_version = false;
82 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +090083 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 bool no_resource_deduping = false;
85 bool no_xml_namespaces = false;
86 bool do_not_compress_anything = false;
87 std::unordered_set<std::string> extensions_to_not_compress;
88
89 // Static lib options.
90 bool static_lib = false;
91 bool no_static_lib_packages = false;
92
93 // AndroidManifest.xml massaging options.
94 ManifestFixerOptions manifest_fixer_options;
95
96 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -070098
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080099 // Flattening options.
100 TableFlattenerOptions table_flattener_options;
101
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 TableSplitterOptions table_splitter_options;
104 std::vector<SplitConstraints> split_constraints;
105 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 std::unordered_map<ResourceName, ResourceId> stable_id_map;
109 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110};
111
Adam Lesinski64587af2016-02-18 18:33:06 -0800112class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 LinkContext() : name_mangler_({}) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 NameMangler* GetNameMangler() override { return &name_mangler_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
121 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800123
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 const std::string& GetCompilationPackage() override {
125 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800129 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 uint8_t GetPackageId() override { return package_id_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700133
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 void SetPackageId(uint8_t id) { package_id_ = id; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 SymbolTable* GetExternalSymbols() override { return &symbols_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 bool IsVerbose() override { return verbose_; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 int GetMinSdkVersion() override { return min_sdk_version_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 void SetMinSdkVersion(int minSdk) { min_sdk_version_ = minSdk; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700145
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 DISALLOW_COPY_AND_ASSIGN(LinkContext);
148
149 StdErrDiagnostics diagnostics_;
150 NameMangler name_mangler_;
151 std::string compilation_package_;
152 uint8_t package_id_ = 0x0;
153 SymbolTable symbols_;
154 bool verbose_ = false;
155 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156};
157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158static bool CopyFileToArchive(io::IFile* file, const std::string& out_path,
159 uint32_t compression_flags,
160 IArchiveWriter* writer, IAaptContext* context) {
161 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 context->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 << "failed to open file");
Adam Lesinski355f2852016-02-13 20:26:45 -0800165 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 }
167
168 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data->data());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 const size_t buffer_size = data->size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 if (context->IsVerbose()) {
172 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 << " to archive");
174 }
175
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 if (writer->StartEntry(out_path, compression_flags)) {
177 if (writer->WriteEntry(buffer, buffer_size)) {
178 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 return true;
180 }
181 }
182 }
183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 context->GetDiagnostics()->Error(DiagMessage() << "failed to write file "
185 << out_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800187}
188
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
190 Maybe<size_t> max_sdk_level, bool keep_raw_values,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 IArchiveWriter* writer, IAaptContext* context) {
192 BigBuffer buffer(1024);
193 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 options.keep_raw_values = keep_raw_values;
195 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800198 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 }
200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 DiagMessage msg;
203 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 if (max_sdk_level) {
205 msg << " maxSdkLevel=" << max_sdk_level.value()
206 << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 }
210
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 if (writer->StartEntry(path, ArchiveEntry::kCompress)) {
212 if (writer->WriteEntry(buffer)) {
213 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 return true;
215 }
216 }
217 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 << " to archive");
220 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800221}
222
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 const void* data,
225 size_t len,
Adam Lesinski355f2852016-02-13 20:26:45 -0800226 IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 pb::ResourceTable pb_table;
228 if (!pb_table.ParseFromArray(data, len)) {
229 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 return {};
231 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800232
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 if (!table) {
236 return {};
237 }
238 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800239}
240
241/**
242 * Inflates an XML file from the source path.
243 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 IDiagnostics* diag) {
246 std::ifstream fin(path, std::ifstream::binary);
247 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 return {};
250 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800252}
253
Adam Lesinski355f2852016-02-13 20:26:45 -0800254struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 bool no_auto_version = false;
256 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900257 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 bool no_xml_namespaces = false;
259 bool keep_raw_values = false;
260 bool do_not_compress_anything = false;
261 bool update_proguard_spec = false;
262 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800263};
264
265class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 public:
267 ResourceFileFlattener(const ResourceFileFlattenerOptions& options,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 IAaptContext* context, proguard::KeepSet* keep_set)
269 : options_(options), context_(context), keep_set_(keep_set) {}
Adam Lesinski355f2852016-02-13 20:26:45 -0800270
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800272
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 private:
274 struct FileOperation {
275 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700276
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 // The entry this file came from.
278 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700279
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700282
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700285
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 std::string dst_path;
288 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800290
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800292
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
294 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800295
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 ResourceFileFlattenerOptions options_;
297 IAaptContext* context_;
298 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800299};
300
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
302 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 return 0;
304 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800305
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306 for (const std::string& extension : options_.extensions_to_not_compress) {
307 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800309 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 }
311 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800312}
313
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900314static bool IsTransitionElement(const std::string& name) {
315 return
316 name == "fade" ||
317 name == "changeBounds" ||
318 name == "slide" ||
319 name == "explode" ||
320 name == "changeImageTransform" ||
321 name == "changeTransform" ||
322 name == "changeClipBounds" ||
323 name == "autoTransition" ||
324 name == "recolor" ||
325 name == "changeScroll" ||
326 name == "transitionSet" ||
327 name == "transition" ||
328 name == "transitionManager";
329}
330
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331bool ResourceFileFlattener::LinkAndVersionXmlFile(
332 ResourceTable* table, FileOperation* file_op,
333 std::queue<FileOperation>* out_file_op_queue) {
334 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700336
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 if (context_->IsVerbose()) {
338 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 }
340
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341 XmlReferenceLinker xml_linker;
342 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 return false;
344 }
345
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 if (options_.update_proguard_spec &&
347 !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 return false;
349 }
350
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 if (options_.no_xml_namespaces) {
352 XmlNamespaceRemover namespace_remover;
353 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800355 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800357
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 if (!options_.no_auto_version) {
359 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 xml::Element* el = xml::FindRootElement(doc);
362 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363 if (el->name == "vector" || el->name == "animated-vector") {
364 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700367 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700369 }
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900370 if (options_.no_version_transitions) {
371 // Skip this if it is a transition resource.
372 xml::Element* el = xml::FindRootElement(doc);
373 if (el && el->namespace_uri.empty()) {
374 if (IsTransitionElement(el->name)) {
375 // We are NOT going to version this file.
376 file_op->skip_version = true;
377 return true;
378 }
379 }
380 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700381
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383
384 // Find the first SDK level used that is higher than this defined config and
385 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 const int min_sdk_version = context_->GetMinSdkVersion();
387 for (int sdk_level : xml_linker.sdk_levels()) {
388 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
389 if (!ShouldGenerateVersionedResource(file_op->entry, config,
390 sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 // If we shouldn't generate a versioned resource, stop checking.
392 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800393 }
394
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 ResourceFile versioned_file_desc = doc->file;
396 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700397
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398 FileOperation new_file_op;
399 new_file_op.xml_to_flatten = util::make_unique<xml::XmlResource>(
400 versioned_file_desc, doc->root->Clone());
401 new_file_op.config = versioned_file_desc.config;
402 new_file_op.entry = file_op->entry;
403 new_file_op.dst_path = ResourceUtils::BuildResourceFileName(
404 versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800405
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 if (context_->IsVerbose()) {
407 context_->GetDiagnostics()->Note(
408 DiagMessage(versioned_file_desc.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 << "auto-versioning resource from config '" << config << "' -> '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800411 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 bool added = table->AddFileReferenceAllowMangled(
414 versioned_file_desc.name, versioned_file_desc.config,
415 versioned_file_desc.source, new_file_op.dst_path, nullptr,
416 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 if (!added) {
418 return false;
419 }
420
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 break;
423 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800424 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 }
426 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800427}
428
429/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 * Do not insert or remove any resources while executing in this function. It
431 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800432 * corrupt the iteration order.
433 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434bool ResourceFileFlattener::Flatten(ResourceTable* table,
435 IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 bool error = false;
437 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438 config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800439
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 for (auto& pkg : table->packages) {
441 for (auto& type : pkg->types) {
442 // Sort by config and name, so that we get better locality in the zip
443 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 config_sorted_files.clear();
445 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800446
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 // Populate the queue with all files in the ResourceTable.
448 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 for (auto& config_value : entry->values) {
450 FileReference* file_ref =
451 ValueCast<FileReference>(config_value->value.get());
452 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700453 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700455
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700460 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 }
462
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 FileOperation file_op;
464 file_op.entry = entry.get();
465 file_op.dst_path = *file_ref->path;
466 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 if (type->type != ResourceType::kRaw &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 (util::EndsWith(src_path, ".xml.flat") ||
471 util::EndsWith(src_path, ".xml"))) {
472 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 << "failed to open file");
476 return false;
477 }
478
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 file_op.xml_to_flatten =
480 xml::Inflate(data->data(), data->size(),
481 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 return false;
485 }
486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 file_op.xml_to_flatten->file.config = config_value->config;
488 file_op.xml_to_flatten->file.source = file_ref->GetSource();
489 file_op.xml_to_flatten->file.name =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 ResourceName(pkg->name, type->type, entry->name);
491
492 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496
497 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700498 // else we end up copying the string in the std::make_pair() method,
499 // then creating a StringPiece from the copy, which would cause us
500 // to end up referencing garbage in the map.
501 const StringPiece entry_name(entry->name);
502 config_sorted_files[std::make_pair(
503 config_value->config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 }
505 }
506 }
507
508 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509 for (; !file_operations.empty(); file_operations.pop()) {
510 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700513 error = true;
514 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700515 }
516
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
518 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 // creating a StringPiece from the copy, which would cause us to end up
520 // referencing garbage in the map.
521 const StringPiece entry_name(file_op.entry->name);
522 config_sorted_files[std::make_pair(file_op.config, entry_name)] =
523 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 }
525
526 if (error) {
527 return false;
528 }
529
530 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 for (auto& map_entry : config_sorted_files) {
532 const ConfigDescription& config = map_entry.first.first;
533 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700534
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 if (file_op.xml_to_flatten) {
536 Maybe<size_t> max_sdk_level;
537 if (!options_.no_auto_version && !file_op.skip_version) {
538 max_sdk_level =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700540 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 }
542
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 bool result = FlattenXml(
544 file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
545 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 if (!result) {
547 error = true;
548 }
549 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700550 bool result = CopyFileToArchive(
551 file_op.file_to_copy, file_op.dst_path,
552 GetCompressionFlags(file_op.dst_path), archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 if (!result) {
554 error = true;
555 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700556 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700558 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 }
560 return !error;
561}
562
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700563static bool WriteStableIdMapToPath(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700564 IDiagnostics* diag,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 const std::unordered_map<ResourceName, ResourceId>& id_map,
566 const std::string& id_map_path) {
567 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700568 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700569 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 return false;
571 }
572
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700573 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 const ResourceName& name = entry.first;
575 const ResourceId& id = entry.second;
576 fout << name << " = " << id << "\n";
577 }
578
579 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 diag->Error(DiagMessage(id_map_path)
581 << "failed writing to file: "
582 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700583 return false;
584 }
585
586 return true;
587}
588
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589static bool LoadStableIdMap(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 IDiagnostics* diag, const std::string& path,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 std::string content;
593 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 return false;
596 }
597
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700598 out_id_map->clear();
599 size_t line_no = 0;
600 for (StringPiece line : util::Tokenize(content, '\n')) {
601 line_no++;
602 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 if (line.empty()) {
604 continue;
605 }
606
607 auto iter = std::find(line.begin(), line.end(), '=');
608 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 return false;
611 }
612
613 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700614 StringPiece res_name_str =
615 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
616 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
617 diag->Error(DiagMessage(Source(path, line_no))
618 << "invalid resource name '" << res_name_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 return false;
620 }
621
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
623 const size_t res_id_str_len = line.size() - res_id_start_idx;
624 StringPiece res_id_str =
625 util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
628 if (!maybe_id) {
629 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '"
630 << res_id_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631 return false;
632 }
633
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 }
636 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700637}
638
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700639static bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag,
640 std::string* out_path,
641 SplitConstraints* out_split) {
642 std::vector<std::string> parts = util::Split(arg, ':');
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 if (parts.size() != 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700644 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
645 diag->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 DiagMessage()
647 << "should be --split path/to/output.apk:<config>[,<config>...]");
648 return false;
649 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700650 *out_path = parts[0];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 std::vector<ConfigDescription> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 configs.push_back({});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700654 if (!ConfigDescription::Parse(config_str, &configs.back())) {
655 diag->Error(DiagMessage() << "invalid config '" << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 << "' in split parameter '" << arg << "'");
657 return false;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700658 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 out_split->configs.insert(configs.begin(), configs.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 return true;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700662}
663
Adam Lesinskifb48d292015-11-07 15:52:13 -0800664class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 public:
666 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700667 : options_(options),
668 context_(context),
669 final_table_(),
670 file_collection_(util::make_unique<io::FileCollection>()) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700671
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 /**
673 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700676 bool LoadSymbolsFromIncludePaths() {
677 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700678 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 for (const std::string& path : options_.include_paths) {
680 if (context_->IsVerbose()) {
681 context_->GetDiagnostics()->Note(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 << "loading include path");
683 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700684
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700686 std::string error_str;
687 std::unique_ptr<ResourceTable> static_include =
688 LoadStaticLibrary(path, &error_str);
689 if (static_include) {
690 if (!options_.static_lib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 // Can't include static libraries when not building a static library.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700692 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700693 DiagMessage(path)
694 << "can't include static library when building app");
695 return false;
696 }
697
698 // If we are using --no-static-lib-packages, we need to rename the
699 // package of this
700 // table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 if (ResourceTablePackage* pkg =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700703 static_include->FindPackageById(0x7f)) {
704 pkg->name = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705 }
706 }
707
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700708 context_->GetExternalSymbols()->AppendSource(
709 util::make_unique<ResourceTableSymbolSource>(static_include.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700710
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700711 static_table_includes_.push_back(std::move(static_include));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700712
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700713 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700715 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 return false;
717 }
718
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719 if (!asset_source->AddAssetPath(path)) {
720 context_->GetDiagnostics()->Error(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 << "failed to load include path");
722 return false;
723 }
724 }
725
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700726 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 return true;
728 }
729
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 IDiagnostics* diag) {
732 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 if (xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get())) {
734 AppInfo app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736 if (!manifest_el->namespace_uri.empty() ||
737 manifest_el->name != "manifest") {
738 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700739 << "root tag must be <manifest>");
740 return {};
741 }
742
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700743 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
744 if (!package_attr) {
745 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700746 << "<manifest> must have a 'package' attribute");
747 return {};
748 }
749
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750 app_info.package = package_attr->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700752 if (xml::Attribute* version_code_attr =
753 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
754 Maybe<uint32_t> maybe_code =
755 ResourceUtils::ParseInt(version_code_attr->value);
756 if (!maybe_code) {
757 diag->Error(DiagMessage(xml_res->file.source.WithLine(
758 manifest_el->line_number))
759 << "invalid android:versionCode '"
760 << version_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 return {};
762 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700763 app_info.version_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700764 }
765
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766 if (xml::Attribute* revision_code_attr =
767 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
768 Maybe<uint32_t> maybe_code =
769 ResourceUtils::ParseInt(revision_code_attr->value);
770 if (!maybe_code) {
771 diag->Error(DiagMessage(xml_res->file.source.WithLine(
772 manifest_el->line_number))
773 << "invalid android:revisionCode '"
774 << revision_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775 return {};
776 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 app_info.revision_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 }
779
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700780 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
781 if (xml::Attribute* min_sdk = uses_sdk_el->FindAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700782 xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700783 app_info.min_sdk_version = min_sdk->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700784 }
785 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700787 }
788 return {};
789 }
790
791 /**
792 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it
793 * linked.
794 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700795 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700796 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700797 bool VerifyNoExternalPackages() {
798 auto is_ext_package_func =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700799 [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700800 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
801 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700802 };
803
804 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700805 for (const auto& package : final_table_.packages) {
806 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700807 // We have a package that is not related to the one we're building!
808 for (const auto& type : package->types) {
809 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700810 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700812 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700814 // for the 'android' package. This is due to legacy reasons.
815 if (ValueCast<Id>(config_value->value.get()) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700816 package->name == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700817 context_->GetDiagnostics()->Warn(
818 DiagMessage(config_value->value->GetSource())
819 << "generated id '" << res_name
820 << "' for external package '" << package->name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700821 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700822 context_->GetDiagnostics()->Error(
823 DiagMessage(config_value->value->GetSource())
824 << "defined resource '" << res_name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700825 << "' for external package '" << package->name << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700826 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700828 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 }
830 }
831 }
832 }
833
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700834 auto new_end_iter =
835 std::remove_if(final_table_.packages.begin(),
836 final_table_.packages.end(), is_ext_package_func);
837 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838 return !error;
839 }
840
841 /**
842 * Returns true if no IDs have been set, false otherwise.
843 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700844 bool VerifyNoIdsSet() {
845 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700846 for (const auto& type : package->types) {
847 if (type->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700848 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700849 DiagMessage() << "type " << type->type << " has ID " << std::hex
850 << (int)type->id.value() << std::dec
851 << " assigned");
852 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700853 }
854
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 for (const auto& entry : type->entries) {
856 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857 ResourceNameRef res_name(package->name, type->type, entry->name);
858 context_->GetDiagnostics()->Error(
859 DiagMessage() << "entry " << res_name << " has ID " << std::hex
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700860 << (int)entry->id.value() << std::dec
861 << " assigned");
862 return false;
863 }
864 }
865 }
866 }
867 return true;
868 }
869
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700870 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
871 if (options_.output_to_directory) {
872 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700873 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700874 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875 }
876 }
877
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700878 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800880 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700881 if (!flattener.Consume(context_, table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 return false;
883 }
884
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700885 if (writer->StartEntry("resources.arsc", ArchiveEntry::kAlign)) {
886 if (writer->WriteEntry(buffer)) {
887 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888 return true;
889 }
890 }
891 }
892
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700893 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700894 DiagMessage() << "failed to write resources.arsc to archive");
895 return false;
896 }
897
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700898 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700899 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 if (!writer->StartEntry("resources.arsc.flat", 0)) {
901 context_->GetDiagnostics()->Error(DiagMessage() << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700902 return false;
903 }
904
905 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700906 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 {
908 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700909 // ZeroCopyOutputStream interface.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700910 CopyingOutputStreamAdaptor adaptor(writer);
911
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700912 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
913 if (!pb_table->SerializeToZeroCopyStream(&adaptor)) {
914 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700915 return false;
916 }
917 }
918
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700919 if (!writer->FinishEntry()) {
920 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700921 << "failed to finish entry");
922 return false;
923 }
924 return true;
925 }
926
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700927 bool WriteJavaFile(ResourceTable* table,
928 const StringPiece& package_name_to_generate,
929 const StringPiece& out_package,
930 const JavaClassGeneratorOptions& java_options) {
931 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700932 return true;
933 }
934
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700935 std::string out_path = options_.generate_java_class_path.value();
936 file::AppendPath(&out_path, file::PackageToPath(out_package));
937 if (!file::mkdirs(out_path)) {
938 context_->GetDiagnostics()->Error(
939 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700940 return false;
941 }
942
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700943 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700944
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700945 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700946 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700947 context_->GetDiagnostics()->Error(
948 DiagMessage() << "failed writing to '" << out_path << "': "
949 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700950 return false;
951 }
952
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700953 JavaClassGenerator generator(context_, table, java_options);
954 if (!generator.Generate(package_name_to_generate, out_package, &fout)) {
955 context_->GetDiagnostics()->Error(DiagMessage(out_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700956 << generator.getError());
957 return false;
958 }
959
960 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700961 context_->GetDiagnostics()->Error(
962 DiagMessage() << "failed writing to '" << out_path << "': "
963 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700964 }
965 return true;
966 }
967
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700968 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
969 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700970 return true;
971 }
972
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700973 std::unique_ptr<ClassDefinition> manifest_class =
974 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700975
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700976 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 // Something bad happened, but we already logged it, so exit.
978 return false;
979 }
980
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700981 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700982 // Empty Manifest class, no need to generate it.
983 return true;
984 }
985
986 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700987 for (const std::string& annotation : options_.javadoc_annotations) {
988 std::string proper_annotation = "@";
989 proper_annotation += annotation;
990 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700991 }
992
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700993 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700994
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700995 std::string out_path = options_.generate_java_class_path.value();
996 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700997
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700998 if (!file::mkdirs(out_path)) {
999 context_->GetDiagnostics()->Error(
1000 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001001 return false;
1002 }
1003
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001004 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001005
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001006 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001008 context_->GetDiagnostics()->Error(
1009 DiagMessage() << "failed writing to '" << out_path << "': "
1010 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001011 return false;
1012 }
1013
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001014 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8,
1015 true, &fout)) {
1016 context_->GetDiagnostics()->Error(
1017 DiagMessage() << "failed writing to '" << out_path << "': "
1018 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001019 return false;
1020 }
1021 return true;
1022 }
1023
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001024 bool WriteProguardFile(const Maybe<std::string>& out,
1025 const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001026 if (!out) {
1027 return true;
1028 }
1029
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001030 const std::string& out_path = out.value();
1031 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001032 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001033 context_->GetDiagnostics()->Error(
1034 DiagMessage() << "failed to open '" << out_path << "': "
1035 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001036 return false;
1037 }
1038
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001039 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001040 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001041 context_->GetDiagnostics()->Error(
1042 DiagMessage() << "failed writing to '" << out_path << "': "
1043 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001044 return false;
1045 }
1046 return true;
1047 }
1048
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001049 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1050 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001051 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001052 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001053 if (!collection) {
1054 return {};
1055 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001056 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001057 }
1058
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001059 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001060 io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001061 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001062 if (!file) {
1063 return {};
1064 }
1065
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001066 std::unique_ptr<io::IData> data = file->OpenAsData();
1067 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1068 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001069 }
1070
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001071 bool MergeStaticLibrary(const std::string& input, bool override) {
1072 if (context_->IsVerbose()) {
1073 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001074 << "merging static library " << input);
1075 }
1076
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001077 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001078 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001079 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001080 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001081 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001082 return false;
1083 }
1084
1085 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001086 LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001087 if (!table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001088 context_->GetDiagnostics()->Error(DiagMessage(input)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001089 << "invalid static library");
1090 return false;
1091 }
1092
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001093 ResourceTablePackage* pkg = table->FindPackageById(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001094 if (!pkg) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001095 context_->GetDiagnostics()->Error(DiagMessage(input)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001096 << "static library has no package");
1097 return false;
1098 }
1099
1100 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001101 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001102 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001103 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001104
1105 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001106 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001107 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001108 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 }
1110
1111 pkg->name = "";
1112 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001113 result = table_merger_->MergeOverlay(Source(input), table.get(),
1114 collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001115 } else {
1116 result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001117 table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001118 }
1119
1120 } else {
1121 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001122 // preserved and resource names are mangled.
1123 result = table_merger_->MergeAndMangle(Source(input), pkg->name,
1124 table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001125 }
1126
1127 if (!result) {
1128 return false;
1129 }
1130
1131 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001132 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001133 return true;
1134 }
1135
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001136 bool MergeResourceTable(io::IFile* file, bool override) {
1137 if (context_->IsVerbose()) {
1138 context_->GetDiagnostics()->Note(
1139 DiagMessage() << "merging resource table " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001140 }
1141
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001142 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001143 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001144 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001145 << "failed to open file");
1146 return false;
1147 }
1148
1149 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001150 LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1151 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001152 if (!table) {
1153 return false;
1154 }
1155
1156 bool result = false;
1157 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001158 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001159 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001160 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001161 }
1162 return result;
1163 }
1164
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001165 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001166 bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001167 if (context_->IsVerbose()) {
1168 context_->GetDiagnostics()->Note(
1169 DiagMessage() << "merging '" << file_desc->name
1170 << "' from compiled file " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001171 }
1172
1173 bool result = false;
1174 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001175 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001176 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001177 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001178 }
1179
1180 if (!result) {
1181 return false;
1182 }
1183
1184 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001185 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1186 if (exported_symbol.name.package.empty()) {
1187 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001188 }
1189
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001190 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001191
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001192 Maybe<ResourceName> mangled_name =
1193 context_->GetNameMangler()->MangleName(exported_symbol.name);
1194 if (mangled_name) {
1195 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001196 }
1197
1198 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001199 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1200 bool result = final_table_.AddResourceAllowMangled(
1201 res_name, ConfigDescription::DefaultConfig(), std::string(),
1202 std::move(id), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001203 if (!result) {
1204 return false;
1205 }
1206 }
1207 return true;
1208 }
1209
1210 /**
1211 * Takes a path to load as a ZIP file and merges the files within into the
1212 * master ResourceTable.
1213 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001214 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001215 *
1216 * An io::IFileCollection is created from the ZIP file and added to the set of
1217 * io::IFileCollections that are open.
1218 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001219 bool MergeArchive(const std::string& input, bool override) {
1220 if (context_->IsVerbose()) {
1221 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001222 << input);
1223 }
1224
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001225 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001226 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001227 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001228 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001229 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001230 return false;
1231 }
1232
1233 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001234 for (auto iter = collection->Iterator(); iter->HasNext();) {
1235 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001236 error = true;
1237 }
1238 }
1239
1240 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001241 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001242 return !error;
1243 }
1244
1245 /**
1246 * Takes a path to load and merge into the master ResourceTable. If override
1247 * is true,
1248 * conflicting resources are allowed to override each other, in order of last
1249 * seen.
1250 *
1251 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1252 * as ZIP archive
1253 * and the files within are merged individually.
1254 *
1255 * Otherwise the files is processed on its own.
1256 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001257 bool MergePath(const std::string& path, bool override) {
1258 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1259 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1260 return MergeArchive(path, override);
1261 } else if (util::EndsWith(path, ".apk")) {
1262 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001263 }
1264
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001265 io::IFile* file = file_collection_->InsertFile(path);
1266 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001267 }
1268
1269 /**
1270 * Takes a file to load and merge into the master ResourceTable. If override
1271 * is true,
1272 * conflicting resources are allowed to override each other, in order of last
1273 * seen.
1274 *
1275 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1276 * merged into the
1277 * master ResourceTable. If the file ends with .flat, then it is treated like
1278 * a compiled file
1279 * and the header data is read and merged into the final ResourceTable.
1280 *
1281 * All other file types are ignored. This is because these files could be
1282 * coming from a zip,
1283 * where we could have other files like classes.dex.
1284 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001285 bool MergeFile(io::IFile* file, bool override) {
1286 const Source& src = file->GetSource();
1287 if (util::EndsWith(src.path, ".arsc.flat")) {
1288 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001289
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001290 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001291 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001292 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001293 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001294 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001295 return false;
1296 }
1297
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001298 CompiledFileInputStream input_stream(data->data(), data->size());
1299 uint32_t num_files = 0;
1300 if (!input_stream.ReadLittleEndian32(&num_files)) {
1301 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001302 << "failed read num files");
1303 return false;
1304 }
1305
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001306 for (uint32_t i = 0; i < num_files; i++) {
1307 pb::CompiledFile compiled_file;
1308 if (!input_stream.ReadCompiledFile(&compiled_file)) {
1309 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001310 DiagMessage(src) << "failed to read compiled file header");
1311 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001312 }
1313
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001314 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001315 if (!input_stream.ReadDataMetaData(&offset, &len)) {
1316 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001317 << "failed to read data meta data");
1318 return false;
1319 }
1320
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001321 std::unique_ptr<ResourceFile> resource_file =
1322 DeserializeCompiledFileFromPb(compiled_file, file->GetSource(),
1323 context_->GetDiagnostics());
1324 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001325 return false;
1326 }
1327
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001328 if (!MergeCompiledFile(file->CreateFileSegment(offset, len),
1329 resource_file.get(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001330 return false;
1331 }
1332 }
1333 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001334 } else if (util::EndsWith(src.path, ".xml") ||
1335 util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001336 // Since AAPT compiles these file types and appends .flat to them, seeing
1337 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001338 const StringPiece file_type =
1339 util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1340 context_->GetDiagnostics()->Error(DiagMessage(src)
1341 << "uncompiled " << file_type
Adam Lesinski6a396c12016-10-20 14:38:23 -07001342 << " file passed as argument. Must be "
1343 "compiled first into .flat file.");
1344 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001345 }
1346
1347 // Ignore non .flat files. This could be classes.dex or something else that
1348 // happens
1349 // to be in an archive.
1350 return true;
1351 }
1352
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001353 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(
1354 const AppInfo& app_info, const SplitConstraints& constraints) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001355 std::unique_ptr<xml::XmlResource> doc =
1356 util::make_unique<xml::XmlResource>();
1357
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001358 std::unique_ptr<xml::Namespace> namespace_android =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001359 util::make_unique<xml::Namespace>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360 namespace_android->namespace_uri = xml::kSchemaAndroid;
1361 namespace_android->namespace_prefix = "android";
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001362
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001363 std::unique_ptr<xml::Element> manifest_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001364 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001365 manifest_el->name = "manifest";
1366 manifest_el->attributes.push_back(
1367 xml::Attribute{"", "package", app_info.package});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001368
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001369 if (app_info.version_code) {
1370 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001371 xml::Attribute{xml::kSchemaAndroid, "versionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001372 std::to_string(app_info.version_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001373 }
1374
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001375 if (app_info.revision_code) {
1376 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001377 xml::Attribute{xml::kSchemaAndroid, "revisionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001378 std::to_string(app_info.revision_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001379 }
1380
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001381 std::stringstream split_name;
1382 split_name << "config." << util::Joiner(constraints.configs, "_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001383
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001384 manifest_el->attributes.push_back(
1385 xml::Attribute{"", "split", split_name.str()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001386
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001387 std::unique_ptr<xml::Element> application_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001388 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001389 application_el->name = "application";
1390 application_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001391 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false"});
1392
Adam Lesinskie343eb12016-10-27 16:31:58 -07001393 manifest_el->AppendChild(std::move(application_el));
1394 namespace_android->AppendChild(std::move(manifest_el));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001395 doc->root = std::move(namespace_android);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001396 return doc;
1397 }
1398
1399 /**
1400 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001401 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001402 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001403 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001404 xml::XmlResource* manifest, ResourceTable* table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001405 const bool keep_raw_values = options_.static_lib;
1406 bool result = FlattenXml(manifest, "AndroidManifest.xml", {},
1407 keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001408 if (!result) {
1409 return false;
1410 }
1411
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001412 ResourceFileFlattenerOptions file_flattener_options;
1413 file_flattener_options.keep_raw_values = keep_raw_values;
1414 file_flattener_options.do_not_compress_anything =
1415 options_.do_not_compress_anything;
1416 file_flattener_options.extensions_to_not_compress =
1417 options_.extensions_to_not_compress;
1418 file_flattener_options.no_auto_version = options_.no_auto_version;
1419 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001420 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001421 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1422 file_flattener_options.update_proguard_spec =
1423 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001424
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001425 ResourceFileFlattener file_flattener(file_flattener_options, context_,
1426 keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001427
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001428 if (!file_flattener.Flatten(table, writer)) {
1429 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001430 << "failed linking file resources");
1431 return false;
1432 }
1433
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001434 if (options_.static_lib) {
1435 if (!FlattenTableToPb(table, writer)) {
1436 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001437 DiagMessage() << "failed to write resources.arsc.flat");
1438 return false;
1439 }
1440 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001441 if (!FlattenTable(table, writer)) {
1442 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001443 << "failed to write resources.arsc");
1444 return false;
1445 }
1446 }
1447 return true;
1448 }
1449
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001450 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001451 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001452 std::unique_ptr<xml::XmlResource> manifest_xml =
1453 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1454 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001455 return 1;
1456 }
1457
1458 // First extract the Package name without modifying it (via
1459 // --rename-manifest-package).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001460 if (Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1461 manifest_xml.get(), context_->GetDiagnostics())) {
1462 const AppInfo& app_info = maybe_app_info.value();
1463 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001464 }
1465
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001466 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1467 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001468 return 1;
1469 }
1470
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001471 Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1472 manifest_xml.get(), context_->GetDiagnostics());
1473 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001474 return 1;
1475 }
1476
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001477 const AppInfo& app_info = maybe_app_info.value();
1478 if (app_info.min_sdk_version) {
1479 if (Maybe<int> maybe_min_sdk_version = ResourceUtils::ParseSdkVersion(
1480 app_info.min_sdk_version.value())) {
1481 context_->SetMinSdkVersion(maybe_min_sdk_version.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001482 }
1483 }
1484
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001485 context_->SetNameManglerPolicy(
1486 NameManglerPolicy{context_->GetCompilationPackage()});
1487 if (context_->GetCompilationPackage() == "android") {
1488 context_->SetPackageId(0x01);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001489 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001490 context_->SetPackageId(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001491 }
1492
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001493 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001494 return 1;
1495 }
1496
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001497 TableMergerOptions table_merger_options;
1498 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
1499 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_,
1500 table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001501
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001502 if (context_->IsVerbose()) {
1503 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001504 << "linking package '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001505 << context_->GetCompilationPackage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001506 << "' with package ID " << std::hex
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001507 << (int)context_->GetPackageId());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001508 }
1509
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001510 for (const std::string& input : input_files) {
1511 if (!MergePath(input, false)) {
1512 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001513 << "failed parsing input");
1514 return 1;
1515 }
1516 }
1517
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001518 for (const std::string& input : options_.overlay_files) {
1519 if (!MergePath(input, true)) {
1520 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001521 << "failed parsing overlays");
1522 return 1;
1523 }
1524 }
1525
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001526 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001527 return 1;
1528 }
1529
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001530 if (!options_.static_lib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001531 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001532 if (!mover.Consume(context_, &final_table_)) {
1533 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001534 DiagMessage() << "failed moving private attributes");
1535 return 1;
1536 }
1537
1538 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001539 IdAssigner id_assigner(&options_.stable_id_map);
1540 if (!id_assigner.Consume(context_, &final_table_)) {
1541 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001542 << "failed assigning IDs");
1543 return 1;
1544 }
1545
1546 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001547 if (options_.resource_id_map_path) {
1548 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001549 for (auto& type : package->types) {
1550 for (auto& entry : type->entries) {
1551 ResourceName name(package->name, type->type, entry->name);
1552 // The IDs are guaranteed to exist.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001553 options_.stable_id_map[std::move(name)] = ResourceId(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001554 package->id.value(), type->id.value(), entry->id.value());
1555 }
1556 }
1557 }
1558
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001559 if (!WriteStableIdMapToPath(context_->GetDiagnostics(),
1560 options_.stable_id_map,
1561 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001562 return 1;
1563 }
1564 }
1565 } else {
1566 // Static libs are merged with other apps, and ID collisions are bad, so
1567 // verify that
1568 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001569 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001570 return 1;
1571 }
1572 }
1573
1574 // Add the names to mangle based on our source merge earlier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001575 context_->SetNameManglerPolicy(NameManglerPolicy{
1576 context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001577
1578 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001579 context_->GetExternalSymbols()->PrependSource(
1580 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001581
1582 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001583 if (!linker.Consume(context_, &final_table_)) {
1584 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001585 << "failed linking references");
1586 return 1;
1587 }
1588
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001589 if (options_.static_lib) {
1590 if (!options_.products.empty()) {
1591 context_->GetDiagnostics()
1592 ->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001593 << "can't select products when building static library");
1594 }
1595 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 ProductFilter product_filter(options_.products);
1597 if (!product_filter.Consume(context_, &final_table_)) {
1598 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001599 << "failed stripping products");
1600 return 1;
1601 }
1602 }
1603
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001604 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001605 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001606 if (!versioner.Consume(context_, &final_table_)) {
1607 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001608 << "failed versioning styles");
1609 return 1;
1610 }
1611 }
1612
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001613 if (!options_.static_lib && context_->GetMinSdkVersion() > 0) {
1614 if (context_->IsVerbose()) {
1615 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001616 DiagMessage() << "collapsing resource versions for minimum SDK "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001617 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001618 }
1619
1620 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001621 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001622 return 1;
1623 }
1624 }
1625
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001626 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001627 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001628 if (!deduper.Consume(context_, &final_table_)) {
1629 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001630 << "failed deduping resources");
1631 return 1;
1632 }
1633 }
1634
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001635 proguard::KeepSet proguard_keep_set;
1636 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001637
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001638 if (options_.static_lib) {
1639 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001640 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001641 context_->GetDiagnostics()
1642 ->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001643 << "can't strip resources when building static library");
1644 }
1645 } else {
1646 // Adjust the SplitConstraints so that their SDK version is stripped if it
1647 // is less
1648 // than or equal to the minSdk. Otherwise the resources that have had
1649 // their SDK version
1650 // stripped due to minSdk won't ever match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001651 std::vector<SplitConstraints> adjusted_constraints_list;
1652 adjusted_constraints_list.reserve(options_.split_constraints.size());
1653 for (const SplitConstraints& constraints : options_.split_constraints) {
1654 SplitConstraints adjusted_constraints;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001655 for (const ConfigDescription& config : constraints.configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001656 if (config.sdkVersion <= context_->GetMinSdkVersion()) {
1657 adjusted_constraints.configs.insert(config.CopyWithoutSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001658 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001659 adjusted_constraints.configs.insert(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001660 }
1661 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001662 adjusted_constraints_list.push_back(std::move(adjusted_constraints));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001663 }
1664
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001665 TableSplitter table_splitter(adjusted_constraints_list,
1666 options_.table_splitter_options);
1667 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001668 return 1;
1669 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001670 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001671
1672 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001673 auto path_iter = options_.split_paths.begin();
1674 auto split_constraints_iter = adjusted_constraints_list.begin();
1675 for (std::unique_ptr<ResourceTable>& split_table :
1676 table_splitter.splits()) {
1677 if (context_->IsVerbose()) {
1678 context_->GetDiagnostics()->Note(
1679 DiagMessage(*path_iter)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001680 << "generating split with configurations '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001681 << util::Joiner(split_constraints_iter->configs, ", ") << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001682 }
1683
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001684 std::unique_ptr<IArchiveWriter> archive_writer =
1685 MakeArchiveWriter(*path_iter);
1686 if (!archive_writer) {
1687 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001688 << "failed to create archive");
1689 return 1;
1690 }
1691
1692 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001693 std::unique_ptr<xml::XmlResource> split_manifest =
1694 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001695
1696 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001697 if (!linker.Consume(context_, split_manifest.get())) {
1698 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001699 DiagMessage() << "failed to create Split AndroidManifest.xml");
1700 return 1;
1701 }
1702
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001703 if (!WriteApk(archive_writer.get(), &proguard_keep_set,
1704 split_manifest.get(), split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001705 return 1;
1706 }
1707
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001708 ++path_iter;
1709 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001710 }
1711 }
1712
1713 // Start writing the base APK.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001714 std::unique_ptr<IArchiveWriter> archive_writer =
1715 MakeArchiveWriter(options_.output_path);
1716 if (!archive_writer) {
1717 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001718 << "failed to create archive");
1719 return 1;
1720 }
1721
1722 bool error = false;
1723 {
1724 // AndroidManifest.xml has no resource name, but the CallSite is built
1725 // from the name
1726 // (aka, which package the AndroidManifest.xml is coming from).
1727 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001728 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001729
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001730 XmlReferenceLinker manifest_linker;
1731 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1732 if (options_.generate_proguard_rules_path &&
1733 !proguard::CollectProguardRulesForManifest(
1734 Source(options_.manifest_path), manifest_xml.get(),
1735 &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001736 error = true;
1737 }
1738
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001739 if (options_.generate_main_dex_proguard_rules_path &&
1740 !proguard::CollectProguardRulesForManifest(
1741 Source(options_.manifest_path), manifest_xml.get(),
1742 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001743 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001744 }
1745
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001746 if (options_.generate_java_class_path) {
1747 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001748 error = true;
1749 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001750 }
1751
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001752 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001753 // PackageParser will fail if URIs are removed from
1754 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001755 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1756 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001757 error = true;
1758 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001759 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001760 } else {
1761 error = true;
1762 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001763 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001764
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001765 if (error) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001766 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001767 << "failed processing manifest");
1768 return 1;
1769 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001770
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001771 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(),
1772 &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001773 return 1;
1774 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001775
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001776 if (options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001777 JavaClassGeneratorOptions options;
1778 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001779 options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001780
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001781 if (options_.static_lib || options_.generate_non_final_ids) {
1782 options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001783 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001784
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001785 const StringPiece actual_package = context_->GetCompilationPackage();
1786 StringPiece output_package = context_->GetCompilationPackage();
1787 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001788 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001789 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001790 }
1791
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001792 if (options_.private_symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001793 // If we defined a private symbols package, we only emit Public symbols
1794 // to the original package, and private and public symbols to the
1795 // private package.
1796
1797 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001798 if (!WriteJavaFile(&final_table_, context_->GetCompilationPackage(),
1799 output_package, options)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001800 return 1;
1801 }
1802
1803 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001804 output_package = options_.private_symbols.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001805 }
1806
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001807 if (!WriteJavaFile(&final_table_, actual_package, output_package,
1808 options)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001809 return 1;
1810 }
1811
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001812 for (const std::string& extra_package : options_.extra_java_packages) {
1813 if (!WriteJavaFile(&final_table_, actual_package, extra_package,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001814 options)) {
1815 return 1;
1816 }
1817 }
1818 }
1819
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001820 if (!WriteProguardFile(options_.generate_proguard_rules_path,
1821 proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001822 return 1;
1823 }
1824
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001825 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1826 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001827 return 1;
1828 }
1829
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001830 if (context_->IsVerbose()) {
1831 DebugPrintTableOptions debug_print_table_options;
1832 debug_print_table_options.show_sources = true;
1833 Debug::PrintTable(&final_table_, debug_print_table_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001834 }
1835 return 0;
1836 }
1837
1838 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001839 LinkOptions options_;
1840 LinkContext* context_;
1841 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001842
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001843 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001844
1845 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001846 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001847
1848 // A vector of IFileCollections. This is mainly here to keep ownership of the
1849 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001850 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001851
1852 // A vector of ResourceTables. This is here to retain ownership, so that the
1853 // SymbolTable
1854 // can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001855 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001856};
1857
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001858int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001859 LinkContext context;
1860 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001861 std::vector<std::string> overlay_arg_list;
1862 std::vector<std::string> extra_java_packages;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001863 Maybe<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001864 Maybe<std::string> preferred_density;
1865 Maybe<std::string> product_list;
1866 bool legacy_x_flag = false;
1867 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001868 bool verbose = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001869 Maybe<std::string> stable_id_file_path;
1870 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001871 Flags flags =
1872 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001873 .RequiredFlag("-o", "Output path", &options.output_path)
1874 .RequiredFlag("--manifest", "Path to the Android manifest to build",
1875 &options.manifest_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001876 .OptionalFlagList("-I", "Adds an Android APK to link against", &options.include_paths)
1877 .OptionalFlagList("-R",
1878 "Compilation unit to link, using `overlay` semantics.\n"
1879 "The last conflicting resource given takes precedence.",
1880 &overlay_arg_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001881 .OptionalFlag("--java", "Directory in which to generate R.java",
1882 &options.generate_java_class_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001883 .OptionalFlag("--proguard", "Output file for generated Proguard rules",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001884 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001885 .OptionalFlag("--proguard-main-dex",
1886 "Output file for generated Proguard rules for the main dex",
1887 &options.generate_main_dex_proguard_rules_path)
1888 .OptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001889 &options.no_auto_version)
1890 .OptionalSwitch("--no-version-vectors",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001891 "Disables automatic versioning of vector drawables. "
1892 "Use this only\n"
1893 "when building with vector drawable support library",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001894 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001895 .OptionalSwitch("--no-version-transitions",
1896 "Disables automatic versioning of transition resources. "
1897 "Use this only\n"
1898 "when building with transition support library",
1899 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001900 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001901 "Disables automatic deduping of resources with\n"
1902 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001903 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001904 .OptionalSwitch("--enable-sparse-encoding",
1905 "Enables encoding sparse entries using a binary search tree.\n"
1906 "This decreases APK size at the cost of resource retrieval performance.",
1907 &options.table_flattener_options.use_sparse_entries)
1908 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01",
1909 &legacy_x_flag)
1910 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001911 &require_localization)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001912 .OptionalFlag("-c",
1913 "Comma separated list of configurations to include. The default\n"
1914 "is all configurations",
1915 &configs)
1916 .OptionalFlag("--preferred-density",
1917 "Selects the closest matching density and strips out all others.",
1918 &preferred_density)
1919 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001920 .OptionalSwitch("--output-to-dir",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001921 "Outputs the APK contents to a directory specified "
1922 "by -o",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001923 &options.output_to_directory)
1924 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001925 "Removes XML namespace prefix and URI "
1926 "information from AndroidManifest.xml\nand XML "
1927 "binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001928 &options.no_xml_namespaces)
1929 .OptionalFlag("--min-sdk-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001930 "Default minimum SDK version to use for "
1931 "AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001932 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001933 .OptionalFlag("--target-sdk-version",
1934 "Default target SDK version to use for "
1935 "AndroidManifest.xml",
1936 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001937 .OptionalFlag("--version-code",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001938 "Version code (integer) to inject into the "
1939 "AndroidManifest.xml if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001940 &options.manifest_fixer_options.version_code_default)
1941 .OptionalFlag("--version-name",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001942 "Version name to inject into the AndroidManifest.xml "
1943 "if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001944 &options.manifest_fixer_options.version_name_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001945 .OptionalSwitch("--static-lib", "Generate a static Android library", &options.static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001946 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001947 "Merge all library resources under the app's package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001948 &options.no_static_lib_packages)
1949 .OptionalSwitch("--non-final-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001950 "Generates R.java without the final modifier.\n"
1951 "This is implied when --static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001952 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001953 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001954 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001955 .OptionalFlag("--emit-ids",
1956 "Emit a file at the given path with a list of name to ID\n"
1957 "mappings, suitable for use with --stable-ids.",
1958 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001959 .OptionalFlag("--private-symbols",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001960 "Package name to use when generating R.java for "
1961 "private symbols.\n"
1962 "If not specified, public and private symbols will use "
1963 "the application's "
1964 "package name",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001965 &options.private_symbols)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001966 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001967 &options.custom_java_package)
1968 .OptionalFlagList("--extra-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001969 "Generate the same R.java but with different "
1970 "package names",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001971 &extra_java_packages)
1972 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001973 "Adds a JavaDoc annotation to all "
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001974 "generated Java classes",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001975 &options.javadoc_annotations)
1976 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001977 "Allows the addition of new resources in "
1978 "overlays without <add-resource> tags",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001979 &options.auto_add_overlay)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001980 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001981 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001982 .OptionalFlag("--rename-instrumentation-target-package",
1983 "Changes the name of the target package for instrumentation. "
1984 "Most useful "
1985 "when used\nin conjunction with --rename-manifest-package",
1986 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001987 .OptionalFlagList("-0", "File extensions not to compress",
1988 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001989 .OptionalFlagList("--split",
1990 "Split resources matching a set of configs out to a "
1991 "Split APK.\nSyntax: path/to/output.apk:<config>[,<config>[...]]",
1992 &split_args)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001993 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001994
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001995 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001996 return 1;
1997 }
1998
1999 // Expand all argument-files passed into the command line. These start with
2000 // '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002001 std::vector<std::string> arg_list;
2002 for (const std::string& arg : flags.GetArgs()) {
2003 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002004 const std::string path = arg.substr(1, arg.size() - 1);
2005 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002006 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
2007 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002008 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002009 }
2010 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002011 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002012 }
2013 }
2014
2015 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002016 for (const std::string& arg : overlay_arg_list) {
2017 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002018 const std::string path = arg.substr(1, arg.size() - 1);
2019 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002020 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
2021 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002022 return 1;
2023 }
2024 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002025 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002026 }
2027 }
2028
2029 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002030 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002031 }
2032
2033 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002034 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002035 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002036 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002037 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002038 }
2039 }
2040
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002041 if (product_list) {
2042 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002043 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002044 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002045 }
2046 }
2047 }
2048
2049 AxisConfigFilter filter;
2050 if (configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002051 for (const StringPiece& config_str : util::Tokenize(configs.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002052 ConfigDescription config;
2053 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002054 if (lv.InitFromFilterString(config_str)) {
2055 lv.WriteTo(&config);
2056 } else if (!ConfigDescription::Parse(config_str, &config)) {
2057 context.GetDiagnostics()->Error(DiagMessage() << "invalid config '"
2058 << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002059 << "' for -c option");
2060 return 1;
2061 }
2062
2063 if (config.density != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002064 context.GetDiagnostics()->Warn(DiagMessage() << "ignoring density '"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002065 << config
2066 << "' for -c option");
2067 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002068 filter.AddConfig(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002069 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002070 }
2071
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002072 options.table_splitter_options.config_filter = &filter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002073 }
2074
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002075 if (preferred_density) {
2076 ConfigDescription preferred_density_config;
2077 if (!ConfigDescription::Parse(preferred_density.value(),
2078 &preferred_density_config)) {
2079 context.GetDiagnostics()->Error(
2080 DiagMessage() << "invalid density '" << preferred_density.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002081 << "' for --preferred-density option");
2082 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002083 }
2084
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002085 // Clear the version that can be automatically added.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002086 preferred_density_config.sdkVersion = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002087
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002088 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002089 ConfigDescription::CONFIG_DENSITY) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002090 context.GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002091 DiagMessage() << "invalid preferred density '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002092 << preferred_density.value() << "'. "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002093 << "Preferred density must only be a density value");
2094 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002095 }
Pierre Lecesne672384b2017-02-06 10:29:02 +00002096 options.table_splitter_options.preferred_densities.push_back(preferred_density_config.density);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002097 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002098
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002099 if (!options.static_lib && stable_id_file_path) {
2100 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2101 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002102 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002103 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002104 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002105
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002106 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002107 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002108 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2109 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2110 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2111 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2112
2113 // Parse the split parameters.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002114 for (const std::string& split_arg : split_args) {
2115 options.split_paths.push_back({});
2116 options.split_constraints.push_back({});
2117 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(),
2118 &options.split_paths.back(),
2119 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002120 return 1;
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002121 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002122 }
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002123
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002124 // Turn off auto versioning for static-libs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002125 if (options.static_lib) {
2126 options.no_auto_version = true;
2127 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002128 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002129 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002130
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002131 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002132 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002133}
2134
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002135} // namespace aapt