blob: 717978eaa2262cf141c12fb636312c1a659c81af [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"
26#include "google/protobuf/io/coded_stream.h"
27
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "AppInfo.h"
29#include "Debug.h"
30#include "Flags.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080031#include "Locale.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032#include "NameMangler.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080033#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034#include "compile/IdAssigner.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080035#include "filter/ConfigFilter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036#include "flatten/Archive.h"
37#include "flatten/TableFlattener.h"
38#include "flatten/XmlFlattener.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080039#include "io/FileSystem.h"
40#include "io/ZipArchive.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070041#include "java/JavaClassGenerator.h"
42#include "java/ManifestClassGenerator.h"
43#include "java/ProguardRules.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044#include "link/Linkers.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045#include "link/ManifestFixer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080046#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047#include "link/TableMerger.h"
48#include "process/IResourceTableConsumer.h"
49#include "process/SymbolTable.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080050#include "proto/ProtoSerialize.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080051#include "split/TableSplitter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052#include "unflatten/BinaryResourceParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053#include "util/Files.h"
54#include "util/StringPiece.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080055#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070058
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059namespace aapt {
60
61struct LinkOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 std::string output_path;
63 std::string manifest_path;
64 std::vector<std::string> include_paths;
65 std::vector<std::string> overlay_files;
66 bool output_to_directory = false;
67 bool auto_add_overlay = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070068
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 // Java/Proguard options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 Maybe<std::string> generate_java_class_path;
71 Maybe<std::string> custom_java_package;
72 std::set<std::string> extra_java_packages;
73 Maybe<std::string> generate_proguard_rules_path;
74 Maybe<std::string> generate_main_dex_proguard_rules_path;
75 bool generate_non_final_ids = false;
76 std::vector<std::string> javadoc_annotations;
77 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070078
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 // Optimizations/features.
80 bool no_auto_version = false;
81 bool no_version_vectors = false;
82 bool no_resource_deduping = false;
83 bool no_xml_namespaces = false;
84 bool do_not_compress_anything = false;
85 std::unordered_set<std::string> extensions_to_not_compress;
86
87 // Static lib options.
88 bool static_lib = false;
89 bool no_static_lib_packages = false;
90
91 // AndroidManifest.xml massaging options.
92 ManifestFixerOptions manifest_fixer_options;
93
94 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -070096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 TableSplitterOptions table_splitter_options;
99 std::vector<SplitConstraints> split_constraints;
100 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700101
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 std::unordered_map<ResourceName, ResourceId> stable_id_map;
104 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700105};
106
Adam Lesinski64587af2016-02-18 18:33:06 -0800107class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 LinkContext() : name_mangler_({}) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 NameMangler* GetNameMangler() override { return &name_mangler_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
116 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 const std::string& GetCompilationPackage() override {
120 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 void SetCompilationPackage(const StringPiece& package_name) {
124 compilation_package_ = package_name.ToString();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800126
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 uint8_t GetPackageId() override { return package_id_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 void SetPackageId(uint8_t id) { package_id_ = id; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800130
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 SymbolTable* GetExternalSymbols() override { return &symbols_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 bool IsVerbose() override { return verbose_; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800134
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 int GetMinSdkVersion() override { return min_sdk_version_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700138
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 void SetMinSdkVersion(int minSdk) { min_sdk_version_ = minSdk; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 DISALLOW_COPY_AND_ASSIGN(LinkContext);
143
144 StdErrDiagnostics diagnostics_;
145 NameMangler name_mangler_;
146 std::string compilation_package_;
147 uint8_t package_id_ = 0x0;
148 SymbolTable symbols_;
149 bool verbose_ = false;
150 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151};
152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153static bool CopyFileToArchive(io::IFile* file, const std::string& out_path,
154 uint32_t compression_flags,
155 IArchiveWriter* writer, IAaptContext* context) {
156 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 context->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 << "failed to open file");
Adam Lesinski355f2852016-02-13 20:26:45 -0800160 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 }
162
163 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data->data());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 const size_t buffer_size = data->size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 if (context->IsVerbose()) {
167 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 << " to archive");
169 }
170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 if (writer->StartEntry(out_path, compression_flags)) {
172 if (writer->WriteEntry(buffer, buffer_size)) {
173 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 return true;
175 }
176 }
177 }
178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 context->GetDiagnostics()->Error(DiagMessage() << "failed to write file "
180 << out_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800182}
183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
185 Maybe<size_t> max_sdk_level, bool keep_raw_values,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 IArchiveWriter* writer, IAaptContext* context) {
187 BigBuffer buffer(1024);
188 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 options.keep_raw_values = keep_raw_values;
190 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800193 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 }
195
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 DiagMessage msg;
198 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 if (max_sdk_level) {
200 msg << " maxSdkLevel=" << max_sdk_level.value()
201 << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 }
205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 if (writer->StartEntry(path, ArchiveEntry::kCompress)) {
207 if (writer->WriteEntry(buffer)) {
208 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 return true;
210 }
211 }
212 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 << " to archive");
215 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800216}
217
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 const void* data,
220 size_t len,
Adam Lesinski355f2852016-02-13 20:26:45 -0800221 IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222 pb::ResourceTable pb_table;
223 if (!pb_table.ParseFromArray(data, len)) {
224 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 return {};
226 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800227
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 if (!table) {
231 return {};
232 }
233 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800234}
235
236/**
237 * Inflates an XML file from the source path.
238 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 IDiagnostics* diag) {
241 std::ifstream fin(path, std::ifstream::binary);
242 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 return {};
245 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800247}
248
Adam Lesinski355f2852016-02-13 20:26:45 -0800249struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 bool no_auto_version = false;
251 bool no_version_vectors = false;
252 bool no_xml_namespaces = false;
253 bool keep_raw_values = false;
254 bool do_not_compress_anything = false;
255 bool update_proguard_spec = false;
256 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800257};
258
259class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 public:
261 ResourceFileFlattener(const ResourceFileFlattenerOptions& options,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 IAaptContext* context, proguard::KeepSet* keep_set)
263 : options_(options), context_(context), keep_set_(keep_set) {}
Adam Lesinski355f2852016-02-13 20:26:45 -0800264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800266
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 private:
268 struct FileOperation {
269 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700270
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 // The entry this file came from.
272 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700273
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700276
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700279
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 std::string dst_path;
282 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800286
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
288 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 ResourceFileFlattenerOptions options_;
291 IAaptContext* context_;
292 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800293};
294
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
296 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 return 0;
298 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800299
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 for (const std::string& extension : options_.extensions_to_not_compress) {
301 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800303 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 }
305 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800306}
307
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308bool ResourceFileFlattener::LinkAndVersionXmlFile(
309 ResourceTable* table, FileOperation* file_op,
310 std::queue<FileOperation>* out_file_op_queue) {
311 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700313
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 if (context_->IsVerbose()) {
315 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 }
317
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 XmlReferenceLinker xml_linker;
319 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 return false;
321 }
322
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 if (options_.update_proguard_spec &&
324 !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700325 return false;
326 }
327
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 if (options_.no_xml_namespaces) {
329 XmlNamespaceRemover namespace_remover;
330 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800332 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800334
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 if (!options_.no_auto_version) {
336 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 xml::Element* el = xml::FindRootElement(doc);
339 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 if (el->name == "vector" || el->name == "animated-vector") {
341 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700344 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700346 }
347
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349
350 // Find the first SDK level used that is higher than this defined config and
351 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700352 const int min_sdk_version = context_->GetMinSdkVersion();
353 for (int sdk_level : xml_linker.sdk_levels()) {
354 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
355 if (!ShouldGenerateVersionedResource(file_op->entry, config,
356 sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 // If we shouldn't generate a versioned resource, stop checking.
358 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800359 }
360
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 ResourceFile versioned_file_desc = doc->file;
362 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700363
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 FileOperation new_file_op;
365 new_file_op.xml_to_flatten = util::make_unique<xml::XmlResource>(
366 versioned_file_desc, doc->root->Clone());
367 new_file_op.config = versioned_file_desc.config;
368 new_file_op.entry = file_op->entry;
369 new_file_op.dst_path = ResourceUtils::BuildResourceFileName(
370 versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800371
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 if (context_->IsVerbose()) {
373 context_->GetDiagnostics()->Note(
374 DiagMessage(versioned_file_desc.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 << "auto-versioning resource from config '" << config << "' -> '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376 << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800377 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 bool added = table->AddFileReferenceAllowMangled(
380 versioned_file_desc.name, versioned_file_desc.config,
381 versioned_file_desc.source, new_file_op.dst_path, nullptr,
382 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383 if (!added) {
384 return false;
385 }
386
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 break;
389 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800390 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 }
392 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800393}
394
395/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 * Do not insert or remove any resources while executing in this function. It
397 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800398 * corrupt the iteration order.
399 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400bool ResourceFileFlattener::Flatten(ResourceTable* table,
401 IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 bool error = false;
403 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800405
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 for (auto& pkg : table->packages) {
407 for (auto& type : pkg->types) {
408 // Sort by config and name, so that we get better locality in the zip
409 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 config_sorted_files.clear();
411 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800412
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 // Populate the queue with all files in the ResourceTable.
414 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 for (auto& config_value : entry->values) {
416 FileReference* file_ref =
417 ValueCast<FileReference>(config_value->value.get());
418 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700419 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700421
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700426 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 }
428
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 FileOperation file_op;
430 file_op.entry = entry.get();
431 file_op.dst_path = *file_ref->path;
432 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 if (type->type != ResourceType::kRaw &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436 (util::EndsWith(src_path, ".xml.flat") ||
437 util::EndsWith(src_path, ".xml"))) {
438 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 << "failed to open file");
442 return false;
443 }
444
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 file_op.xml_to_flatten =
446 xml::Inflate(data->data(), data->size(),
447 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 return false;
451 }
452
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700453 file_op.xml_to_flatten->file.config = config_value->config;
454 file_op.xml_to_flatten->file.source = file_ref->GetSource();
455 file_op.xml_to_flatten->file.name =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 ResourceName(pkg->name, type->type, entry->name);
457
458 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700462
463 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 // else we end up copying the string in the std::make_pair() method,
465 // then creating a StringPiece from the copy, which would cause us
466 // to end up referencing garbage in the map.
467 const StringPiece entry_name(entry->name);
468 config_sorted_files[std::make_pair(
469 config_value->config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 }
471 }
472 }
473
474 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 for (; !file_operations.empty(); file_operations.pop()) {
476 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 error = true;
480 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700481 }
482
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
484 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700485 // creating a StringPiece from the copy, which would cause us to end up
486 // referencing garbage in the map.
487 const StringPiece entry_name(file_op.entry->name);
488 config_sorted_files[std::make_pair(file_op.config, entry_name)] =
489 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 }
491
492 if (error) {
493 return false;
494 }
495
496 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497 for (auto& map_entry : config_sorted_files) {
498 const ConfigDescription& config = map_entry.first.first;
499 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700501 if (file_op.xml_to_flatten) {
502 Maybe<size_t> max_sdk_level;
503 if (!options_.no_auto_version && !file_op.skip_version) {
504 max_sdk_level =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 }
508
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509 bool result = FlattenXml(
510 file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
511 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 if (!result) {
513 error = true;
514 }
515 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 bool result = CopyFileToArchive(
517 file_op.file_to_copy, file_op.dst_path,
518 GetCompressionFlags(file_op.dst_path), archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700519 if (!result) {
520 error = true;
521 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700522 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700524 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 }
526 return !error;
527}
528
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529static bool WriteStableIdMapToPath(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 IDiagnostics* diag,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 const std::unordered_map<ResourceName, ResourceId>& id_map,
532 const std::string& id_map_path) {
533 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700534 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 return false;
537 }
538
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700539 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 const ResourceName& name = entry.first;
541 const ResourceId& id = entry.second;
542 fout << name << " = " << id << "\n";
543 }
544
545 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700546 diag->Error(DiagMessage(id_map_path)
547 << "failed writing to file: "
548 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 return false;
550 }
551
552 return true;
553}
554
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555static bool LoadStableIdMap(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 IDiagnostics* diag, const std::string& path,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700557 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 std::string content;
559 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 return false;
562 }
563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 out_id_map->clear();
565 size_t line_no = 0;
566 for (StringPiece line : util::Tokenize(content, '\n')) {
567 line_no++;
568 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 if (line.empty()) {
570 continue;
571 }
572
573 auto iter = std::find(line.begin(), line.end(), '=');
574 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 return false;
577 }
578
579 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 StringPiece res_name_str =
581 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
582 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
583 diag->Error(DiagMessage(Source(path, line_no))
584 << "invalid resource name '" << res_name_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 return false;
586 }
587
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
589 const size_t res_id_str_len = line.size() - res_id_start_idx;
590 StringPiece res_id_str =
591 util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
594 if (!maybe_id) {
595 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '"
596 << res_id_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 return false;
598 }
599
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700600 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 }
602 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700603}
604
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700605static bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag,
606 std::string* out_path,
607 SplitConstraints* out_split) {
608 std::vector<std::string> parts = util::Split(arg, ':');
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700609 if (parts.size() != 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700610 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
611 diag->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 DiagMessage()
613 << "should be --split path/to/output.apk:<config>[,<config>...]");
614 return false;
615 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700616 *out_path = parts[0];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 std::vector<ConfigDescription> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 configs.push_back({});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700620 if (!ConfigDescription::Parse(config_str, &configs.back())) {
621 diag->Error(DiagMessage() << "invalid config '" << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622 << "' in split parameter '" << arg << "'");
623 return false;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700624 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700626 out_split->configs.insert(configs.begin(), configs.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627 return true;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700628}
629
Adam Lesinskifb48d292015-11-07 15:52:13 -0800630class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631 public:
632 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 : options_(options),
634 context_(context),
635 final_table_(),
636 file_collection_(util::make_unique<io::FileCollection>()) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700637
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 /**
639 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700640 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700642 bool LoadSymbolsFromIncludePaths() {
643 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 for (const std::string& path : options_.include_paths) {
646 if (context_->IsVerbose()) {
647 context_->GetDiagnostics()->Note(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 << "loading include path");
649 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700650
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652 std::string error_str;
653 std::unique_ptr<ResourceTable> static_include =
654 LoadStaticLibrary(path, &error_str);
655 if (static_include) {
656 if (!options_.static_lib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 // Can't include static libraries when not building a static library.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700658 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 DiagMessage(path)
660 << "can't include static library when building app");
661 return false;
662 }
663
664 // If we are using --no-static-lib-packages, we need to rename the
665 // package of this
666 // table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700667 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 if (ResourceTablePackage* pkg =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700669 static_include->FindPackageById(0x7f)) {
670 pkg->name = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 }
672 }
673
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 context_->GetExternalSymbols()->AppendSource(
675 util::make_unique<ResourceTableSymbolSource>(static_include.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700677 static_table_includes_.push_back(std::move(static_include));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700678
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 return false;
683 }
684
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685 if (!asset_source->AddAssetPath(path)) {
686 context_->GetDiagnostics()->Error(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 << "failed to load include path");
688 return false;
689 }
690 }
691
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700692 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700693 return true;
694 }
695
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700696 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 IDiagnostics* diag) {
698 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700699 if (xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get())) {
700 AppInfo app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700702 if (!manifest_el->namespace_uri.empty() ||
703 manifest_el->name != "manifest") {
704 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705 << "root tag must be <manifest>");
706 return {};
707 }
708
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700709 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
710 if (!package_attr) {
711 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700712 << "<manifest> must have a 'package' attribute");
713 return {};
714 }
715
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 app_info.package = package_attr->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700718 if (xml::Attribute* version_code_attr =
719 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
720 Maybe<uint32_t> maybe_code =
721 ResourceUtils::ParseInt(version_code_attr->value);
722 if (!maybe_code) {
723 diag->Error(DiagMessage(xml_res->file.source.WithLine(
724 manifest_el->line_number))
725 << "invalid android:versionCode '"
726 << version_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 return {};
728 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700729 app_info.version_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 }
731
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700732 if (xml::Attribute* revision_code_attr =
733 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
734 Maybe<uint32_t> maybe_code =
735 ResourceUtils::ParseInt(revision_code_attr->value);
736 if (!maybe_code) {
737 diag->Error(DiagMessage(xml_res->file.source.WithLine(
738 manifest_el->line_number))
739 << "invalid android:revisionCode '"
740 << revision_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 return {};
742 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700743 app_info.revision_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700744 }
745
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700746 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
747 if (xml::Attribute* min_sdk = uses_sdk_el->FindAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700749 app_info.min_sdk_version = min_sdk->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 }
751 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700752 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700753 }
754 return {};
755 }
756
757 /**
758 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it
759 * linked.
760 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700763 bool VerifyNoExternalPackages() {
764 auto is_ext_package_func =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765 [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
767 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768 };
769
770 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700771 for (const auto& package : final_table_.packages) {
772 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773 // We have a package that is not related to the one we're building!
774 for (const auto& type : package->types) {
775 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700777
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700778 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700779 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700780 // for the 'android' package. This is due to legacy reasons.
781 if (ValueCast<Id>(config_value->value.get()) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700782 package->name == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700783 context_->GetDiagnostics()->Warn(
784 DiagMessage(config_value->value->GetSource())
785 << "generated id '" << res_name
786 << "' for external package '" << package->name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700787 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700788 context_->GetDiagnostics()->Error(
789 DiagMessage(config_value->value->GetSource())
790 << "defined resource '" << res_name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700791 << "' for external package '" << package->name << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700792 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700793 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700794 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700795 }
796 }
797 }
798 }
799
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700800 auto new_end_iter =
801 std::remove_if(final_table_.packages.begin(),
802 final_table_.packages.end(), is_ext_package_func);
803 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700804 return !error;
805 }
806
807 /**
808 * Returns true if no IDs have been set, false otherwise.
809 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700810 bool VerifyNoIdsSet() {
811 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700812 for (const auto& type : package->types) {
813 if (type->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700814 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700815 DiagMessage() << "type " << type->type << " has ID " << std::hex
816 << (int)type->id.value() << std::dec
817 << " assigned");
818 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700819 }
820
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700821 for (const auto& entry : type->entries) {
822 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700823 ResourceNameRef res_name(package->name, type->type, entry->name);
824 context_->GetDiagnostics()->Error(
825 DiagMessage() << "entry " << res_name << " has ID " << std::hex
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700826 << (int)entry->id.value() << std::dec
827 << " assigned");
828 return false;
829 }
830 }
831 }
832 }
833 return true;
834 }
835
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700836 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
837 if (options_.output_to_directory) {
838 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700840 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700841 }
842 }
843
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700844 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700845 BigBuffer buffer(1024);
846 TableFlattener flattener(&buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700847 if (!flattener.Consume(context_, table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 return false;
849 }
850
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700851 if (writer->StartEntry("resources.arsc", ArchiveEntry::kAlign)) {
852 if (writer->WriteEntry(buffer)) {
853 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700854 return true;
855 }
856 }
857 }
858
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700859 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700860 DiagMessage() << "failed to write resources.arsc to archive");
861 return false;
862 }
863
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700864 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700865 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700866 if (!writer->StartEntry("resources.arsc.flat", 0)) {
867 context_->GetDiagnostics()->Error(DiagMessage() << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700868 return false;
869 }
870
871 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700872 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700873 {
874 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700875 // ZeroCopyOutputStream interface.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700876 CopyingOutputStreamAdaptor adaptor(writer);
877
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700878 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
879 if (!pb_table->SerializeToZeroCopyStream(&adaptor)) {
880 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700881 return false;
882 }
883 }
884
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700885 if (!writer->FinishEntry()) {
886 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700887 << "failed to finish entry");
888 return false;
889 }
890 return true;
891 }
892
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700893 bool WriteJavaFile(ResourceTable* table,
894 const StringPiece& package_name_to_generate,
895 const StringPiece& out_package,
896 const JavaClassGeneratorOptions& java_options) {
897 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700898 return true;
899 }
900
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700901 std::string out_path = options_.generate_java_class_path.value();
902 file::AppendPath(&out_path, file::PackageToPath(out_package));
903 if (!file::mkdirs(out_path)) {
904 context_->GetDiagnostics()->Error(
905 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700906 return false;
907 }
908
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700909 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700910
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700911 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700912 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700913 context_->GetDiagnostics()->Error(
914 DiagMessage() << "failed writing to '" << out_path << "': "
915 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700916 return false;
917 }
918
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700919 JavaClassGenerator generator(context_, table, java_options);
920 if (!generator.Generate(package_name_to_generate, out_package, &fout)) {
921 context_->GetDiagnostics()->Error(DiagMessage(out_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700922 << generator.getError());
923 return false;
924 }
925
926 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700927 context_->GetDiagnostics()->Error(
928 DiagMessage() << "failed writing to '" << out_path << "': "
929 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700930 }
931 return true;
932 }
933
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700934 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
935 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700936 return true;
937 }
938
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700939 std::unique_ptr<ClassDefinition> manifest_class =
940 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700941
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700942 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700943 // Something bad happened, but we already logged it, so exit.
944 return false;
945 }
946
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700947 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700948 // Empty Manifest class, no need to generate it.
949 return true;
950 }
951
952 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700953 for (const std::string& annotation : options_.javadoc_annotations) {
954 std::string proper_annotation = "@";
955 proper_annotation += annotation;
956 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700957 }
958
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700959 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700960
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700961 std::string out_path = options_.generate_java_class_path.value();
962 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700963
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700964 if (!file::mkdirs(out_path)) {
965 context_->GetDiagnostics()->Error(
966 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700967 return false;
968 }
969
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700970 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700971
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700972 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700973 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700974 context_->GetDiagnostics()->Error(
975 DiagMessage() << "failed writing to '" << out_path << "': "
976 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 return false;
978 }
979
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700980 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8,
981 true, &fout)) {
982 context_->GetDiagnostics()->Error(
983 DiagMessage() << "failed writing to '" << out_path << "': "
984 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 return false;
986 }
987 return true;
988 }
989
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700990 bool WriteProguardFile(const Maybe<std::string>& out,
991 const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700992 if (!out) {
993 return true;
994 }
995
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700996 const std::string& out_path = out.value();
997 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700998 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700999 context_->GetDiagnostics()->Error(
1000 DiagMessage() << "failed to open '" << out_path << "': "
1001 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001002 return false;
1003 }
1004
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001005 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001006 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001007 context_->GetDiagnostics()->Error(
1008 DiagMessage() << "failed writing to '" << out_path << "': "
1009 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010 return false;
1011 }
1012 return true;
1013 }
1014
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001015 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1016 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001017 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001018 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001019 if (!collection) {
1020 return {};
1021 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001022 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001023 }
1024
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001025 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001026 io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001028 if (!file) {
1029 return {};
1030 }
1031
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001032 std::unique_ptr<io::IData> data = file->OpenAsData();
1033 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1034 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001035 }
1036
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001037 bool MergeStaticLibrary(const std::string& input, bool override) {
1038 if (context_->IsVerbose()) {
1039 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001040 << "merging static library " << input);
1041 }
1042
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001043 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001044 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001045 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001046 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001047 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001048 return false;
1049 }
1050
1051 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001052 LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001053 if (!table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001054 context_->GetDiagnostics()->Error(DiagMessage(input)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001055 << "invalid static library");
1056 return false;
1057 }
1058
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001059 ResourceTablePackage* pkg = table->FindPackageById(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001060 if (!pkg) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001061 context_->GetDiagnostics()->Error(DiagMessage(input)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001062 << "static library has no package");
1063 return false;
1064 }
1065
1066 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001067 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001068 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001069 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001070
1071 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001072 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001073 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001074 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075 }
1076
1077 pkg->name = "";
1078 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001079 result = table_merger_->MergeOverlay(Source(input), table.get(),
1080 collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001081 } else {
1082 result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001083 table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001084 }
1085
1086 } else {
1087 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001088 // preserved and resource names are mangled.
1089 result = table_merger_->MergeAndMangle(Source(input), pkg->name,
1090 table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091 }
1092
1093 if (!result) {
1094 return false;
1095 }
1096
1097 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001098 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001099 return true;
1100 }
1101
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001102 bool MergeResourceTable(io::IFile* file, bool override) {
1103 if (context_->IsVerbose()) {
1104 context_->GetDiagnostics()->Note(
1105 DiagMessage() << "merging resource table " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001106 }
1107
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001108 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001110 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001111 << "failed to open file");
1112 return false;
1113 }
1114
1115 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001116 LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1117 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001118 if (!table) {
1119 return false;
1120 }
1121
1122 bool result = false;
1123 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001124 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001125 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001126 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001127 }
1128 return result;
1129 }
1130
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001131 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001132 bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001133 if (context_->IsVerbose()) {
1134 context_->GetDiagnostics()->Note(
1135 DiagMessage() << "merging '" << file_desc->name
1136 << "' from compiled file " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001137 }
1138
1139 bool result = false;
1140 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001141 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001142 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001143 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001144 }
1145
1146 if (!result) {
1147 return false;
1148 }
1149
1150 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001151 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1152 if (exported_symbol.name.package.empty()) {
1153 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001154 }
1155
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001156 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001157
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001158 Maybe<ResourceName> mangled_name =
1159 context_->GetNameMangler()->MangleName(exported_symbol.name);
1160 if (mangled_name) {
1161 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001162 }
1163
1164 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001165 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1166 bool result = final_table_.AddResourceAllowMangled(
1167 res_name, ConfigDescription::DefaultConfig(), std::string(),
1168 std::move(id), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001169 if (!result) {
1170 return false;
1171 }
1172 }
1173 return true;
1174 }
1175
1176 /**
1177 * Takes a path to load as a ZIP file and merges the files within into the
1178 * master ResourceTable.
1179 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001180 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001181 *
1182 * An io::IFileCollection is created from the ZIP file and added to the set of
1183 * io::IFileCollections that are open.
1184 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001185 bool MergeArchive(const std::string& input, bool override) {
1186 if (context_->IsVerbose()) {
1187 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001188 << input);
1189 }
1190
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001191 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001192 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001193 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001194 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001195 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001196 return false;
1197 }
1198
1199 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001200 for (auto iter = collection->Iterator(); iter->HasNext();) {
1201 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001202 error = true;
1203 }
1204 }
1205
1206 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001207 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001208 return !error;
1209 }
1210
1211 /**
1212 * Takes a path to load and merge into the master ResourceTable. If override
1213 * is true,
1214 * conflicting resources are allowed to override each other, in order of last
1215 * seen.
1216 *
1217 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1218 * as ZIP archive
1219 * and the files within are merged individually.
1220 *
1221 * Otherwise the files is processed on its own.
1222 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001223 bool MergePath(const std::string& path, bool override) {
1224 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1225 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1226 return MergeArchive(path, override);
1227 } else if (util::EndsWith(path, ".apk")) {
1228 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001229 }
1230
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001231 io::IFile* file = file_collection_->InsertFile(path);
1232 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001233 }
1234
1235 /**
1236 * Takes a file to load and merge into the master ResourceTable. If override
1237 * is true,
1238 * conflicting resources are allowed to override each other, in order of last
1239 * seen.
1240 *
1241 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1242 * merged into the
1243 * master ResourceTable. If the file ends with .flat, then it is treated like
1244 * a compiled file
1245 * and the header data is read and merged into the final ResourceTable.
1246 *
1247 * All other file types are ignored. This is because these files could be
1248 * coming from a zip,
1249 * where we could have other files like classes.dex.
1250 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001251 bool MergeFile(io::IFile* file, bool override) {
1252 const Source& src = file->GetSource();
1253 if (util::EndsWith(src.path, ".arsc.flat")) {
1254 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001255
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001256 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001257 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001258 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001259 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001260 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001261 return false;
1262 }
1263
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001264 CompiledFileInputStream input_stream(data->data(), data->size());
1265 uint32_t num_files = 0;
1266 if (!input_stream.ReadLittleEndian32(&num_files)) {
1267 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001268 << "failed read num files");
1269 return false;
1270 }
1271
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001272 for (uint32_t i = 0; i < num_files; i++) {
1273 pb::CompiledFile compiled_file;
1274 if (!input_stream.ReadCompiledFile(&compiled_file)) {
1275 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001276 DiagMessage(src) << "failed to read compiled file header");
1277 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001278 }
1279
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001280 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001281 if (!input_stream.ReadDataMetaData(&offset, &len)) {
1282 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001283 << "failed to read data meta data");
1284 return false;
1285 }
1286
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001287 std::unique_ptr<ResourceFile> resource_file =
1288 DeserializeCompiledFileFromPb(compiled_file, file->GetSource(),
1289 context_->GetDiagnostics());
1290 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001291 return false;
1292 }
1293
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001294 if (!MergeCompiledFile(file->CreateFileSegment(offset, len),
1295 resource_file.get(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001296 return false;
1297 }
1298 }
1299 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001300 } else if (util::EndsWith(src.path, ".xml") ||
1301 util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001302 // Since AAPT compiles these file types and appends .flat to them, seeing
1303 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001304 const StringPiece file_type =
1305 util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1306 context_->GetDiagnostics()->Error(DiagMessage(src)
1307 << "uncompiled " << file_type
Adam Lesinski6a396c12016-10-20 14:38:23 -07001308 << " file passed as argument. Must be "
1309 "compiled first into .flat file.");
1310 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001311 }
1312
1313 // Ignore non .flat files. This could be classes.dex or something else that
1314 // happens
1315 // to be in an archive.
1316 return true;
1317 }
1318
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001319 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(
1320 const AppInfo& app_info, const SplitConstraints& constraints) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001321 std::unique_ptr<xml::XmlResource> doc =
1322 util::make_unique<xml::XmlResource>();
1323
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001324 std::unique_ptr<xml::Namespace> namespace_android =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001325 util::make_unique<xml::Namespace>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001326 namespace_android->namespace_uri = xml::kSchemaAndroid;
1327 namespace_android->namespace_prefix = "android";
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001328
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001329 std::unique_ptr<xml::Element> manifest_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001330 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001331 manifest_el->name = "manifest";
1332 manifest_el->attributes.push_back(
1333 xml::Attribute{"", "package", app_info.package});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001334
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001335 if (app_info.version_code) {
1336 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001337 xml::Attribute{xml::kSchemaAndroid, "versionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001338 std::to_string(app_info.version_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001339 }
1340
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001341 if (app_info.revision_code) {
1342 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001343 xml::Attribute{xml::kSchemaAndroid, "revisionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001344 std::to_string(app_info.revision_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001345 }
1346
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001347 std::stringstream split_name;
1348 split_name << "config." << util::Joiner(constraints.configs, "_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001349
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001350 manifest_el->attributes.push_back(
1351 xml::Attribute{"", "split", split_name.str()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001352
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001353 std::unique_ptr<xml::Element> application_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001354 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001355 application_el->name = "application";
1356 application_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001357 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false"});
1358
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001359 manifest_el->AddChild(std::move(application_el));
1360 namespace_android->AddChild(std::move(manifest_el));
1361 doc->root = std::move(namespace_android);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001362 return doc;
1363 }
1364
1365 /**
1366 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001367 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001368 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001369 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001370 xml::XmlResource* manifest, ResourceTable* table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001371 const bool keep_raw_values = options_.static_lib;
1372 bool result = FlattenXml(manifest, "AndroidManifest.xml", {},
1373 keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001374 if (!result) {
1375 return false;
1376 }
1377
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001378 ResourceFileFlattenerOptions file_flattener_options;
1379 file_flattener_options.keep_raw_values = keep_raw_values;
1380 file_flattener_options.do_not_compress_anything =
1381 options_.do_not_compress_anything;
1382 file_flattener_options.extensions_to_not_compress =
1383 options_.extensions_to_not_compress;
1384 file_flattener_options.no_auto_version = options_.no_auto_version;
1385 file_flattener_options.no_version_vectors = options_.no_version_vectors;
1386 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1387 file_flattener_options.update_proguard_spec =
1388 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001389
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001390 ResourceFileFlattener file_flattener(file_flattener_options, context_,
1391 keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001392
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001393 if (!file_flattener.Flatten(table, writer)) {
1394 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001395 << "failed linking file resources");
1396 return false;
1397 }
1398
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001399 if (options_.static_lib) {
1400 if (!FlattenTableToPb(table, writer)) {
1401 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001402 DiagMessage() << "failed to write resources.arsc.flat");
1403 return false;
1404 }
1405 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001406 if (!FlattenTable(table, writer)) {
1407 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001408 << "failed to write resources.arsc");
1409 return false;
1410 }
1411 }
1412 return true;
1413 }
1414
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001415 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001416 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001417 std::unique_ptr<xml::XmlResource> manifest_xml =
1418 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1419 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001420 return 1;
1421 }
1422
1423 // First extract the Package name without modifying it (via
1424 // --rename-manifest-package).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001425 if (Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1426 manifest_xml.get(), context_->GetDiagnostics())) {
1427 const AppInfo& app_info = maybe_app_info.value();
1428 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001429 }
1430
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001431 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1432 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001433 return 1;
1434 }
1435
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001436 Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1437 manifest_xml.get(), context_->GetDiagnostics());
1438 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439 return 1;
1440 }
1441
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001442 const AppInfo& app_info = maybe_app_info.value();
1443 if (app_info.min_sdk_version) {
1444 if (Maybe<int> maybe_min_sdk_version = ResourceUtils::ParseSdkVersion(
1445 app_info.min_sdk_version.value())) {
1446 context_->SetMinSdkVersion(maybe_min_sdk_version.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001447 }
1448 }
1449
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001450 context_->SetNameManglerPolicy(
1451 NameManglerPolicy{context_->GetCompilationPackage()});
1452 if (context_->GetCompilationPackage() == "android") {
1453 context_->SetPackageId(0x01);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001454 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001455 context_->SetPackageId(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001456 }
1457
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001458 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001459 return 1;
1460 }
1461
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001462 TableMergerOptions table_merger_options;
1463 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
1464 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_,
1465 table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001466
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001467 if (context_->IsVerbose()) {
1468 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001469 << "linking package '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001470 << context_->GetCompilationPackage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001471 << "' with package ID " << std::hex
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001472 << (int)context_->GetPackageId());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001473 }
1474
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001475 for (const std::string& input : input_files) {
1476 if (!MergePath(input, false)) {
1477 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001478 << "failed parsing input");
1479 return 1;
1480 }
1481 }
1482
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001483 for (const std::string& input : options_.overlay_files) {
1484 if (!MergePath(input, true)) {
1485 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001486 << "failed parsing overlays");
1487 return 1;
1488 }
1489 }
1490
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001491 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001492 return 1;
1493 }
1494
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001495 if (!options_.static_lib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001496 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001497 if (!mover.Consume(context_, &final_table_)) {
1498 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001499 DiagMessage() << "failed moving private attributes");
1500 return 1;
1501 }
1502
1503 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001504 IdAssigner id_assigner(&options_.stable_id_map);
1505 if (!id_assigner.Consume(context_, &final_table_)) {
1506 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001507 << "failed assigning IDs");
1508 return 1;
1509 }
1510
1511 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001512 if (options_.resource_id_map_path) {
1513 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001514 for (auto& type : package->types) {
1515 for (auto& entry : type->entries) {
1516 ResourceName name(package->name, type->type, entry->name);
1517 // The IDs are guaranteed to exist.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001518 options_.stable_id_map[std::move(name)] = ResourceId(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001519 package->id.value(), type->id.value(), entry->id.value());
1520 }
1521 }
1522 }
1523
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001524 if (!WriteStableIdMapToPath(context_->GetDiagnostics(),
1525 options_.stable_id_map,
1526 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001527 return 1;
1528 }
1529 }
1530 } else {
1531 // Static libs are merged with other apps, and ID collisions are bad, so
1532 // verify that
1533 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001534 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 return 1;
1536 }
1537 }
1538
1539 // Add the names to mangle based on our source merge earlier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001540 context_->SetNameManglerPolicy(NameManglerPolicy{
1541 context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001542
1543 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001544 context_->GetExternalSymbols()->PrependSource(
1545 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001546
1547 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001548 if (!linker.Consume(context_, &final_table_)) {
1549 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001550 << "failed linking references");
1551 return 1;
1552 }
1553
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001554 if (options_.static_lib) {
1555 if (!options_.products.empty()) {
1556 context_->GetDiagnostics()
1557 ->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001558 << "can't select products when building static library");
1559 }
1560 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001561 ProductFilter product_filter(options_.products);
1562 if (!product_filter.Consume(context_, &final_table_)) {
1563 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001564 << "failed stripping products");
1565 return 1;
1566 }
1567 }
1568
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001569 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001570 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001571 if (!versioner.Consume(context_, &final_table_)) {
1572 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001573 << "failed versioning styles");
1574 return 1;
1575 }
1576 }
1577
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001578 if (!options_.static_lib && context_->GetMinSdkVersion() > 0) {
1579 if (context_->IsVerbose()) {
1580 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001581 DiagMessage() << "collapsing resource versions for minimum SDK "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001582 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001583 }
1584
1585 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001586 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001587 return 1;
1588 }
1589 }
1590
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001591 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001592 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001593 if (!deduper.Consume(context_, &final_table_)) {
1594 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001595 << "failed deduping resources");
1596 return 1;
1597 }
1598 }
1599
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001600 proguard::KeepSet proguard_keep_set;
1601 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001602
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001603 if (options_.static_lib) {
1604 if (options_.table_splitter_options.config_filter != nullptr ||
1605 options_.table_splitter_options.preferred_density) {
1606 context_->GetDiagnostics()
1607 ->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001608 << "can't strip resources when building static library");
1609 }
1610 } else {
1611 // Adjust the SplitConstraints so that their SDK version is stripped if it
1612 // is less
1613 // than or equal to the minSdk. Otherwise the resources that have had
1614 // their SDK version
1615 // stripped due to minSdk won't ever match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001616 std::vector<SplitConstraints> adjusted_constraints_list;
1617 adjusted_constraints_list.reserve(options_.split_constraints.size());
1618 for (const SplitConstraints& constraints : options_.split_constraints) {
1619 SplitConstraints adjusted_constraints;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001620 for (const ConfigDescription& config : constraints.configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001621 if (config.sdkVersion <= context_->GetMinSdkVersion()) {
1622 adjusted_constraints.configs.insert(config.CopyWithoutSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001623 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001624 adjusted_constraints.configs.insert(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001625 }
1626 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001627 adjusted_constraints_list.push_back(std::move(adjusted_constraints));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001628 }
1629
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001630 TableSplitter table_splitter(adjusted_constraints_list,
1631 options_.table_splitter_options);
1632 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001633 return 1;
1634 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001635 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001636
1637 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001638 auto path_iter = options_.split_paths.begin();
1639 auto split_constraints_iter = adjusted_constraints_list.begin();
1640 for (std::unique_ptr<ResourceTable>& split_table :
1641 table_splitter.splits()) {
1642 if (context_->IsVerbose()) {
1643 context_->GetDiagnostics()->Note(
1644 DiagMessage(*path_iter)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001645 << "generating split with configurations '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001646 << util::Joiner(split_constraints_iter->configs, ", ") << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001647 }
1648
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001649 std::unique_ptr<IArchiveWriter> archive_writer =
1650 MakeArchiveWriter(*path_iter);
1651 if (!archive_writer) {
1652 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001653 << "failed to create archive");
1654 return 1;
1655 }
1656
1657 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001658 std::unique_ptr<xml::XmlResource> split_manifest =
1659 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001660
1661 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001662 if (!linker.Consume(context_, split_manifest.get())) {
1663 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001664 DiagMessage() << "failed to create Split AndroidManifest.xml");
1665 return 1;
1666 }
1667
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001668 if (!WriteApk(archive_writer.get(), &proguard_keep_set,
1669 split_manifest.get(), split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001670 return 1;
1671 }
1672
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001673 ++path_iter;
1674 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001675 }
1676 }
1677
1678 // Start writing the base APK.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001679 std::unique_ptr<IArchiveWriter> archive_writer =
1680 MakeArchiveWriter(options_.output_path);
1681 if (!archive_writer) {
1682 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001683 << "failed to create archive");
1684 return 1;
1685 }
1686
1687 bool error = false;
1688 {
1689 // AndroidManifest.xml has no resource name, but the CallSite is built
1690 // from the name
1691 // (aka, which package the AndroidManifest.xml is coming from).
1692 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001693 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001694
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001695 XmlReferenceLinker manifest_linker;
1696 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1697 if (options_.generate_proguard_rules_path &&
1698 !proguard::CollectProguardRulesForManifest(
1699 Source(options_.manifest_path), manifest_xml.get(),
1700 &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001701 error = true;
1702 }
1703
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001704 if (options_.generate_main_dex_proguard_rules_path &&
1705 !proguard::CollectProguardRulesForManifest(
1706 Source(options_.manifest_path), manifest_xml.get(),
1707 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001708 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001709 }
1710
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001711 if (options_.generate_java_class_path) {
1712 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001713 error = true;
1714 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001715 }
1716
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001717 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001718 // PackageParser will fail if URIs are removed from
1719 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001720 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1721 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001722 error = true;
1723 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001724 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001725 } else {
1726 error = true;
1727 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001728 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001729
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001730 if (error) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001731 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001732 << "failed processing manifest");
1733 return 1;
1734 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001735
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001736 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(),
1737 &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001738 return 1;
1739 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001740
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001741 if (options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001742 JavaClassGeneratorOptions options;
1743 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001744 options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001745
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001746 if (options_.static_lib || options_.generate_non_final_ids) {
1747 options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001748 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001749
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001750 const StringPiece actual_package = context_->GetCompilationPackage();
1751 StringPiece output_package = context_->GetCompilationPackage();
1752 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001753 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001754 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001755 }
1756
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001757 if (options_.private_symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001758 // If we defined a private symbols package, we only emit Public symbols
1759 // to the original package, and private and public symbols to the
1760 // private package.
1761
1762 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001763 if (!WriteJavaFile(&final_table_, context_->GetCompilationPackage(),
1764 output_package, options)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001765 return 1;
1766 }
1767
1768 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001769 output_package = options_.private_symbols.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001770 }
1771
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001772 if (!WriteJavaFile(&final_table_, actual_package, output_package,
1773 options)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001774 return 1;
1775 }
1776
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001777 for (const std::string& extra_package : options_.extra_java_packages) {
1778 if (!WriteJavaFile(&final_table_, actual_package, extra_package,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001779 options)) {
1780 return 1;
1781 }
1782 }
1783 }
1784
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001785 if (!WriteProguardFile(options_.generate_proguard_rules_path,
1786 proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001787 return 1;
1788 }
1789
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001790 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1791 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001792 return 1;
1793 }
1794
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001795 if (context_->IsVerbose()) {
1796 DebugPrintTableOptions debug_print_table_options;
1797 debug_print_table_options.show_sources = true;
1798 Debug::PrintTable(&final_table_, debug_print_table_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001799 }
1800 return 0;
1801 }
1802
1803 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001804 LinkOptions options_;
1805 LinkContext* context_;
1806 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001807
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001808 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001809
1810 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001811 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001812
1813 // A vector of IFileCollections. This is mainly here to keep ownership of the
1814 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001815 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001816
1817 // A vector of ResourceTables. This is here to retain ownership, so that the
1818 // SymbolTable
1819 // can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001820 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001821};
1822
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001823int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001824 LinkContext context;
1825 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001826 std::vector<std::string> overlay_arg_list;
1827 std::vector<std::string> extra_java_packages;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001828 Maybe<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001829 Maybe<std::string> preferred_density;
1830 Maybe<std::string> product_list;
1831 bool legacy_x_flag = false;
1832 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001833 bool verbose = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001834 Maybe<std::string> stable_id_file_path;
1835 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001836 Flags flags =
1837 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001838 .RequiredFlag("-o", "Output path", &options.output_path)
1839 .RequiredFlag("--manifest", "Path to the Android manifest to build",
1840 &options.manifest_path)
1841 .OptionalFlagList("-I", "Adds an Android APK to link against",
1842 &options.include_paths)
1843 .OptionalFlagList(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001844 "-R",
1845 "Compilation unit to link, using `overlay` semantics.\n"
1846 "The last conflicting resource given takes precedence.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001847 &overlay_arg_list)
1848 .OptionalFlag("--java", "Directory in which to generate R.java",
1849 &options.generate_java_class_path)
1850 .OptionalFlag("--proguard",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001851 "Output file for generated Proguard rules",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001852 &options.generate_proguard_rules_path)
1853 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001854 "--proguard-main-dex",
1855 "Output file for generated Proguard rules for the main dex",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001856 &options.generate_main_dex_proguard_rules_path)
1857 .OptionalSwitch("--no-auto-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001858 "Disables automatic style and layout SDK versioning",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001859 &options.no_auto_version)
1860 .OptionalSwitch("--no-version-vectors",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001861 "Disables automatic versioning of vector drawables. "
1862 "Use this only\n"
1863 "when building with vector drawable support library",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001864 &options.no_version_vectors)
1865 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001866 "Disables automatic deduping of resources with\n"
1867 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001868 &options.no_resource_deduping)
1869 .OptionalSwitch(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001870 "-x",
1871 "Legacy flag that specifies to use the package identifier 0x01",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001872 &legacy_x_flag)
1873 .OptionalSwitch("-z",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001874 "Require localization of strings marked 'suggested'",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001875 &require_localization)
1876 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001877 "-c",
1878 "Comma separated list of configurations to include. The default\n"
1879 "is all configurations",
1880 &configs)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001881 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001882 "--preferred-density",
1883 "Selects the closest matching density and strips out all others.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001884 &preferred_density)
1885 .OptionalFlag("--product",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001886 "Comma separated list of product names to keep",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001887 &product_list)
1888 .OptionalSwitch("--output-to-dir",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001889 "Outputs the APK contents to a directory specified "
1890 "by -o",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001891 &options.output_to_directory)
1892 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001893 "Removes XML namespace prefix and URI "
1894 "information from AndroidManifest.xml\nand XML "
1895 "binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001896 &options.no_xml_namespaces)
1897 .OptionalFlag("--min-sdk-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001898 "Default minimum SDK version to use for "
1899 "AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001900 &options.manifest_fixer_options.min_sdk_version_default)
1901 .OptionalFlag(
1902 "--target-sdk-version",
1903 "Default target SDK version to use for "
1904 "AndroidManifest.xml",
1905 &options.manifest_fixer_options.target_sdk_version_default)
1906 .OptionalFlag("--version-code",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001907 "Version code (integer) to inject into the "
1908 "AndroidManifest.xml if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001909 &options.manifest_fixer_options.version_code_default)
1910 .OptionalFlag("--version-name",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001911 "Version name to inject into the AndroidManifest.xml "
1912 "if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001913 &options.manifest_fixer_options.version_name_default)
1914 .OptionalSwitch("--static-lib", "Generate a static Android library",
1915 &options.static_lib)
1916 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001917 "Merge all library resources under the app's package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001918 &options.no_static_lib_packages)
1919 .OptionalSwitch("--non-final-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001920 "Generates R.java without the final modifier.\n"
1921 "This is implied when --static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001922 &options.generate_non_final_ids)
1923 .OptionalFlag("--stable-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001924 "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001925 &stable_id_file_path)
1926 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001927 "--emit-ids",
1928 "Emit a file at the given path with a list of name to ID\n"
1929 "mappings, suitable for use with --stable-ids.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001930 &options.resource_id_map_path)
1931 .OptionalFlag("--private-symbols",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001932 "Package name to use when generating R.java for "
1933 "private symbols.\n"
1934 "If not specified, public and private symbols will use "
1935 "the application's "
1936 "package name",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001937 &options.private_symbols)
1938 .OptionalFlag("--custom-package",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001939 "Custom Java package under which to generate R.java",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001940 &options.custom_java_package)
1941 .OptionalFlagList("--extra-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001942 "Generate the same R.java but with different "
1943 "package names",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001944 &extra_java_packages)
1945 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001946 "Adds a JavaDoc annotation to all "
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001947 "generated Java classes",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001948 &options.javadoc_annotations)
1949 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001950 "Allows the addition of new resources in "
1951 "overlays without <add-resource> tags",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001952 &options.auto_add_overlay)
1953 .OptionalFlag("--rename-manifest-package",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001954 "Renames the package in AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001955 &options.manifest_fixer_options.rename_manifest_package)
1956 .OptionalFlag(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001957 "--rename-instrumentation-target-package",
1958 "Changes the name of the target package for instrumentation. "
1959 "Most useful "
1960 "when used\nin conjunction with --rename-manifest-package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001961 &options.manifest_fixer_options
1962 .rename_instrumentation_target_package)
1963 .OptionalFlagList("-0", "File extensions not to compress",
1964 &options.extensions_to_not_compress)
1965 .OptionalFlagList(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001966 "--split",
1967 "Split resources matching a set of configs out to a "
1968 "Split APK.\nSyntax: path/to/output.apk:<config>[,<config>[...]]",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001969 &split_args)
1970 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001971
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001972 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001973 return 1;
1974 }
1975
1976 // Expand all argument-files passed into the command line. These start with
1977 // '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001978 std::vector<std::string> arg_list;
1979 for (const std::string& arg : flags.GetArgs()) {
1980 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001981 const std::string path = arg.substr(1, arg.size() - 1);
1982 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001983 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
1984 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001985 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001986 }
1987 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001988 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001989 }
1990 }
1991
1992 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001993 for (const std::string& arg : overlay_arg_list) {
1994 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001995 const std::string path = arg.substr(1, arg.size() - 1);
1996 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001997 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
1998 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001999 return 1;
2000 }
2001 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002002 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002003 }
2004 }
2005
2006 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002007 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002008 }
2009
2010 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002011 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002012 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002013 for (StringPiece package : util::Split(extra_package, ':')) {
2014 options.extra_java_packages.insert(package.ToString());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002015 }
2016 }
2017
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002018 if (product_list) {
2019 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002020 if (product != "" && product != "default") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002021 options.products.insert(product.ToString());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002022 }
2023 }
2024 }
2025
2026 AxisConfigFilter filter;
2027 if (configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002028 for (const StringPiece& config_str : util::Tokenize(configs.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002029 ConfigDescription config;
2030 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002031 if (lv.InitFromFilterString(config_str)) {
2032 lv.WriteTo(&config);
2033 } else if (!ConfigDescription::Parse(config_str, &config)) {
2034 context.GetDiagnostics()->Error(DiagMessage() << "invalid config '"
2035 << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002036 << "' for -c option");
2037 return 1;
2038 }
2039
2040 if (config.density != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002041 context.GetDiagnostics()->Warn(DiagMessage() << "ignoring density '"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002042 << config
2043 << "' for -c option");
2044 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002045 filter.AddConfig(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002046 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002047 }
2048
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002049 options.table_splitter_options.config_filter = &filter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002050 }
2051
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002052 if (preferred_density) {
2053 ConfigDescription preferred_density_config;
2054 if (!ConfigDescription::Parse(preferred_density.value(),
2055 &preferred_density_config)) {
2056 context.GetDiagnostics()->Error(
2057 DiagMessage() << "invalid density '" << preferred_density.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002058 << "' for --preferred-density option");
2059 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002060 }
2061
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002062 // Clear the version that can be automatically added.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002063 preferred_density_config.sdkVersion = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002064
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002065 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002066 ConfigDescription::CONFIG_DENSITY) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002067 context.GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002068 DiagMessage() << "invalid preferred density '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002069 << preferred_density.value() << "'. "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002070 << "Preferred density must only be a density value");
2071 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002072 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002073 options.table_splitter_options.preferred_density =
2074 preferred_density_config.density;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002075 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002076
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002077 if (!options.static_lib && stable_id_file_path) {
2078 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2079 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002080 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002081 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002082 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002083
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002084 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002085 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002086 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2087 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2088 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2089 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2090
2091 // Parse the split parameters.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002092 for (const std::string& split_arg : split_args) {
2093 options.split_paths.push_back({});
2094 options.split_constraints.push_back({});
2095 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(),
2096 &options.split_paths.back(),
2097 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002098 return 1;
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002099 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002100 }
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002101
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002102 // Turn off auto versioning for static-libs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002103 if (options.static_lib) {
2104 options.no_auto_version = true;
2105 options.no_version_vectors = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002106 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002107
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002108 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002109 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002110}
2111
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002112} // namespace aapt