blob: 7f715895e282cc4b9061d9bc33c360e3947d1174 [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 Lesinskif34b6f42017-03-03 16:33:26 -080026#include "android-base/stringprintf.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080027#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028#include "google/protobuf/io/coded_stream.h"
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "AppInfo.h"
31#include "Debug.h"
32#include "Flags.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080033#include "Locale.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034#include "NameMangler.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080035#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036#include "compile/IdAssigner.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080037#include "filter/ConfigFilter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038#include "flatten/Archive.h"
39#include "flatten/TableFlattener.h"
40#include "flatten/XmlFlattener.h"
Adam Lesinski06460ef2017-03-14 18:52:13 -070041#include "io/BigBufferInputStream.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080042#include "io/FileSystem.h"
43#include "io/ZipArchive.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070044#include "java/JavaClassGenerator.h"
45#include "java/ManifestClassGenerator.h"
46#include "java/ProguardRules.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047#include "link/Linkers.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048#include "link/ManifestFixer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080049#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050#include "link/TableMerger.h"
Adam Lesinskid48944a2017-02-21 14:22:30 -080051#include "optimize/ResourceDeduper.h"
52#include "optimize/VersionCollapser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053#include "process/IResourceTableConsumer.h"
54#include "process/SymbolTable.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080055#include "proto/ProtoSerialize.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080056#include "split/TableSplitter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057#include "unflatten/BinaryResourceParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058#include "util/Files.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080059#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060
Adam Lesinskid5083f62017-01-16 15:07:21 -080061using android::StringPiece;
Adam Lesinskif34b6f42017-03-03 16:33:26 -080062using android::base::StringPrintf;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070064
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065namespace aapt {
66
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080067// The type of package to build.
68enum class PackageType {
69 kApp,
70 kSharedLib,
71 kStaticLib,
72};
73
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074struct LinkOptions {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080075 PackageType package_type = PackageType::kApp;
76
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 std::string output_path;
78 std::string manifest_path;
79 std::vector<std::string> include_paths;
80 std::vector<std::string> overlay_files;
Adam Lesinskib39ad7c2017-03-13 11:40:48 -070081 std::vector<std::string> assets_dirs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 bool output_to_directory = false;
83 bool auto_add_overlay = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070084
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 // Java/Proguard options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 Maybe<std::string> generate_java_class_path;
87 Maybe<std::string> custom_java_package;
88 std::set<std::string> extra_java_packages;
89 Maybe<std::string> generate_proguard_rules_path;
90 Maybe<std::string> generate_main_dex_proguard_rules_path;
91 bool generate_non_final_ids = false;
92 std::vector<std::string> javadoc_annotations;
93 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070094
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 // Optimizations/features.
96 bool no_auto_version = false;
97 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +090098 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 bool no_resource_deduping = false;
100 bool no_xml_namespaces = false;
101 bool do_not_compress_anything = false;
102 std::unordered_set<std::string> extensions_to_not_compress;
103
104 // Static lib options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 bool no_static_lib_packages = false;
106
107 // AndroidManifest.xml massaging options.
108 ManifestFixerOptions manifest_fixer_options;
109
110 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700112
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800113 // Flattening options.
114 TableFlattenerOptions table_flattener_options;
115
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 TableSplitterOptions table_splitter_options;
118 std::vector<SplitConstraints> split_constraints;
119 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 std::unordered_map<ResourceName, ResourceId> stable_id_map;
123 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124};
125
Adam Lesinski64587af2016-02-18 18:33:06 -0800126class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 public:
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800128 LinkContext() : name_mangler_({}), symbols_(&name_mangler_) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 NameMangler* GetNameMangler() override { return &name_mangler_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700133
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
135 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 const std::string& GetCompilationPackage() override {
139 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800143 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 uint8_t GetPackageId() override { return package_id_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 void SetPackageId(uint8_t id) { package_id_ = id; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 SymbolTable* GetExternalSymbols() override { return &symbols_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 bool IsVerbose() override { return verbose_; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 int GetMinSdkVersion() override { return min_sdk_version_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 void SetMinSdkVersion(int minSdk) { min_sdk_version_ = minSdk; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700159
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 DISALLOW_COPY_AND_ASSIGN(LinkContext);
162
163 StdErrDiagnostics diagnostics_;
164 NameMangler name_mangler_;
165 std::string compilation_package_;
166 uint8_t package_id_ = 0x0;
167 SymbolTable symbols_;
168 bool verbose_ = false;
169 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170};
171
Adam Lesinski06460ef2017-03-14 18:52:13 -0700172static bool CopyInputStreamToArchive(io::InputStream* in, const std::string& out_path,
173 uint32_t compression_flags, IArchiveWriter* writer,
174 IAaptContext* context) {
175 if (context->IsVerbose()) {
176 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
177 }
178
179 if (!writer->WriteFile(out_path, compression_flags, in)) {
180 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
181 << " to archive: " << writer->GetError());
182 return false;
183 }
184 return true;
185}
186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187static bool CopyFileToArchive(io::IFile* file, const std::string& out_path,
188 uint32_t compression_flags,
189 IArchiveWriter* writer, IAaptContext* context) {
190 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 if (!data) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700192 context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file");
Adam Lesinski355f2852016-02-13 20:26:45 -0800193 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 }
Adam Lesinski06460ef2017-03-14 18:52:13 -0700195 return CopyInputStreamToArchive(data.get(), out_path, compression_flags, writer, context);
196}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197
Adam Lesinski06460ef2017-03-14 18:52:13 -0700198static bool CopyProtoToArchive(::google::protobuf::MessageLite* proto_msg,
199 const std::string& out_path, uint32_t compression_flags,
200 IArchiveWriter* writer, IAaptContext* context) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700202 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 }
204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 if (writer->StartEntry(out_path, compression_flags)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700206 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
207 {
208 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
209 ::google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
210 if (!proto_msg->SerializeToZeroCopyStream(&adaptor)) {
211 context->GetDiagnostics()->Error(DiagMessage()
212 << "failed to write " << out_path << " to archive");
213 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 }
215 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216
Adam Lesinski06460ef2017-03-14 18:52:13 -0700217 if (writer->FinishEntry()) {
218 return true;
219 }
220 }
221 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
222 << " to archive: " << writer->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800224}
225
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
227 Maybe<size_t> max_sdk_level, bool keep_raw_values,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 IArchiveWriter* writer, IAaptContext* context) {
229 BigBuffer buffer(1024);
230 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 options.keep_raw_values = keep_raw_values;
232 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800235 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 }
237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239 DiagMessage msg;
240 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 if (max_sdk_level) {
242 msg << " maxSdkLevel=" << max_sdk_level.value()
243 << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 }
247
Adam Lesinski06460ef2017-03-14 18:52:13 -0700248 io::BigBufferInputStream input_stream(&buffer);
249 return CopyInputStreamToArchive(&input_stream, path.to_string(), ArchiveEntry::kCompress, writer,
250 context);
Adam Lesinski355f2852016-02-13 20:26:45 -0800251}
252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 const void* data,
255 size_t len,
Adam Lesinski355f2852016-02-13 20:26:45 -0800256 IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 pb::ResourceTable pb_table;
258 if (!pb_table.ParseFromArray(data, len)) {
259 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 return {};
261 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800262
Adam Lesinski06460ef2017-03-14 18:52:13 -0700263 std::unique_ptr<ResourceTable> table = DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 if (!table) {
265 return {};
266 }
267 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800268}
269
270/**
271 * Inflates an XML file from the source path.
272 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 IDiagnostics* diag) {
275 std::ifstream fin(path, std::ifstream::binary);
276 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 return {};
279 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800281}
282
Adam Lesinski355f2852016-02-13 20:26:45 -0800283struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 bool no_auto_version = false;
285 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900286 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 bool no_xml_namespaces = false;
288 bool keep_raw_values = false;
289 bool do_not_compress_anything = false;
290 bool update_proguard_spec = false;
291 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800292};
293
294class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 public:
296 ResourceFileFlattener(const ResourceFileFlattenerOptions& options,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 IAaptContext* context, proguard::KeepSet* keep_set)
298 : options_(options), context_(context), keep_set_(keep_set) {}
Adam Lesinski355f2852016-02-13 20:26:45 -0800299
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800301
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 private:
303 struct FileOperation {
304 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700305
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 // The entry this file came from.
307 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700308
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700309 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700311
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700314
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 std::string dst_path;
317 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800319
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800321
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
323 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800324
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 ResourceFileFlattenerOptions options_;
326 IAaptContext* context_;
327 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800328};
329
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
331 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 return 0;
333 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800334
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 for (const std::string& extension : options_.extensions_to_not_compress) {
336 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800338 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 }
340 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800341}
342
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900343static bool IsTransitionElement(const std::string& name) {
344 return
345 name == "fade" ||
346 name == "changeBounds" ||
347 name == "slide" ||
348 name == "explode" ||
349 name == "changeImageTransform" ||
350 name == "changeTransform" ||
351 name == "changeClipBounds" ||
352 name == "autoTransition" ||
353 name == "recolor" ||
354 name == "changeScroll" ||
355 name == "transitionSet" ||
356 name == "transition" ||
357 name == "transitionManager";
358}
359
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360bool ResourceFileFlattener::LinkAndVersionXmlFile(
361 ResourceTable* table, FileOperation* file_op,
362 std::queue<FileOperation>* out_file_op_queue) {
363 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700365
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366 if (context_->IsVerbose()) {
367 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 }
369
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 XmlReferenceLinker xml_linker;
371 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 return false;
373 }
374
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 if (options_.update_proguard_spec &&
376 !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700377 return false;
378 }
379
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 if (options_.no_xml_namespaces) {
381 XmlNamespaceRemover namespace_remover;
382 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800384 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700385 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800386
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 if (!options_.no_auto_version) {
388 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390 xml::Element* el = xml::FindRootElement(doc);
391 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 if (el->name == "vector" || el->name == "animated-vector") {
393 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700396 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700398 }
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900399 if (options_.no_version_transitions) {
400 // Skip this if it is a transition resource.
401 xml::Element* el = xml::FindRootElement(doc);
402 if (el && el->namespace_uri.empty()) {
403 if (IsTransitionElement(el->name)) {
404 // We are NOT going to version this file.
405 file_op->skip_version = true;
406 return true;
407 }
408 }
409 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700410
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412
413 // Find the first SDK level used that is higher than this defined config and
414 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 const int min_sdk_version = context_->GetMinSdkVersion();
416 for (int sdk_level : xml_linker.sdk_levels()) {
417 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
418 if (!ShouldGenerateVersionedResource(file_op->entry, config,
419 sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 // If we shouldn't generate a versioned resource, stop checking.
421 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800422 }
423
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 ResourceFile versioned_file_desc = doc->file;
425 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700426
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 FileOperation new_file_op;
428 new_file_op.xml_to_flatten = util::make_unique<xml::XmlResource>(
429 versioned_file_desc, doc->root->Clone());
430 new_file_op.config = versioned_file_desc.config;
431 new_file_op.entry = file_op->entry;
432 new_file_op.dst_path = ResourceUtils::BuildResourceFileName(
433 versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800434
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700435 if (context_->IsVerbose()) {
436 context_->GetDiagnostics()->Note(
437 DiagMessage(versioned_file_desc.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 << "auto-versioning resource from config '" << config << "' -> '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800440 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 bool added = table->AddFileReferenceAllowMangled(
443 versioned_file_desc.name, versioned_file_desc.config,
444 versioned_file_desc.source, new_file_op.dst_path, nullptr,
445 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 if (!added) {
447 return false;
448 }
449
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 break;
452 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800453 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454 }
455 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800456}
457
458/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 * Do not insert or remove any resources while executing in this function. It
460 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800461 * corrupt the iteration order.
462 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463bool ResourceFileFlattener::Flatten(ResourceTable* table,
464 IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 bool error = false;
466 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800468
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 for (auto& pkg : table->packages) {
470 for (auto& type : pkg->types) {
471 // Sort by config and name, so that we get better locality in the zip
472 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700473 config_sorted_files.clear();
474 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800475
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 // Populate the queue with all files in the ResourceTable.
477 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 for (auto& config_value : entry->values) {
479 FileReference* file_ref =
480 ValueCast<FileReference>(config_value->value.get());
481 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700482 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700484
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700485 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700489 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 }
491
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 FileOperation file_op;
493 file_op.entry = entry.get();
494 file_op.dst_path = *file_ref->path;
495 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 if (type->type != ResourceType::kRaw &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499 (util::EndsWith(src_path, ".xml.flat") ||
500 util::EndsWith(src_path, ".xml"))) {
501 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700503 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 << "failed to open file");
505 return false;
506 }
507
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700508 file_op.xml_to_flatten =
509 xml::Inflate(data->data(), data->size(),
510 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700513 return false;
514 }
515
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 file_op.xml_to_flatten->file.config = config_value->config;
517 file_op.xml_to_flatten->file.source = file_ref->GetSource();
518 file_op.xml_to_flatten->file.name =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700519 ResourceName(pkg->name, type->type, entry->name);
520
521 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525
526 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 // else we end up copying the string in the std::make_pair() method,
528 // then creating a StringPiece from the copy, which would cause us
529 // to end up referencing garbage in the map.
530 const StringPiece entry_name(entry->name);
531 config_sorted_files[std::make_pair(
532 config_value->config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700533 }
534 }
535 }
536
537 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700538 for (; !file_operations.empty(); file_operations.pop()) {
539 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 error = true;
543 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700544 }
545
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
547 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 // creating a StringPiece from the copy, which would cause us to end up
549 // referencing garbage in the map.
550 const StringPiece entry_name(file_op.entry->name);
551 config_sorted_files[std::make_pair(file_op.config, entry_name)] =
552 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 }
554
555 if (error) {
556 return false;
557 }
558
559 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 for (auto& map_entry : config_sorted_files) {
561 const ConfigDescription& config = map_entry.first.first;
562 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 if (file_op.xml_to_flatten) {
565 Maybe<size_t> max_sdk_level;
566 if (!options_.no_auto_version && !file_op.skip_version) {
567 max_sdk_level =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700568 std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700569 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 }
571
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572 bool result = FlattenXml(
573 file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
574 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700575 if (!result) {
576 error = true;
577 }
578 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 bool result = CopyFileToArchive(
580 file_op.file_to_copy, file_op.dst_path,
581 GetCompressionFlags(file_op.dst_path), archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 if (!result) {
583 error = true;
584 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700585 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700587 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700588 }
589 return !error;
590}
591
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592static bool WriteStableIdMapToPath(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 IDiagnostics* diag,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 const std::unordered_map<ResourceName, ResourceId>& id_map,
595 const std::string& id_map_path) {
596 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700598 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 return false;
600 }
601
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 const ResourceName& name = entry.first;
604 const ResourceId& id = entry.second;
605 fout << name << " = " << id << "\n";
606 }
607
608 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 diag->Error(DiagMessage(id_map_path)
610 << "failed writing to file: "
611 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 return false;
613 }
614
615 return true;
616}
617
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618static bool LoadStableIdMap(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 IDiagnostics* diag, const std::string& path,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700620 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 std::string content;
622 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700623 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700624 return false;
625 }
626
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 out_id_map->clear();
628 size_t line_no = 0;
629 for (StringPiece line : util::Tokenize(content, '\n')) {
630 line_no++;
631 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700632 if (line.empty()) {
633 continue;
634 }
635
636 auto iter = std::find(line.begin(), line.end(), '=');
637 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700638 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700639 return false;
640 }
641
642 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700643 StringPiece res_name_str =
644 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
645 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
646 diag->Error(DiagMessage(Source(path, line_no))
647 << "invalid resource name '" << res_name_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 return false;
649 }
650
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700651 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
652 const size_t res_id_str_len = line.size() - res_id_start_idx;
653 StringPiece res_id_str =
654 util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700656 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
657 if (!maybe_id) {
658 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '"
659 << res_id_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700660 return false;
661 }
662
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700663 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 }
665 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700666}
667
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668static bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag,
669 std::string* out_path,
670 SplitConstraints* out_split) {
671 std::vector<std::string> parts = util::Split(arg, ':');
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 if (parts.size() != 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
674 diag->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 DiagMessage()
676 << "should be --split path/to/output.apk:<config>[,<config>...]");
677 return false;
678 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 *out_path = parts[0];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 std::vector<ConfigDescription> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 configs.push_back({});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 if (!ConfigDescription::Parse(config_str, &configs.back())) {
684 diag->Error(DiagMessage() << "invalid config '" << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 << "' in split parameter '" << arg << "'");
686 return false;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700687 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 out_split->configs.insert(configs.begin(), configs.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 return true;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700691}
692
Adam Lesinskifb48d292015-11-07 15:52:13 -0800693class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 public:
695 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700696 : options_(options),
697 context_(context),
698 final_table_(),
699 file_collection_(util::make_unique<io::FileCollection>()) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700700
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 /**
702 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700703 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700704 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 bool LoadSymbolsFromIncludePaths() {
706 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700708 for (const std::string& path : options_.include_paths) {
709 if (context_->IsVerbose()) {
710 context_->GetDiagnostics()->Note(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 << "loading include path");
712 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700713
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700715 std::string error_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800716 std::unique_ptr<ResourceTable> include_static = LoadStaticLibrary(path, &error_str);
717 if (include_static) {
718 if (options_.package_type != PackageType::kStaticLib) {
719 // Can't include static libraries when not building a static library (they have no IDs
720 // assigned).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700721 context_->GetDiagnostics()->Error(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800722 DiagMessage(path) << "can't include static library when not building a static lib");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 return false;
724 }
725
726 // If we are using --no-static-lib-packages, we need to rename the
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800727 // package of this table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728 if (options_.no_static_lib_packages) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800729 // Since package names can differ, and multiple packages can exist in a ResourceTable,
730 // we place the requirement that all static libraries are built with the package
731 // ID 0x7f. So if one is not found, this is an error.
732 if (ResourceTablePackage* pkg = include_static->FindPackageById(kAppPackageId)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 pkg->name = context_->GetCompilationPackage();
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800734 } else {
735 context_->GetDiagnostics()->Error(DiagMessage(path)
736 << "no package with ID 0x7f found in static library");
737 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 }
739 }
740
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700741 context_->GetExternalSymbols()->AppendSource(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800742 util::make_unique<ResourceTableSymbolSource>(include_static.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800744 static_table_includes_.push_back(std::move(include_static));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700745
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700746 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 return false;
750 }
751
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700752 if (!asset_source->AddAssetPath(path)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800753 context_->GetDiagnostics()->Error(DiagMessage(path) << "failed to load include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 return false;
755 }
756 }
757
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800758 // Capture the shared libraries so that the final resource table can be properly flattened
759 // with support for shared libraries.
760 for (auto& entry : asset_source->GetAssignedPackageIds()) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800761 if (entry.first > kFrameworkPackageId && entry.first < kAppPackageId) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800762 final_table_.included_packages_[entry.first] = entry.second;
763 }
764 }
765
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700767 return true;
768 }
769
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800770 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700771 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800772 xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
773 if (manifest_el == nullptr) {
774 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775 }
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800776
777 AppInfo app_info;
778
779 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
780 diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>");
781 return {};
782 }
783
784 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
785 if (!package_attr) {
786 diag->Error(DiagMessage(xml_res->file.source)
787 << "<manifest> must have a 'package' attribute");
788 return {};
789 }
790 app_info.package = package_attr->value;
791
792 if (xml::Attribute* version_code_attr =
793 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
794 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_attr->value);
795 if (!maybe_code) {
796 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
797 << "invalid android:versionCode '" << version_code_attr->value << "'");
798 return {};
799 }
800 app_info.version_code = maybe_code.value();
801 }
802
803 if (xml::Attribute* revision_code_attr =
804 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
805 Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(revision_code_attr->value);
806 if (!maybe_code) {
807 diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
808 << "invalid android:revisionCode '" << revision_code_attr->value << "'");
809 return {};
810 }
811 app_info.revision_code = maybe_code.value();
812 }
813
814 if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
815 if (!split_name_attr->value.empty()) {
816 app_info.split_name = split_name_attr->value;
817 }
818 }
819
820 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
821 if (xml::Attribute* min_sdk =
822 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
823 app_info.min_sdk_version = min_sdk->value;
824 }
825 }
826 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 }
828
829 /**
Adam Lesinskib0c47ef2017-03-06 20:05:57 -0800830 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700831 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700832 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700834 bool VerifyNoExternalPackages() {
835 auto is_ext_package_func =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700836 [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700837 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
838 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839 };
840
841 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700842 for (const auto& package : final_table_.packages) {
843 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 // We have a package that is not related to the one we're building!
845 for (const auto& type : package->types) {
846 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700847 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700849 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700850 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700851 // for the 'android' package. This is due to legacy reasons.
852 if (ValueCast<Id>(config_value->value.get()) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700853 package->name == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700854 context_->GetDiagnostics()->Warn(
855 DiagMessage(config_value->value->GetSource())
856 << "generated id '" << res_name
857 << "' for external package '" << package->name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700858 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700859 context_->GetDiagnostics()->Error(
860 DiagMessage(config_value->value->GetSource())
861 << "defined resource '" << res_name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700862 << "' for external package '" << package->name << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700863 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700864 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700865 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700866 }
867 }
868 }
869 }
870
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 auto new_end_iter =
872 std::remove_if(final_table_.packages.begin(),
873 final_table_.packages.end(), is_ext_package_func);
874 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875 return !error;
876 }
877
878 /**
879 * Returns true if no IDs have been set, false otherwise.
880 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700881 bool VerifyNoIdsSet() {
882 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700883 for (const auto& type : package->types) {
884 if (type->id) {
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800885 context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID "
886 << StringPrintf("%02x", type->id.value())
887 << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700889 }
890
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700891 for (const auto& entry : type->entries) {
892 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700893 ResourceNameRef res_name(package->name, type->type, entry->name);
894 context_->GetDiagnostics()->Error(
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800895 DiagMessage() << "entry " << res_name << " has ID "
896 << StringPrintf("%02x", entry->id.value()) << " assigned");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700897 return false;
898 }
899 }
900 }
901 }
902 return true;
903 }
904
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700905 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
906 if (options_.output_to_directory) {
907 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700908 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700909 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700910 }
911 }
912
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700913 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700914 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800915 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700916 if (!flattener.Consume(context_, table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700917 context_->GetDiagnostics()->Error(DiagMessage() << "failed to flatten resource table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700918 return false;
919 }
920
Adam Lesinski06460ef2017-03-14 18:52:13 -0700921 io::BigBufferInputStream input_stream(&buffer);
922 return CopyInputStreamToArchive(&input_stream, "resources.arsc", ArchiveEntry::kAlign, writer,
923 context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700924 }
925
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700926 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700927 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
928 return CopyProtoToArchive(pb_table.get(), "resources.arsc.flat", 0, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700929 }
930
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700931 bool WriteJavaFile(ResourceTable* table,
932 const StringPiece& package_name_to_generate,
933 const StringPiece& out_package,
934 const JavaClassGeneratorOptions& java_options) {
935 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700936 return true;
937 }
938
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700939 std::string out_path = options_.generate_java_class_path.value();
940 file::AppendPath(&out_path, file::PackageToPath(out_package));
941 if (!file::mkdirs(out_path)) {
942 context_->GetDiagnostics()->Error(
943 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700944 return false;
945 }
946
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700947 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700948
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700949 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700950 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700951 context_->GetDiagnostics()->Error(
952 DiagMessage() << "failed writing to '" << out_path << "': "
953 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700954 return false;
955 }
956
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700957 JavaClassGenerator generator(context_, table, java_options);
958 if (!generator.Generate(package_name_to_generate, out_package, &fout)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700959 context_->GetDiagnostics()->Error(DiagMessage(out_path) << generator.getError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700960 return false;
961 }
962
963 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700964 context_->GetDiagnostics()->Error(
965 DiagMessage() << "failed writing to '" << out_path << "': "
966 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700967 }
968 return true;
969 }
970
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700971 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
972 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700973 return true;
974 }
975
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700976 std::unique_ptr<ClassDefinition> manifest_class =
977 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700978
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700979 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700980 // Something bad happened, but we already logged it, so exit.
981 return false;
982 }
983
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 // Empty Manifest class, no need to generate it.
986 return true;
987 }
988
989 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700990 for (const std::string& annotation : options_.javadoc_annotations) {
991 std::string proper_annotation = "@";
992 proper_annotation += annotation;
993 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700994 }
995
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700996 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700997
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700998 std::string out_path = options_.generate_java_class_path.value();
999 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001000
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001001 if (!file::mkdirs(out_path)) {
1002 context_->GetDiagnostics()->Error(
1003 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001004 return false;
1005 }
1006
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001007 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001008
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001009 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001011 context_->GetDiagnostics()->Error(
1012 DiagMessage() << "failed writing to '" << out_path << "': "
1013 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001014 return false;
1015 }
1016
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8,
1018 true, &fout)) {
1019 context_->GetDiagnostics()->Error(
1020 DiagMessage() << "failed writing to '" << out_path << "': "
1021 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001022 return false;
1023 }
1024 return true;
1025 }
1026
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 bool WriteProguardFile(const Maybe<std::string>& out,
1028 const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001029 if (!out) {
1030 return true;
1031 }
1032
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001033 const std::string& out_path = out.value();
1034 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001035 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001036 context_->GetDiagnostics()->Error(
1037 DiagMessage() << "failed to open '" << out_path << "': "
1038 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039 return false;
1040 }
1041
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001042 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001043 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001044 context_->GetDiagnostics()->Error(
1045 DiagMessage() << "failed writing to '" << out_path << "': "
1046 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001047 return false;
1048 }
1049 return true;
1050 }
1051
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001052 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1053 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001054 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001055 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001056 if (!collection) {
1057 return {};
1058 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001059 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001060 }
1061
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001062 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001063 io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001064 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001065 if (!file) {
1066 return {};
1067 }
1068
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001069 std::unique_ptr<io::IData> data = file->OpenAsData();
1070 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1071 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001072 }
1073
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001074 bool MergeStaticLibrary(const std::string& input, bool override) {
1075 if (context_->IsVerbose()) {
1076 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001077 << "merging static library " << input);
1078 }
1079
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001080 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001081 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001082 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001083 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001084 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001085 return false;
1086 }
1087
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001088 std::unique_ptr<ResourceTable> table = LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001089 if (!table) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001090 context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091 return false;
1092 }
1093
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001094 ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001095 if (!pkg) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001096 context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001097 return false;
1098 }
1099
1100 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001101 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001102 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001103 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001104
1105 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001106 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001107 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001108 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 }
1110
1111 pkg->name = "";
1112 if (override) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001113 result = table_merger_->MergeOverlay(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001114 } else {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001115 result = table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001116 }
1117
1118 } else {
1119 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001120 // preserved and resource names are mangled.
1121 result = table_merger_->MergeAndMangle(Source(input), pkg->name,
1122 table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001123 }
1124
1125 if (!result) {
1126 return false;
1127 }
1128
1129 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001130 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001131 return true;
1132 }
1133
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001134 bool MergeResourceTable(io::IFile* file, bool override) {
1135 if (context_->IsVerbose()) {
1136 context_->GetDiagnostics()->Note(
1137 DiagMessage() << "merging resource table " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001138 }
1139
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001140 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001141 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001142 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001143 << "failed to open file");
1144 return false;
1145 }
1146
1147 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001148 LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1149 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001150 if (!table) {
1151 return false;
1152 }
1153
1154 bool result = false;
1155 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001156 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001157 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001158 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001159 }
1160 return result;
1161 }
1162
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001163 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001164 bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001165 if (context_->IsVerbose()) {
1166 context_->GetDiagnostics()->Note(
1167 DiagMessage() << "merging '" << file_desc->name
1168 << "' from compiled file " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001169 }
1170
1171 bool result = false;
1172 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001173 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001174 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001175 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001176 }
1177
1178 if (!result) {
1179 return false;
1180 }
1181
1182 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001183 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1184 if (exported_symbol.name.package.empty()) {
1185 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001186 }
1187
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001188 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001189
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001190 Maybe<ResourceName> mangled_name =
1191 context_->GetNameMangler()->MangleName(exported_symbol.name);
1192 if (mangled_name) {
1193 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001194 }
1195
1196 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001197 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1198 bool result = final_table_.AddResourceAllowMangled(
1199 res_name, ConfigDescription::DefaultConfig(), std::string(),
1200 std::move(id), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001201 if (!result) {
1202 return false;
1203 }
1204 }
1205 return true;
1206 }
1207
1208 /**
1209 * Takes a path to load as a ZIP file and merges the files within into the
1210 * master ResourceTable.
1211 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001212 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001213 *
1214 * An io::IFileCollection is created from the ZIP file and added to the set of
1215 * io::IFileCollections that are open.
1216 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001217 bool MergeArchive(const std::string& input, bool override) {
1218 if (context_->IsVerbose()) {
1219 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001220 << input);
1221 }
1222
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001223 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001224 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001225 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001226 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001227 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001228 return false;
1229 }
1230
1231 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001232 for (auto iter = collection->Iterator(); iter->HasNext();) {
1233 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001234 error = true;
1235 }
1236 }
1237
1238 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001239 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001240 return !error;
1241 }
1242
1243 /**
1244 * Takes a path to load and merge into the master ResourceTable. If override
1245 * is true,
1246 * conflicting resources are allowed to override each other, in order of last
1247 * seen.
1248 *
1249 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1250 * as ZIP archive
1251 * and the files within are merged individually.
1252 *
1253 * Otherwise the files is processed on its own.
1254 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001255 bool MergePath(const std::string& path, bool override) {
1256 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1257 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1258 return MergeArchive(path, override);
1259 } else if (util::EndsWith(path, ".apk")) {
1260 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001261 }
1262
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001263 io::IFile* file = file_collection_->InsertFile(path);
1264 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001265 }
1266
1267 /**
1268 * Takes a file to load and merge into the master ResourceTable. If override
1269 * is true,
1270 * conflicting resources are allowed to override each other, in order of last
1271 * seen.
1272 *
1273 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1274 * merged into the
1275 * master ResourceTable. If the file ends with .flat, then it is treated like
1276 * a compiled file
1277 * and the header data is read and merged into the final ResourceTable.
1278 *
1279 * All other file types are ignored. This is because these files could be
1280 * coming from a zip,
1281 * where we could have other files like classes.dex.
1282 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001283 bool MergeFile(io::IFile* file, bool override) {
1284 const Source& src = file->GetSource();
1285 if (util::EndsWith(src.path, ".arsc.flat")) {
1286 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001287
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001288 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001289 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001290 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001291 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001292 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001293 return false;
1294 }
1295
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001296 CompiledFileInputStream input_stream(data->data(), data->size());
1297 uint32_t num_files = 0;
1298 if (!input_stream.ReadLittleEndian32(&num_files)) {
1299 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001300 << "failed read num files");
1301 return false;
1302 }
1303
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001304 for (uint32_t i = 0; i < num_files; i++) {
1305 pb::CompiledFile compiled_file;
1306 if (!input_stream.ReadCompiledFile(&compiled_file)) {
1307 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001308 DiagMessage(src) << "failed to read compiled file header");
1309 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001310 }
1311
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001312 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001313 if (!input_stream.ReadDataMetaData(&offset, &len)) {
1314 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001315 << "failed to read data meta data");
1316 return false;
1317 }
1318
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001319 std::unique_ptr<ResourceFile> resource_file =
1320 DeserializeCompiledFileFromPb(compiled_file, file->GetSource(),
1321 context_->GetDiagnostics());
1322 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001323 return false;
1324 }
1325
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001326 if (!MergeCompiledFile(file->CreateFileSegment(offset, len),
1327 resource_file.get(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001328 return false;
1329 }
1330 }
1331 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001332 } else if (util::EndsWith(src.path, ".xml") ||
1333 util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001334 // Since AAPT compiles these file types and appends .flat to them, seeing
1335 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001336 const StringPiece file_type =
1337 util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1338 context_->GetDiagnostics()->Error(DiagMessage(src)
1339 << "uncompiled " << file_type
Adam Lesinski6a396c12016-10-20 14:38:23 -07001340 << " file passed as argument. Must be "
1341 "compiled first into .flat file.");
1342 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001343 }
1344
1345 // Ignore non .flat files. This could be classes.dex or something else that
1346 // happens
1347 // to be in an archive.
1348 return true;
1349 }
1350
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001351 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
1352 const SplitConstraints& constraints) {
1353 std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001354
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001355 std::unique_ptr<xml::Namespace> namespace_android = util::make_unique<xml::Namespace>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001356 namespace_android->namespace_uri = xml::kSchemaAndroid;
1357 namespace_android->namespace_prefix = "android";
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001358
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001359 std::unique_ptr<xml::Element> manifest_el = util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360 manifest_el->name = "manifest";
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001361 manifest_el->attributes.push_back(xml::Attribute{"", "package", app_info.package});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001362
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001363 if (app_info.version_code) {
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001364 manifest_el->attributes.push_back(xml::Attribute{
1365 xml::kSchemaAndroid, "versionCode", std::to_string(app_info.version_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001366 }
1367
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001368 if (app_info.revision_code) {
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001369 manifest_el->attributes.push_back(xml::Attribute{
1370 xml::kSchemaAndroid, "revisionCode", std::to_string(app_info.revision_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001371 }
1372
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001373 std::stringstream split_name;
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001374 if (app_info.split_name) {
1375 split_name << app_info.split_name.value() << ".";
1376 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001377 split_name << "config." << util::Joiner(constraints.configs, "_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001379 manifest_el->attributes.push_back(xml::Attribute{"", "split", split_name.str()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001380
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001381 if (app_info.split_name) {
1382 manifest_el->attributes.push_back(
1383 xml::Attribute{"", "configForSplit", app_info.split_name.value()});
1384 }
1385
1386 std::unique_ptr<xml::Element> application_el = util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001387 application_el->name = "application";
Adam Lesinskib0c47ef2017-03-06 20:05:57 -08001388 application_el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "hasCode", "false"});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001389
Adam Lesinskie343eb12016-10-27 16:31:58 -07001390 manifest_el->AppendChild(std::move(application_el));
1391 namespace_android->AppendChild(std::move(manifest_el));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001392 doc->root = std::move(namespace_android);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001393 return doc;
1394 }
1395
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001396 bool CopyAssetsDirsToApk(IArchiveWriter* writer) {
1397 std::map<std::string, std::unique_ptr<io::RegularFile>> merged_assets;
1398 for (const std::string& assets_dir : options_.assets_dirs) {
1399 Maybe<std::vector<std::string>> files =
1400 file::FindFiles(assets_dir, context_->GetDiagnostics(), nullptr);
1401 if (!files) {
1402 return false;
1403 }
1404
1405 for (const std::string& file : files.value()) {
1406 std::string full_key = "assets/" + file;
1407 std::string full_path = assets_dir;
1408 file::AppendPath(&full_path, file);
1409
1410 auto iter = merged_assets.find(full_key);
1411 if (iter == merged_assets.end()) {
1412 merged_assets.emplace(std::move(full_key),
1413 util::make_unique<io::RegularFile>(Source(std::move(full_path))));
1414 } else if (context_->IsVerbose()) {
1415 context_->GetDiagnostics()->Warn(DiagMessage(iter->second->GetSource())
1416 << "asset file overrides '" << full_path << "'");
1417 }
1418 }
1419 }
1420
1421 for (auto& entry : merged_assets) {
1422 uint32_t compression_flags = ArchiveEntry::kCompress;
1423 std::string extension = file::GetExtension(entry.first).to_string();
1424 if (options_.extensions_to_not_compress.count(extension) > 0) {
1425 compression_flags = 0u;
1426 }
1427
1428 if (!CopyFileToArchive(entry.second.get(), entry.first, compression_flags, writer,
1429 context_)) {
1430 return false;
1431 }
1432 }
1433 return true;
1434 }
1435
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001436 /**
1437 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001438 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001440 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001441 xml::XmlResource* manifest, ResourceTable* table) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001442 const bool keep_raw_values = options_.package_type == PackageType::kStaticLib;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001443 bool result = FlattenXml(manifest, "AndroidManifest.xml", {},
1444 keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001445 if (!result) {
1446 return false;
1447 }
1448
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001449 ResourceFileFlattenerOptions file_flattener_options;
1450 file_flattener_options.keep_raw_values = keep_raw_values;
1451 file_flattener_options.do_not_compress_anything =
1452 options_.do_not_compress_anything;
1453 file_flattener_options.extensions_to_not_compress =
1454 options_.extensions_to_not_compress;
1455 file_flattener_options.no_auto_version = options_.no_auto_version;
1456 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001457 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001458 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1459 file_flattener_options.update_proguard_spec =
1460 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001461
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001462 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001463
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001464 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001465 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001466 return false;
1467 }
1468
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001469 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001470 if (!FlattenTableToPb(table, writer)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001471 return false;
1472 }
1473 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001474 if (!FlattenTable(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001475 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001476 return false;
1477 }
1478 }
1479 return true;
1480 }
1481
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001482 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001483 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001484 std::unique_ptr<xml::XmlResource> manifest_xml =
1485 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1486 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001487 return 1;
1488 }
1489
1490 // First extract the Package name without modifying it (via
1491 // --rename-manifest-package).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001492 if (Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1493 manifest_xml.get(), context_->GetDiagnostics())) {
1494 const AppInfo& app_info = maybe_app_info.value();
1495 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001496 }
1497
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001498 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1499 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001500 return 1;
1501 }
1502
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001503 Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1504 manifest_xml.get(), context_->GetDiagnostics());
1505 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001506 return 1;
1507 }
1508
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001509 const AppInfo& app_info = maybe_app_info.value();
1510 if (app_info.min_sdk_version) {
1511 if (Maybe<int> maybe_min_sdk_version = ResourceUtils::ParseSdkVersion(
1512 app_info.min_sdk_version.value())) {
1513 context_->SetMinSdkVersion(maybe_min_sdk_version.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001514 }
1515 }
1516
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001517 context_->SetNameManglerPolicy(
1518 NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001519
1520 // Override the package ID when it is "android".
1521 if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001522 context_->SetPackageId(0x01);
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001523
1524 // Verify we're building a regular app.
1525 if (options_.package_type != PackageType::kApp) {
1526 context_->GetDiagnostics()->Error(
1527 DiagMessage() << "package 'android' can only be built as a regular app");
1528 return 1;
1529 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001530 }
1531
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001532 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001533 return 1;
1534 }
1535
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001536 TableMergerOptions table_merger_options;
1537 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
1538 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_,
1539 table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001540
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001541 if (context_->IsVerbose()) {
1542 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001543 << StringPrintf("linking package '%s' using package ID %02x",
1544 context_->GetCompilationPackage().data(),
1545 context_->GetPackageId()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001546 }
1547
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001548 for (const std::string& input : input_files) {
1549 if (!MergePath(input, false)) {
1550 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001551 << "failed parsing input");
1552 return 1;
1553 }
1554 }
1555
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001556 for (const std::string& input : options_.overlay_files) {
1557 if (!MergePath(input, true)) {
1558 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001559 << "failed parsing overlays");
1560 return 1;
1561 }
1562 }
1563
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001564 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001565 return 1;
1566 }
1567
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001568 if (options_.package_type != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001569 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001570 if (!mover.Consume(context_, &final_table_)) {
1571 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001572 DiagMessage() << "failed moving private attributes");
1573 return 1;
1574 }
1575
1576 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001577 IdAssigner id_assigner(&options_.stable_id_map);
1578 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001579 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001580 return 1;
1581 }
1582
1583 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001584 if (options_.resource_id_map_path) {
1585 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001586 for (auto& type : package->types) {
1587 for (auto& entry : type->entries) {
1588 ResourceName name(package->name, type->type, entry->name);
1589 // The IDs are guaranteed to exist.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001590 options_.stable_id_map[std::move(name)] = ResourceId(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001591 package->id.value(), type->id.value(), entry->id.value());
1592 }
1593 }
1594 }
1595
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 if (!WriteStableIdMapToPath(context_->GetDiagnostics(),
1597 options_.stable_id_map,
1598 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001599 return 1;
1600 }
1601 }
1602 } else {
1603 // Static libs are merged with other apps, and ID collisions are bad, so
1604 // verify that
1605 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001606 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001607 return 1;
1608 }
1609 }
1610
1611 // Add the names to mangle based on our source merge earlier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001612 context_->SetNameManglerPolicy(NameManglerPolicy{
1613 context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001614
1615 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001616 context_->GetExternalSymbols()->PrependSource(
1617 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001618
1619 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001620 if (!linker.Consume(context_, &final_table_)) {
1621 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001622 << "failed linking references");
1623 return 1;
1624 }
1625
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001626 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001627 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001628 context_->GetDiagnostics()->Warn(DiagMessage()
1629 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001630 }
1631 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001632 ProductFilter product_filter(options_.products);
1633 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001634 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001635 return 1;
1636 }
1637 }
1638
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001639 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001640 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001641 if (!versioner.Consume(context_, &final_table_)) {
1642 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001643 << "failed versioning styles");
1644 return 1;
1645 }
1646 }
1647
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001648 if (options_.package_type != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001649 if (context_->IsVerbose()) {
1650 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001651 DiagMessage() << "collapsing resource versions for minimum SDK "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001652 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001653 }
1654
1655 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001656 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001657 return 1;
1658 }
1659 }
1660
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001661 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001662 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001663 if (!deduper.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001664 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001665 return 1;
1666 }
1667 }
1668
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001669 proguard::KeepSet proguard_keep_set;
1670 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001671
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001672 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001673 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001674 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001675 context_->GetDiagnostics()->Warn(DiagMessage()
1676 << "can't strip resources when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001677 }
1678 } else {
1679 // Adjust the SplitConstraints so that their SDK version is stripped if it
1680 // is less
1681 // than or equal to the minSdk. Otherwise the resources that have had
1682 // their SDK version
1683 // stripped due to minSdk won't ever match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001684 std::vector<SplitConstraints> adjusted_constraints_list;
1685 adjusted_constraints_list.reserve(options_.split_constraints.size());
1686 for (const SplitConstraints& constraints : options_.split_constraints) {
1687 SplitConstraints adjusted_constraints;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001688 for (const ConfigDescription& config : constraints.configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001689 if (config.sdkVersion <= context_->GetMinSdkVersion()) {
1690 adjusted_constraints.configs.insert(config.CopyWithoutSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001691 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001692 adjusted_constraints.configs.insert(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001693 }
1694 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001695 adjusted_constraints_list.push_back(std::move(adjusted_constraints));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001696 }
1697
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001698 TableSplitter table_splitter(adjusted_constraints_list,
1699 options_.table_splitter_options);
1700 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001701 return 1;
1702 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001703 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001704
1705 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001706 auto path_iter = options_.split_paths.begin();
1707 auto split_constraints_iter = adjusted_constraints_list.begin();
1708 for (std::unique_ptr<ResourceTable>& split_table :
1709 table_splitter.splits()) {
1710 if (context_->IsVerbose()) {
1711 context_->GetDiagnostics()->Note(
1712 DiagMessage(*path_iter)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001713 << "generating split with configurations '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001714 << util::Joiner(split_constraints_iter->configs, ", ") << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001715 }
1716
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001717 std::unique_ptr<IArchiveWriter> archive_writer =
1718 MakeArchiveWriter(*path_iter);
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 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001726 std::unique_ptr<xml::XmlResource> split_manifest =
1727 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001728
1729 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001730 if (!linker.Consume(context_, split_manifest.get())) {
1731 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001732 DiagMessage() << "failed to create Split AndroidManifest.xml");
1733 return 1;
1734 }
1735
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001736 if (!WriteApk(archive_writer.get(), &proguard_keep_set,
1737 split_manifest.get(), split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001738 return 1;
1739 }
1740
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001741 ++path_iter;
1742 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001743 }
1744 }
1745
1746 // Start writing the base APK.
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001747 std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(options_.output_path);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001748 if (!archive_writer) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001749 context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001750 return 1;
1751 }
1752
1753 bool error = false;
1754 {
1755 // AndroidManifest.xml has no resource name, but the CallSite is built
1756 // from the name
1757 // (aka, which package the AndroidManifest.xml is coming from).
1758 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001759 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001760
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001761 XmlReferenceLinker manifest_linker;
1762 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1763 if (options_.generate_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001764 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1765 manifest_xml.get(), &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001766 error = true;
1767 }
1768
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001769 if (options_.generate_main_dex_proguard_rules_path &&
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001770 !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path),
1771 manifest_xml.get(),
1772 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001773 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001774 }
1775
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001776 if (options_.generate_java_class_path) {
1777 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001778 error = true;
1779 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001780 }
1781
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001782 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001783 // PackageParser will fail if URIs are removed from
1784 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001785 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1786 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001787 error = true;
1788 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001789 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001790 } else {
1791 error = true;
1792 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001793 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001794
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001795 if (error) {
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001796 context_->GetDiagnostics()->Error(DiagMessage() << "failed processing manifest");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001797 return 1;
1798 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001799
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001800 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(), &final_table_)) {
1801 return 1;
1802 }
1803
1804 if (!CopyAssetsDirsToApk(archive_writer.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001805 return 1;
1806 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001807
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001808 if (options_.generate_java_class_path) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001809 // The set of packages whose R class to call in the main classes
1810 // onResourcesLoaded callback.
1811 std::vector<std::string> packages_to_callback;
1812
1813 JavaClassGeneratorOptions template_options;
1814 template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1815 template_options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001816
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001817 if (options_.package_type == PackageType::kStaticLib || options_.generate_non_final_ids) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001818 template_options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001819 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001820
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001821 if (options_.package_type == PackageType::kSharedLib) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001822 template_options.use_final = false;
1823 template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{};
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001824 }
1825
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001826 const StringPiece actual_package = context_->GetCompilationPackage();
1827 StringPiece output_package = context_->GetCompilationPackage();
1828 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001829 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001830 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001831 }
1832
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001833 // Generate the private symbols if required.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001834 if (options_.private_symbols) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001835 packages_to_callback.push_back(options_.private_symbols.value());
1836
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001837 // If we defined a private symbols package, we only emit Public symbols
1838 // to the original package, and private and public symbols to the
1839 // private package.
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001840 JavaClassGeneratorOptions options = template_options;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001841 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001842 if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001843 options)) {
1844 return 1;
1845 }
1846 }
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001847
1848 // Generate all the symbols for all extra packages.
1849 for (const std::string& extra_package : options_.extra_java_packages) {
1850 packages_to_callback.push_back(extra_package);
1851
1852 JavaClassGeneratorOptions options = template_options;
1853 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
1854 if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) {
1855 return 1;
1856 }
1857 }
1858
1859 // Generate the main public R class.
1860 JavaClassGeneratorOptions options = template_options;
1861
1862 // Only generate public symbols if we have a private package.
1863 if (options_.private_symbols) {
1864 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
1865 }
1866
1867 if (options.rewrite_callback_options) {
1868 options.rewrite_callback_options.value().packages_to_callback =
1869 std::move(packages_to_callback);
1870 }
1871
1872 if (!WriteJavaFile(&final_table_, actual_package, output_package, options)) {
1873 return 1;
1874 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001875 }
1876
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -08001877 if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001878 return 1;
1879 }
1880
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001881 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1882 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001883 return 1;
1884 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001885 return 0;
1886 }
1887
1888 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001889 LinkOptions options_;
1890 LinkContext* context_;
1891 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001892
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001893 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001894
1895 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001896 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001897
1898 // A vector of IFileCollections. This is mainly here to keep ownership of the
1899 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001900 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001901
1902 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001903 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001904 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001905
1906 // The set of shared libraries being used, mapping their assigned package ID to package name.
1907 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001908};
1909
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001910int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001911 LinkContext context;
1912 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001913 std::vector<std::string> overlay_arg_list;
1914 std::vector<std::string> extra_java_packages;
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001915 Maybe<std::string> package_id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001916 Maybe<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001917 Maybe<std::string> preferred_density;
1918 Maybe<std::string> product_list;
1919 bool legacy_x_flag = false;
1920 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001921 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001922 bool shared_lib = false;
1923 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001924 Maybe<std::string> stable_id_file_path;
1925 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001926 Flags flags =
1927 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001928 .RequiredFlag("-o", "Output path", &options.output_path)
1929 .RequiredFlag("--manifest", "Path to the Android manifest to build",
1930 &options.manifest_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001931 .OptionalFlagList("-I", "Adds an Android APK to link against", &options.include_paths)
Adam Lesinskib39ad7c2017-03-13 11:40:48 -07001932 .OptionalFlagList("-A",
1933 "An assets directory to include in the APK. These are unprocessed.",
1934 &options.assets_dirs)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001935 .OptionalFlagList("-R",
1936 "Compilation unit to link, using `overlay` semantics.\n"
1937 "The last conflicting resource given takes precedence.",
1938 &overlay_arg_list)
Adam Lesinskif34b6f42017-03-03 16:33:26 -08001939 .OptionalFlag("--package-id",
1940 "Specify the package ID to use for this app. Must be greater or equal to\n"
1941 "0x7f and can't be used with --static-lib or --shared-lib.",
1942 &package_id)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001943 .OptionalFlag("--java", "Directory in which to generate R.java",
1944 &options.generate_java_class_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001945 .OptionalFlag("--proguard", "Output file for generated Proguard rules",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001946 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001947 .OptionalFlag("--proguard-main-dex",
1948 "Output file for generated Proguard rules for the main dex",
1949 &options.generate_main_dex_proguard_rules_path)
1950 .OptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001951 &options.no_auto_version)
1952 .OptionalSwitch("--no-version-vectors",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001953 "Disables automatic versioning of vector drawables. "
1954 "Use this only\n"
1955 "when building with vector drawable support library",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001956 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001957 .OptionalSwitch("--no-version-transitions",
1958 "Disables automatic versioning of transition resources. "
1959 "Use this only\n"
1960 "when building with transition support library",
1961 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001962 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001963 "Disables automatic deduping of resources with\n"
1964 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001965 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001966 .OptionalSwitch("--enable-sparse-encoding",
1967 "Enables encoding sparse entries using a binary search tree.\n"
1968 "This decreases APK size at the cost of resource retrieval performance.",
1969 &options.table_flattener_options.use_sparse_entries)
1970 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01",
1971 &legacy_x_flag)
1972 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001973 &require_localization)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001974 .OptionalFlag("-c",
1975 "Comma separated list of configurations to include. The default\n"
1976 "is all configurations",
1977 &configs)
1978 .OptionalFlag("--preferred-density",
1979 "Selects the closest matching density and strips out all others.",
1980 &preferred_density)
1981 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001982 .OptionalSwitch("--output-to-dir",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001983 "Outputs the APK contents to a directory specified "
1984 "by -o",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001985 &options.output_to_directory)
1986 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001987 "Removes XML namespace prefix and URI "
1988 "information from AndroidManifest.xml\nand XML "
1989 "binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001990 &options.no_xml_namespaces)
1991 .OptionalFlag("--min-sdk-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001992 "Default minimum SDK version to use for "
1993 "AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001994 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001995 .OptionalFlag("--target-sdk-version",
1996 "Default target SDK version to use for "
1997 "AndroidManifest.xml",
1998 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001999 .OptionalFlag("--version-code",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002000 "Version code (integer) to inject into the "
2001 "AndroidManifest.xml if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002002 &options.manifest_fixer_options.version_code_default)
2003 .OptionalFlag("--version-name",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002004 "Version name to inject into the AndroidManifest.xml "
2005 "if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002006 &options.manifest_fixer_options.version_name_default)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002007 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library", &shared_lib)
2008 .OptionalSwitch("--static-lib", "Generate a static Android library", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002009 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002010 "Merge all library resources under the app's package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002011 &options.no_static_lib_packages)
2012 .OptionalSwitch("--non-final-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002013 "Generates R.java without the final modifier.\n"
2014 "This is implied when --static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002015 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002016 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002017 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002018 .OptionalFlag("--emit-ids",
2019 "Emit a file at the given path with a list of name to ID\n"
2020 "mappings, suitable for use with --stable-ids.",
2021 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002022 .OptionalFlag("--private-symbols",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002023 "Package name to use when generating R.java for "
2024 "private symbols.\n"
2025 "If not specified, public and private symbols will use "
2026 "the application's "
2027 "package name",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002028 &options.private_symbols)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002029 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002030 &options.custom_java_package)
2031 .OptionalFlagList("--extra-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002032 "Generate the same R.java but with different "
2033 "package names",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002034 &extra_java_packages)
2035 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002036 "Adds a JavaDoc annotation to all "
Adam Lesinskid0f116b2016-07-08 15:00:32 -07002037 "generated Java classes",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002038 &options.javadoc_annotations)
2039 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002040 "Allows the addition of new resources in "
2041 "overlays without <add-resource> tags",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002042 &options.auto_add_overlay)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002043 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002044 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002045 .OptionalFlag("--rename-instrumentation-target-package",
2046 "Changes the name of the target package for instrumentation. "
2047 "Most useful "
2048 "when used\nin conjunction with --rename-manifest-package",
2049 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002050 .OptionalFlagList("-0", "File extensions not to compress",
2051 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002052 .OptionalFlagList("--split",
2053 "Split resources matching a set of configs out to a "
2054 "Split APK.\nSyntax: path/to/output.apk:<config>[,<config>[...]]",
2055 &split_args)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002056 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002057
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002058 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002059 return 1;
2060 }
2061
2062 // Expand all argument-files passed into the command line. These start with
2063 // '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002064 std::vector<std::string> arg_list;
2065 for (const std::string& arg : flags.GetArgs()) {
2066 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002067 const std::string path = arg.substr(1, arg.size() - 1);
2068 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002069 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
2070 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002071 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002072 }
2073 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002074 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002075 }
2076 }
2077
2078 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002079 for (const std::string& arg : overlay_arg_list) {
2080 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002081 const std::string path = arg.substr(1, arg.size() - 1);
2082 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002083 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
2084 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002085 return 1;
2086 }
2087 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002088 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002089 }
2090 }
2091
2092 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002093 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002094 }
2095
Adam Lesinskif34b6f42017-03-03 16:33:26 -08002096 if (shared_lib && static_lib) {
2097 context.GetDiagnostics()->Error(DiagMessage()
2098 << "only one of --shared-lib and --static-lib can be defined");
2099 return 1;
2100 }
2101
2102 if (shared_lib) {
2103 options.package_type = PackageType::kSharedLib;
2104 context.SetPackageId(0x00);
2105 } else if (static_lib) {
2106 options.package_type = PackageType::kStaticLib;
2107 context.SetPackageId(kAppPackageId);
2108 } else {
2109 options.package_type = PackageType::kApp;
2110 context.SetPackageId(kAppPackageId);
2111 }
2112
2113 if (package_id) {
2114 if (options.package_type != PackageType::kApp) {
2115 context.GetDiagnostics()->Error(
2116 DiagMessage() << "can't specify --package-id when not building a regular app");
2117 return 1;
2118 }
2119
2120 const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id.value());
2121 if (!maybe_package_id_int) {
2122 context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id.value()
2123 << "' is not a valid integer");
2124 return 1;
2125 }
2126
2127 const uint32_t package_id_int = maybe_package_id_int.value();
2128 if (package_id_int < kAppPackageId || package_id_int > std::numeric_limits<uint8_t>::max()) {
2129 context.GetDiagnostics()->Error(
2130 DiagMessage() << StringPrintf(
2131 "invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int));
2132 return 1;
2133 }
2134 context.SetPackageId(static_cast<uint8_t>(package_id_int));
2135 }
2136
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002137 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002138 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002139 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002140 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002141 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002142 }
2143 }
2144
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002145 if (product_list) {
2146 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002147 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002148 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002149 }
2150 }
2151 }
2152
2153 AxisConfigFilter filter;
2154 if (configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002155 for (const StringPiece& config_str : util::Tokenize(configs.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002156 ConfigDescription config;
2157 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002158 if (lv.InitFromFilterString(config_str)) {
2159 lv.WriteTo(&config);
2160 } else if (!ConfigDescription::Parse(config_str, &config)) {
2161 context.GetDiagnostics()->Error(DiagMessage() << "invalid config '"
2162 << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002163 << "' for -c option");
2164 return 1;
2165 }
2166
2167 if (config.density != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002168 context.GetDiagnostics()->Warn(DiagMessage() << "ignoring density '"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002169 << config
2170 << "' for -c option");
2171 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002172 filter.AddConfig(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002173 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002174 }
2175
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002176 options.table_splitter_options.config_filter = &filter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002177 }
2178
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002179 if (preferred_density) {
2180 ConfigDescription preferred_density_config;
2181 if (!ConfigDescription::Parse(preferred_density.value(),
2182 &preferred_density_config)) {
2183 context.GetDiagnostics()->Error(
2184 DiagMessage() << "invalid density '" << preferred_density.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002185 << "' for --preferred-density option");
2186 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002187 }
2188
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002189 // Clear the version that can be automatically added.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002190 preferred_density_config.sdkVersion = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002191
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002192 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002193 ConfigDescription::CONFIG_DENSITY) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002194 context.GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002195 DiagMessage() << "invalid preferred density '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002196 << preferred_density.value() << "'. "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002197 << "Preferred density must only be a density value");
2198 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002199 }
Pierre Lecesne672384b2017-02-06 10:29:02 +00002200 options.table_splitter_options.preferred_densities.push_back(preferred_density_config.density);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002201 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002202
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002203 if (options.package_type != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002204 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2205 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002206 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002207 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002208 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002209
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002210 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002211 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002212 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2213 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2214 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2215 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2216
2217 // Parse the split parameters.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002218 for (const std::string& split_arg : split_args) {
2219 options.split_paths.push_back({});
2220 options.split_constraints.push_back({});
2221 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(),
2222 &options.split_paths.back(),
2223 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002224 return 1;
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002225 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002226 }
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002227
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002228 // Turn off auto versioning for static-libs.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002229 if (options.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002230 options.no_auto_version = true;
2231 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002232 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002233 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002234
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002235 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002236 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002237}
2238
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002239} // namespace aapt