blob: 0501a3bb5fcad77234e995c7341f4711be091af7 [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 Lesinskicacb28f2016-10-19 12:18:14 -070099 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 TableSplitterOptions table_splitter_options;
101 std::vector<SplitConstraints> split_constraints;
102 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 std::unordered_map<ResourceName, ResourceId> stable_id_map;
106 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107};
108
Adam Lesinski64587af2016-02-18 18:33:06 -0800109class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 LinkContext() : name_mangler_({}) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 NameMangler* GetNameMangler() override { return &name_mangler_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
118 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 const std::string& GetCompilationPackage() override {
122 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800126 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800128
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 uint8_t GetPackageId() override { return package_id_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700130
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 void SetPackageId(uint8_t id) { package_id_ = id; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 SymbolTable* GetExternalSymbols() override { return &symbols_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800134
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 bool IsVerbose() override { return verbose_; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800138
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 int GetMinSdkVersion() override { return min_sdk_version_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 void SetMinSdkVersion(int minSdk) { min_sdk_version_ = minSdk; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700142
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 DISALLOW_COPY_AND_ASSIGN(LinkContext);
145
146 StdErrDiagnostics diagnostics_;
147 NameMangler name_mangler_;
148 std::string compilation_package_;
149 uint8_t package_id_ = 0x0;
150 SymbolTable symbols_;
151 bool verbose_ = false;
152 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153};
154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155static bool CopyFileToArchive(io::IFile* file, const std::string& out_path,
156 uint32_t compression_flags,
157 IArchiveWriter* writer, IAaptContext* context) {
158 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 context->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 << "failed to open file");
Adam Lesinski355f2852016-02-13 20:26:45 -0800162 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 }
164
165 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data->data());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 const size_t buffer_size = data->size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 if (context->IsVerbose()) {
169 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 << " to archive");
171 }
172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 if (writer->StartEntry(out_path, compression_flags)) {
174 if (writer->WriteEntry(buffer, buffer_size)) {
175 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 return true;
177 }
178 }
179 }
180
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 context->GetDiagnostics()->Error(DiagMessage() << "failed to write file "
182 << out_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800184}
185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
187 Maybe<size_t> max_sdk_level, bool keep_raw_values,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 IArchiveWriter* writer, IAaptContext* context) {
189 BigBuffer buffer(1024);
190 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 options.keep_raw_values = keep_raw_values;
192 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800195 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 }
197
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 DiagMessage msg;
200 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 if (max_sdk_level) {
202 msg << " maxSdkLevel=" << max_sdk_level.value()
203 << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 }
207
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 if (writer->StartEntry(path, ArchiveEntry::kCompress)) {
209 if (writer->WriteEntry(buffer)) {
210 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 return true;
212 }
213 }
214 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 << " to archive");
217 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800218}
219
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700220static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 const void* data,
222 size_t len,
Adam Lesinski355f2852016-02-13 20:26:45 -0800223 IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 pb::ResourceTable pb_table;
225 if (!pb_table.ParseFromArray(data, len)) {
226 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 return {};
228 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800229
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 if (!table) {
233 return {};
234 }
235 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800236}
237
238/**
239 * Inflates an XML file from the source path.
240 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 IDiagnostics* diag) {
243 std::ifstream fin(path, std::ifstream::binary);
244 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 return {};
247 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800249}
250
Adam Lesinski355f2852016-02-13 20:26:45 -0800251struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252 bool no_auto_version = false;
253 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900254 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 bool no_xml_namespaces = false;
256 bool keep_raw_values = false;
257 bool do_not_compress_anything = false;
258 bool update_proguard_spec = false;
259 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800260};
261
262class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 public:
264 ResourceFileFlattener(const ResourceFileFlattenerOptions& options,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 IAaptContext* context, proguard::KeepSet* keep_set)
266 : options_(options), context_(context), keep_set_(keep_set) {}
Adam Lesinski355f2852016-02-13 20:26:45 -0800267
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800269
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 private:
271 struct FileOperation {
272 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700273
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 // The entry this file came from.
275 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700276
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700279
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700282
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 std::string dst_path;
285 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800287
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
291 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800292
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 ResourceFileFlattenerOptions options_;
294 IAaptContext* context_;
295 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800296};
297
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
299 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 return 0;
301 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800302
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 for (const std::string& extension : options_.extensions_to_not_compress) {
304 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800306 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 }
308 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800309}
310
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900311static bool IsTransitionElement(const std::string& name) {
312 return
313 name == "fade" ||
314 name == "changeBounds" ||
315 name == "slide" ||
316 name == "explode" ||
317 name == "changeImageTransform" ||
318 name == "changeTransform" ||
319 name == "changeClipBounds" ||
320 name == "autoTransition" ||
321 name == "recolor" ||
322 name == "changeScroll" ||
323 name == "transitionSet" ||
324 name == "transition" ||
325 name == "transitionManager";
326}
327
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328bool ResourceFileFlattener::LinkAndVersionXmlFile(
329 ResourceTable* table, FileOperation* file_op,
330 std::queue<FileOperation>* out_file_op_queue) {
331 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 if (context_->IsVerbose()) {
335 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 }
337
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 XmlReferenceLinker xml_linker;
339 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 return false;
341 }
342
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 if (options_.update_proguard_spec &&
344 !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 return false;
346 }
347
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 if (options_.no_xml_namespaces) {
349 XmlNamespaceRemover namespace_remover;
350 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800352 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800354
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355 if (!options_.no_auto_version) {
356 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 xml::Element* el = xml::FindRootElement(doc);
359 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 if (el->name == "vector" || el->name == "animated-vector") {
361 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700364 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700366 }
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900367 if (options_.no_version_transitions) {
368 // Skip this if it is a transition resource.
369 xml::Element* el = xml::FindRootElement(doc);
370 if (el && el->namespace_uri.empty()) {
371 if (IsTransitionElement(el->name)) {
372 // We are NOT going to version this file.
373 file_op->skip_version = true;
374 return true;
375 }
376 }
377 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700378
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380
381 // Find the first SDK level used that is higher than this defined config and
382 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700383 const int min_sdk_version = context_->GetMinSdkVersion();
384 for (int sdk_level : xml_linker.sdk_levels()) {
385 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
386 if (!ShouldGenerateVersionedResource(file_op->entry, config,
387 sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 // If we shouldn't generate a versioned resource, stop checking.
389 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800390 }
391
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 ResourceFile versioned_file_desc = doc->file;
393 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700394
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 FileOperation new_file_op;
396 new_file_op.xml_to_flatten = util::make_unique<xml::XmlResource>(
397 versioned_file_desc, doc->root->Clone());
398 new_file_op.config = versioned_file_desc.config;
399 new_file_op.entry = file_op->entry;
400 new_file_op.dst_path = ResourceUtils::BuildResourceFileName(
401 versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800402
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 if (context_->IsVerbose()) {
404 context_->GetDiagnostics()->Note(
405 DiagMessage(versioned_file_desc.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 << "auto-versioning resource from config '" << config << "' -> '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800408 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 bool added = table->AddFileReferenceAllowMangled(
411 versioned_file_desc.name, versioned_file_desc.config,
412 versioned_file_desc.source, new_file_op.dst_path, nullptr,
413 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 if (!added) {
415 return false;
416 }
417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 break;
420 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800421 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 }
423 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800424}
425
426/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 * Do not insert or remove any resources while executing in this function. It
428 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800429 * corrupt the iteration order.
430 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431bool ResourceFileFlattener::Flatten(ResourceTable* table,
432 IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 bool error = false;
434 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700435 config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800436
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437 for (auto& pkg : table->packages) {
438 for (auto& type : pkg->types) {
439 // Sort by config and name, so that we get better locality in the zip
440 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 config_sorted_files.clear();
442 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800443
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 // Populate the queue with all files in the ResourceTable.
445 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 for (auto& config_value : entry->values) {
447 FileReference* file_ref =
448 ValueCast<FileReference>(config_value->value.get());
449 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700450 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700452
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700453 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700457 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 }
459
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 FileOperation file_op;
461 file_op.entry = entry.get();
462 file_op.dst_path = *file_ref->path;
463 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 if (type->type != ResourceType::kRaw &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 (util::EndsWith(src_path, ".xml.flat") ||
468 util::EndsWith(src_path, ".xml"))) {
469 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 << "failed to open file");
473 return false;
474 }
475
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 file_op.xml_to_flatten =
477 xml::Inflate(data->data(), data->size(),
478 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 return false;
482 }
483
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 file_op.xml_to_flatten->file.config = config_value->config;
485 file_op.xml_to_flatten->file.source = file_ref->GetSource();
486 file_op.xml_to_flatten->file.name =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 ResourceName(pkg->name, type->type, entry->name);
488
489 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493
494 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 // else we end up copying the string in the std::make_pair() method,
496 // then creating a StringPiece from the copy, which would cause us
497 // to end up referencing garbage in the map.
498 const StringPiece entry_name(entry->name);
499 config_sorted_files[std::make_pair(
500 config_value->config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 }
502 }
503 }
504
505 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 for (; !file_operations.empty(); file_operations.pop()) {
507 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700510 error = true;
511 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700512 }
513
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
515 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 // creating a StringPiece from the copy, which would cause us to end up
517 // referencing garbage in the map.
518 const StringPiece entry_name(file_op.entry->name);
519 config_sorted_files[std::make_pair(file_op.config, entry_name)] =
520 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 }
522
523 if (error) {
524 return false;
525 }
526
527 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 for (auto& map_entry : config_sorted_files) {
529 const ConfigDescription& config = map_entry.first.first;
530 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 if (file_op.xml_to_flatten) {
533 Maybe<size_t> max_sdk_level;
534 if (!options_.no_auto_version && !file_op.skip_version) {
535 max_sdk_level =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700537 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 }
539
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700540 bool result = FlattenXml(
541 file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
542 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 if (!result) {
544 error = true;
545 }
546 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 bool result = CopyFileToArchive(
548 file_op.file_to_copy, file_op.dst_path,
549 GetCompressionFlags(file_op.dst_path), archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 if (!result) {
551 error = true;
552 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700553 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700555 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 }
557 return !error;
558}
559
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560static bool WriteStableIdMapToPath(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 IDiagnostics* diag,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 const std::unordered_map<ResourceName, ResourceId>& id_map,
563 const std::string& id_map_path) {
564 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 return false;
568 }
569
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700570 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 const ResourceName& name = entry.first;
572 const ResourceId& id = entry.second;
573 fout << name << " = " << id << "\n";
574 }
575
576 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 diag->Error(DiagMessage(id_map_path)
578 << "failed writing to file: "
579 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 return false;
581 }
582
583 return true;
584}
585
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586static bool LoadStableIdMap(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 IDiagnostics* diag, const std::string& path,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 std::string content;
590 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 return false;
593 }
594
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700595 out_id_map->clear();
596 size_t line_no = 0;
597 for (StringPiece line : util::Tokenize(content, '\n')) {
598 line_no++;
599 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 if (line.empty()) {
601 continue;
602 }
603
604 auto iter = std::find(line.begin(), line.end(), '=');
605 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 return false;
608 }
609
610 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700611 StringPiece res_name_str =
612 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
613 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
614 diag->Error(DiagMessage(Source(path, line_no))
615 << "invalid resource name '" << res_name_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 return false;
617 }
618
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
620 const size_t res_id_str_len = line.size() - res_id_start_idx;
621 StringPiece res_id_str =
622 util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
625 if (!maybe_id) {
626 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '"
627 << res_id_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700628 return false;
629 }
630
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700631 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700632 }
633 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700634}
635
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636static bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag,
637 std::string* out_path,
638 SplitConstraints* out_split) {
639 std::vector<std::string> parts = util::Split(arg, ':');
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 if (parts.size() != 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700641 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
642 diag->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 DiagMessage()
644 << "should be --split path/to/output.apk:<config>[,<config>...]");
645 return false;
646 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 *out_path = parts[0];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 std::vector<ConfigDescription> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700649 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700650 configs.push_back({});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700651 if (!ConfigDescription::Parse(config_str, &configs.back())) {
652 diag->Error(DiagMessage() << "invalid config '" << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 << "' in split parameter '" << arg << "'");
654 return false;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700655 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 out_split->configs.insert(configs.begin(), configs.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 return true;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700659}
660
Adam Lesinskifb48d292015-11-07 15:52:13 -0800661class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700662 public:
663 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 : options_(options),
665 context_(context),
666 final_table_(),
667 file_collection_(util::make_unique<io::FileCollection>()) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700668
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 /**
670 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700671 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 bool LoadSymbolsFromIncludePaths() {
674 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700676 for (const std::string& path : options_.include_paths) {
677 if (context_->IsVerbose()) {
678 context_->GetDiagnostics()->Note(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 << "loading include path");
680 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700681
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 std::string error_str;
684 std::unique_ptr<ResourceTable> static_include =
685 LoadStaticLibrary(path, &error_str);
686 if (static_include) {
687 if (!options_.static_lib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 // Can't include static libraries when not building a static library.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 DiagMessage(path)
691 << "can't include static library when building app");
692 return false;
693 }
694
695 // If we are using --no-static-lib-packages, we need to rename the
696 // package of this
697 // table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 if (ResourceTablePackage* pkg =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700700 static_include->FindPackageById(0x7f)) {
701 pkg->name = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 }
703 }
704
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 context_->GetExternalSymbols()->AppendSource(
706 util::make_unique<ResourceTableSymbolSource>(static_include.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700708 static_table_includes_.push_back(std::move(static_include));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700710 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700713 return false;
714 }
715
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 if (!asset_source->AddAssetPath(path)) {
717 context_->GetDiagnostics()->Error(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718 << "failed to load include path");
719 return false;
720 }
721 }
722
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 return true;
725 }
726
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700727 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700728 IDiagnostics* diag) {
729 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 if (xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get())) {
731 AppInfo app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 if (!manifest_el->namespace_uri.empty() ||
734 manifest_el->name != "manifest") {
735 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700736 << "root tag must be <manifest>");
737 return {};
738 }
739
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700740 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
741 if (!package_attr) {
742 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 << "<manifest> must have a 'package' attribute");
744 return {};
745 }
746
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700747 app_info.package = package_attr->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700749 if (xml::Attribute* version_code_attr =
750 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
751 Maybe<uint32_t> maybe_code =
752 ResourceUtils::ParseInt(version_code_attr->value);
753 if (!maybe_code) {
754 diag->Error(DiagMessage(xml_res->file.source.WithLine(
755 manifest_el->line_number))
756 << "invalid android:versionCode '"
757 << version_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 return {};
759 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760 app_info.version_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 }
762
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700763 if (xml::Attribute* revision_code_attr =
764 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
765 Maybe<uint32_t> maybe_code =
766 ResourceUtils::ParseInt(revision_code_attr->value);
767 if (!maybe_code) {
768 diag->Error(DiagMessage(xml_res->file.source.WithLine(
769 manifest_el->line_number))
770 << "invalid android:revisionCode '"
771 << revision_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700772 return {};
773 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700774 app_info.revision_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775 }
776
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
778 if (xml::Attribute* min_sdk = uses_sdk_el->FindAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700779 xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700780 app_info.min_sdk_version = min_sdk->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 }
782 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700783 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700784 }
785 return {};
786 }
787
788 /**
789 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it
790 * linked.
791 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700792 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700793 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700794 bool VerifyNoExternalPackages() {
795 auto is_ext_package_func =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700796 [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700797 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
798 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700799 };
800
801 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700802 for (const auto& package : final_table_.packages) {
803 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700804 // We have a package that is not related to the one we're building!
805 for (const auto& type : package->types) {
806 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700807 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700808
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700809 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700810 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700811 // for the 'android' package. This is due to legacy reasons.
812 if (ValueCast<Id>(config_value->value.get()) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 package->name == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700814 context_->GetDiagnostics()->Warn(
815 DiagMessage(config_value->value->GetSource())
816 << "generated id '" << res_name
817 << "' for external package '" << package->name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700818 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700819 context_->GetDiagnostics()->Error(
820 DiagMessage(config_value->value->GetSource())
821 << "defined resource '" << res_name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 << "' for external package '" << package->name << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700823 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700824 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700825 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700826 }
827 }
828 }
829 }
830
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700831 auto new_end_iter =
832 std::remove_if(final_table_.packages.begin(),
833 final_table_.packages.end(), is_ext_package_func);
834 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700835 return !error;
836 }
837
838 /**
839 * Returns true if no IDs have been set, false otherwise.
840 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700841 bool VerifyNoIdsSet() {
842 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700843 for (const auto& type : package->types) {
844 if (type->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700845 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700846 DiagMessage() << "type " << type->type << " has ID " << std::hex
847 << (int)type->id.value() << std::dec
848 << " assigned");
849 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700850 }
851
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700852 for (const auto& entry : type->entries) {
853 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700854 ResourceNameRef res_name(package->name, type->type, entry->name);
855 context_->GetDiagnostics()->Error(
856 DiagMessage() << "entry " << res_name << " has ID " << std::hex
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857 << (int)entry->id.value() << std::dec
858 << " assigned");
859 return false;
860 }
861 }
862 }
863 }
864 return true;
865 }
866
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700867 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
868 if (options_.output_to_directory) {
869 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700870 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700872 }
873 }
874
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700875 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700876 BigBuffer buffer(1024);
877 TableFlattener flattener(&buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700878 if (!flattener.Consume(context_, table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 return false;
880 }
881
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700882 if (writer->StartEntry("resources.arsc", ArchiveEntry::kAlign)) {
883 if (writer->WriteEntry(buffer)) {
884 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700885 return true;
886 }
887 }
888 }
889
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700890 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700891 DiagMessage() << "failed to write resources.arsc to archive");
892 return false;
893 }
894
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700895 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700896 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700897 if (!writer->StartEntry("resources.arsc.flat", 0)) {
898 context_->GetDiagnostics()->Error(DiagMessage() << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700899 return false;
900 }
901
902 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700904 {
905 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700906 // ZeroCopyOutputStream interface.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 CopyingOutputStreamAdaptor adaptor(writer);
908
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700909 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
910 if (!pb_table->SerializeToZeroCopyStream(&adaptor)) {
911 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700912 return false;
913 }
914 }
915
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700916 if (!writer->FinishEntry()) {
917 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700918 << "failed to finish entry");
919 return false;
920 }
921 return true;
922 }
923
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700924 bool WriteJavaFile(ResourceTable* table,
925 const StringPiece& package_name_to_generate,
926 const StringPiece& out_package,
927 const JavaClassGeneratorOptions& java_options) {
928 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700929 return true;
930 }
931
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700932 std::string out_path = options_.generate_java_class_path.value();
933 file::AppendPath(&out_path, file::PackageToPath(out_package));
934 if (!file::mkdirs(out_path)) {
935 context_->GetDiagnostics()->Error(
936 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700937 return false;
938 }
939
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700940 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700941
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700942 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700943 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700944 context_->GetDiagnostics()->Error(
945 DiagMessage() << "failed writing to '" << out_path << "': "
946 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700947 return false;
948 }
949
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700950 JavaClassGenerator generator(context_, table, java_options);
951 if (!generator.Generate(package_name_to_generate, out_package, &fout)) {
952 context_->GetDiagnostics()->Error(DiagMessage(out_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953 << generator.getError());
954 return false;
955 }
956
957 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700958 context_->GetDiagnostics()->Error(
959 DiagMessage() << "failed writing to '" << out_path << "': "
960 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700961 }
962 return true;
963 }
964
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700965 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
966 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700967 return true;
968 }
969
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700970 std::unique_ptr<ClassDefinition> manifest_class =
971 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700972
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700973 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700974 // Something bad happened, but we already logged it, so exit.
975 return false;
976 }
977
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700978 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700979 // Empty Manifest class, no need to generate it.
980 return true;
981 }
982
983 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 for (const std::string& annotation : options_.javadoc_annotations) {
985 std::string proper_annotation = "@";
986 proper_annotation += annotation;
987 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700988 }
989
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700990 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700991
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700992 std::string out_path = options_.generate_java_class_path.value();
993 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700994
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700995 if (!file::mkdirs(out_path)) {
996 context_->GetDiagnostics()->Error(
997 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700998 return false;
999 }
1000
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001001 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001002
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001003 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001004 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001005 context_->GetDiagnostics()->Error(
1006 DiagMessage() << "failed writing to '" << out_path << "': "
1007 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001008 return false;
1009 }
1010
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001011 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8,
1012 true, &fout)) {
1013 context_->GetDiagnostics()->Error(
1014 DiagMessage() << "failed writing to '" << out_path << "': "
1015 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001016 return false;
1017 }
1018 return true;
1019 }
1020
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001021 bool WriteProguardFile(const Maybe<std::string>& out,
1022 const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001023 if (!out) {
1024 return true;
1025 }
1026
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 const std::string& out_path = out.value();
1028 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001029 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001030 context_->GetDiagnostics()->Error(
1031 DiagMessage() << "failed to open '" << out_path << "': "
1032 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001033 return false;
1034 }
1035
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001036 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001037 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001038 context_->GetDiagnostics()->Error(
1039 DiagMessage() << "failed writing to '" << out_path << "': "
1040 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001041 return false;
1042 }
1043 return true;
1044 }
1045
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001046 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1047 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001048 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001049 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001050 if (!collection) {
1051 return {};
1052 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001053 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001054 }
1055
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001056 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001057 io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001058 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001059 if (!file) {
1060 return {};
1061 }
1062
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001063 std::unique_ptr<io::IData> data = file->OpenAsData();
1064 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1065 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001066 }
1067
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001068 bool MergeStaticLibrary(const std::string& input, bool override) {
1069 if (context_->IsVerbose()) {
1070 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001071 << "merging static library " << input);
1072 }
1073
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001074 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001076 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001077 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001078 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001079 return false;
1080 }
1081
1082 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001083 LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001084 if (!table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001085 context_->GetDiagnostics()->Error(DiagMessage(input)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001086 << "invalid static library");
1087 return false;
1088 }
1089
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001090 ResourceTablePackage* pkg = table->FindPackageById(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091 if (!pkg) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001092 context_->GetDiagnostics()->Error(DiagMessage(input)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001093 << "static library has no package");
1094 return false;
1095 }
1096
1097 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001098 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001099 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001100 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001101
1102 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001103 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001104 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001105 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001106 }
1107
1108 pkg->name = "";
1109 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001110 result = table_merger_->MergeOverlay(Source(input), table.get(),
1111 collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001112 } else {
1113 result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001114 table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001115 }
1116
1117 } else {
1118 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001119 // preserved and resource names are mangled.
1120 result = table_merger_->MergeAndMangle(Source(input), pkg->name,
1121 table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001122 }
1123
1124 if (!result) {
1125 return false;
1126 }
1127
1128 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001129 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001130 return true;
1131 }
1132
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001133 bool MergeResourceTable(io::IFile* file, bool override) {
1134 if (context_->IsVerbose()) {
1135 context_->GetDiagnostics()->Note(
1136 DiagMessage() << "merging resource table " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001137 }
1138
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001139 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001140 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001141 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001142 << "failed to open file");
1143 return false;
1144 }
1145
1146 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001147 LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1148 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001149 if (!table) {
1150 return false;
1151 }
1152
1153 bool result = false;
1154 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001155 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001156 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001157 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001158 }
1159 return result;
1160 }
1161
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001162 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001163 bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001164 if (context_->IsVerbose()) {
1165 context_->GetDiagnostics()->Note(
1166 DiagMessage() << "merging '" << file_desc->name
1167 << "' from compiled file " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001168 }
1169
1170 bool result = false;
1171 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001172 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001173 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001174 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001175 }
1176
1177 if (!result) {
1178 return false;
1179 }
1180
1181 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001182 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1183 if (exported_symbol.name.package.empty()) {
1184 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001185 }
1186
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001187 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001188
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001189 Maybe<ResourceName> mangled_name =
1190 context_->GetNameMangler()->MangleName(exported_symbol.name);
1191 if (mangled_name) {
1192 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001193 }
1194
1195 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001196 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1197 bool result = final_table_.AddResourceAllowMangled(
1198 res_name, ConfigDescription::DefaultConfig(), std::string(),
1199 std::move(id), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001200 if (!result) {
1201 return false;
1202 }
1203 }
1204 return true;
1205 }
1206
1207 /**
1208 * Takes a path to load as a ZIP file and merges the files within into the
1209 * master ResourceTable.
1210 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001211 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001212 *
1213 * An io::IFileCollection is created from the ZIP file and added to the set of
1214 * io::IFileCollections that are open.
1215 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001216 bool MergeArchive(const std::string& input, bool override) {
1217 if (context_->IsVerbose()) {
1218 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001219 << input);
1220 }
1221
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001222 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001223 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001224 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001225 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001226 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001227 return false;
1228 }
1229
1230 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001231 for (auto iter = collection->Iterator(); iter->HasNext();) {
1232 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001233 error = true;
1234 }
1235 }
1236
1237 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001238 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001239 return !error;
1240 }
1241
1242 /**
1243 * Takes a path to load and merge into the master ResourceTable. If override
1244 * is true,
1245 * conflicting resources are allowed to override each other, in order of last
1246 * seen.
1247 *
1248 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1249 * as ZIP archive
1250 * and the files within are merged individually.
1251 *
1252 * Otherwise the files is processed on its own.
1253 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001254 bool MergePath(const std::string& path, bool override) {
1255 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1256 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1257 return MergeArchive(path, override);
1258 } else if (util::EndsWith(path, ".apk")) {
1259 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001260 }
1261
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001262 io::IFile* file = file_collection_->InsertFile(path);
1263 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001264 }
1265
1266 /**
1267 * Takes a file to load and merge into the master ResourceTable. If override
1268 * is true,
1269 * conflicting resources are allowed to override each other, in order of last
1270 * seen.
1271 *
1272 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1273 * merged into the
1274 * master ResourceTable. If the file ends with .flat, then it is treated like
1275 * a compiled file
1276 * and the header data is read and merged into the final ResourceTable.
1277 *
1278 * All other file types are ignored. This is because these files could be
1279 * coming from a zip,
1280 * where we could have other files like classes.dex.
1281 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001282 bool MergeFile(io::IFile* file, bool override) {
1283 const Source& src = file->GetSource();
1284 if (util::EndsWith(src.path, ".arsc.flat")) {
1285 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001286
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001287 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001288 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001289 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001290 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001291 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001292 return false;
1293 }
1294
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001295 CompiledFileInputStream input_stream(data->data(), data->size());
1296 uint32_t num_files = 0;
1297 if (!input_stream.ReadLittleEndian32(&num_files)) {
1298 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001299 << "failed read num files");
1300 return false;
1301 }
1302
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001303 for (uint32_t i = 0; i < num_files; i++) {
1304 pb::CompiledFile compiled_file;
1305 if (!input_stream.ReadCompiledFile(&compiled_file)) {
1306 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001307 DiagMessage(src) << "failed to read compiled file header");
1308 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001309 }
1310
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001311 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001312 if (!input_stream.ReadDataMetaData(&offset, &len)) {
1313 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001314 << "failed to read data meta data");
1315 return false;
1316 }
1317
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001318 std::unique_ptr<ResourceFile> resource_file =
1319 DeserializeCompiledFileFromPb(compiled_file, file->GetSource(),
1320 context_->GetDiagnostics());
1321 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001322 return false;
1323 }
1324
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001325 if (!MergeCompiledFile(file->CreateFileSegment(offset, len),
1326 resource_file.get(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001327 return false;
1328 }
1329 }
1330 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001331 } else if (util::EndsWith(src.path, ".xml") ||
1332 util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001333 // Since AAPT compiles these file types and appends .flat to them, seeing
1334 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001335 const StringPiece file_type =
1336 util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1337 context_->GetDiagnostics()->Error(DiagMessage(src)
1338 << "uncompiled " << file_type
Adam Lesinski6a396c12016-10-20 14:38:23 -07001339 << " file passed as argument. Must be "
1340 "compiled first into .flat file.");
1341 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001342 }
1343
1344 // Ignore non .flat files. This could be classes.dex or something else that
1345 // happens
1346 // to be in an archive.
1347 return true;
1348 }
1349
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001350 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(
1351 const AppInfo& app_info, const SplitConstraints& constraints) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001352 std::unique_ptr<xml::XmlResource> doc =
1353 util::make_unique<xml::XmlResource>();
1354
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001355 std::unique_ptr<xml::Namespace> namespace_android =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001356 util::make_unique<xml::Namespace>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001357 namespace_android->namespace_uri = xml::kSchemaAndroid;
1358 namespace_android->namespace_prefix = "android";
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001359
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360 std::unique_ptr<xml::Element> manifest_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001361 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001362 manifest_el->name = "manifest";
1363 manifest_el->attributes.push_back(
1364 xml::Attribute{"", "package", app_info.package});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001365
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001366 if (app_info.version_code) {
1367 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001368 xml::Attribute{xml::kSchemaAndroid, "versionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001369 std::to_string(app_info.version_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001370 }
1371
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001372 if (app_info.revision_code) {
1373 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001374 xml::Attribute{xml::kSchemaAndroid, "revisionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001375 std::to_string(app_info.revision_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001376 }
1377
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001378 std::stringstream split_name;
1379 split_name << "config." << util::Joiner(constraints.configs, "_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001380
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001381 manifest_el->attributes.push_back(
1382 xml::Attribute{"", "split", split_name.str()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001383
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001384 std::unique_ptr<xml::Element> application_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001385 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001386 application_el->name = "application";
1387 application_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001388 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false"});
1389
Adam Lesinskie343eb12016-10-27 16:31:58 -07001390 manifest_el->AppendChild(std::move(application_el));
1391 namespace_android->AppendChild(std::move(manifest_el));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001392 doc->root = std::move(namespace_android);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001393 return doc;
1394 }
1395
1396 /**
1397 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001398 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001399 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001401 xml::XmlResource* manifest, ResourceTable* table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001402 const bool keep_raw_values = options_.static_lib;
1403 bool result = FlattenXml(manifest, "AndroidManifest.xml", {},
1404 keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001405 if (!result) {
1406 return false;
1407 }
1408
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001409 ResourceFileFlattenerOptions file_flattener_options;
1410 file_flattener_options.keep_raw_values = keep_raw_values;
1411 file_flattener_options.do_not_compress_anything =
1412 options_.do_not_compress_anything;
1413 file_flattener_options.extensions_to_not_compress =
1414 options_.extensions_to_not_compress;
1415 file_flattener_options.no_auto_version = options_.no_auto_version;
1416 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001417 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001418 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1419 file_flattener_options.update_proguard_spec =
1420 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001421
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001422 ResourceFileFlattener file_flattener(file_flattener_options, context_,
1423 keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001424
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001425 if (!file_flattener.Flatten(table, writer)) {
1426 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001427 << "failed linking file resources");
1428 return false;
1429 }
1430
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001431 if (options_.static_lib) {
1432 if (!FlattenTableToPb(table, writer)) {
1433 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001434 DiagMessage() << "failed to write resources.arsc.flat");
1435 return false;
1436 }
1437 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001438 if (!FlattenTable(table, writer)) {
1439 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001440 << "failed to write resources.arsc");
1441 return false;
1442 }
1443 }
1444 return true;
1445 }
1446
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001447 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001448 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001449 std::unique_ptr<xml::XmlResource> manifest_xml =
1450 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1451 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001452 return 1;
1453 }
1454
1455 // First extract the Package name without modifying it (via
1456 // --rename-manifest-package).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001457 if (Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1458 manifest_xml.get(), context_->GetDiagnostics())) {
1459 const AppInfo& app_info = maybe_app_info.value();
1460 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001461 }
1462
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001463 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1464 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001465 return 1;
1466 }
1467
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001468 Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1469 manifest_xml.get(), context_->GetDiagnostics());
1470 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001471 return 1;
1472 }
1473
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001474 const AppInfo& app_info = maybe_app_info.value();
1475 if (app_info.min_sdk_version) {
1476 if (Maybe<int> maybe_min_sdk_version = ResourceUtils::ParseSdkVersion(
1477 app_info.min_sdk_version.value())) {
1478 context_->SetMinSdkVersion(maybe_min_sdk_version.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001479 }
1480 }
1481
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001482 context_->SetNameManglerPolicy(
1483 NameManglerPolicy{context_->GetCompilationPackage()});
1484 if (context_->GetCompilationPackage() == "android") {
1485 context_->SetPackageId(0x01);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001486 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001487 context_->SetPackageId(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001488 }
1489
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001490 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001491 return 1;
1492 }
1493
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001494 TableMergerOptions table_merger_options;
1495 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
1496 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_,
1497 table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001498
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001499 if (context_->IsVerbose()) {
1500 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001501 << "linking package '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001502 << context_->GetCompilationPackage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001503 << "' with package ID " << std::hex
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001504 << (int)context_->GetPackageId());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001505 }
1506
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001507 for (const std::string& input : input_files) {
1508 if (!MergePath(input, false)) {
1509 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001510 << "failed parsing input");
1511 return 1;
1512 }
1513 }
1514
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001515 for (const std::string& input : options_.overlay_files) {
1516 if (!MergePath(input, true)) {
1517 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001518 << "failed parsing overlays");
1519 return 1;
1520 }
1521 }
1522
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001523 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001524 return 1;
1525 }
1526
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001527 if (!options_.static_lib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001528 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001529 if (!mover.Consume(context_, &final_table_)) {
1530 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001531 DiagMessage() << "failed moving private attributes");
1532 return 1;
1533 }
1534
1535 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001536 IdAssigner id_assigner(&options_.stable_id_map);
1537 if (!id_assigner.Consume(context_, &final_table_)) {
1538 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001539 << "failed assigning IDs");
1540 return 1;
1541 }
1542
1543 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001544 if (options_.resource_id_map_path) {
1545 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001546 for (auto& type : package->types) {
1547 for (auto& entry : type->entries) {
1548 ResourceName name(package->name, type->type, entry->name);
1549 // The IDs are guaranteed to exist.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001550 options_.stable_id_map[std::move(name)] = ResourceId(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001551 package->id.value(), type->id.value(), entry->id.value());
1552 }
1553 }
1554 }
1555
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001556 if (!WriteStableIdMapToPath(context_->GetDiagnostics(),
1557 options_.stable_id_map,
1558 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001559 return 1;
1560 }
1561 }
1562 } else {
1563 // Static libs are merged with other apps, and ID collisions are bad, so
1564 // verify that
1565 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001566 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001567 return 1;
1568 }
1569 }
1570
1571 // Add the names to mangle based on our source merge earlier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001572 context_->SetNameManglerPolicy(NameManglerPolicy{
1573 context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001574
1575 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001576 context_->GetExternalSymbols()->PrependSource(
1577 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001578
1579 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001580 if (!linker.Consume(context_, &final_table_)) {
1581 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001582 << "failed linking references");
1583 return 1;
1584 }
1585
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001586 if (options_.static_lib) {
1587 if (!options_.products.empty()) {
1588 context_->GetDiagnostics()
1589 ->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001590 << "can't select products when building static library");
1591 }
1592 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001593 ProductFilter product_filter(options_.products);
1594 if (!product_filter.Consume(context_, &final_table_)) {
1595 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001596 << "failed stripping products");
1597 return 1;
1598 }
1599 }
1600
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001601 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001602 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001603 if (!versioner.Consume(context_, &final_table_)) {
1604 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001605 << "failed versioning styles");
1606 return 1;
1607 }
1608 }
1609
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001610 if (!options_.static_lib && context_->GetMinSdkVersion() > 0) {
1611 if (context_->IsVerbose()) {
1612 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001613 DiagMessage() << "collapsing resource versions for minimum SDK "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001614 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001615 }
1616
1617 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001618 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001619 return 1;
1620 }
1621 }
1622
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001623 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001624 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001625 if (!deduper.Consume(context_, &final_table_)) {
1626 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001627 << "failed deduping resources");
1628 return 1;
1629 }
1630 }
1631
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001632 proguard::KeepSet proguard_keep_set;
1633 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001634
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001635 if (options_.static_lib) {
1636 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001637 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001638 context_->GetDiagnostics()
1639 ->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001640 << "can't strip resources when building static library");
1641 }
1642 } else {
1643 // Adjust the SplitConstraints so that their SDK version is stripped if it
1644 // is less
1645 // than or equal to the minSdk. Otherwise the resources that have had
1646 // their SDK version
1647 // stripped due to minSdk won't ever match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001648 std::vector<SplitConstraints> adjusted_constraints_list;
1649 adjusted_constraints_list.reserve(options_.split_constraints.size());
1650 for (const SplitConstraints& constraints : options_.split_constraints) {
1651 SplitConstraints adjusted_constraints;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001652 for (const ConfigDescription& config : constraints.configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001653 if (config.sdkVersion <= context_->GetMinSdkVersion()) {
1654 adjusted_constraints.configs.insert(config.CopyWithoutSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001655 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001656 adjusted_constraints.configs.insert(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001657 }
1658 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001659 adjusted_constraints_list.push_back(std::move(adjusted_constraints));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001660 }
1661
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001662 TableSplitter table_splitter(adjusted_constraints_list,
1663 options_.table_splitter_options);
1664 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001665 return 1;
1666 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001667 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001668
1669 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001670 auto path_iter = options_.split_paths.begin();
1671 auto split_constraints_iter = adjusted_constraints_list.begin();
1672 for (std::unique_ptr<ResourceTable>& split_table :
1673 table_splitter.splits()) {
1674 if (context_->IsVerbose()) {
1675 context_->GetDiagnostics()->Note(
1676 DiagMessage(*path_iter)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001677 << "generating split with configurations '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001678 << util::Joiner(split_constraints_iter->configs, ", ") << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001679 }
1680
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001681 std::unique_ptr<IArchiveWriter> archive_writer =
1682 MakeArchiveWriter(*path_iter);
1683 if (!archive_writer) {
1684 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001685 << "failed to create archive");
1686 return 1;
1687 }
1688
1689 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001690 std::unique_ptr<xml::XmlResource> split_manifest =
1691 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001692
1693 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001694 if (!linker.Consume(context_, split_manifest.get())) {
1695 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001696 DiagMessage() << "failed to create Split AndroidManifest.xml");
1697 return 1;
1698 }
1699
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001700 if (!WriteApk(archive_writer.get(), &proguard_keep_set,
1701 split_manifest.get(), split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001702 return 1;
1703 }
1704
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001705 ++path_iter;
1706 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001707 }
1708 }
1709
1710 // Start writing the base APK.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001711 std::unique_ptr<IArchiveWriter> archive_writer =
1712 MakeArchiveWriter(options_.output_path);
1713 if (!archive_writer) {
1714 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001715 << "failed to create archive");
1716 return 1;
1717 }
1718
1719 bool error = false;
1720 {
1721 // AndroidManifest.xml has no resource name, but the CallSite is built
1722 // from the name
1723 // (aka, which package the AndroidManifest.xml is coming from).
1724 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001725 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001726
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001727 XmlReferenceLinker manifest_linker;
1728 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1729 if (options_.generate_proguard_rules_path &&
1730 !proguard::CollectProguardRulesForManifest(
1731 Source(options_.manifest_path), manifest_xml.get(),
1732 &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001733 error = true;
1734 }
1735
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001736 if (options_.generate_main_dex_proguard_rules_path &&
1737 !proguard::CollectProguardRulesForManifest(
1738 Source(options_.manifest_path), manifest_xml.get(),
1739 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001740 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001741 }
1742
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001743 if (options_.generate_java_class_path) {
1744 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001745 error = true;
1746 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001747 }
1748
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001749 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001750 // PackageParser will fail if URIs are removed from
1751 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001752 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1753 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001754 error = true;
1755 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001756 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001757 } else {
1758 error = true;
1759 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001760 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001761
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001762 if (error) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001763 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001764 << "failed processing manifest");
1765 return 1;
1766 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001767
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001768 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(),
1769 &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001770 return 1;
1771 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001772
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001773 if (options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001774 JavaClassGeneratorOptions options;
1775 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001776 options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001777
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001778 if (options_.static_lib || options_.generate_non_final_ids) {
1779 options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001780 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001781
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001782 const StringPiece actual_package = context_->GetCompilationPackage();
1783 StringPiece output_package = context_->GetCompilationPackage();
1784 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001785 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001786 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001787 }
1788
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001789 if (options_.private_symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001790 // If we defined a private symbols package, we only emit Public symbols
1791 // to the original package, and private and public symbols to the
1792 // private package.
1793
1794 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001795 if (!WriteJavaFile(&final_table_, context_->GetCompilationPackage(),
1796 output_package, options)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001797 return 1;
1798 }
1799
1800 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001801 output_package = options_.private_symbols.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001802 }
1803
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001804 if (!WriteJavaFile(&final_table_, actual_package, output_package,
1805 options)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001806 return 1;
1807 }
1808
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001809 for (const std::string& extra_package : options_.extra_java_packages) {
1810 if (!WriteJavaFile(&final_table_, actual_package, extra_package,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001811 options)) {
1812 return 1;
1813 }
1814 }
1815 }
1816
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001817 if (!WriteProguardFile(options_.generate_proguard_rules_path,
1818 proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001819 return 1;
1820 }
1821
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001822 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1823 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001824 return 1;
1825 }
1826
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001827 if (context_->IsVerbose()) {
1828 DebugPrintTableOptions debug_print_table_options;
1829 debug_print_table_options.show_sources = true;
1830 Debug::PrintTable(&final_table_, debug_print_table_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001831 }
1832 return 0;
1833 }
1834
1835 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001836 LinkOptions options_;
1837 LinkContext* context_;
1838 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001839
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001840 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001841
1842 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001843 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001844
1845 // A vector of IFileCollections. This is mainly here to keep ownership of the
1846 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001847 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001848
1849 // A vector of ResourceTables. This is here to retain ownership, so that the
1850 // SymbolTable
1851 // can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001852 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001853};
1854
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001855int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001856 LinkContext context;
1857 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001858 std::vector<std::string> overlay_arg_list;
1859 std::vector<std::string> extra_java_packages;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001860 Maybe<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001861 Maybe<std::string> preferred_density;
1862 Maybe<std::string> product_list;
1863 bool legacy_x_flag = false;
1864 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001865 bool verbose = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001866 Maybe<std::string> stable_id_file_path;
1867 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001868 Flags flags =
1869 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001870 .RequiredFlag("-o", "Output path", &options.output_path)
1871 .RequiredFlag("--manifest", "Path to the Android manifest to build",
1872 &options.manifest_path)
1873 .OptionalFlagList("-I", "Adds an Android APK to link against",
1874 &options.include_paths)
1875 .OptionalFlagList(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001876 "-R",
1877 "Compilation unit to link, using `overlay` semantics.\n"
1878 "The last conflicting resource given takes precedence.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001879 &overlay_arg_list)
1880 .OptionalFlag("--java", "Directory in which to generate R.java",
1881 &options.generate_java_class_path)
1882 .OptionalFlag("--proguard",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001883 "Output file for generated Proguard rules",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001884 &options.generate_proguard_rules_path)
1885 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001886 "--proguard-main-dex",
1887 "Output file for generated Proguard rules for the main dex",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001888 &options.generate_main_dex_proguard_rules_path)
1889 .OptionalSwitch("--no-auto-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001890 "Disables automatic style and layout SDK versioning",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001891 &options.no_auto_version)
1892 .OptionalSwitch("--no-version-vectors",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001893 "Disables automatic versioning of vector drawables. "
1894 "Use this only\n"
1895 "when building with vector drawable support library",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001896 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001897 .OptionalSwitch("--no-version-transitions",
1898 "Disables automatic versioning of transition resources. "
1899 "Use this only\n"
1900 "when building with transition support library",
1901 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001902 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001903 "Disables automatic deduping of resources with\n"
1904 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001905 &options.no_resource_deduping)
1906 .OptionalSwitch(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001907 "-x",
1908 "Legacy flag that specifies to use the package identifier 0x01",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001909 &legacy_x_flag)
1910 .OptionalSwitch("-z",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001911 "Require localization of strings marked 'suggested'",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001912 &require_localization)
1913 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001914 "-c",
1915 "Comma separated list of configurations to include. The default\n"
1916 "is all configurations",
1917 &configs)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001918 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001919 "--preferred-density",
1920 "Selects the closest matching density and strips out all others.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001921 &preferred_density)
1922 .OptionalFlag("--product",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001923 "Comma separated list of product names to keep",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001924 &product_list)
1925 .OptionalSwitch("--output-to-dir",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001926 "Outputs the APK contents to a directory specified "
1927 "by -o",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001928 &options.output_to_directory)
1929 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001930 "Removes XML namespace prefix and URI "
1931 "information from AndroidManifest.xml\nand XML "
1932 "binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001933 &options.no_xml_namespaces)
1934 .OptionalFlag("--min-sdk-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001935 "Default minimum SDK version to use for "
1936 "AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001937 &options.manifest_fixer_options.min_sdk_version_default)
1938 .OptionalFlag(
1939 "--target-sdk-version",
1940 "Default target SDK version to use for "
1941 "AndroidManifest.xml",
1942 &options.manifest_fixer_options.target_sdk_version_default)
1943 .OptionalFlag("--version-code",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001944 "Version code (integer) to inject into the "
1945 "AndroidManifest.xml if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001946 &options.manifest_fixer_options.version_code_default)
1947 .OptionalFlag("--version-name",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001948 "Version name to inject into the AndroidManifest.xml "
1949 "if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001950 &options.manifest_fixer_options.version_name_default)
1951 .OptionalSwitch("--static-lib", "Generate a static Android library",
1952 &options.static_lib)
1953 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001954 "Merge all library resources under the app's package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001955 &options.no_static_lib_packages)
1956 .OptionalSwitch("--non-final-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001957 "Generates R.java without the final modifier.\n"
1958 "This is implied when --static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001959 &options.generate_non_final_ids)
1960 .OptionalFlag("--stable-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001961 "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001962 &stable_id_file_path)
1963 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001964 "--emit-ids",
1965 "Emit a file at the given path with a list of name to ID\n"
1966 "mappings, suitable for use with --stable-ids.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001967 &options.resource_id_map_path)
1968 .OptionalFlag("--private-symbols",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001969 "Package name to use when generating R.java for "
1970 "private symbols.\n"
1971 "If not specified, public and private symbols will use "
1972 "the application's "
1973 "package name",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001974 &options.private_symbols)
1975 .OptionalFlag("--custom-package",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001976 "Custom Java package under which to generate R.java",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001977 &options.custom_java_package)
1978 .OptionalFlagList("--extra-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001979 "Generate the same R.java but with different "
1980 "package names",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001981 &extra_java_packages)
1982 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001983 "Adds a JavaDoc annotation to all "
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001984 "generated Java classes",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001985 &options.javadoc_annotations)
1986 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001987 "Allows the addition of new resources in "
1988 "overlays without <add-resource> tags",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001989 &options.auto_add_overlay)
1990 .OptionalFlag("--rename-manifest-package",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001991 "Renames the package in AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001992 &options.manifest_fixer_options.rename_manifest_package)
1993 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001994 "--rename-instrumentation-target-package",
1995 "Changes the name of the target package for instrumentation. "
1996 "Most useful "
1997 "when used\nin conjunction with --rename-manifest-package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001998 &options.manifest_fixer_options
1999 .rename_instrumentation_target_package)
2000 .OptionalFlagList("-0", "File extensions not to compress",
2001 &options.extensions_to_not_compress)
2002 .OptionalFlagList(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002003 "--split",
2004 "Split resources matching a set of configs out to a "
2005 "Split APK.\nSyntax: path/to/output.apk:<config>[,<config>[...]]",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002006 &split_args)
2007 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002008
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002009 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002010 return 1;
2011 }
2012
2013 // Expand all argument-files passed into the command line. These start with
2014 // '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002015 std::vector<std::string> arg_list;
2016 for (const std::string& arg : flags.GetArgs()) {
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, &arg_list, &error)) {
2021 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002022 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002023 }
2024 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002025 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002026 }
2027 }
2028
2029 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002030 for (const std::string& arg : overlay_arg_list) {
2031 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002032 const std::string path = arg.substr(1, arg.size() - 1);
2033 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002034 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
2035 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002036 return 1;
2037 }
2038 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002039 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002040 }
2041 }
2042
2043 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002044 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002045 }
2046
2047 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002048 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002049 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002050 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002051 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002052 }
2053 }
2054
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002055 if (product_list) {
2056 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002057 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002058 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002059 }
2060 }
2061 }
2062
2063 AxisConfigFilter filter;
2064 if (configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002065 for (const StringPiece& config_str : util::Tokenize(configs.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002066 ConfigDescription config;
2067 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002068 if (lv.InitFromFilterString(config_str)) {
2069 lv.WriteTo(&config);
2070 } else if (!ConfigDescription::Parse(config_str, &config)) {
2071 context.GetDiagnostics()->Error(DiagMessage() << "invalid config '"
2072 << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002073 << "' for -c option");
2074 return 1;
2075 }
2076
2077 if (config.density != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002078 context.GetDiagnostics()->Warn(DiagMessage() << "ignoring density '"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002079 << config
2080 << "' for -c option");
2081 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002082 filter.AddConfig(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002083 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002084 }
2085
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002086 options.table_splitter_options.config_filter = &filter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002087 }
2088
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002089 if (preferred_density) {
2090 ConfigDescription preferred_density_config;
2091 if (!ConfigDescription::Parse(preferred_density.value(),
2092 &preferred_density_config)) {
2093 context.GetDiagnostics()->Error(
2094 DiagMessage() << "invalid density '" << preferred_density.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002095 << "' for --preferred-density option");
2096 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002097 }
2098
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002099 // Clear the version that can be automatically added.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002100 preferred_density_config.sdkVersion = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002101
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002102 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002103 ConfigDescription::CONFIG_DENSITY) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002104 context.GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002105 DiagMessage() << "invalid preferred density '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002106 << preferred_density.value() << "'. "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002107 << "Preferred density must only be a density value");
2108 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002109 }
Pierre Lecesne672384b2017-02-06 10:29:02 +00002110 options.table_splitter_options.preferred_densities.push_back(preferred_density_config.density);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002111 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002112
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002113 if (!options.static_lib && stable_id_file_path) {
2114 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2115 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002116 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002117 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002118 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002119
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002120 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002121 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002122 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2123 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2124 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2125 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2126
2127 // Parse the split parameters.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002128 for (const std::string& split_arg : split_args) {
2129 options.split_paths.push_back({});
2130 options.split_constraints.push_back({});
2131 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(),
2132 &options.split_paths.back(),
2133 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002134 return 1;
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002135 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002136 }
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002137
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002138 // Turn off auto versioning for static-libs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002139 if (options.static_lib) {
2140 options.no_auto_version = true;
2141 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002142 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002143 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002144
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002145 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002146 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002147}
2148
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002149} // namespace aapt