blob: dd8e14b40fa8155c0b3c9746a240ab732e5016e3 [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
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001100 std::unique_ptr<ResourceTable> table = LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001101 if (!table) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001102 context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001103 return false;
1104 }
1105
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001106 ResourceTablePackage* pkg = table->FindPackageById(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001107 if (!pkg) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001108 context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 return false;
1110 }
1111
1112 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001113 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001114 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001115 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001116
1117 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001118 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001119 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001120 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001121 }
1122
1123 pkg->name = "";
1124 if (override) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001125 result = table_merger_->MergeOverlay(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001126 } else {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001127 result = table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001128 }
1129
1130 } else {
1131 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001132 // preserved and resource names are mangled.
1133 result = table_merger_->MergeAndMangle(Source(input), pkg->name,
1134 table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001135 }
1136
1137 if (!result) {
1138 return false;
1139 }
1140
1141 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001142 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001143 return true;
1144 }
1145
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001146 bool MergeResourceTable(io::IFile* file, bool override) {
1147 if (context_->IsVerbose()) {
1148 context_->GetDiagnostics()->Note(
1149 DiagMessage() << "merging resource table " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001150 }
1151
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001152 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001153 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001154 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001155 << "failed to open file");
1156 return false;
1157 }
1158
1159 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001160 LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1161 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001162 if (!table) {
1163 return false;
1164 }
1165
1166 bool result = false;
1167 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001168 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001169 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001170 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001171 }
1172 return result;
1173 }
1174
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001175 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001176 bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001177 if (context_->IsVerbose()) {
1178 context_->GetDiagnostics()->Note(
1179 DiagMessage() << "merging '" << file_desc->name
1180 << "' from compiled file " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001181 }
1182
1183 bool result = false;
1184 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001185 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001186 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001187 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001188 }
1189
1190 if (!result) {
1191 return false;
1192 }
1193
1194 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001195 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1196 if (exported_symbol.name.package.empty()) {
1197 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001198 }
1199
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001200 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001201
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001202 Maybe<ResourceName> mangled_name =
1203 context_->GetNameMangler()->MangleName(exported_symbol.name);
1204 if (mangled_name) {
1205 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001206 }
1207
1208 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001209 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1210 bool result = final_table_.AddResourceAllowMangled(
1211 res_name, ConfigDescription::DefaultConfig(), std::string(),
1212 std::move(id), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001213 if (!result) {
1214 return false;
1215 }
1216 }
1217 return true;
1218 }
1219
1220 /**
1221 * Takes a path to load as a ZIP file and merges the files within into the
1222 * master ResourceTable.
1223 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001224 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001225 *
1226 * An io::IFileCollection is created from the ZIP file and added to the set of
1227 * io::IFileCollections that are open.
1228 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001229 bool MergeArchive(const std::string& input, bool override) {
1230 if (context_->IsVerbose()) {
1231 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001232 << input);
1233 }
1234
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001235 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001236 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001237 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001238 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001239 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001240 return false;
1241 }
1242
1243 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001244 for (auto iter = collection->Iterator(); iter->HasNext();) {
1245 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001246 error = true;
1247 }
1248 }
1249
1250 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001251 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001252 return !error;
1253 }
1254
1255 /**
1256 * Takes a path to load and merge into the master ResourceTable. If override
1257 * is true,
1258 * conflicting resources are allowed to override each other, in order of last
1259 * seen.
1260 *
1261 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1262 * as ZIP archive
1263 * and the files within are merged individually.
1264 *
1265 * Otherwise the files is processed on its own.
1266 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001267 bool MergePath(const std::string& path, bool override) {
1268 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1269 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1270 return MergeArchive(path, override);
1271 } else if (util::EndsWith(path, ".apk")) {
1272 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001273 }
1274
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001275 io::IFile* file = file_collection_->InsertFile(path);
1276 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001277 }
1278
1279 /**
1280 * Takes a file to load and merge into the master ResourceTable. If override
1281 * is true,
1282 * conflicting resources are allowed to override each other, in order of last
1283 * seen.
1284 *
1285 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1286 * merged into the
1287 * master ResourceTable. If the file ends with .flat, then it is treated like
1288 * a compiled file
1289 * and the header data is read and merged into the final ResourceTable.
1290 *
1291 * All other file types are ignored. This is because these files could be
1292 * coming from a zip,
1293 * where we could have other files like classes.dex.
1294 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001295 bool MergeFile(io::IFile* file, bool override) {
1296 const Source& src = file->GetSource();
1297 if (util::EndsWith(src.path, ".arsc.flat")) {
1298 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001299
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001300 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001301 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001302 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001303 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001304 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001305 return false;
1306 }
1307
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308 CompiledFileInputStream input_stream(data->data(), data->size());
1309 uint32_t num_files = 0;
1310 if (!input_stream.ReadLittleEndian32(&num_files)) {
1311 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001312 << "failed read num files");
1313 return false;
1314 }
1315
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001316 for (uint32_t i = 0; i < num_files; i++) {
1317 pb::CompiledFile compiled_file;
1318 if (!input_stream.ReadCompiledFile(&compiled_file)) {
1319 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001320 DiagMessage(src) << "failed to read compiled file header");
1321 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001322 }
1323
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001324 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001325 if (!input_stream.ReadDataMetaData(&offset, &len)) {
1326 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001327 << "failed to read data meta data");
1328 return false;
1329 }
1330
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001331 std::unique_ptr<ResourceFile> resource_file =
1332 DeserializeCompiledFileFromPb(compiled_file, file->GetSource(),
1333 context_->GetDiagnostics());
1334 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001335 return false;
1336 }
1337
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001338 if (!MergeCompiledFile(file->CreateFileSegment(offset, len),
1339 resource_file.get(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001340 return false;
1341 }
1342 }
1343 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001344 } else if (util::EndsWith(src.path, ".xml") ||
1345 util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001346 // Since AAPT compiles these file types and appends .flat to them, seeing
1347 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001348 const StringPiece file_type =
1349 util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1350 context_->GetDiagnostics()->Error(DiagMessage(src)
1351 << "uncompiled " << file_type
Adam Lesinski6a396c12016-10-20 14:38:23 -07001352 << " file passed as argument. Must be "
1353 "compiled first into .flat file.");
1354 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001355 }
1356
1357 // Ignore non .flat files. This could be classes.dex or something else that
1358 // happens
1359 // to be in an archive.
1360 return true;
1361 }
1362
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001363 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(
1364 const AppInfo& app_info, const SplitConstraints& constraints) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001365 std::unique_ptr<xml::XmlResource> doc =
1366 util::make_unique<xml::XmlResource>();
1367
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001368 std::unique_ptr<xml::Namespace> namespace_android =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001369 util::make_unique<xml::Namespace>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001370 namespace_android->namespace_uri = xml::kSchemaAndroid;
1371 namespace_android->namespace_prefix = "android";
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001372
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001373 std::unique_ptr<xml::Element> manifest_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001374 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001375 manifest_el->name = "manifest";
1376 manifest_el->attributes.push_back(
1377 xml::Attribute{"", "package", app_info.package});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001379 if (app_info.version_code) {
1380 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001381 xml::Attribute{xml::kSchemaAndroid, "versionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001382 std::to_string(app_info.version_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001383 }
1384
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001385 if (app_info.revision_code) {
1386 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001387 xml::Attribute{xml::kSchemaAndroid, "revisionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001388 std::to_string(app_info.revision_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001389 }
1390
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001391 std::stringstream split_name;
1392 split_name << "config." << util::Joiner(constraints.configs, "_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001393
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001394 manifest_el->attributes.push_back(
1395 xml::Attribute{"", "split", split_name.str()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001396
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001397 std::unique_ptr<xml::Element> application_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001398 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001399 application_el->name = "application";
1400 application_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001401 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false"});
1402
Adam Lesinskie343eb12016-10-27 16:31:58 -07001403 manifest_el->AppendChild(std::move(application_el));
1404 namespace_android->AppendChild(std::move(manifest_el));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001405 doc->root = std::move(namespace_android);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001406 return doc;
1407 }
1408
1409 /**
1410 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001411 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001412 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001413 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001414 xml::XmlResource* manifest, ResourceTable* table) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001415 const bool keep_raw_values = options_.package_type == PackageType::kStaticLib;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001416 bool result = FlattenXml(manifest, "AndroidManifest.xml", {},
1417 keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001418 if (!result) {
1419 return false;
1420 }
1421
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001422 ResourceFileFlattenerOptions file_flattener_options;
1423 file_flattener_options.keep_raw_values = keep_raw_values;
1424 file_flattener_options.do_not_compress_anything =
1425 options_.do_not_compress_anything;
1426 file_flattener_options.extensions_to_not_compress =
1427 options_.extensions_to_not_compress;
1428 file_flattener_options.no_auto_version = options_.no_auto_version;
1429 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001430 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001431 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1432 file_flattener_options.update_proguard_spec =
1433 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001434
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001435 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001436
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001437 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001438 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439 return false;
1440 }
1441
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001442 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001443 if (!FlattenTableToPb(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001444 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001445 return false;
1446 }
1447 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001448 if (!FlattenTable(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001449 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001450 return false;
1451 }
1452 }
1453 return true;
1454 }
1455
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001456 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001457 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001458 std::unique_ptr<xml::XmlResource> manifest_xml =
1459 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1460 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001461 return 1;
1462 }
1463
1464 // First extract the Package name without modifying it (via
1465 // --rename-manifest-package).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001466 if (Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1467 manifest_xml.get(), context_->GetDiagnostics())) {
1468 const AppInfo& app_info = maybe_app_info.value();
1469 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001470 }
1471
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001472 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1473 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001474 return 1;
1475 }
1476
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001477 Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1478 manifest_xml.get(), context_->GetDiagnostics());
1479 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001480 return 1;
1481 }
1482
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001483 const AppInfo& app_info = maybe_app_info.value();
1484 if (app_info.min_sdk_version) {
1485 if (Maybe<int> maybe_min_sdk_version = ResourceUtils::ParseSdkVersion(
1486 app_info.min_sdk_version.value())) {
1487 context_->SetMinSdkVersion(maybe_min_sdk_version.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001488 }
1489 }
1490
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001491 context_->SetNameManglerPolicy(
1492 NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001493 if (options_.package_type == PackageType::kSharedLib) {
1494 context_->SetPackageId(0x00);
1495 } else if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001496 context_->SetPackageId(0x01);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001497 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001498 context_->SetPackageId(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001499 }
1500
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001501 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001502 return 1;
1503 }
1504
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001505 TableMergerOptions table_merger_options;
1506 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
1507 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_,
1508 table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001509
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001510 if (context_->IsVerbose()) {
1511 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001512 << "linking package '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001513 << context_->GetCompilationPackage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001514 << "' with package ID " << std::hex
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001515 << (int)context_->GetPackageId());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001516 }
1517
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001518 for (const std::string& input : input_files) {
1519 if (!MergePath(input, false)) {
1520 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001521 << "failed parsing input");
1522 return 1;
1523 }
1524 }
1525
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001526 for (const std::string& input : options_.overlay_files) {
1527 if (!MergePath(input, true)) {
1528 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001529 << "failed parsing overlays");
1530 return 1;
1531 }
1532 }
1533
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001534 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 return 1;
1536 }
1537
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001538 if (options_.package_type != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001539 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001540 if (!mover.Consume(context_, &final_table_)) {
1541 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001542 DiagMessage() << "failed moving private attributes");
1543 return 1;
1544 }
1545
1546 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001547 IdAssigner id_assigner(&options_.stable_id_map);
1548 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001549 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001550 return 1;
1551 }
1552
1553 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001554 if (options_.resource_id_map_path) {
1555 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001556 for (auto& type : package->types) {
1557 for (auto& entry : type->entries) {
1558 ResourceName name(package->name, type->type, entry->name);
1559 // The IDs are guaranteed to exist.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001560 options_.stable_id_map[std::move(name)] = ResourceId(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001561 package->id.value(), type->id.value(), entry->id.value());
1562 }
1563 }
1564 }
1565
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001566 if (!WriteStableIdMapToPath(context_->GetDiagnostics(),
1567 options_.stable_id_map,
1568 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001569 return 1;
1570 }
1571 }
1572 } else {
1573 // Static libs are merged with other apps, and ID collisions are bad, so
1574 // verify that
1575 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001576 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001577 return 1;
1578 }
1579 }
1580
1581 // Add the names to mangle based on our source merge earlier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001582 context_->SetNameManglerPolicy(NameManglerPolicy{
1583 context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001584
1585 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001586 context_->GetExternalSymbols()->PrependSource(
1587 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001588
1589 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001590 if (!linker.Consume(context_, &final_table_)) {
1591 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001592 << "failed linking references");
1593 return 1;
1594 }
1595
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001596 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001597 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001598 context_->GetDiagnostics()->Warn(DiagMessage()
1599 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001600 }
1601 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001602 ProductFilter product_filter(options_.products);
1603 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001604 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001605 return 1;
1606 }
1607 }
1608
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001609 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001610 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001611 if (!versioner.Consume(context_, &final_table_)) {
1612 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001613 << "failed versioning styles");
1614 return 1;
1615 }
1616 }
1617
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001618 if (options_.package_type != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001619 if (context_->IsVerbose()) {
1620 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001621 DiagMessage() << "collapsing resource versions for minimum SDK "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001622 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001623 }
1624
1625 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001626 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001627 return 1;
1628 }
1629 }
1630
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001631 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001632 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001633 if (!deduper.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001634 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001635 return 1;
1636 }
1637 }
1638
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001639 proguard::KeepSet proguard_keep_set;
1640 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001641
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001642 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001643 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001644 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001645 context_->GetDiagnostics()->Warn(DiagMessage()
1646 << "can't strip resources when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001647 }
1648 } else {
1649 // Adjust the SplitConstraints so that their SDK version is stripped if it
1650 // is less
1651 // than or equal to the minSdk. Otherwise the resources that have had
1652 // their SDK version
1653 // stripped due to minSdk won't ever match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001654 std::vector<SplitConstraints> adjusted_constraints_list;
1655 adjusted_constraints_list.reserve(options_.split_constraints.size());
1656 for (const SplitConstraints& constraints : options_.split_constraints) {
1657 SplitConstraints adjusted_constraints;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001658 for (const ConfigDescription& config : constraints.configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001659 if (config.sdkVersion <= context_->GetMinSdkVersion()) {
1660 adjusted_constraints.configs.insert(config.CopyWithoutSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001661 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001662 adjusted_constraints.configs.insert(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001663 }
1664 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001665 adjusted_constraints_list.push_back(std::move(adjusted_constraints));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001666 }
1667
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001668 TableSplitter table_splitter(adjusted_constraints_list,
1669 options_.table_splitter_options);
1670 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001671 return 1;
1672 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001673 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001674
1675 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001676 auto path_iter = options_.split_paths.begin();
1677 auto split_constraints_iter = adjusted_constraints_list.begin();
1678 for (std::unique_ptr<ResourceTable>& split_table :
1679 table_splitter.splits()) {
1680 if (context_->IsVerbose()) {
1681 context_->GetDiagnostics()->Note(
1682 DiagMessage(*path_iter)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001683 << "generating split with configurations '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001684 << util::Joiner(split_constraints_iter->configs, ", ") << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001685 }
1686
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001687 std::unique_ptr<IArchiveWriter> archive_writer =
1688 MakeArchiveWriter(*path_iter);
1689 if (!archive_writer) {
1690 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001691 << "failed to create archive");
1692 return 1;
1693 }
1694
1695 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001696 std::unique_ptr<xml::XmlResource> split_manifest =
1697 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001698
1699 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001700 if (!linker.Consume(context_, split_manifest.get())) {
1701 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001702 DiagMessage() << "failed to create Split AndroidManifest.xml");
1703 return 1;
1704 }
1705
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001706 if (!WriteApk(archive_writer.get(), &proguard_keep_set,
1707 split_manifest.get(), split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001708 return 1;
1709 }
1710
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001711 ++path_iter;
1712 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001713 }
1714 }
1715
1716 // Start writing the base APK.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001717 std::unique_ptr<IArchiveWriter> archive_writer =
1718 MakeArchiveWriter(options_.output_path);
1719 if (!archive_writer) {
1720 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001721 << "failed to create archive");
1722 return 1;
1723 }
1724
1725 bool error = false;
1726 {
1727 // AndroidManifest.xml has no resource name, but the CallSite is built
1728 // from the name
1729 // (aka, which package the AndroidManifest.xml is coming from).
1730 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001731 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001732
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001733 XmlReferenceLinker manifest_linker;
1734 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1735 if (options_.generate_proguard_rules_path &&
1736 !proguard::CollectProguardRulesForManifest(
1737 Source(options_.manifest_path), manifest_xml.get(),
1738 &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001739 error = true;
1740 }
1741
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001742 if (options_.generate_main_dex_proguard_rules_path &&
1743 !proguard::CollectProguardRulesForManifest(
1744 Source(options_.manifest_path), manifest_xml.get(),
1745 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001746 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001747 }
1748
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001749 if (options_.generate_java_class_path) {
1750 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001751 error = true;
1752 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001753 }
1754
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001755 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001756 // PackageParser will fail if URIs are removed from
1757 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001758 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1759 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001760 error = true;
1761 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001762 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001763 } else {
1764 error = true;
1765 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001766 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001767
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001768 if (error) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001769 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001770 << "failed processing manifest");
1771 return 1;
1772 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001773
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001774 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(),
1775 &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001776 return 1;
1777 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001778
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001779 if (options_.generate_java_class_path) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001780 // The set of packages whose R class to call in the main classes
1781 // onResourcesLoaded callback.
1782 std::vector<std::string> packages_to_callback;
1783
1784 JavaClassGeneratorOptions template_options;
1785 template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1786 template_options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001787
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001788 if (options_.package_type == PackageType::kStaticLib || options_.generate_non_final_ids) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001789 template_options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001790 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001791
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001792 if (options_.package_type == PackageType::kSharedLib) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001793 template_options.use_final = false;
1794 template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{};
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001795 }
1796
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001797 const StringPiece actual_package = context_->GetCompilationPackage();
1798 StringPiece output_package = context_->GetCompilationPackage();
1799 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001800 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001801 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001802 }
1803
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001804 // Generate the private symbols if required.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001805 if (options_.private_symbols) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001806 packages_to_callback.push_back(options_.private_symbols.value());
1807
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001808 // If we defined a private symbols package, we only emit Public symbols
1809 // to the original package, and private and public symbols to the
1810 // private package.
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001811 JavaClassGeneratorOptions options = template_options;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001812 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001813 if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001814 options)) {
1815 return 1;
1816 }
1817 }
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001818
1819 // Generate all the symbols for all extra packages.
1820 for (const std::string& extra_package : options_.extra_java_packages) {
1821 packages_to_callback.push_back(extra_package);
1822
1823 JavaClassGeneratorOptions options = template_options;
1824 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1825 if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) {
1826 return 1;
1827 }
1828 }
1829
1830 // Generate the main public R class.
1831 JavaClassGeneratorOptions options = template_options;
1832
1833 // Only generate public symbols if we have a private package.
1834 if (options_.private_symbols) {
1835 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
1836 }
1837
1838 if (options.rewrite_callback_options) {
1839 options.rewrite_callback_options.value().packages_to_callback =
1840 std::move(packages_to_callback);
1841 }
1842
1843 if (!WriteJavaFile(&final_table_, actual_package, output_package, options)) {
1844 return 1;
1845 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001846 }
1847
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001848 if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001849 return 1;
1850 }
1851
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001852 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1853 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001854 return 1;
1855 }
1856
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001857 if (context_->IsVerbose()) {
1858 DebugPrintTableOptions debug_print_table_options;
1859 debug_print_table_options.show_sources = true;
1860 Debug::PrintTable(&final_table_, debug_print_table_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001861 }
1862 return 0;
1863 }
1864
1865 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001866 LinkOptions options_;
1867 LinkContext* context_;
1868 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001869
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001870 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001871
1872 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001873 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001874
1875 // A vector of IFileCollections. This is mainly here to keep ownership of the
1876 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001877 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001878
1879 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001880 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001881 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001882
1883 // The set of shared libraries being used, mapping their assigned package ID to package name.
1884 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001885};
1886
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001887int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001888 LinkContext context;
1889 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001890 std::vector<std::string> overlay_arg_list;
1891 std::vector<std::string> extra_java_packages;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001892 Maybe<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001893 Maybe<std::string> preferred_density;
1894 Maybe<std::string> product_list;
1895 bool legacy_x_flag = false;
1896 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001897 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001898 bool shared_lib = false;
1899 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001900 Maybe<std::string> stable_id_file_path;
1901 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001902 Flags flags =
1903 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001904 .RequiredFlag("-o", "Output path", &options.output_path)
1905 .RequiredFlag("--manifest", "Path to the Android manifest to build",
1906 &options.manifest_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001907 .OptionalFlagList("-I", "Adds an Android APK to link against", &options.include_paths)
1908 .OptionalFlagList("-R",
1909 "Compilation unit to link, using `overlay` semantics.\n"
1910 "The last conflicting resource given takes precedence.",
1911 &overlay_arg_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001912 .OptionalFlag("--java", "Directory in which to generate R.java",
1913 &options.generate_java_class_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001914 .OptionalFlag("--proguard", "Output file for generated Proguard rules",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001915 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001916 .OptionalFlag("--proguard-main-dex",
1917 "Output file for generated Proguard rules for the main dex",
1918 &options.generate_main_dex_proguard_rules_path)
1919 .OptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001920 &options.no_auto_version)
1921 .OptionalSwitch("--no-version-vectors",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001922 "Disables automatic versioning of vector drawables. "
1923 "Use this only\n"
1924 "when building with vector drawable support library",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001925 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001926 .OptionalSwitch("--no-version-transitions",
1927 "Disables automatic versioning of transition resources. "
1928 "Use this only\n"
1929 "when building with transition support library",
1930 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001931 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001932 "Disables automatic deduping of resources with\n"
1933 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001934 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001935 .OptionalSwitch("--enable-sparse-encoding",
1936 "Enables encoding sparse entries using a binary search tree.\n"
1937 "This decreases APK size at the cost of resource retrieval performance.",
1938 &options.table_flattener_options.use_sparse_entries)
1939 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01",
1940 &legacy_x_flag)
1941 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001942 &require_localization)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001943 .OptionalFlag("-c",
1944 "Comma separated list of configurations to include. The default\n"
1945 "is all configurations",
1946 &configs)
1947 .OptionalFlag("--preferred-density",
1948 "Selects the closest matching density and strips out all others.",
1949 &preferred_density)
1950 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001951 .OptionalSwitch("--output-to-dir",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001952 "Outputs the APK contents to a directory specified "
1953 "by -o",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001954 &options.output_to_directory)
1955 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001956 "Removes XML namespace prefix and URI "
1957 "information from AndroidManifest.xml\nand XML "
1958 "binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001959 &options.no_xml_namespaces)
1960 .OptionalFlag("--min-sdk-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001961 "Default minimum SDK version to use for "
1962 "AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001963 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001964 .OptionalFlag("--target-sdk-version",
1965 "Default target SDK version to use for "
1966 "AndroidManifest.xml",
1967 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001968 .OptionalFlag("--version-code",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001969 "Version code (integer) to inject into the "
1970 "AndroidManifest.xml if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001971 &options.manifest_fixer_options.version_code_default)
1972 .OptionalFlag("--version-name",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001973 "Version name to inject into the AndroidManifest.xml "
1974 "if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001975 &options.manifest_fixer_options.version_name_default)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001976 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library", &shared_lib)
1977 .OptionalSwitch("--static-lib", "Generate a static Android library", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001978 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001979 "Merge all library resources under the app's package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001980 &options.no_static_lib_packages)
1981 .OptionalSwitch("--non-final-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001982 "Generates R.java without the final modifier.\n"
1983 "This is implied when --static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001984 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001985 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001986 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001987 .OptionalFlag("--emit-ids",
1988 "Emit a file at the given path with a list of name to ID\n"
1989 "mappings, suitable for use with --stable-ids.",
1990 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001991 .OptionalFlag("--private-symbols",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001992 "Package name to use when generating R.java for "
1993 "private symbols.\n"
1994 "If not specified, public and private symbols will use "
1995 "the application's "
1996 "package name",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001997 &options.private_symbols)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001998 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001999 &options.custom_java_package)
2000 .OptionalFlagList("--extra-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002001 "Generate the same R.java but with different "
2002 "package names",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002003 &extra_java_packages)
2004 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002005 "Adds a JavaDoc annotation to all "
Adam Lesinskid0f116b2016-07-08 15:00:32 -07002006 "generated Java classes",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002007 &options.javadoc_annotations)
2008 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002009 "Allows the addition of new resources in "
2010 "overlays without <add-resource> tags",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002011 &options.auto_add_overlay)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002012 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002013 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002014 .OptionalFlag("--rename-instrumentation-target-package",
2015 "Changes the name of the target package for instrumentation. "
2016 "Most useful "
2017 "when used\nin conjunction with --rename-manifest-package",
2018 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002019 .OptionalFlagList("-0", "File extensions not to compress",
2020 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002021 .OptionalFlagList("--split",
2022 "Split resources matching a set of configs out to a "
2023 "Split APK.\nSyntax: path/to/output.apk:<config>[,<config>[...]]",
2024 &split_args)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002025 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002026
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002027 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002028 return 1;
2029 }
2030
2031 // Expand all argument-files passed into the command line. These start with
2032 // '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002033 std::vector<std::string> arg_list;
2034 for (const std::string& arg : flags.GetArgs()) {
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, &arg_list, &error)) {
2039 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002040 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002041 }
2042 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002043 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002044 }
2045 }
2046
2047 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002048 for (const std::string& arg : overlay_arg_list) {
2049 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002050 const std::string path = arg.substr(1, arg.size() - 1);
2051 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002052 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
2053 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002054 return 1;
2055 }
2056 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002057 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002058 }
2059 }
2060
2061 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002062 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002063 }
2064
2065 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002066 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002067 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002068 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002069 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002070 }
2071 }
2072
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002073 if (product_list) {
2074 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002075 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002076 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002077 }
2078 }
2079 }
2080
2081 AxisConfigFilter filter;
2082 if (configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002083 for (const StringPiece& config_str : util::Tokenize(configs.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002084 ConfigDescription config;
2085 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002086 if (lv.InitFromFilterString(config_str)) {
2087 lv.WriteTo(&config);
2088 } else if (!ConfigDescription::Parse(config_str, &config)) {
2089 context.GetDiagnostics()->Error(DiagMessage() << "invalid config '"
2090 << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002091 << "' for -c option");
2092 return 1;
2093 }
2094
2095 if (config.density != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002096 context.GetDiagnostics()->Warn(DiagMessage() << "ignoring density '"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002097 << config
2098 << "' for -c option");
2099 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002100 filter.AddConfig(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002101 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002102 }
2103
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002104 options.table_splitter_options.config_filter = &filter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002105 }
2106
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002107 if (preferred_density) {
2108 ConfigDescription preferred_density_config;
2109 if (!ConfigDescription::Parse(preferred_density.value(),
2110 &preferred_density_config)) {
2111 context.GetDiagnostics()->Error(
2112 DiagMessage() << "invalid density '" << preferred_density.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002113 << "' for --preferred-density option");
2114 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002115 }
2116
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002117 // Clear the version that can be automatically added.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002118 preferred_density_config.sdkVersion = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002119
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002120 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002121 ConfigDescription::CONFIG_DENSITY) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002122 context.GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002123 DiagMessage() << "invalid preferred density '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002124 << preferred_density.value() << "'. "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002125 << "Preferred density must only be a density value");
2126 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002127 }
Pierre Lecesne672384b2017-02-06 10:29:02 +00002128 options.table_splitter_options.preferred_densities.push_back(preferred_density_config.density);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002129 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002130
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002131 if (shared_lib && static_lib) {
2132 context.GetDiagnostics()->Error(DiagMessage()
2133 << "only one of --shared-lib and --static-lib can be defined");
2134 return 1;
2135 }
2136
2137 if (shared_lib) {
2138 options.package_type = PackageType::kSharedLib;
2139 } else if (static_lib) {
2140 options.package_type = PackageType::kStaticLib;
2141 }
2142
2143 if (options.package_type != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002144 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2145 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002146 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002147 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002148 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002149
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002150 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002151 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002152 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2153 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2154 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2155 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2156
2157 // Parse the split parameters.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002158 for (const std::string& split_arg : split_args) {
2159 options.split_paths.push_back({});
2160 options.split_constraints.push_back({});
2161 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(),
2162 &options.split_paths.back(),
2163 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002164 return 1;
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002165 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002166 }
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002167
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002168 // Turn off auto versioning for static-libs.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002169 if (options.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002170 options.no_auto_version = true;
2171 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002172 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002173 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002174
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002175 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002176 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002177}
2178
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002179} // namespace aapt