blob: 49052169b6e6636804846b5fdd93daf155d0b48b [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include <sys/stat.h>
18
19#include <fstream>
20#include <queue>
21#include <unordered_map>
22#include <vector>
23
24#include "android-base/errors.h"
25#include "android-base/file.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "google/protobuf/io/coded_stream.h"
28
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "AppInfo.h"
30#include "Debug.h"
31#include "Flags.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080032#include "Locale.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033#include "NameMangler.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080034#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035#include "compile/IdAssigner.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080036#include "filter/ConfigFilter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037#include "flatten/Archive.h"
38#include "flatten/TableFlattener.h"
39#include "flatten/XmlFlattener.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080040#include "io/FileSystem.h"
41#include "io/ZipArchive.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070042#include "java/JavaClassGenerator.h"
43#include "java/ManifestClassGenerator.h"
44#include "java/ProguardRules.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045#include "link/Linkers.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046#include "link/ManifestFixer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080047#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048#include "link/TableMerger.h"
Adam Lesinskid48944a2017-02-21 14:22:30 -080049#include "optimize/ResourceDeduper.h"
50#include "optimize/VersionCollapser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051#include "process/IResourceTableConsumer.h"
52#include "process/SymbolTable.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080053#include "proto/ProtoSerialize.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080054#include "split/TableSplitter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055#include "unflatten/BinaryResourceParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056#include "util/Files.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080057#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058
Adam Lesinskid5083f62017-01-16 15:07:21 -080059using android::StringPiece;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070061
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062namespace aapt {
63
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080064// The type of package to build.
65enum class PackageType {
66 kApp,
67 kSharedLib,
68 kStaticLib,
69};
70
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071struct LinkOptions {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080072 PackageType package_type = PackageType::kApp;
73
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 std::string output_path;
75 std::string manifest_path;
76 std::vector<std::string> include_paths;
77 std::vector<std::string> overlay_files;
78 bool output_to_directory = false;
79 bool auto_add_overlay = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070080
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 // Java/Proguard options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 Maybe<std::string> generate_java_class_path;
83 Maybe<std::string> custom_java_package;
84 std::set<std::string> extra_java_packages;
85 Maybe<std::string> generate_proguard_rules_path;
86 Maybe<std::string> generate_main_dex_proguard_rules_path;
87 bool generate_non_final_ids = false;
88 std::vector<std::string> javadoc_annotations;
89 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070090
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 // Optimizations/features.
92 bool no_auto_version = false;
93 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +090094 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 bool no_resource_deduping = false;
96 bool no_xml_namespaces = false;
97 bool do_not_compress_anything = false;
98 std::unordered_set<std::string> extensions_to_not_compress;
99
100 // Static lib options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 bool no_static_lib_packages = false;
102
103 // AndroidManifest.xml massaging options.
104 ManifestFixerOptions manifest_fixer_options;
105
106 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700108
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800109 // Flattening options.
110 TableFlattenerOptions table_flattener_options;
111
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 TableSplitterOptions table_splitter_options;
114 std::vector<SplitConstraints> split_constraints;
115 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 std::unordered_map<ResourceName, ResourceId> stable_id_map;
119 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120};
121
Adam Lesinski64587af2016-02-18 18:33:06 -0800122class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 public:
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800124 LinkContext() : name_mangler_({}), symbols_(&name_mangler_) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 NameMangler* GetNameMangler() override { return &name_mangler_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
131 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800133
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 const std::string& GetCompilationPackage() override {
135 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800139 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 uint8_t GetPackageId() override { return package_id_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 void SetPackageId(uint8_t id) { package_id_ = id; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 SymbolTable* GetExternalSymbols() override { return &symbols_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 bool IsVerbose() override { return verbose_; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 int GetMinSdkVersion() override { return min_sdk_version_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 void SetMinSdkVersion(int minSdk) { min_sdk_version_ = minSdk; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700155
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 DISALLOW_COPY_AND_ASSIGN(LinkContext);
158
159 StdErrDiagnostics diagnostics_;
160 NameMangler name_mangler_;
161 std::string compilation_package_;
162 uint8_t package_id_ = 0x0;
163 SymbolTable symbols_;
164 bool verbose_ = false;
165 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166};
167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168static bool CopyFileToArchive(io::IFile* file, const std::string& out_path,
169 uint32_t compression_flags,
170 IArchiveWriter* writer, IAaptContext* context) {
171 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 context->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 << "failed to open file");
Adam Lesinski355f2852016-02-13 20:26:45 -0800175 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 }
177
178 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data->data());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 const size_t buffer_size = data->size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 if (context->IsVerbose()) {
182 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 << " to archive");
184 }
185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 if (writer->StartEntry(out_path, compression_flags)) {
187 if (writer->WriteEntry(buffer, buffer_size)) {
188 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 return true;
190 }
191 }
192 }
193
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 context->GetDiagnostics()->Error(DiagMessage() << "failed to write file "
195 << out_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800197}
198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
200 Maybe<size_t> max_sdk_level, bool keep_raw_values,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 IArchiveWriter* writer, IAaptContext* context) {
202 BigBuffer buffer(1024);
203 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 options.keep_raw_values = keep_raw_values;
205 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800208 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 }
210
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 DiagMessage msg;
213 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 if (max_sdk_level) {
215 msg << " maxSdkLevel=" << max_sdk_level.value()
216 << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 }
220
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 if (writer->StartEntry(path, ArchiveEntry::kCompress)) {
222 if (writer->WriteEntry(buffer)) {
223 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 return true;
225 }
226 }
227 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 << " to archive");
230 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800231}
232
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 const void* data,
235 size_t len,
Adam Lesinski355f2852016-02-13 20:26:45 -0800236 IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 pb::ResourceTable pb_table;
238 if (!pb_table.ParseFromArray(data, len)) {
239 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 return {};
241 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800242
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 if (!table) {
246 return {};
247 }
248 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800249}
250
251/**
252 * Inflates an XML file from the source path.
253 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 IDiagnostics* diag) {
256 std::ifstream fin(path, std::ifstream::binary);
257 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 return {};
260 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800262}
263
Adam Lesinski355f2852016-02-13 20:26:45 -0800264struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 bool no_auto_version = false;
266 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900267 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 bool no_xml_namespaces = false;
269 bool keep_raw_values = false;
270 bool do_not_compress_anything = false;
271 bool update_proguard_spec = false;
272 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800273};
274
275class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 public:
277 ResourceFileFlattener(const ResourceFileFlattenerOptions& options,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 IAaptContext* context, proguard::KeepSet* keep_set)
279 : options_(options), context_(context), keep_set_(keep_set) {}
Adam Lesinski355f2852016-02-13 20:26:45 -0800280
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800282
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 private:
284 struct FileOperation {
285 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700286
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 // The entry this file came from.
288 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700289
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700292
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700295
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 std::string dst_path;
298 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800300
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800302
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
304 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800305
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306 ResourceFileFlattenerOptions options_;
307 IAaptContext* context_;
308 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800309};
310
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
312 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 return 0;
314 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800315
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 for (const std::string& extension : options_.extensions_to_not_compress) {
317 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800319 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 }
321 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800322}
323
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900324static bool IsTransitionElement(const std::string& name) {
325 return
326 name == "fade" ||
327 name == "changeBounds" ||
328 name == "slide" ||
329 name == "explode" ||
330 name == "changeImageTransform" ||
331 name == "changeTransform" ||
332 name == "changeClipBounds" ||
333 name == "autoTransition" ||
334 name == "recolor" ||
335 name == "changeScroll" ||
336 name == "transitionSet" ||
337 name == "transition" ||
338 name == "transitionManager";
339}
340
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341bool ResourceFileFlattener::LinkAndVersionXmlFile(
342 ResourceTable* table, FileOperation* file_op,
343 std::queue<FileOperation>* out_file_op_queue) {
344 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700346
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700347 if (context_->IsVerbose()) {
348 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 }
350
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 XmlReferenceLinker xml_linker;
352 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 return false;
354 }
355
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 if (options_.update_proguard_spec &&
357 !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 return false;
359 }
360
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 if (options_.no_xml_namespaces) {
362 XmlNamespaceRemover namespace_remover;
363 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800365 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800367
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 if (!options_.no_auto_version) {
369 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 xml::Element* el = xml::FindRootElement(doc);
372 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 if (el->name == "vector" || el->name == "animated-vector") {
374 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700377 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700379 }
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900380 if (options_.no_version_transitions) {
381 // Skip this if it is a transition resource.
382 xml::Element* el = xml::FindRootElement(doc);
383 if (el && el->namespace_uri.empty()) {
384 if (IsTransitionElement(el->name)) {
385 // We are NOT going to version this file.
386 file_op->skip_version = true;
387 return true;
388 }
389 }
390 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700391
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393
394 // Find the first SDK level used that is higher than this defined config and
395 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 const int min_sdk_version = context_->GetMinSdkVersion();
397 for (int sdk_level : xml_linker.sdk_levels()) {
398 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
399 if (!ShouldGenerateVersionedResource(file_op->entry, config,
400 sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 // If we shouldn't generate a versioned resource, stop checking.
402 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800403 }
404
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 ResourceFile versioned_file_desc = doc->file;
406 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700407
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 FileOperation new_file_op;
409 new_file_op.xml_to_flatten = util::make_unique<xml::XmlResource>(
410 versioned_file_desc, doc->root->Clone());
411 new_file_op.config = versioned_file_desc.config;
412 new_file_op.entry = file_op->entry;
413 new_file_op.dst_path = ResourceUtils::BuildResourceFileName(
414 versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800415
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416 if (context_->IsVerbose()) {
417 context_->GetDiagnostics()->Note(
418 DiagMessage(versioned_file_desc.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 << "auto-versioning resource from config '" << config << "' -> '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420 << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800421 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 bool added = table->AddFileReferenceAllowMangled(
424 versioned_file_desc.name, versioned_file_desc.config,
425 versioned_file_desc.source, new_file_op.dst_path, nullptr,
426 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 if (!added) {
428 return false;
429 }
430
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 break;
433 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800434 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 }
436 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800437}
438
439/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 * Do not insert or remove any resources while executing in this function. It
441 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800442 * corrupt the iteration order.
443 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444bool ResourceFileFlattener::Flatten(ResourceTable* table,
445 IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 bool error = false;
447 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800449
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 for (auto& pkg : table->packages) {
451 for (auto& type : pkg->types) {
452 // Sort by config and name, so that we get better locality in the zip
453 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 config_sorted_files.clear();
455 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800456
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 // Populate the queue with all files in the ResourceTable.
458 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 for (auto& config_value : entry->values) {
460 FileReference* file_ref =
461 ValueCast<FileReference>(config_value->value.get());
462 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700463 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700465
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700466 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700470 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 }
472
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700473 FileOperation file_op;
474 file_op.entry = entry.get();
475 file_op.dst_path = *file_ref->path;
476 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 if (type->type != ResourceType::kRaw &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 (util::EndsWith(src_path, ".xml.flat") ||
481 util::EndsWith(src_path, ".xml"))) {
482 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 << "failed to open file");
486 return false;
487 }
488
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 file_op.xml_to_flatten =
490 xml::Inflate(data->data(), data->size(),
491 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 return false;
495 }
496
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497 file_op.xml_to_flatten->file.config = config_value->config;
498 file_op.xml_to_flatten->file.source = file_ref->GetSource();
499 file_op.xml_to_flatten->file.name =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 ResourceName(pkg->name, type->type, entry->name);
501
502 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700503 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506
507 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700508 // else we end up copying the string in the std::make_pair() method,
509 // then creating a StringPiece from the copy, which would cause us
510 // to end up referencing garbage in the map.
511 const StringPiece entry_name(entry->name);
512 config_sorted_files[std::make_pair(
513 config_value->config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 }
515 }
516 }
517
518 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 for (; !file_operations.empty(); file_operations.pop()) {
520 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 error = true;
524 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700525 }
526
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
528 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529 // creating a StringPiece from the copy, which would cause us to end up
530 // referencing garbage in the map.
531 const StringPiece entry_name(file_op.entry->name);
532 config_sorted_files[std::make_pair(file_op.config, entry_name)] =
533 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700534 }
535
536 if (error) {
537 return false;
538 }
539
540 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 for (auto& map_entry : config_sorted_files) {
542 const ConfigDescription& config = map_entry.first.first;
543 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700545 if (file_op.xml_to_flatten) {
546 Maybe<size_t> max_sdk_level;
547 if (!options_.no_auto_version && !file_op.skip_version) {
548 max_sdk_level =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700550 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 }
552
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700553 bool result = FlattenXml(
554 file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
555 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 if (!result) {
557 error = true;
558 }
559 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 bool result = CopyFileToArchive(
561 file_op.file_to_copy, file_op.dst_path,
562 GetCompressionFlags(file_op.dst_path), archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 if (!result) {
564 error = true;
565 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700566 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700568 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 }
570 return !error;
571}
572
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700573static bool WriteStableIdMapToPath(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 IDiagnostics* diag,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 const std::unordered_map<ResourceName, ResourceId>& id_map,
576 const std::string& id_map_path) {
577 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 return false;
581 }
582
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584 const ResourceName& name = entry.first;
585 const ResourceId& id = entry.second;
586 fout << name << " = " << id << "\n";
587 }
588
589 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700590 diag->Error(DiagMessage(id_map_path)
591 << "failed writing to file: "
592 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 return false;
594 }
595
596 return true;
597}
598
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599static bool LoadStableIdMap(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 IDiagnostics* diag, const std::string& path,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 std::string content;
603 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700604 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 return false;
606 }
607
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700608 out_id_map->clear();
609 size_t line_no = 0;
610 for (StringPiece line : util::Tokenize(content, '\n')) {
611 line_no++;
612 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 if (line.empty()) {
614 continue;
615 }
616
617 auto iter = std::find(line.begin(), line.end(), '=');
618 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 return false;
621 }
622
623 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 StringPiece res_name_str =
625 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
626 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
627 diag->Error(DiagMessage(Source(path, line_no))
628 << "invalid resource name '" << res_name_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 return false;
630 }
631
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
633 const size_t res_id_str_len = line.size() - res_id_start_idx;
634 StringPiece res_id_str =
635 util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
638 if (!maybe_id) {
639 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '"
640 << res_id_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 return false;
642 }
643
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700644 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 }
646 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700647}
648
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700649static bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag,
650 std::string* out_path,
651 SplitConstraints* out_split) {
652 std::vector<std::string> parts = util::Split(arg, ':');
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 if (parts.size() != 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700654 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
655 diag->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 DiagMessage()
657 << "should be --split path/to/output.apk:<config>[,<config>...]");
658 return false;
659 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 *out_path = parts[0];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 std::vector<ConfigDescription> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700662 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 configs.push_back({});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 if (!ConfigDescription::Parse(config_str, &configs.back())) {
665 diag->Error(DiagMessage() << "invalid config '" << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 << "' in split parameter '" << arg << "'");
667 return false;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700668 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700670 out_split->configs.insert(configs.begin(), configs.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 return true;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700672}
673
Adam Lesinskifb48d292015-11-07 15:52:13 -0800674class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 public:
676 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700677 : options_(options),
678 context_(context),
679 final_table_(),
680 file_collection_(util::make_unique<io::FileCollection>()) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700681
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 /**
683 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700684 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700686 bool LoadSymbolsFromIncludePaths() {
687 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 for (const std::string& path : options_.include_paths) {
690 if (context_->IsVerbose()) {
691 context_->GetDiagnostics()->Note(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700692 << "loading include path");
693 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700694
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700696 std::string error_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800697 std::unique_ptr<ResourceTable> include_static = LoadStaticLibrary(path, &error_str);
698 if (include_static) {
699 if (options_.package_type != PackageType::kStaticLib) {
700 // Can't include static libraries when not building a static library (they have no IDs
701 // assigned).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700702 context_->GetDiagnostics()->Error(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800703 DiagMessage(path) << "can't include static library when not building a static lib");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700704 return false;
705 }
706
707 // If we are using --no-static-lib-packages, we need to rename the
708 // package of this
709 // table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700710 if (options_.no_static_lib_packages) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800711 if (ResourceTablePackage* pkg = include_static->FindPackageById(0x7f)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 pkg->name = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700713 }
714 }
715
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 context_->GetExternalSymbols()->AppendSource(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800717 util::make_unique<ResourceTableSymbolSource>(include_static.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800719 static_table_includes_.push_back(std::move(include_static));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700721 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 return false;
725 }
726
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700727 if (!asset_source->AddAssetPath(path)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800728 context_->GetDiagnostics()->Error(DiagMessage(path) << "failed to load include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729 return false;
730 }
731 }
732
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800733 // Capture the shared libraries so that the final resource table can be properly flattened
734 // with support for shared libraries.
735 for (auto& entry : asset_source->GetAssignedPackageIds()) {
736 if (entry.first > 0x01 && entry.first < 0x7f) {
737 final_table_.included_packages_[entry.first] = entry.second;
738 }
739 }
740
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700741 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 return true;
743 }
744
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700745 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700746 IDiagnostics* diag) {
747 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 if (xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get())) {
749 AppInfo app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700751 if (!manifest_el->namespace_uri.empty() ||
752 manifest_el->name != "manifest") {
753 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 << "root tag must be <manifest>");
755 return {};
756 }
757
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700758 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
759 if (!package_attr) {
760 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 << "<manifest> must have a 'package' attribute");
762 return {};
763 }
764
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700765 app_info.package = package_attr->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700767 if (xml::Attribute* version_code_attr =
768 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
769 Maybe<uint32_t> maybe_code =
770 ResourceUtils::ParseInt(version_code_attr->value);
771 if (!maybe_code) {
772 diag->Error(DiagMessage(xml_res->file.source.WithLine(
773 manifest_el->line_number))
774 << "invalid android:versionCode '"
775 << version_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700776 return {};
777 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700778 app_info.version_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700779 }
780
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700781 if (xml::Attribute* revision_code_attr =
782 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
783 Maybe<uint32_t> maybe_code =
784 ResourceUtils::ParseInt(revision_code_attr->value);
785 if (!maybe_code) {
786 diag->Error(DiagMessage(xml_res->file.source.WithLine(
787 manifest_el->line_number))
788 << "invalid android:revisionCode '"
789 << revision_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700790 return {};
791 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700792 app_info.revision_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700793 }
794
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700795 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
796 if (xml::Attribute* min_sdk = uses_sdk_el->FindAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700797 xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700798 app_info.min_sdk_version = min_sdk->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700799 }
800 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700801 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700802 }
803 return {};
804 }
805
806 /**
807 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it
808 * linked.
809 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700810 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700812 bool VerifyNoExternalPackages() {
813 auto is_ext_package_func =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700814 [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700815 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
816 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817 };
818
819 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700820 for (const auto& package : final_table_.packages) {
821 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 // We have a package that is not related to the one we're building!
823 for (const auto& type : package->types) {
824 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700825 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700826
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700827 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700828 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700829 // for the 'android' package. This is due to legacy reasons.
830 if (ValueCast<Id>(config_value->value.get()) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700831 package->name == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700832 context_->GetDiagnostics()->Warn(
833 DiagMessage(config_value->value->GetSource())
834 << "generated id '" << res_name
835 << "' for external package '" << package->name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700836 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700837 context_->GetDiagnostics()->Error(
838 DiagMessage(config_value->value->GetSource())
839 << "defined resource '" << res_name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840 << "' for external package '" << package->name << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700841 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700842 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700843 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 }
845 }
846 }
847 }
848
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700849 auto new_end_iter =
850 std::remove_if(final_table_.packages.begin(),
851 final_table_.packages.end(), is_ext_package_func);
852 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700853 return !error;
854 }
855
856 /**
857 * Returns true if no IDs have been set, false otherwise.
858 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700859 bool VerifyNoIdsSet() {
860 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 for (const auto& type : package->types) {
862 if (type->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700863 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700864 DiagMessage() << "type " << type->type << " has ID " << std::hex
865 << (int)type->id.value() << std::dec
866 << " assigned");
867 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700868 }
869
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700870 for (const auto& entry : type->entries) {
871 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700872 ResourceNameRef res_name(package->name, type->type, entry->name);
873 context_->GetDiagnostics()->Error(
874 DiagMessage() << "entry " << res_name << " has ID " << std::hex
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875 << (int)entry->id.value() << std::dec
876 << " assigned");
877 return false;
878 }
879 }
880 }
881 }
882 return true;
883 }
884
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700885 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
886 if (options_.output_to_directory) {
887 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700889 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700890 }
891 }
892
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700893 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700894 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800895 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700896 if (!flattener.Consume(context_, table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700897 return false;
898 }
899
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 if (writer->StartEntry("resources.arsc", ArchiveEntry::kAlign)) {
901 if (writer->WriteEntry(buffer)) {
902 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700903 return true;
904 }
905 }
906 }
907
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700908 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700909 DiagMessage() << "failed to write resources.arsc to archive");
910 return false;
911 }
912
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700913 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700914 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700915 if (!writer->StartEntry("resources.arsc.flat", 0)) {
916 context_->GetDiagnostics()->Error(DiagMessage() << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700917 return false;
918 }
919
920 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700921 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700922 {
923 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700924 // ZeroCopyOutputStream interface.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700925 CopyingOutputStreamAdaptor adaptor(writer);
926
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700927 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
928 if (!pb_table->SerializeToZeroCopyStream(&adaptor)) {
929 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700930 return false;
931 }
932 }
933
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700934 if (!writer->FinishEntry()) {
935 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700936 << "failed to finish entry");
937 return false;
938 }
939 return true;
940 }
941
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700942 bool WriteJavaFile(ResourceTable* table,
943 const StringPiece& package_name_to_generate,
944 const StringPiece& out_package,
945 const JavaClassGeneratorOptions& java_options) {
946 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700947 return true;
948 }
949
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700950 std::string out_path = options_.generate_java_class_path.value();
951 file::AppendPath(&out_path, file::PackageToPath(out_package));
952 if (!file::mkdirs(out_path)) {
953 context_->GetDiagnostics()->Error(
954 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700955 return false;
956 }
957
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700958 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700959
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700960 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700961 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700962 context_->GetDiagnostics()->Error(
963 DiagMessage() << "failed writing to '" << out_path << "': "
964 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700965 return false;
966 }
967
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700968 JavaClassGenerator generator(context_, table, java_options);
969 if (!generator.Generate(package_name_to_generate, out_package, &fout)) {
970 context_->GetDiagnostics()->Error(DiagMessage(out_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700971 << generator.getError());
972 return false;
973 }
974
975 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700976 context_->GetDiagnostics()->Error(
977 DiagMessage() << "failed writing to '" << out_path << "': "
978 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700979 }
980 return true;
981 }
982
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700983 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
984 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 return true;
986 }
987
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700988 std::unique_ptr<ClassDefinition> manifest_class =
989 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700990
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700991 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700992 // Something bad happened, but we already logged it, so exit.
993 return false;
994 }
995
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700996 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700997 // Empty Manifest class, no need to generate it.
998 return true;
999 }
1000
1001 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001002 for (const std::string& annotation : options_.javadoc_annotations) {
1003 std::string proper_annotation = "@";
1004 proper_annotation += annotation;
1005 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001006 }
1007
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001008 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001009
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001010 std::string out_path = options_.generate_java_class_path.value();
1011 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001013 if (!file::mkdirs(out_path)) {
1014 context_->GetDiagnostics()->Error(
1015 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001016 return false;
1017 }
1018
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001019 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001020
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001021 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001022 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001023 context_->GetDiagnostics()->Error(
1024 DiagMessage() << "failed writing to '" << out_path << "': "
1025 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001026 return false;
1027 }
1028
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001029 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8,
1030 true, &fout)) {
1031 context_->GetDiagnostics()->Error(
1032 DiagMessage() << "failed writing to '" << out_path << "': "
1033 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001034 return false;
1035 }
1036 return true;
1037 }
1038
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001039 bool WriteProguardFile(const Maybe<std::string>& out,
1040 const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001041 if (!out) {
1042 return true;
1043 }
1044
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001045 const std::string& out_path = out.value();
1046 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001047 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001048 context_->GetDiagnostics()->Error(
1049 DiagMessage() << "failed to open '" << out_path << "': "
1050 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001051 return false;
1052 }
1053
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001054 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001055 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001056 context_->GetDiagnostics()->Error(
1057 DiagMessage() << "failed writing to '" << out_path << "': "
1058 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001059 return false;
1060 }
1061 return true;
1062 }
1063
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001064 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1065 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001066 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001067 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001068 if (!collection) {
1069 return {};
1070 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001071 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001072 }
1073
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001074 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075 io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001076 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001077 if (!file) {
1078 return {};
1079 }
1080
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001081 std::unique_ptr<io::IData> data = file->OpenAsData();
1082 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1083 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001084 }
1085
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001086 bool MergeStaticLibrary(const std::string& input, bool override) {
1087 if (context_->IsVerbose()) {
1088 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001089 << "merging static library " << input);
1090 }
1091
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001092 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001093 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001094 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001095 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001096 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001097 return false;
1098 }
1099
1100 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001101 LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001102 if (!table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001103 context_->GetDiagnostics()->Error(DiagMessage(input)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001104 << "invalid static library");
1105 return false;
1106 }
1107
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001108 ResourceTablePackage* pkg = table->FindPackageById(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 if (!pkg) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001110 context_->GetDiagnostics()->Error(DiagMessage(input)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001111 << "static library has no package");
1112 return false;
1113 }
1114
1115 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001116 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001117 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001118 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001119
1120 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001121 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001122 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001123 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001124 }
1125
1126 pkg->name = "";
1127 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001128 result = table_merger_->MergeOverlay(Source(input), table.get(),
1129 collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001130 } else {
1131 result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001132 table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001133 }
1134
1135 } else {
1136 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001137 // preserved and resource names are mangled.
1138 result = table_merger_->MergeAndMangle(Source(input), pkg->name,
1139 table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001140 }
1141
1142 if (!result) {
1143 return false;
1144 }
1145
1146 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001147 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001148 return true;
1149 }
1150
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001151 bool MergeResourceTable(io::IFile* file, bool override) {
1152 if (context_->IsVerbose()) {
1153 context_->GetDiagnostics()->Note(
1154 DiagMessage() << "merging resource table " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001155 }
1156
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001157 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001158 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001159 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001160 << "failed to open file");
1161 return false;
1162 }
1163
1164 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001165 LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1166 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001167 if (!table) {
1168 return false;
1169 }
1170
1171 bool result = false;
1172 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001173 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001174 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001175 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001176 }
1177 return result;
1178 }
1179
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001180 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001181 bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001182 if (context_->IsVerbose()) {
1183 context_->GetDiagnostics()->Note(
1184 DiagMessage() << "merging '" << file_desc->name
1185 << "' from compiled file " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001186 }
1187
1188 bool result = false;
1189 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001190 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001191 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001192 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001193 }
1194
1195 if (!result) {
1196 return false;
1197 }
1198
1199 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001200 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1201 if (exported_symbol.name.package.empty()) {
1202 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001203 }
1204
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001205 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001206
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001207 Maybe<ResourceName> mangled_name =
1208 context_->GetNameMangler()->MangleName(exported_symbol.name);
1209 if (mangled_name) {
1210 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001211 }
1212
1213 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001214 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1215 bool result = final_table_.AddResourceAllowMangled(
1216 res_name, ConfigDescription::DefaultConfig(), std::string(),
1217 std::move(id), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001218 if (!result) {
1219 return false;
1220 }
1221 }
1222 return true;
1223 }
1224
1225 /**
1226 * Takes a path to load as a ZIP file and merges the files within into the
1227 * master ResourceTable.
1228 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001229 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001230 *
1231 * An io::IFileCollection is created from the ZIP file and added to the set of
1232 * io::IFileCollections that are open.
1233 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001234 bool MergeArchive(const std::string& input, bool override) {
1235 if (context_->IsVerbose()) {
1236 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001237 << input);
1238 }
1239
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001240 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001241 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001242 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001243 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001244 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001245 return false;
1246 }
1247
1248 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001249 for (auto iter = collection->Iterator(); iter->HasNext();) {
1250 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001251 error = true;
1252 }
1253 }
1254
1255 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001256 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001257 return !error;
1258 }
1259
1260 /**
1261 * Takes a path to load and merge into the master ResourceTable. If override
1262 * is true,
1263 * conflicting resources are allowed to override each other, in order of last
1264 * seen.
1265 *
1266 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1267 * as ZIP archive
1268 * and the files within are merged individually.
1269 *
1270 * Otherwise the files is processed on its own.
1271 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001272 bool MergePath(const std::string& path, bool override) {
1273 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1274 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1275 return MergeArchive(path, override);
1276 } else if (util::EndsWith(path, ".apk")) {
1277 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001278 }
1279
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001280 io::IFile* file = file_collection_->InsertFile(path);
1281 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001282 }
1283
1284 /**
1285 * Takes a file to load and merge into the master ResourceTable. If override
1286 * is true,
1287 * conflicting resources are allowed to override each other, in order of last
1288 * seen.
1289 *
1290 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1291 * merged into the
1292 * master ResourceTable. If the file ends with .flat, then it is treated like
1293 * a compiled file
1294 * and the header data is read and merged into the final ResourceTable.
1295 *
1296 * All other file types are ignored. This is because these files could be
1297 * coming from a zip,
1298 * where we could have other files like classes.dex.
1299 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001300 bool MergeFile(io::IFile* file, bool override) {
1301 const Source& src = file->GetSource();
1302 if (util::EndsWith(src.path, ".arsc.flat")) {
1303 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001304
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001305 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001307 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001308 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001309 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001310 return false;
1311 }
1312
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001313 CompiledFileInputStream input_stream(data->data(), data->size());
1314 uint32_t num_files = 0;
1315 if (!input_stream.ReadLittleEndian32(&num_files)) {
1316 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001317 << "failed read num files");
1318 return false;
1319 }
1320
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001321 for (uint32_t i = 0; i < num_files; i++) {
1322 pb::CompiledFile compiled_file;
1323 if (!input_stream.ReadCompiledFile(&compiled_file)) {
1324 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001325 DiagMessage(src) << "failed to read compiled file header");
1326 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001327 }
1328
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001329 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001330 if (!input_stream.ReadDataMetaData(&offset, &len)) {
1331 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001332 << "failed to read data meta data");
1333 return false;
1334 }
1335
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001336 std::unique_ptr<ResourceFile> resource_file =
1337 DeserializeCompiledFileFromPb(compiled_file, file->GetSource(),
1338 context_->GetDiagnostics());
1339 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001340 return false;
1341 }
1342
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001343 if (!MergeCompiledFile(file->CreateFileSegment(offset, len),
1344 resource_file.get(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001345 return false;
1346 }
1347 }
1348 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001349 } else if (util::EndsWith(src.path, ".xml") ||
1350 util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001351 // Since AAPT compiles these file types and appends .flat to them, seeing
1352 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001353 const StringPiece file_type =
1354 util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1355 context_->GetDiagnostics()->Error(DiagMessage(src)
1356 << "uncompiled " << file_type
Adam Lesinski6a396c12016-10-20 14:38:23 -07001357 << " file passed as argument. Must be "
1358 "compiled first into .flat file.");
1359 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001360 }
1361
1362 // Ignore non .flat files. This could be classes.dex or something else that
1363 // happens
1364 // to be in an archive.
1365 return true;
1366 }
1367
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001368 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(
1369 const AppInfo& app_info, const SplitConstraints& constraints) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001370 std::unique_ptr<xml::XmlResource> doc =
1371 util::make_unique<xml::XmlResource>();
1372
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001373 std::unique_ptr<xml::Namespace> namespace_android =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001374 util::make_unique<xml::Namespace>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001375 namespace_android->namespace_uri = xml::kSchemaAndroid;
1376 namespace_android->namespace_prefix = "android";
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001377
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001378 std::unique_ptr<xml::Element> manifest_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001379 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001380 manifest_el->name = "manifest";
1381 manifest_el->attributes.push_back(
1382 xml::Attribute{"", "package", app_info.package});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001383
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001384 if (app_info.version_code) {
1385 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001386 xml::Attribute{xml::kSchemaAndroid, "versionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001387 std::to_string(app_info.version_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001388 }
1389
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001390 if (app_info.revision_code) {
1391 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001392 xml::Attribute{xml::kSchemaAndroid, "revisionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001393 std::to_string(app_info.revision_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001394 }
1395
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001396 std::stringstream split_name;
1397 split_name << "config." << util::Joiner(constraints.configs, "_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001398
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001399 manifest_el->attributes.push_back(
1400 xml::Attribute{"", "split", split_name.str()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001401
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001402 std::unique_ptr<xml::Element> application_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001404 application_el->name = "application";
1405 application_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001406 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false"});
1407
Adam Lesinskie343eb12016-10-27 16:31:58 -07001408 manifest_el->AppendChild(std::move(application_el));
1409 namespace_android->AppendChild(std::move(manifest_el));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001410 doc->root = std::move(namespace_android);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001411 return doc;
1412 }
1413
1414 /**
1415 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001416 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001417 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001418 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001419 xml::XmlResource* manifest, ResourceTable* table) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001420 const bool keep_raw_values = options_.package_type == PackageType::kStaticLib;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001421 bool result = FlattenXml(manifest, "AndroidManifest.xml", {},
1422 keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001423 if (!result) {
1424 return false;
1425 }
1426
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001427 ResourceFileFlattenerOptions file_flattener_options;
1428 file_flattener_options.keep_raw_values = keep_raw_values;
1429 file_flattener_options.do_not_compress_anything =
1430 options_.do_not_compress_anything;
1431 file_flattener_options.extensions_to_not_compress =
1432 options_.extensions_to_not_compress;
1433 file_flattener_options.no_auto_version = options_.no_auto_version;
1434 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001435 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001436 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1437 file_flattener_options.update_proguard_spec =
1438 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001440 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001441
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001442 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001443 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001444 return false;
1445 }
1446
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001447 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001448 if (!FlattenTableToPb(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001449 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001450 return false;
1451 }
1452 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001453 if (!FlattenTable(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001454 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001455 return false;
1456 }
1457 }
1458 return true;
1459 }
1460
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001461 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001462 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001463 std::unique_ptr<xml::XmlResource> manifest_xml =
1464 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1465 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001466 return 1;
1467 }
1468
1469 // First extract the Package name without modifying it (via
1470 // --rename-manifest-package).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001471 if (Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1472 manifest_xml.get(), context_->GetDiagnostics())) {
1473 const AppInfo& app_info = maybe_app_info.value();
1474 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001475 }
1476
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001477 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1478 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001479 return 1;
1480 }
1481
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001482 Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1483 manifest_xml.get(), context_->GetDiagnostics());
1484 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001485 return 1;
1486 }
1487
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001488 const AppInfo& app_info = maybe_app_info.value();
1489 if (app_info.min_sdk_version) {
1490 if (Maybe<int> maybe_min_sdk_version = ResourceUtils::ParseSdkVersion(
1491 app_info.min_sdk_version.value())) {
1492 context_->SetMinSdkVersion(maybe_min_sdk_version.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001493 }
1494 }
1495
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001496 context_->SetNameManglerPolicy(
1497 NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001498 if (options_.package_type == PackageType::kSharedLib) {
1499 context_->SetPackageId(0x00);
1500 } else if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001501 context_->SetPackageId(0x01);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001502 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001503 context_->SetPackageId(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001504 }
1505
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001506 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001507 return 1;
1508 }
1509
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001510 TableMergerOptions table_merger_options;
1511 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
1512 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_,
1513 table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001514
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001515 if (context_->IsVerbose()) {
1516 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001517 << "linking package '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001518 << context_->GetCompilationPackage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001519 << "' with package ID " << std::hex
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001520 << (int)context_->GetPackageId());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001521 }
1522
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001523 for (const std::string& input : input_files) {
1524 if (!MergePath(input, false)) {
1525 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001526 << "failed parsing input");
1527 return 1;
1528 }
1529 }
1530
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001531 for (const std::string& input : options_.overlay_files) {
1532 if (!MergePath(input, true)) {
1533 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001534 << "failed parsing overlays");
1535 return 1;
1536 }
1537 }
1538
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001539 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001540 return 1;
1541 }
1542
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001543 if (options_.package_type != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001544 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001545 if (!mover.Consume(context_, &final_table_)) {
1546 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001547 DiagMessage() << "failed moving private attributes");
1548 return 1;
1549 }
1550
1551 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001552 IdAssigner id_assigner(&options_.stable_id_map);
1553 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001554 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001555 return 1;
1556 }
1557
1558 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001559 if (options_.resource_id_map_path) {
1560 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001561 for (auto& type : package->types) {
1562 for (auto& entry : type->entries) {
1563 ResourceName name(package->name, type->type, entry->name);
1564 // The IDs are guaranteed to exist.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001565 options_.stable_id_map[std::move(name)] = ResourceId(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001566 package->id.value(), type->id.value(), entry->id.value());
1567 }
1568 }
1569 }
1570
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001571 if (!WriteStableIdMapToPath(context_->GetDiagnostics(),
1572 options_.stable_id_map,
1573 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001574 return 1;
1575 }
1576 }
1577 } else {
1578 // Static libs are merged with other apps, and ID collisions are bad, so
1579 // verify that
1580 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001581 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001582 return 1;
1583 }
1584 }
1585
1586 // Add the names to mangle based on our source merge earlier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001587 context_->SetNameManglerPolicy(NameManglerPolicy{
1588 context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001589
1590 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001591 context_->GetExternalSymbols()->PrependSource(
1592 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001593
1594 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001595 if (!linker.Consume(context_, &final_table_)) {
1596 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001597 << "failed linking references");
1598 return 1;
1599 }
1600
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001601 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001602 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001603 context_->GetDiagnostics()->Warn(DiagMessage()
1604 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001605 }
1606 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001607 ProductFilter product_filter(options_.products);
1608 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001609 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001610 return 1;
1611 }
1612 }
1613
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001614 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001615 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001616 if (!versioner.Consume(context_, &final_table_)) {
1617 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001618 << "failed versioning styles");
1619 return 1;
1620 }
1621 }
1622
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001623 if (options_.package_type != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001624 if (context_->IsVerbose()) {
1625 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001626 DiagMessage() << "collapsing resource versions for minimum SDK "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001627 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001628 }
1629
1630 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001631 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001632 return 1;
1633 }
1634 }
1635
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001636 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001637 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001638 if (!deduper.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001639 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001640 return 1;
1641 }
1642 }
1643
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001644 proguard::KeepSet proguard_keep_set;
1645 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001646
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001647 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001648 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001649 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001650 context_->GetDiagnostics()->Warn(DiagMessage()
1651 << "can't strip resources when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001652 }
1653 } else {
1654 // Adjust the SplitConstraints so that their SDK version is stripped if it
1655 // is less
1656 // than or equal to the minSdk. Otherwise the resources that have had
1657 // their SDK version
1658 // stripped due to minSdk won't ever match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001659 std::vector<SplitConstraints> adjusted_constraints_list;
1660 adjusted_constraints_list.reserve(options_.split_constraints.size());
1661 for (const SplitConstraints& constraints : options_.split_constraints) {
1662 SplitConstraints adjusted_constraints;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001663 for (const ConfigDescription& config : constraints.configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001664 if (config.sdkVersion <= context_->GetMinSdkVersion()) {
1665 adjusted_constraints.configs.insert(config.CopyWithoutSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001666 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001667 adjusted_constraints.configs.insert(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001668 }
1669 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001670 adjusted_constraints_list.push_back(std::move(adjusted_constraints));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001671 }
1672
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001673 TableSplitter table_splitter(adjusted_constraints_list,
1674 options_.table_splitter_options);
1675 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001676 return 1;
1677 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001678 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001679
1680 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001681 auto path_iter = options_.split_paths.begin();
1682 auto split_constraints_iter = adjusted_constraints_list.begin();
1683 for (std::unique_ptr<ResourceTable>& split_table :
1684 table_splitter.splits()) {
1685 if (context_->IsVerbose()) {
1686 context_->GetDiagnostics()->Note(
1687 DiagMessage(*path_iter)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001688 << "generating split with configurations '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001689 << util::Joiner(split_constraints_iter->configs, ", ") << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001690 }
1691
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001692 std::unique_ptr<IArchiveWriter> archive_writer =
1693 MakeArchiveWriter(*path_iter);
1694 if (!archive_writer) {
1695 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001696 << "failed to create archive");
1697 return 1;
1698 }
1699
1700 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001701 std::unique_ptr<xml::XmlResource> split_manifest =
1702 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001703
1704 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001705 if (!linker.Consume(context_, split_manifest.get())) {
1706 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001707 DiagMessage() << "failed to create Split AndroidManifest.xml");
1708 return 1;
1709 }
1710
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001711 if (!WriteApk(archive_writer.get(), &proguard_keep_set,
1712 split_manifest.get(), split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001713 return 1;
1714 }
1715
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001716 ++path_iter;
1717 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001718 }
1719 }
1720
1721 // Start writing the base APK.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001722 std::unique_ptr<IArchiveWriter> archive_writer =
1723 MakeArchiveWriter(options_.output_path);
1724 if (!archive_writer) {
1725 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001726 << "failed to create archive");
1727 return 1;
1728 }
1729
1730 bool error = false;
1731 {
1732 // AndroidManifest.xml has no resource name, but the CallSite is built
1733 // from the name
1734 // (aka, which package the AndroidManifest.xml is coming from).
1735 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001736 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001737
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001738 XmlReferenceLinker manifest_linker;
1739 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1740 if (options_.generate_proguard_rules_path &&
1741 !proguard::CollectProguardRulesForManifest(
1742 Source(options_.manifest_path), manifest_xml.get(),
1743 &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001744 error = true;
1745 }
1746
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001747 if (options_.generate_main_dex_proguard_rules_path &&
1748 !proguard::CollectProguardRulesForManifest(
1749 Source(options_.manifest_path), manifest_xml.get(),
1750 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001751 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001752 }
1753
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001754 if (options_.generate_java_class_path) {
1755 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001756 error = true;
1757 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001758 }
1759
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001760 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001761 // PackageParser will fail if URIs are removed from
1762 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001763 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1764 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001765 error = true;
1766 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001767 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001768 } else {
1769 error = true;
1770 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001771 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001772
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001773 if (error) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001774 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001775 << "failed processing manifest");
1776 return 1;
1777 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001778
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001779 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(),
1780 &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001781 return 1;
1782 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001783
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001784 if (options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001785 JavaClassGeneratorOptions options;
1786 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001787 options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001788
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001789 if (options_.package_type == PackageType::kStaticLib || options_.generate_non_final_ids) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001790 options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001791 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001792
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001793 if (options_.package_type == PackageType::kSharedLib) {
1794 options.use_final = false;
1795 options.generate_rewrite_callback = true;
1796 }
1797
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001798 const StringPiece actual_package = context_->GetCompilationPackage();
1799 StringPiece output_package = context_->GetCompilationPackage();
1800 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001801 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001802 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001803 }
1804
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001805 if (options_.private_symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001806 // If we defined a private symbols package, we only emit Public symbols
1807 // to the original package, and private and public symbols to the
1808 // private package.
1809
1810 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001811 if (!WriteJavaFile(&final_table_, context_->GetCompilationPackage(),
1812 output_package, options)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001813 return 1;
1814 }
1815
1816 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001817 output_package = options_.private_symbols.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001818 }
1819
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001820 if (!WriteJavaFile(&final_table_, actual_package, output_package,
1821 options)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001822 return 1;
1823 }
1824
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001825 for (const std::string& extra_package : options_.extra_java_packages) {
1826 if (!WriteJavaFile(&final_table_, actual_package, extra_package,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001827 options)) {
1828 return 1;
1829 }
1830 }
1831 }
1832
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001833 if (!WriteProguardFile(options_.generate_proguard_rules_path,
1834 proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001835 return 1;
1836 }
1837
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001838 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1839 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001840 return 1;
1841 }
1842
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001843 if (context_->IsVerbose()) {
1844 DebugPrintTableOptions debug_print_table_options;
1845 debug_print_table_options.show_sources = true;
1846 Debug::PrintTable(&final_table_, debug_print_table_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001847 }
1848 return 0;
1849 }
1850
1851 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001852 LinkOptions options_;
1853 LinkContext* context_;
1854 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001855
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001856 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001857
1858 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001859 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001860
1861 // A vector of IFileCollections. This is mainly here to keep ownership of the
1862 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001863 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001864
1865 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001866 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001867 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001868
1869 // The set of shared libraries being used, mapping their assigned package ID to package name.
1870 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001871};
1872
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001873int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001874 LinkContext context;
1875 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001876 std::vector<std::string> overlay_arg_list;
1877 std::vector<std::string> extra_java_packages;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001878 Maybe<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001879 Maybe<std::string> preferred_density;
1880 Maybe<std::string> product_list;
1881 bool legacy_x_flag = false;
1882 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001883 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001884 bool shared_lib = false;
1885 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001886 Maybe<std::string> stable_id_file_path;
1887 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001888 Flags flags =
1889 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001890 .RequiredFlag("-o", "Output path", &options.output_path)
1891 .RequiredFlag("--manifest", "Path to the Android manifest to build",
1892 &options.manifest_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001893 .OptionalFlagList("-I", "Adds an Android APK to link against", &options.include_paths)
1894 .OptionalFlagList("-R",
1895 "Compilation unit to link, using `overlay` semantics.\n"
1896 "The last conflicting resource given takes precedence.",
1897 &overlay_arg_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001898 .OptionalFlag("--java", "Directory in which to generate R.java",
1899 &options.generate_java_class_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001900 .OptionalFlag("--proguard", "Output file for generated Proguard rules",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001901 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001902 .OptionalFlag("--proguard-main-dex",
1903 "Output file for generated Proguard rules for the main dex",
1904 &options.generate_main_dex_proguard_rules_path)
1905 .OptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001906 &options.no_auto_version)
1907 .OptionalSwitch("--no-version-vectors",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001908 "Disables automatic versioning of vector drawables. "
1909 "Use this only\n"
1910 "when building with vector drawable support library",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001911 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001912 .OptionalSwitch("--no-version-transitions",
1913 "Disables automatic versioning of transition resources. "
1914 "Use this only\n"
1915 "when building with transition support library",
1916 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001917 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001918 "Disables automatic deduping of resources with\n"
1919 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001920 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001921 .OptionalSwitch("--enable-sparse-encoding",
1922 "Enables encoding sparse entries using a binary search tree.\n"
1923 "This decreases APK size at the cost of resource retrieval performance.",
1924 &options.table_flattener_options.use_sparse_entries)
1925 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01",
1926 &legacy_x_flag)
1927 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001928 &require_localization)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001929 .OptionalFlag("-c",
1930 "Comma separated list of configurations to include. The default\n"
1931 "is all configurations",
1932 &configs)
1933 .OptionalFlag("--preferred-density",
1934 "Selects the closest matching density and strips out all others.",
1935 &preferred_density)
1936 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001937 .OptionalSwitch("--output-to-dir",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001938 "Outputs the APK contents to a directory specified "
1939 "by -o",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001940 &options.output_to_directory)
1941 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001942 "Removes XML namespace prefix and URI "
1943 "information from AndroidManifest.xml\nand XML "
1944 "binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001945 &options.no_xml_namespaces)
1946 .OptionalFlag("--min-sdk-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001947 "Default minimum SDK version to use for "
1948 "AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001949 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001950 .OptionalFlag("--target-sdk-version",
1951 "Default target SDK version to use for "
1952 "AndroidManifest.xml",
1953 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001954 .OptionalFlag("--version-code",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001955 "Version code (integer) to inject into the "
1956 "AndroidManifest.xml if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001957 &options.manifest_fixer_options.version_code_default)
1958 .OptionalFlag("--version-name",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001959 "Version name to inject into the AndroidManifest.xml "
1960 "if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001961 &options.manifest_fixer_options.version_name_default)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001962 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library", &shared_lib)
1963 .OptionalSwitch("--static-lib", "Generate a static Android library", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001964 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001965 "Merge all library resources under the app's package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001966 &options.no_static_lib_packages)
1967 .OptionalSwitch("--non-final-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001968 "Generates R.java without the final modifier.\n"
1969 "This is implied when --static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001970 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001971 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001972 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001973 .OptionalFlag("--emit-ids",
1974 "Emit a file at the given path with a list of name to ID\n"
1975 "mappings, suitable for use with --stable-ids.",
1976 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001977 .OptionalFlag("--private-symbols",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001978 "Package name to use when generating R.java for "
1979 "private symbols.\n"
1980 "If not specified, public and private symbols will use "
1981 "the application's "
1982 "package name",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001983 &options.private_symbols)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001984 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001985 &options.custom_java_package)
1986 .OptionalFlagList("--extra-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001987 "Generate the same R.java but with different "
1988 "package names",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001989 &extra_java_packages)
1990 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001991 "Adds a JavaDoc annotation to all "
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001992 "generated Java classes",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001993 &options.javadoc_annotations)
1994 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001995 "Allows the addition of new resources in "
1996 "overlays without <add-resource> tags",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001997 &options.auto_add_overlay)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001998 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001999 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002000 .OptionalFlag("--rename-instrumentation-target-package",
2001 "Changes the name of the target package for instrumentation. "
2002 "Most useful "
2003 "when used\nin conjunction with --rename-manifest-package",
2004 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002005 .OptionalFlagList("-0", "File extensions not to compress",
2006 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002007 .OptionalFlagList("--split",
2008 "Split resources matching a set of configs out to a "
2009 "Split APK.\nSyntax: path/to/output.apk:<config>[,<config>[...]]",
2010 &split_args)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002011 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002012
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002013 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002014 return 1;
2015 }
2016
2017 // Expand all argument-files passed into the command line. These start with
2018 // '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002019 std::vector<std::string> arg_list;
2020 for (const std::string& arg : flags.GetArgs()) {
2021 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002022 const std::string path = arg.substr(1, arg.size() - 1);
2023 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002024 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
2025 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002026 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002027 }
2028 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002029 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002030 }
2031 }
2032
2033 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002034 for (const std::string& arg : overlay_arg_list) {
2035 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002036 const std::string path = arg.substr(1, arg.size() - 1);
2037 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002038 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
2039 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002040 return 1;
2041 }
2042 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002043 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002044 }
2045 }
2046
2047 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002048 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002049 }
2050
2051 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002052 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002053 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002054 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002055 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002056 }
2057 }
2058
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002059 if (product_list) {
2060 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002061 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002062 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002063 }
2064 }
2065 }
2066
2067 AxisConfigFilter filter;
2068 if (configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002069 for (const StringPiece& config_str : util::Tokenize(configs.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002070 ConfigDescription config;
2071 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002072 if (lv.InitFromFilterString(config_str)) {
2073 lv.WriteTo(&config);
2074 } else if (!ConfigDescription::Parse(config_str, &config)) {
2075 context.GetDiagnostics()->Error(DiagMessage() << "invalid config '"
2076 << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002077 << "' for -c option");
2078 return 1;
2079 }
2080
2081 if (config.density != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002082 context.GetDiagnostics()->Warn(DiagMessage() << "ignoring density '"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002083 << config
2084 << "' for -c option");
2085 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002086 filter.AddConfig(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002087 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002088 }
2089
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002090 options.table_splitter_options.config_filter = &filter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002091 }
2092
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002093 if (preferred_density) {
2094 ConfigDescription preferred_density_config;
2095 if (!ConfigDescription::Parse(preferred_density.value(),
2096 &preferred_density_config)) {
2097 context.GetDiagnostics()->Error(
2098 DiagMessage() << "invalid density '" << preferred_density.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002099 << "' for --preferred-density option");
2100 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002101 }
2102
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002103 // Clear the version that can be automatically added.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002104 preferred_density_config.sdkVersion = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002105
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002106 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002107 ConfigDescription::CONFIG_DENSITY) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002108 context.GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002109 DiagMessage() << "invalid preferred density '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002110 << preferred_density.value() << "'. "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002111 << "Preferred density must only be a density value");
2112 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002113 }
Pierre Lecesne672384b2017-02-06 10:29:02 +00002114 options.table_splitter_options.preferred_densities.push_back(preferred_density_config.density);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002115 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002116
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002117 if (shared_lib && static_lib) {
2118 context.GetDiagnostics()->Error(DiagMessage()
2119 << "only one of --shared-lib and --static-lib can be defined");
2120 return 1;
2121 }
2122
2123 if (shared_lib) {
2124 options.package_type = PackageType::kSharedLib;
2125 } else if (static_lib) {
2126 options.package_type = PackageType::kStaticLib;
2127 }
2128
2129 if (options.package_type != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002130 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2131 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002132 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002133 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002134 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002135
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002136 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002137 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002138 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2139 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2140 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2141 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2142
2143 // Parse the split parameters.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002144 for (const std::string& split_arg : split_args) {
2145 options.split_paths.push_back({});
2146 options.split_constraints.push_back({});
2147 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(),
2148 &options.split_paths.back(),
2149 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002150 return 1;
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002151 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002152 }
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002153
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002154 // Turn off auto versioning for static-libs.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002155 if (options.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002156 options.no_auto_version = true;
2157 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002158 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002159 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002160
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002161 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002162 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002163}
2164
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002165} // namespace aapt