blob: c3ce0760f554dc367ffad256356432f8517a7631 [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;
83 bool no_resource_deduping = false;
84 bool no_xml_namespaces = false;
85 bool do_not_compress_anything = false;
86 std::unordered_set<std::string> extensions_to_not_compress;
87
88 // Static lib options.
89 bool static_lib = false;
90 bool no_static_lib_packages = false;
91
92 // AndroidManifest.xml massaging options.
93 ManifestFixerOptions manifest_fixer_options;
94
95 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -070097
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 TableSplitterOptions table_splitter_options;
100 std::vector<SplitConstraints> split_constraints;
101 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 std::unordered_map<ResourceName, ResourceId> stable_id_map;
105 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106};
107
Adam Lesinski64587af2016-02-18 18:33:06 -0800108class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 LinkContext() : name_mangler_({}) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 NameMangler* GetNameMangler() override { return &name_mangler_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
117 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 const std::string& GetCompilationPackage() override {
121 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800125 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800127
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 uint8_t GetPackageId() override { return package_id_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 void SetPackageId(uint8_t id) { package_id_ = id; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 SymbolTable* GetExternalSymbols() override { return &symbols_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800133
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 bool IsVerbose() override { return verbose_; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 int GetMinSdkVersion() override { return min_sdk_version_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 void SetMinSdkVersion(int minSdk) { min_sdk_version_ = minSdk; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700141
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 DISALLOW_COPY_AND_ASSIGN(LinkContext);
144
145 StdErrDiagnostics diagnostics_;
146 NameMangler name_mangler_;
147 std::string compilation_package_;
148 uint8_t package_id_ = 0x0;
149 SymbolTable symbols_;
150 bool verbose_ = false;
151 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152};
153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154static bool CopyFileToArchive(io::IFile* file, const std::string& out_path,
155 uint32_t compression_flags,
156 IArchiveWriter* writer, IAaptContext* context) {
157 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 context->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 << "failed to open file");
Adam Lesinski355f2852016-02-13 20:26:45 -0800161 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 }
163
164 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data->data());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 const size_t buffer_size = data->size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 if (context->IsVerbose()) {
168 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 << " to archive");
170 }
171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 if (writer->StartEntry(out_path, compression_flags)) {
173 if (writer->WriteEntry(buffer, buffer_size)) {
174 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 return true;
176 }
177 }
178 }
179
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 context->GetDiagnostics()->Error(DiagMessage() << "failed to write file "
181 << out_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800183}
184
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
186 Maybe<size_t> max_sdk_level, bool keep_raw_values,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 IArchiveWriter* writer, IAaptContext* context) {
188 BigBuffer buffer(1024);
189 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 options.keep_raw_values = keep_raw_values;
191 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800194 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 }
196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 DiagMessage msg;
199 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 if (max_sdk_level) {
201 msg << " maxSdkLevel=" << max_sdk_level.value()
202 << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 }
206
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 if (writer->StartEntry(path, ArchiveEntry::kCompress)) {
208 if (writer->WriteEntry(buffer)) {
209 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 return true;
211 }
212 }
213 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 << " to archive");
216 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800217}
218
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 const void* data,
221 size_t len,
Adam Lesinski355f2852016-02-13 20:26:45 -0800222 IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 pb::ResourceTable pb_table;
224 if (!pb_table.ParseFromArray(data, len)) {
225 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 return {};
227 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800228
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 if (!table) {
232 return {};
233 }
234 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800235}
236
237/**
238 * Inflates an XML file from the source path.
239 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 IDiagnostics* diag) {
242 std::ifstream fin(path, std::ifstream::binary);
243 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 return {};
246 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800248}
249
Adam Lesinski355f2852016-02-13 20:26:45 -0800250struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 bool no_auto_version = false;
252 bool no_version_vectors = false;
253 bool no_xml_namespaces = false;
254 bool keep_raw_values = false;
255 bool do_not_compress_anything = false;
256 bool update_proguard_spec = false;
257 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800258};
259
260class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 public:
262 ResourceFileFlattener(const ResourceFileFlattenerOptions& options,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 IAaptContext* context, proguard::KeepSet* keep_set)
264 : options_(options), context_(context), keep_set_(keep_set) {}
Adam Lesinski355f2852016-02-13 20:26:45 -0800265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800267
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 private:
269 struct FileOperation {
270 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700271
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 // The entry this file came from.
273 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700274
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700277
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700280
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700282 std::string dst_path;
283 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800287
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
289 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800290
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 ResourceFileFlattenerOptions options_;
292 IAaptContext* context_;
293 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800294};
295
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
297 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 return 0;
299 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800300
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 for (const std::string& extension : options_.extensions_to_not_compress) {
302 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800304 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 }
306 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800307}
308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309bool ResourceFileFlattener::LinkAndVersionXmlFile(
310 ResourceTable* table, FileOperation* file_op,
311 std::queue<FileOperation>* out_file_op_queue) {
312 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700314
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 if (context_->IsVerbose()) {
316 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 }
318
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 XmlReferenceLinker xml_linker;
320 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 return false;
322 }
323
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 if (options_.update_proguard_spec &&
325 !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 return false;
327 }
328
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329 if (options_.no_xml_namespaces) {
330 XmlNamespaceRemover namespace_remover;
331 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800333 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800335
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 if (!options_.no_auto_version) {
337 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 xml::Element* el = xml::FindRootElement(doc);
340 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 if (el->name == "vector" || el->name == "animated-vector") {
342 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700345 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700346 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700347 }
348
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350
351 // Find the first SDK level used that is higher than this defined config and
352 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 const int min_sdk_version = context_->GetMinSdkVersion();
354 for (int sdk_level : xml_linker.sdk_levels()) {
355 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
356 if (!ShouldGenerateVersionedResource(file_op->entry, config,
357 sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 // If we shouldn't generate a versioned resource, stop checking.
359 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800360 }
361
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362 ResourceFile versioned_file_desc = doc->file;
363 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700364
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 FileOperation new_file_op;
366 new_file_op.xml_to_flatten = util::make_unique<xml::XmlResource>(
367 versioned_file_desc, doc->root->Clone());
368 new_file_op.config = versioned_file_desc.config;
369 new_file_op.entry = file_op->entry;
370 new_file_op.dst_path = ResourceUtils::BuildResourceFileName(
371 versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800372
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373 if (context_->IsVerbose()) {
374 context_->GetDiagnostics()->Note(
375 DiagMessage(versioned_file_desc.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 << "auto-versioning resource from config '" << config << "' -> '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800378 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700379
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 bool added = table->AddFileReferenceAllowMangled(
381 versioned_file_desc.name, versioned_file_desc.config,
382 versioned_file_desc.source, new_file_op.dst_path, nullptr,
383 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700384 if (!added) {
385 return false;
386 }
387
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 break;
390 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800391 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 }
393 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800394}
395
396/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 * Do not insert or remove any resources while executing in this function. It
398 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800399 * corrupt the iteration order.
400 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401bool ResourceFileFlattener::Flatten(ResourceTable* table,
402 IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 bool error = false;
404 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800406
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 for (auto& pkg : table->packages) {
408 for (auto& type : pkg->types) {
409 // Sort by config and name, so that we get better locality in the zip
410 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 config_sorted_files.clear();
412 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800413
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 // Populate the queue with all files in the ResourceTable.
415 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416 for (auto& config_value : entry->values) {
417 FileReference* file_ref =
418 ValueCast<FileReference>(config_value->value.get());
419 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700420 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700422
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700427 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 }
429
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430 FileOperation file_op;
431 file_op.entry = entry.get();
432 file_op.dst_path = *file_ref->path;
433 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700435 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 if (type->type != ResourceType::kRaw &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437 (util::EndsWith(src_path, ".xml.flat") ||
438 util::EndsWith(src_path, ".xml"))) {
439 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 << "failed to open file");
443 return false;
444 }
445
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 file_op.xml_to_flatten =
447 xml::Inflate(data->data(), data->size(),
448 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 return false;
452 }
453
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 file_op.xml_to_flatten->file.config = config_value->config;
455 file_op.xml_to_flatten->file.source = file_ref->GetSource();
456 file_op.xml_to_flatten->file.name =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 ResourceName(pkg->name, type->type, entry->name);
458
459 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700462 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463
464 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 // else we end up copying the string in the std::make_pair() method,
466 // then creating a StringPiece from the copy, which would cause us
467 // to end up referencing garbage in the map.
468 const StringPiece entry_name(entry->name);
469 config_sorted_files[std::make_pair(
470 config_value->config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 }
472 }
473 }
474
475 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 for (; !file_operations.empty(); file_operations.pop()) {
477 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700478
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 error = true;
481 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700482 }
483
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
485 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486 // creating a StringPiece from the copy, which would cause us to end up
487 // referencing garbage in the map.
488 const StringPiece entry_name(file_op.entry->name);
489 config_sorted_files[std::make_pair(file_op.config, entry_name)] =
490 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 }
492
493 if (error) {
494 return false;
495 }
496
497 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700498 for (auto& map_entry : config_sorted_files) {
499 const ConfigDescription& config = map_entry.first.first;
500 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 if (file_op.xml_to_flatten) {
503 Maybe<size_t> max_sdk_level;
504 if (!options_.no_auto_version && !file_op.skip_version) {
505 max_sdk_level =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 }
509
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700510 bool result = FlattenXml(
511 file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
512 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700513 if (!result) {
514 error = true;
515 }
516 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700517 bool result = CopyFileToArchive(
518 file_op.file_to_copy, file_op.dst_path,
519 GetCompressionFlags(file_op.dst_path), archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 if (!result) {
521 error = true;
522 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700523 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700525 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 }
527 return !error;
528}
529
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530static bool WriteStableIdMapToPath(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 IDiagnostics* diag,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 const std::unordered_map<ResourceName, ResourceId>& id_map,
533 const std::string& id_map_path) {
534 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700535 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700536 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537 return false;
538 }
539
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700540 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 const ResourceName& name = entry.first;
542 const ResourceId& id = entry.second;
543 fout << name << " = " << id << "\n";
544 }
545
546 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 diag->Error(DiagMessage(id_map_path)
548 << "failed writing to file: "
549 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 return false;
551 }
552
553 return true;
554}
555
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556static bool LoadStableIdMap(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 IDiagnostics* diag, const std::string& path,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 std::string content;
560 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700562 return false;
563 }
564
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 out_id_map->clear();
566 size_t line_no = 0;
567 for (StringPiece line : util::Tokenize(content, '\n')) {
568 line_no++;
569 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 if (line.empty()) {
571 continue;
572 }
573
574 auto iter = std::find(line.begin(), line.end(), '=');
575 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700577 return false;
578 }
579
580 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 StringPiece res_name_str =
582 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
583 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
584 diag->Error(DiagMessage(Source(path, line_no))
585 << "invalid resource name '" << res_name_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586 return false;
587 }
588
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
590 const size_t res_id_str_len = line.size() - res_id_start_idx;
591 StringPiece res_id_str =
592 util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
595 if (!maybe_id) {
596 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '"
597 << res_id_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 return false;
599 }
600
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 }
603 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700604}
605
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606static bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag,
607 std::string* out_path,
608 SplitConstraints* out_split) {
609 std::vector<std::string> parts = util::Split(arg, ':');
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 if (parts.size() != 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700611 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
612 diag->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 DiagMessage()
614 << "should be --split path/to/output.apk:<config>[,<config>...]");
615 return false;
616 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700617 *out_path = parts[0];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 std::vector<ConfigDescription> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 configs.push_back({});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 if (!ConfigDescription::Parse(config_str, &configs.back())) {
622 diag->Error(DiagMessage() << "invalid config '" << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 << "' in split parameter '" << arg << "'");
624 return false;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700625 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 out_split->configs.insert(configs.begin(), configs.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700628 return true;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700629}
630
Adam Lesinskifb48d292015-11-07 15:52:13 -0800631class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700632 public:
633 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 : options_(options),
635 context_(context),
636 final_table_(),
637 file_collection_(util::make_unique<io::FileCollection>()) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700638
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700639 /**
640 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700641 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700643 bool LoadSymbolsFromIncludePaths() {
644 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700646 for (const std::string& path : options_.include_paths) {
647 if (context_->IsVerbose()) {
648 context_->GetDiagnostics()->Note(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 << "loading include path");
650 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700651
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700653 std::string error_str;
654 std::unique_ptr<ResourceTable> static_include =
655 LoadStaticLibrary(path, &error_str);
656 if (static_include) {
657 if (!options_.static_lib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 // Can't include static libraries when not building a static library.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700659 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700660 DiagMessage(path)
661 << "can't include static library when building app");
662 return false;
663 }
664
665 // If we are using --no-static-lib-packages, we need to rename the
666 // package of this
667 // table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 if (ResourceTablePackage* pkg =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700670 static_include->FindPackageById(0x7f)) {
671 pkg->name = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 }
673 }
674
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 context_->GetExternalSymbols()->AppendSource(
676 util::make_unique<ResourceTableSymbolSource>(static_include.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678 static_table_includes_.push_back(std::move(static_include));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700680 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700681 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700682 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 return false;
684 }
685
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700686 if (!asset_source->AddAssetPath(path)) {
687 context_->GetDiagnostics()->Error(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 << "failed to load include path");
689 return false;
690 }
691 }
692
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700693 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 return true;
695 }
696
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700697 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700698 IDiagnostics* diag) {
699 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700700 if (xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get())) {
701 AppInfo app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700703 if (!manifest_el->namespace_uri.empty() ||
704 manifest_el->name != "manifest") {
705 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700706 << "root tag must be <manifest>");
707 return {};
708 }
709
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700710 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
711 if (!package_attr) {
712 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700713 << "<manifest> must have a 'package' attribute");
714 return {};
715 }
716
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 app_info.package = package_attr->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719 if (xml::Attribute* version_code_attr =
720 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
721 Maybe<uint32_t> maybe_code =
722 ResourceUtils::ParseInt(version_code_attr->value);
723 if (!maybe_code) {
724 diag->Error(DiagMessage(xml_res->file.source.WithLine(
725 manifest_el->line_number))
726 << "invalid android:versionCode '"
727 << version_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700728 return {};
729 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 app_info.version_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 }
732
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 if (xml::Attribute* revision_code_attr =
734 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
735 Maybe<uint32_t> maybe_code =
736 ResourceUtils::ParseInt(revision_code_attr->value);
737 if (!maybe_code) {
738 diag->Error(DiagMessage(xml_res->file.source.WithLine(
739 manifest_el->line_number))
740 << "invalid android:revisionCode '"
741 << revision_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 return {};
743 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700744 app_info.revision_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700745 }
746
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700747 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
748 if (xml::Attribute* min_sdk = uses_sdk_el->FindAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750 app_info.min_sdk_version = min_sdk->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 }
752 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700753 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 }
755 return {};
756 }
757
758 /**
759 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it
760 * linked.
761 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700762 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700763 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700764 bool VerifyNoExternalPackages() {
765 auto is_ext_package_func =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766 [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700767 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
768 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 };
770
771 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700772 for (const auto& package : final_table_.packages) {
773 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700774 // We have a package that is not related to the one we're building!
775 for (const auto& type : package->types) {
776 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700779 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700780 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700781 // for the 'android' package. This is due to legacy reasons.
782 if (ValueCast<Id>(config_value->value.get()) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700783 package->name == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700784 context_->GetDiagnostics()->Warn(
785 DiagMessage(config_value->value->GetSource())
786 << "generated id '" << res_name
787 << "' for external package '" << package->name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700788 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700789 context_->GetDiagnostics()->Error(
790 DiagMessage(config_value->value->GetSource())
791 << "defined resource '" << res_name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700792 << "' for external package '" << package->name << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700793 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700794 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700795 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700796 }
797 }
798 }
799 }
800
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700801 auto new_end_iter =
802 std::remove_if(final_table_.packages.begin(),
803 final_table_.packages.end(), is_ext_package_func);
804 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700805 return !error;
806 }
807
808 /**
809 * Returns true if no IDs have been set, false otherwise.
810 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700811 bool VerifyNoIdsSet() {
812 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 for (const auto& type : package->types) {
814 if (type->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700815 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700816 DiagMessage() << "type " << type->type << " has ID " << std::hex
817 << (int)type->id.value() << std::dec
818 << " assigned");
819 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700820 }
821
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 for (const auto& entry : type->entries) {
823 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700824 ResourceNameRef res_name(package->name, type->type, entry->name);
825 context_->GetDiagnostics()->Error(
826 DiagMessage() << "entry " << res_name << " has ID " << std::hex
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 << (int)entry->id.value() << std::dec
828 << " assigned");
829 return false;
830 }
831 }
832 }
833 }
834 return true;
835 }
836
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700837 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
838 if (options_.output_to_directory) {
839 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700841 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700842 }
843 }
844
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700845 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700846 BigBuffer buffer(1024);
847 TableFlattener flattener(&buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700848 if (!flattener.Consume(context_, table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700849 return false;
850 }
851
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700852 if (writer->StartEntry("resources.arsc", ArchiveEntry::kAlign)) {
853 if (writer->WriteEntry(buffer)) {
854 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 return true;
856 }
857 }
858 }
859
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700860 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 DiagMessage() << "failed to write resources.arsc to archive");
862 return false;
863 }
864
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700865 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700866 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700867 if (!writer->StartEntry("resources.arsc.flat", 0)) {
868 context_->GetDiagnostics()->Error(DiagMessage() << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700869 return false;
870 }
871
872 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700873 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 {
875 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700876 // ZeroCopyOutputStream interface.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700877 CopyingOutputStreamAdaptor adaptor(writer);
878
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700879 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
880 if (!pb_table->SerializeToZeroCopyStream(&adaptor)) {
881 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 return false;
883 }
884 }
885
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700886 if (!writer->FinishEntry()) {
887 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888 << "failed to finish entry");
889 return false;
890 }
891 return true;
892 }
893
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700894 bool WriteJavaFile(ResourceTable* table,
895 const StringPiece& package_name_to_generate,
896 const StringPiece& out_package,
897 const JavaClassGeneratorOptions& java_options) {
898 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700899 return true;
900 }
901
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700902 std::string out_path = options_.generate_java_class_path.value();
903 file::AppendPath(&out_path, file::PackageToPath(out_package));
904 if (!file::mkdirs(out_path)) {
905 context_->GetDiagnostics()->Error(
906 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 return false;
908 }
909
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700910 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700911
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700912 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700913 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700914 context_->GetDiagnostics()->Error(
915 DiagMessage() << "failed writing to '" << out_path << "': "
916 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700917 return false;
918 }
919
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700920 JavaClassGenerator generator(context_, table, java_options);
921 if (!generator.Generate(package_name_to_generate, out_package, &fout)) {
922 context_->GetDiagnostics()->Error(DiagMessage(out_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700923 << generator.getError());
924 return false;
925 }
926
927 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700928 context_->GetDiagnostics()->Error(
929 DiagMessage() << "failed writing to '" << out_path << "': "
930 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700931 }
932 return true;
933 }
934
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700935 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
936 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700937 return true;
938 }
939
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700940 std::unique_ptr<ClassDefinition> manifest_class =
941 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700942
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700943 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700944 // Something bad happened, but we already logged it, so exit.
945 return false;
946 }
947
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700948 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700949 // Empty Manifest class, no need to generate it.
950 return true;
951 }
952
953 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700954 for (const std::string& annotation : options_.javadoc_annotations) {
955 std::string proper_annotation = "@";
956 proper_annotation += annotation;
957 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700958 }
959
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700960 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700961
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700962 std::string out_path = options_.generate_java_class_path.value();
963 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700964
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700965 if (!file::mkdirs(out_path)) {
966 context_->GetDiagnostics()->Error(
967 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700968 return false;
969 }
970
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700971 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700972
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700973 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700974 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700975 context_->GetDiagnostics()->Error(
976 DiagMessage() << "failed writing to '" << out_path << "': "
977 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700978 return false;
979 }
980
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700981 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8,
982 true, &fout)) {
983 context_->GetDiagnostics()->Error(
984 DiagMessage() << "failed writing to '" << out_path << "': "
985 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700986 return false;
987 }
988 return true;
989 }
990
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700991 bool WriteProguardFile(const Maybe<std::string>& out,
992 const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700993 if (!out) {
994 return true;
995 }
996
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700997 const std::string& out_path = out.value();
998 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700999 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001000 context_->GetDiagnostics()->Error(
1001 DiagMessage() << "failed to open '" << out_path << "': "
1002 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001003 return false;
1004 }
1005
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001006 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001008 context_->GetDiagnostics()->Error(
1009 DiagMessage() << "failed writing to '" << out_path << "': "
1010 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001011 return false;
1012 }
1013 return true;
1014 }
1015
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001016 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1017 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001018 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001019 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001020 if (!collection) {
1021 return {};
1022 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001023 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001024 }
1025
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001026 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001027 io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001028 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001029 if (!file) {
1030 return {};
1031 }
1032
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001033 std::unique_ptr<io::IData> data = file->OpenAsData();
1034 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1035 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001036 }
1037
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001038 bool MergeStaticLibrary(const std::string& input, bool override) {
1039 if (context_->IsVerbose()) {
1040 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001041 << "merging static library " << input);
1042 }
1043
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001044 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001045 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001046 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001047 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001048 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001049 return false;
1050 }
1051
1052 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001053 LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001054 if (!table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001055 context_->GetDiagnostics()->Error(DiagMessage(input)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001056 << "invalid static library");
1057 return false;
1058 }
1059
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001060 ResourceTablePackage* pkg = table->FindPackageById(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001061 if (!pkg) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001062 context_->GetDiagnostics()->Error(DiagMessage(input)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001063 << "static library has no package");
1064 return false;
1065 }
1066
1067 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001068 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001069 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001070 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001071
1072 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001073 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001074 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001075 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001076 }
1077
1078 pkg->name = "";
1079 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001080 result = table_merger_->MergeOverlay(Source(input), table.get(),
1081 collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001082 } else {
1083 result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001084 table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001085 }
1086
1087 } else {
1088 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001089 // preserved and resource names are mangled.
1090 result = table_merger_->MergeAndMangle(Source(input), pkg->name,
1091 table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001092 }
1093
1094 if (!result) {
1095 return false;
1096 }
1097
1098 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001099 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001100 return true;
1101 }
1102
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001103 bool MergeResourceTable(io::IFile* file, bool override) {
1104 if (context_->IsVerbose()) {
1105 context_->GetDiagnostics()->Note(
1106 DiagMessage() << "merging resource table " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001107 }
1108
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001109 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001110 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001111 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001112 << "failed to open file");
1113 return false;
1114 }
1115
1116 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001117 LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1118 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001119 if (!table) {
1120 return false;
1121 }
1122
1123 bool result = false;
1124 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001125 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001126 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001127 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001128 }
1129 return result;
1130 }
1131
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001132 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001133 bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001134 if (context_->IsVerbose()) {
1135 context_->GetDiagnostics()->Note(
1136 DiagMessage() << "merging '" << file_desc->name
1137 << "' from compiled file " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001138 }
1139
1140 bool result = false;
1141 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001142 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001143 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001144 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001145 }
1146
1147 if (!result) {
1148 return false;
1149 }
1150
1151 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001152 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1153 if (exported_symbol.name.package.empty()) {
1154 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001155 }
1156
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001157 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001158
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001159 Maybe<ResourceName> mangled_name =
1160 context_->GetNameMangler()->MangleName(exported_symbol.name);
1161 if (mangled_name) {
1162 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001163 }
1164
1165 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001166 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1167 bool result = final_table_.AddResourceAllowMangled(
1168 res_name, ConfigDescription::DefaultConfig(), std::string(),
1169 std::move(id), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001170 if (!result) {
1171 return false;
1172 }
1173 }
1174 return true;
1175 }
1176
1177 /**
1178 * Takes a path to load as a ZIP file and merges the files within into the
1179 * master ResourceTable.
1180 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001181 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001182 *
1183 * An io::IFileCollection is created from the ZIP file and added to the set of
1184 * io::IFileCollections that are open.
1185 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001186 bool MergeArchive(const std::string& input, bool override) {
1187 if (context_->IsVerbose()) {
1188 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001189 << input);
1190 }
1191
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001192 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001193 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001194 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001195 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001196 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001197 return false;
1198 }
1199
1200 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001201 for (auto iter = collection->Iterator(); iter->HasNext();) {
1202 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001203 error = true;
1204 }
1205 }
1206
1207 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001208 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001209 return !error;
1210 }
1211
1212 /**
1213 * Takes a path to load and merge into the master ResourceTable. If override
1214 * is true,
1215 * conflicting resources are allowed to override each other, in order of last
1216 * seen.
1217 *
1218 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1219 * as ZIP archive
1220 * and the files within are merged individually.
1221 *
1222 * Otherwise the files is processed on its own.
1223 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001224 bool MergePath(const std::string& path, bool override) {
1225 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1226 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1227 return MergeArchive(path, override);
1228 } else if (util::EndsWith(path, ".apk")) {
1229 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001230 }
1231
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001232 io::IFile* file = file_collection_->InsertFile(path);
1233 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001234 }
1235
1236 /**
1237 * Takes a file to load and merge into the master ResourceTable. If override
1238 * is true,
1239 * conflicting resources are allowed to override each other, in order of last
1240 * seen.
1241 *
1242 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1243 * merged into the
1244 * master ResourceTable. If the file ends with .flat, then it is treated like
1245 * a compiled file
1246 * and the header data is read and merged into the final ResourceTable.
1247 *
1248 * All other file types are ignored. This is because these files could be
1249 * coming from a zip,
1250 * where we could have other files like classes.dex.
1251 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001252 bool MergeFile(io::IFile* file, bool override) {
1253 const Source& src = file->GetSource();
1254 if (util::EndsWith(src.path, ".arsc.flat")) {
1255 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001256
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001257 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001258 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001259 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001260 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001261 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001262 return false;
1263 }
1264
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001265 CompiledFileInputStream input_stream(data->data(), data->size());
1266 uint32_t num_files = 0;
1267 if (!input_stream.ReadLittleEndian32(&num_files)) {
1268 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001269 << "failed read num files");
1270 return false;
1271 }
1272
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001273 for (uint32_t i = 0; i < num_files; i++) {
1274 pb::CompiledFile compiled_file;
1275 if (!input_stream.ReadCompiledFile(&compiled_file)) {
1276 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001277 DiagMessage(src) << "failed to read compiled file header");
1278 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001279 }
1280
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001281 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001282 if (!input_stream.ReadDataMetaData(&offset, &len)) {
1283 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001284 << "failed to read data meta data");
1285 return false;
1286 }
1287
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001288 std::unique_ptr<ResourceFile> resource_file =
1289 DeserializeCompiledFileFromPb(compiled_file, file->GetSource(),
1290 context_->GetDiagnostics());
1291 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001292 return false;
1293 }
1294
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001295 if (!MergeCompiledFile(file->CreateFileSegment(offset, len),
1296 resource_file.get(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001297 return false;
1298 }
1299 }
1300 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001301 } else if (util::EndsWith(src.path, ".xml") ||
1302 util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001303 // Since AAPT compiles these file types and appends .flat to them, seeing
1304 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001305 const StringPiece file_type =
1306 util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1307 context_->GetDiagnostics()->Error(DiagMessage(src)
1308 << "uncompiled " << file_type
Adam Lesinski6a396c12016-10-20 14:38:23 -07001309 << " file passed as argument. Must be "
1310 "compiled first into .flat file.");
1311 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001312 }
1313
1314 // Ignore non .flat files. This could be classes.dex or something else that
1315 // happens
1316 // to be in an archive.
1317 return true;
1318 }
1319
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001320 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(
1321 const AppInfo& app_info, const SplitConstraints& constraints) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001322 std::unique_ptr<xml::XmlResource> doc =
1323 util::make_unique<xml::XmlResource>();
1324
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001325 std::unique_ptr<xml::Namespace> namespace_android =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001326 util::make_unique<xml::Namespace>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001327 namespace_android->namespace_uri = xml::kSchemaAndroid;
1328 namespace_android->namespace_prefix = "android";
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001329
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001330 std::unique_ptr<xml::Element> manifest_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001331 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001332 manifest_el->name = "manifest";
1333 manifest_el->attributes.push_back(
1334 xml::Attribute{"", "package", app_info.package});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001335
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001336 if (app_info.version_code) {
1337 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001338 xml::Attribute{xml::kSchemaAndroid, "versionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001339 std::to_string(app_info.version_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001340 }
1341
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001342 if (app_info.revision_code) {
1343 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001344 xml::Attribute{xml::kSchemaAndroid, "revisionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001345 std::to_string(app_info.revision_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001346 }
1347
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001348 std::stringstream split_name;
1349 split_name << "config." << util::Joiner(constraints.configs, "_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001350
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001351 manifest_el->attributes.push_back(
1352 xml::Attribute{"", "split", split_name.str()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001353
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001354 std::unique_ptr<xml::Element> application_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001355 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001356 application_el->name = "application";
1357 application_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001358 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false"});
1359
Adam Lesinskie343eb12016-10-27 16:31:58 -07001360 manifest_el->AppendChild(std::move(application_el));
1361 namespace_android->AppendChild(std::move(manifest_el));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001362 doc->root = std::move(namespace_android);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001363 return doc;
1364 }
1365
1366 /**
1367 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001368 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001369 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001370 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001371 xml::XmlResource* manifest, ResourceTable* table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001372 const bool keep_raw_values = options_.static_lib;
1373 bool result = FlattenXml(manifest, "AndroidManifest.xml", {},
1374 keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001375 if (!result) {
1376 return false;
1377 }
1378
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001379 ResourceFileFlattenerOptions file_flattener_options;
1380 file_flattener_options.keep_raw_values = keep_raw_values;
1381 file_flattener_options.do_not_compress_anything =
1382 options_.do_not_compress_anything;
1383 file_flattener_options.extensions_to_not_compress =
1384 options_.extensions_to_not_compress;
1385 file_flattener_options.no_auto_version = options_.no_auto_version;
1386 file_flattener_options.no_version_vectors = options_.no_version_vectors;
1387 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1388 file_flattener_options.update_proguard_spec =
1389 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001390
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001391 ResourceFileFlattener file_flattener(file_flattener_options, context_,
1392 keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001393
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001394 if (!file_flattener.Flatten(table, writer)) {
1395 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001396 << "failed linking file resources");
1397 return false;
1398 }
1399
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400 if (options_.static_lib) {
1401 if (!FlattenTableToPb(table, writer)) {
1402 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403 DiagMessage() << "failed to write resources.arsc.flat");
1404 return false;
1405 }
1406 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001407 if (!FlattenTable(table, writer)) {
1408 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001409 << "failed to write resources.arsc");
1410 return false;
1411 }
1412 }
1413 return true;
1414 }
1415
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001416 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001417 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001418 std::unique_ptr<xml::XmlResource> manifest_xml =
1419 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1420 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001421 return 1;
1422 }
1423
1424 // First extract the Package name without modifying it (via
1425 // --rename-manifest-package).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001426 if (Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1427 manifest_xml.get(), context_->GetDiagnostics())) {
1428 const AppInfo& app_info = maybe_app_info.value();
1429 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001430 }
1431
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001432 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1433 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001434 return 1;
1435 }
1436
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001437 Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1438 manifest_xml.get(), context_->GetDiagnostics());
1439 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001440 return 1;
1441 }
1442
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001443 const AppInfo& app_info = maybe_app_info.value();
1444 if (app_info.min_sdk_version) {
1445 if (Maybe<int> maybe_min_sdk_version = ResourceUtils::ParseSdkVersion(
1446 app_info.min_sdk_version.value())) {
1447 context_->SetMinSdkVersion(maybe_min_sdk_version.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001448 }
1449 }
1450
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001451 context_->SetNameManglerPolicy(
1452 NameManglerPolicy{context_->GetCompilationPackage()});
1453 if (context_->GetCompilationPackage() == "android") {
1454 context_->SetPackageId(0x01);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001455 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001456 context_->SetPackageId(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001457 }
1458
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001459 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001460 return 1;
1461 }
1462
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001463 TableMergerOptions table_merger_options;
1464 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
1465 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_,
1466 table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001467
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001468 if (context_->IsVerbose()) {
1469 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001470 << "linking package '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001471 << context_->GetCompilationPackage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001472 << "' with package ID " << std::hex
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001473 << (int)context_->GetPackageId());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001474 }
1475
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001476 for (const std::string& input : input_files) {
1477 if (!MergePath(input, false)) {
1478 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001479 << "failed parsing input");
1480 return 1;
1481 }
1482 }
1483
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001484 for (const std::string& input : options_.overlay_files) {
1485 if (!MergePath(input, true)) {
1486 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001487 << "failed parsing overlays");
1488 return 1;
1489 }
1490 }
1491
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001492 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001493 return 1;
1494 }
1495
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001496 if (!options_.static_lib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001497 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001498 if (!mover.Consume(context_, &final_table_)) {
1499 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001500 DiagMessage() << "failed moving private attributes");
1501 return 1;
1502 }
1503
1504 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001505 IdAssigner id_assigner(&options_.stable_id_map);
1506 if (!id_assigner.Consume(context_, &final_table_)) {
1507 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001508 << "failed assigning IDs");
1509 return 1;
1510 }
1511
1512 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001513 if (options_.resource_id_map_path) {
1514 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001515 for (auto& type : package->types) {
1516 for (auto& entry : type->entries) {
1517 ResourceName name(package->name, type->type, entry->name);
1518 // The IDs are guaranteed to exist.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001519 options_.stable_id_map[std::move(name)] = ResourceId(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001520 package->id.value(), type->id.value(), entry->id.value());
1521 }
1522 }
1523 }
1524
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001525 if (!WriteStableIdMapToPath(context_->GetDiagnostics(),
1526 options_.stable_id_map,
1527 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001528 return 1;
1529 }
1530 }
1531 } else {
1532 // Static libs are merged with other apps, and ID collisions are bad, so
1533 // verify that
1534 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001535 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001536 return 1;
1537 }
1538 }
1539
1540 // Add the names to mangle based on our source merge earlier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001541 context_->SetNameManglerPolicy(NameManglerPolicy{
1542 context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001543
1544 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001545 context_->GetExternalSymbols()->PrependSource(
1546 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001547
1548 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001549 if (!linker.Consume(context_, &final_table_)) {
1550 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001551 << "failed linking references");
1552 return 1;
1553 }
1554
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001555 if (options_.static_lib) {
1556 if (!options_.products.empty()) {
1557 context_->GetDiagnostics()
1558 ->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001559 << "can't select products when building static library");
1560 }
1561 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001562 ProductFilter product_filter(options_.products);
1563 if (!product_filter.Consume(context_, &final_table_)) {
1564 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001565 << "failed stripping products");
1566 return 1;
1567 }
1568 }
1569
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001570 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001571 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001572 if (!versioner.Consume(context_, &final_table_)) {
1573 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001574 << "failed versioning styles");
1575 return 1;
1576 }
1577 }
1578
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001579 if (!options_.static_lib && context_->GetMinSdkVersion() > 0) {
1580 if (context_->IsVerbose()) {
1581 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001582 DiagMessage() << "collapsing resource versions for minimum SDK "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001583 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001584 }
1585
1586 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001587 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001588 return 1;
1589 }
1590 }
1591
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001592 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001593 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001594 if (!deduper.Consume(context_, &final_table_)) {
1595 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001596 << "failed deduping resources");
1597 return 1;
1598 }
1599 }
1600
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001601 proguard::KeepSet proguard_keep_set;
1602 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001603
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001604 if (options_.static_lib) {
1605 if (options_.table_splitter_options.config_filter != nullptr ||
1606 options_.table_splitter_options.preferred_density) {
1607 context_->GetDiagnostics()
1608 ->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001609 << "can't strip resources when building static library");
1610 }
1611 } else {
1612 // Adjust the SplitConstraints so that their SDK version is stripped if it
1613 // is less
1614 // than or equal to the minSdk. Otherwise the resources that have had
1615 // their SDK version
1616 // stripped due to minSdk won't ever match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001617 std::vector<SplitConstraints> adjusted_constraints_list;
1618 adjusted_constraints_list.reserve(options_.split_constraints.size());
1619 for (const SplitConstraints& constraints : options_.split_constraints) {
1620 SplitConstraints adjusted_constraints;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001621 for (const ConfigDescription& config : constraints.configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001622 if (config.sdkVersion <= context_->GetMinSdkVersion()) {
1623 adjusted_constraints.configs.insert(config.CopyWithoutSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001624 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001625 adjusted_constraints.configs.insert(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001626 }
1627 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001628 adjusted_constraints_list.push_back(std::move(adjusted_constraints));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001629 }
1630
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001631 TableSplitter table_splitter(adjusted_constraints_list,
1632 options_.table_splitter_options);
1633 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001634 return 1;
1635 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001636 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001637
1638 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001639 auto path_iter = options_.split_paths.begin();
1640 auto split_constraints_iter = adjusted_constraints_list.begin();
1641 for (std::unique_ptr<ResourceTable>& split_table :
1642 table_splitter.splits()) {
1643 if (context_->IsVerbose()) {
1644 context_->GetDiagnostics()->Note(
1645 DiagMessage(*path_iter)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001646 << "generating split with configurations '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001647 << util::Joiner(split_constraints_iter->configs, ", ") << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001648 }
1649
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001650 std::unique_ptr<IArchiveWriter> archive_writer =
1651 MakeArchiveWriter(*path_iter);
1652 if (!archive_writer) {
1653 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001654 << "failed to create archive");
1655 return 1;
1656 }
1657
1658 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001659 std::unique_ptr<xml::XmlResource> split_manifest =
1660 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001661
1662 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001663 if (!linker.Consume(context_, split_manifest.get())) {
1664 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001665 DiagMessage() << "failed to create Split AndroidManifest.xml");
1666 return 1;
1667 }
1668
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001669 if (!WriteApk(archive_writer.get(), &proguard_keep_set,
1670 split_manifest.get(), split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001671 return 1;
1672 }
1673
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001674 ++path_iter;
1675 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001676 }
1677 }
1678
1679 // Start writing the base APK.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001680 std::unique_ptr<IArchiveWriter> archive_writer =
1681 MakeArchiveWriter(options_.output_path);
1682 if (!archive_writer) {
1683 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001684 << "failed to create archive");
1685 return 1;
1686 }
1687
1688 bool error = false;
1689 {
1690 // AndroidManifest.xml has no resource name, but the CallSite is built
1691 // from the name
1692 // (aka, which package the AndroidManifest.xml is coming from).
1693 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001694 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001695
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001696 XmlReferenceLinker manifest_linker;
1697 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1698 if (options_.generate_proguard_rules_path &&
1699 !proguard::CollectProguardRulesForManifest(
1700 Source(options_.manifest_path), manifest_xml.get(),
1701 &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001702 error = true;
1703 }
1704
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001705 if (options_.generate_main_dex_proguard_rules_path &&
1706 !proguard::CollectProguardRulesForManifest(
1707 Source(options_.manifest_path), manifest_xml.get(),
1708 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001709 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001710 }
1711
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001712 if (options_.generate_java_class_path) {
1713 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001714 error = true;
1715 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001716 }
1717
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001718 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001719 // PackageParser will fail if URIs are removed from
1720 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001721 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1722 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001723 error = true;
1724 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001725 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001726 } else {
1727 error = true;
1728 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001729 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001730
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001731 if (error) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001732 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001733 << "failed processing manifest");
1734 return 1;
1735 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001736
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001737 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(),
1738 &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001739 return 1;
1740 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001741
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001742 if (options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001743 JavaClassGeneratorOptions options;
1744 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001745 options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001746
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001747 if (options_.static_lib || options_.generate_non_final_ids) {
1748 options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001749 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001750
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001751 const StringPiece actual_package = context_->GetCompilationPackage();
1752 StringPiece output_package = context_->GetCompilationPackage();
1753 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001754 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001755 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001756 }
1757
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001758 if (options_.private_symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001759 // If we defined a private symbols package, we only emit Public symbols
1760 // to the original package, and private and public symbols to the
1761 // private package.
1762
1763 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001764 if (!WriteJavaFile(&final_table_, context_->GetCompilationPackage(),
1765 output_package, options)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001766 return 1;
1767 }
1768
1769 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001770 output_package = options_.private_symbols.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001771 }
1772
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001773 if (!WriteJavaFile(&final_table_, actual_package, output_package,
1774 options)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001775 return 1;
1776 }
1777
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001778 for (const std::string& extra_package : options_.extra_java_packages) {
1779 if (!WriteJavaFile(&final_table_, actual_package, extra_package,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001780 options)) {
1781 return 1;
1782 }
1783 }
1784 }
1785
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001786 if (!WriteProguardFile(options_.generate_proguard_rules_path,
1787 proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001788 return 1;
1789 }
1790
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001791 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1792 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001793 return 1;
1794 }
1795
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001796 if (context_->IsVerbose()) {
1797 DebugPrintTableOptions debug_print_table_options;
1798 debug_print_table_options.show_sources = true;
1799 Debug::PrintTable(&final_table_, debug_print_table_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001800 }
1801 return 0;
1802 }
1803
1804 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001805 LinkOptions options_;
1806 LinkContext* context_;
1807 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001808
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001809 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001810
1811 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001812 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001813
1814 // A vector of IFileCollections. This is mainly here to keep ownership of the
1815 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001816 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001817
1818 // A vector of ResourceTables. This is here to retain ownership, so that the
1819 // SymbolTable
1820 // can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001821 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001822};
1823
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001824int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001825 LinkContext context;
1826 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001827 std::vector<std::string> overlay_arg_list;
1828 std::vector<std::string> extra_java_packages;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001829 Maybe<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001830 Maybe<std::string> preferred_density;
1831 Maybe<std::string> product_list;
1832 bool legacy_x_flag = false;
1833 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001834 bool verbose = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001835 Maybe<std::string> stable_id_file_path;
1836 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001837 Flags flags =
1838 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001839 .RequiredFlag("-o", "Output path", &options.output_path)
1840 .RequiredFlag("--manifest", "Path to the Android manifest to build",
1841 &options.manifest_path)
1842 .OptionalFlagList("-I", "Adds an Android APK to link against",
1843 &options.include_paths)
1844 .OptionalFlagList(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001845 "-R",
1846 "Compilation unit to link, using `overlay` semantics.\n"
1847 "The last conflicting resource given takes precedence.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001848 &overlay_arg_list)
1849 .OptionalFlag("--java", "Directory in which to generate R.java",
1850 &options.generate_java_class_path)
1851 .OptionalFlag("--proguard",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001852 "Output file for generated Proguard rules",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001853 &options.generate_proguard_rules_path)
1854 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001855 "--proguard-main-dex",
1856 "Output file for generated Proguard rules for the main dex",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001857 &options.generate_main_dex_proguard_rules_path)
1858 .OptionalSwitch("--no-auto-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001859 "Disables automatic style and layout SDK versioning",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001860 &options.no_auto_version)
1861 .OptionalSwitch("--no-version-vectors",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001862 "Disables automatic versioning of vector drawables. "
1863 "Use this only\n"
1864 "when building with vector drawable support library",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001865 &options.no_version_vectors)
1866 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001867 "Disables automatic deduping of resources with\n"
1868 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001869 &options.no_resource_deduping)
1870 .OptionalSwitch(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001871 "-x",
1872 "Legacy flag that specifies to use the package identifier 0x01",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001873 &legacy_x_flag)
1874 .OptionalSwitch("-z",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001875 "Require localization of strings marked 'suggested'",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001876 &require_localization)
1877 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001878 "-c",
1879 "Comma separated list of configurations to include. The default\n"
1880 "is all configurations",
1881 &configs)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001882 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001883 "--preferred-density",
1884 "Selects the closest matching density and strips out all others.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001885 &preferred_density)
1886 .OptionalFlag("--product",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001887 "Comma separated list of product names to keep",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001888 &product_list)
1889 .OptionalSwitch("--output-to-dir",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001890 "Outputs the APK contents to a directory specified "
1891 "by -o",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001892 &options.output_to_directory)
1893 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001894 "Removes XML namespace prefix and URI "
1895 "information from AndroidManifest.xml\nand XML "
1896 "binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001897 &options.no_xml_namespaces)
1898 .OptionalFlag("--min-sdk-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001899 "Default minimum SDK version to use for "
1900 "AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001901 &options.manifest_fixer_options.min_sdk_version_default)
1902 .OptionalFlag(
1903 "--target-sdk-version",
1904 "Default target SDK version to use for "
1905 "AndroidManifest.xml",
1906 &options.manifest_fixer_options.target_sdk_version_default)
1907 .OptionalFlag("--version-code",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001908 "Version code (integer) to inject into the "
1909 "AndroidManifest.xml if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001910 &options.manifest_fixer_options.version_code_default)
1911 .OptionalFlag("--version-name",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001912 "Version name to inject into the AndroidManifest.xml "
1913 "if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001914 &options.manifest_fixer_options.version_name_default)
1915 .OptionalSwitch("--static-lib", "Generate a static Android library",
1916 &options.static_lib)
1917 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001918 "Merge all library resources under the app's package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001919 &options.no_static_lib_packages)
1920 .OptionalSwitch("--non-final-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001921 "Generates R.java without the final modifier.\n"
1922 "This is implied when --static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001923 &options.generate_non_final_ids)
1924 .OptionalFlag("--stable-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001925 "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001926 &stable_id_file_path)
1927 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001928 "--emit-ids",
1929 "Emit a file at the given path with a list of name to ID\n"
1930 "mappings, suitable for use with --stable-ids.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001931 &options.resource_id_map_path)
1932 .OptionalFlag("--private-symbols",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001933 "Package name to use when generating R.java for "
1934 "private symbols.\n"
1935 "If not specified, public and private symbols will use "
1936 "the application's "
1937 "package name",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001938 &options.private_symbols)
1939 .OptionalFlag("--custom-package",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001940 "Custom Java package under which to generate R.java",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001941 &options.custom_java_package)
1942 .OptionalFlagList("--extra-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001943 "Generate the same R.java but with different "
1944 "package names",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001945 &extra_java_packages)
1946 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001947 "Adds a JavaDoc annotation to all "
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001948 "generated Java classes",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001949 &options.javadoc_annotations)
1950 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001951 "Allows the addition of new resources in "
1952 "overlays without <add-resource> tags",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001953 &options.auto_add_overlay)
1954 .OptionalFlag("--rename-manifest-package",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001955 "Renames the package in AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001956 &options.manifest_fixer_options.rename_manifest_package)
1957 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001958 "--rename-instrumentation-target-package",
1959 "Changes the name of the target package for instrumentation. "
1960 "Most useful "
1961 "when used\nin conjunction with --rename-manifest-package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001962 &options.manifest_fixer_options
1963 .rename_instrumentation_target_package)
1964 .OptionalFlagList("-0", "File extensions not to compress",
1965 &options.extensions_to_not_compress)
1966 .OptionalFlagList(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001967 "--split",
1968 "Split resources matching a set of configs out to a "
1969 "Split APK.\nSyntax: path/to/output.apk:<config>[,<config>[...]]",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001970 &split_args)
1971 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001972
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001973 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001974 return 1;
1975 }
1976
1977 // Expand all argument-files passed into the command line. These start with
1978 // '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001979 std::vector<std::string> arg_list;
1980 for (const std::string& arg : flags.GetArgs()) {
1981 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001982 const std::string path = arg.substr(1, arg.size() - 1);
1983 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001984 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
1985 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001986 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001987 }
1988 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001989 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001990 }
1991 }
1992
1993 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001994 for (const std::string& arg : overlay_arg_list) {
1995 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001996 const std::string path = arg.substr(1, arg.size() - 1);
1997 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001998 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
1999 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002000 return 1;
2001 }
2002 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002003 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002004 }
2005 }
2006
2007 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002008 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002009 }
2010
2011 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002012 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002013 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002014 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002015 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002016 }
2017 }
2018
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002019 if (product_list) {
2020 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002021 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002022 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002023 }
2024 }
2025 }
2026
2027 AxisConfigFilter filter;
2028 if (configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002029 for (const StringPiece& config_str : util::Tokenize(configs.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002030 ConfigDescription config;
2031 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002032 if (lv.InitFromFilterString(config_str)) {
2033 lv.WriteTo(&config);
2034 } else if (!ConfigDescription::Parse(config_str, &config)) {
2035 context.GetDiagnostics()->Error(DiagMessage() << "invalid config '"
2036 << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002037 << "' for -c option");
2038 return 1;
2039 }
2040
2041 if (config.density != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002042 context.GetDiagnostics()->Warn(DiagMessage() << "ignoring density '"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002043 << config
2044 << "' for -c option");
2045 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002046 filter.AddConfig(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002047 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002048 }
2049
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002050 options.table_splitter_options.config_filter = &filter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002051 }
2052
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002053 if (preferred_density) {
2054 ConfigDescription preferred_density_config;
2055 if (!ConfigDescription::Parse(preferred_density.value(),
2056 &preferred_density_config)) {
2057 context.GetDiagnostics()->Error(
2058 DiagMessage() << "invalid density '" << preferred_density.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002059 << "' for --preferred-density option");
2060 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002061 }
2062
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002063 // Clear the version that can be automatically added.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002064 preferred_density_config.sdkVersion = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002065
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002066 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002067 ConfigDescription::CONFIG_DENSITY) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002068 context.GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002069 DiagMessage() << "invalid preferred density '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002070 << preferred_density.value() << "'. "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002071 << "Preferred density must only be a density value");
2072 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002073 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002074 options.table_splitter_options.preferred_density =
2075 preferred_density_config.density;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002076 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002077
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002078 if (!options.static_lib && stable_id_file_path) {
2079 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2080 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002081 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002082 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002083 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002084
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002085 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002086 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002087 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2088 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2089 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2090 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2091
2092 // Parse the split parameters.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002093 for (const std::string& split_arg : split_args) {
2094 options.split_paths.push_back({});
2095 options.split_constraints.push_back({});
2096 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(),
2097 &options.split_paths.back(),
2098 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002099 return 1;
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002100 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002101 }
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002102
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002103 // Turn off auto versioning for static-libs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002104 if (options.static_lib) {
2105 options.no_auto_version = true;
2106 options.no_version_vectors = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002107 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002108
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002109 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002110 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002111}
2112
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002113} // namespace aapt