blob: 016246131f5fe924e4cd289f64054873b89d9d8a [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include <sys/stat.h>
18
19#include <fstream>
20#include <queue>
21#include <unordered_map>
22#include <vector>
23
24#include "android-base/errors.h"
25#include "android-base/file.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "google/protobuf/io/coded_stream.h"
28
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "AppInfo.h"
30#include "Debug.h"
31#include "Flags.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080032#include "Locale.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033#include "NameMangler.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080034#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035#include "compile/IdAssigner.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080036#include "filter/ConfigFilter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037#include "flatten/Archive.h"
38#include "flatten/TableFlattener.h"
39#include "flatten/XmlFlattener.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080040#include "io/FileSystem.h"
41#include "io/ZipArchive.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070042#include "java/JavaClassGenerator.h"
43#include "java/ManifestClassGenerator.h"
44#include "java/ProguardRules.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045#include "link/Linkers.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046#include "link/ManifestFixer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080047#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048#include "link/TableMerger.h"
49#include "process/IResourceTableConsumer.h"
50#include "process/SymbolTable.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080051#include "proto/ProtoSerialize.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080052#include "split/TableSplitter.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053#include "unflatten/BinaryResourceParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054#include "util/Files.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080055#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056
Adam Lesinskid5083f62017-01-16 15:07:21 -080057using android::StringPiece;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070059
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060namespace aapt {
61
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080062// The type of package to build.
63enum class PackageType {
64 kApp,
65 kSharedLib,
66 kStaticLib,
67};
68
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069struct LinkOptions {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080070 PackageType package_type = PackageType::kApp;
71
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 std::string output_path;
73 std::string manifest_path;
74 std::vector<std::string> include_paths;
75 std::vector<std::string> overlay_files;
76 bool output_to_directory = false;
77 bool auto_add_overlay = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -070078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 // Java/Proguard options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 Maybe<std::string> generate_java_class_path;
81 Maybe<std::string> custom_java_package;
82 std::set<std::string> extra_java_packages;
83 Maybe<std::string> generate_proguard_rules_path;
84 Maybe<std::string> generate_main_dex_proguard_rules_path;
85 bool generate_non_final_ids = false;
86 std::vector<std::string> javadoc_annotations;
87 Maybe<std::string> private_symbols;
Adam Lesinski36c73a52016-08-11 13:39:24 -070088
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 // Optimizations/features.
90 bool no_auto_version = false;
91 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +090092 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 bool no_resource_deduping = false;
94 bool no_xml_namespaces = false;
95 bool do_not_compress_anything = false;
96 std::unordered_set<std::string> extensions_to_not_compress;
97
98 // Static lib options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 bool no_static_lib_packages = false;
100
101 // AndroidManifest.xml massaging options.
102 ManifestFixerOptions manifest_fixer_options;
103
104 // Products to use/filter on.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 std::unordered_set<std::string> products;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700106
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800107 // Flattening options.
108 TableFlattenerOptions table_flattener_options;
109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 // Split APK options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 TableSplitterOptions table_splitter_options;
112 std::vector<SplitConstraints> split_constraints;
113 std::vector<std::string> split_paths;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 // Stable ID options.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 std::unordered_map<ResourceName, ResourceId> stable_id_map;
117 Maybe<std::string> resource_id_map_path;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118};
119
Adam Lesinski64587af2016-02-18 18:33:06 -0800120class LinkContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 public:
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800122 LinkContext() : name_mangler_({}), symbols_(&name_mangler_) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 NameMangler* GetNameMangler() override { return &name_mangler_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
129 name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 const std::string& GetCompilationPackage() override {
133 return compilation_package_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 void SetCompilationPackage(const StringPiece& package_name) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800137 compilation_package_ = package_name.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 uint8_t GetPackageId() override { return package_id_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 void SetPackageId(uint8_t id) { package_id_ = id; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 SymbolTable* GetExternalSymbols() override { return &symbols_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 bool IsVerbose() override { return verbose_; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski64587af2016-02-18 18:33:06 -0800149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 int GetMinSdkVersion() override { return min_sdk_version_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 void SetMinSdkVersion(int minSdk) { min_sdk_version_ = minSdk; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700153
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 DISALLOW_COPY_AND_ASSIGN(LinkContext);
156
157 StdErrDiagnostics diagnostics_;
158 NameMangler name_mangler_;
159 std::string compilation_package_;
160 uint8_t package_id_ = 0x0;
161 SymbolTable symbols_;
162 bool verbose_ = false;
163 int min_sdk_version_ = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164};
165
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166static bool CopyFileToArchive(io::IFile* file, const std::string& out_path,
167 uint32_t compression_flags,
168 IArchiveWriter* writer, IAaptContext* context) {
169 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 context->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 << "failed to open file");
Adam Lesinski355f2852016-02-13 20:26:45 -0800173 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 }
175
176 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data->data());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 const size_t buffer_size = data->size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 if (context->IsVerbose()) {
180 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 << " to archive");
182 }
183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 if (writer->StartEntry(out_path, compression_flags)) {
185 if (writer->WriteEntry(buffer, buffer_size)) {
186 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 return true;
188 }
189 }
190 }
191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 context->GetDiagnostics()->Error(DiagMessage() << "failed to write file "
193 << out_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800195}
196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197static bool FlattenXml(xml::XmlResource* xml_res, const StringPiece& path,
198 Maybe<size_t> max_sdk_level, bool keep_raw_values,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 IArchiveWriter* writer, IAaptContext* context) {
200 BigBuffer buffer(1024);
201 XmlFlattenerOptions options = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 options.keep_raw_values = keep_raw_values;
203 options.max_sdk_level = max_sdk_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 XmlFlattener flattener(&buffer, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 if (!flattener.Consume(context, xml_res)) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800206 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 }
208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 DiagMessage msg;
211 msg << "writing " << path << " to archive";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 if (max_sdk_level) {
213 msg << " maxSdkLevel=" << max_sdk_level.value()
214 << " keepRawValues=" << keep_raw_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 context->GetDiagnostics()->Note(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 }
218
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 if (writer->StartEntry(path, ArchiveEntry::kCompress)) {
220 if (writer->WriteEntry(buffer)) {
221 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 return true;
223 }
224 }
225 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << path
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 << " to archive");
228 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800229}
230
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 const void* data,
233 size_t len,
Adam Lesinski355f2852016-02-13 20:26:45 -0800234 IDiagnostics* diag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 pb::ResourceTable pb_table;
236 if (!pb_table.ParseFromArray(data, len)) {
237 diag->Error(DiagMessage(source) << "invalid compiled table");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 return {};
239 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800240
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 DeserializeTableFromPb(pb_table, source, diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 if (!table) {
244 return {};
245 }
246 return table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800247}
248
249/**
250 * Inflates an XML file from the source path.
251 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 IDiagnostics* diag) {
254 std::ifstream fin(path, std::ifstream::binary);
255 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 diag->Error(DiagMessage(path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 return {};
258 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 return xml::Inflate(&fin, diag, Source(path));
Adam Lesinski355f2852016-02-13 20:26:45 -0800260}
261
Adam Lesinski355f2852016-02-13 20:26:45 -0800262struct ResourceFileFlattenerOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 bool no_auto_version = false;
264 bool no_version_vectors = false;
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900265 bool no_version_transitions = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 bool no_xml_namespaces = false;
267 bool keep_raw_values = false;
268 bool do_not_compress_anything = false;
269 bool update_proguard_spec = false;
270 std::unordered_set<std::string> extensions_to_not_compress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800271};
272
273class ResourceFileFlattener {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 public:
275 ResourceFileFlattener(const ResourceFileFlattenerOptions& options,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 IAaptContext* context, proguard::KeepSet* keep_set)
277 : options_(options), context_(context), keep_set_(keep_set) {}
Adam Lesinski355f2852016-02-13 20:26:45 -0800278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer);
Adam Lesinski355f2852016-02-13 20:26:45 -0800280
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 private:
282 struct FileOperation {
283 ConfigDescription config;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700284
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 // The entry this file came from.
286 const ResourceEntry* entry;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700287
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 // The file to copy as-is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 io::IFile* file_to_copy;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700290
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 // The XML to process and flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 std::unique_ptr<xml::XmlResource> xml_to_flatten;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700293
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 // The destination to write this file to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295 std::string dst_path;
296 bool skip_version = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 };
Adam Lesinski355f2852016-02-13 20:26:45 -0800298
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 uint32_t GetCompressionFlags(const StringPiece& str);
Adam Lesinski355f2852016-02-13 20:26:45 -0800300
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 bool LinkAndVersionXmlFile(ResourceTable* table, FileOperation* file_op,
302 std::queue<FileOperation>* out_file_op_queue);
Adam Lesinski355f2852016-02-13 20:26:45 -0800303
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 ResourceFileFlattenerOptions options_;
305 IAaptContext* context_;
306 proguard::KeepSet* keep_set_;
Adam Lesinski355f2852016-02-13 20:26:45 -0800307};
308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) {
310 if (options_.do_not_compress_anything) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 return 0;
312 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800313
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 for (const std::string& extension : options_.extensions_to_not_compress) {
315 if (util::EndsWith(str, extension)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 return 0;
Adam Lesinski355f2852016-02-13 20:26:45 -0800317 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 }
319 return ArchiveEntry::kCompress;
Adam Lesinski355f2852016-02-13 20:26:45 -0800320}
321
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900322static bool IsTransitionElement(const std::string& name) {
323 return
324 name == "fade" ||
325 name == "changeBounds" ||
326 name == "slide" ||
327 name == "explode" ||
328 name == "changeImageTransform" ||
329 name == "changeTransform" ||
330 name == "changeClipBounds" ||
331 name == "autoTransition" ||
332 name == "recolor" ||
333 name == "changeScroll" ||
334 name == "transitionSet" ||
335 name == "transition" ||
336 name == "transitionManager";
337}
338
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339bool ResourceFileFlattener::LinkAndVersionXmlFile(
340 ResourceTable* table, FileOperation* file_op,
341 std::queue<FileOperation>* out_file_op_queue) {
342 xml::XmlResource* doc = file_op->xml_to_flatten.get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 const Source& src = doc->file.source;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700344
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700345 if (context_->IsVerbose()) {
346 context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 }
348
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 XmlReferenceLinker xml_linker;
350 if (!xml_linker.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 return false;
352 }
353
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354 if (options_.update_proguard_spec &&
355 !proguard::CollectProguardRules(src, doc, keep_set_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 return false;
357 }
358
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 if (options_.no_xml_namespaces) {
360 XmlNamespaceRemover namespace_remover;
361 if (!namespace_remover.Consume(context_, doc)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 return false;
Adam Lesinski355f2852016-02-13 20:26:45 -0800363 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800365
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366 if (!options_.no_auto_version) {
367 if (options_.no_version_vectors) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 // Skip this if it is a vector or animated-vector.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369 xml::Element* el = xml::FindRootElement(doc);
370 if (el && el->namespace_uri.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371 if (el->name == "vector" || el->name == "animated-vector") {
372 // We are NOT going to version this file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373 file_op->skip_version = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 return true;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700375 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700377 }
Yuichi Araki4d35cca2017-01-18 20:42:17 +0900378 if (options_.no_version_transitions) {
379 // Skip this if it is a transition resource.
380 xml::Element* el = xml::FindRootElement(doc);
381 if (el && el->namespace_uri.empty()) {
382 if (IsTransitionElement(el->name)) {
383 // We are NOT going to version this file.
384 file_op->skip_version = true;
385 return true;
386 }
387 }
388 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700389
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390 const ConfigDescription& config = file_op->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391
392 // Find the first SDK level used that is higher than this defined config and
393 // not superseded by a lower or equal SDK level resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 const int min_sdk_version = context_->GetMinSdkVersion();
395 for (int sdk_level : xml_linker.sdk_levels()) {
396 if (sdk_level > min_sdk_version && sdk_level > config.sdkVersion) {
397 if (!ShouldGenerateVersionedResource(file_op->entry, config,
398 sdk_level)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 // If we shouldn't generate a versioned resource, stop checking.
400 break;
Adam Lesinski626a69f2016-03-03 10:09:26 -0800401 }
402
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 ResourceFile versioned_file_desc = doc->file;
404 versioned_file_desc.config.sdkVersion = (uint16_t)sdk_level;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700405
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 FileOperation new_file_op;
407 new_file_op.xml_to_flatten = util::make_unique<xml::XmlResource>(
408 versioned_file_desc, doc->root->Clone());
409 new_file_op.config = versioned_file_desc.config;
410 new_file_op.entry = file_op->entry;
411 new_file_op.dst_path = ResourceUtils::BuildResourceFileName(
412 versioned_file_desc, context_->GetNameMangler());
Adam Lesinski355f2852016-02-13 20:26:45 -0800413
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 if (context_->IsVerbose()) {
415 context_->GetDiagnostics()->Note(
416 DiagMessage(versioned_file_desc.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 << "auto-versioning resource from config '" << config << "' -> '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 << versioned_file_desc.config << "'");
Adam Lesinski355f2852016-02-13 20:26:45 -0800419 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 bool added = table->AddFileReferenceAllowMangled(
422 versioned_file_desc.name, versioned_file_desc.config,
423 versioned_file_desc.source, new_file_op.dst_path, nullptr,
424 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 if (!added) {
426 return false;
427 }
428
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 out_file_op_queue->push(std::move(new_file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 break;
431 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800432 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 }
434 return true;
Adam Lesinski355f2852016-02-13 20:26:45 -0800435}
436
437/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 * Do not insert or remove any resources while executing in this function. It
439 * will
Adam Lesinski355f2852016-02-13 20:26:45 -0800440 * corrupt the iteration order.
441 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442bool ResourceFileFlattener::Flatten(ResourceTable* table,
443 IArchiveWriter* archive_writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 bool error = false;
445 std::map<std::pair<ConfigDescription, StringPiece>, FileOperation>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 config_sorted_files;
Adam Lesinski355f2852016-02-13 20:26:45 -0800447
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 for (auto& pkg : table->packages) {
449 for (auto& type : pkg->types) {
450 // Sort by config and name, so that we get better locality in the zip
451 // file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700452 config_sorted_files.clear();
453 std::queue<FileOperation> file_operations;
Adam Lesinski355f2852016-02-13 20:26:45 -0800454
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 // Populate the queue with all files in the ResourceTable.
456 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 for (auto& config_value : entry->values) {
458 FileReference* file_ref =
459 ValueCast<FileReference>(config_value->value.get());
460 if (!file_ref) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700461 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700462 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 io::IFile* file = file_ref->file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700466 context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 << "file not found");
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700468 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 }
470
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 FileOperation file_op;
472 file_op.entry = entry.get();
473 file_op.dst_path = *file_ref->path;
474 file_op.config = config_value->config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 const StringPiece src_path = file->GetSource().path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 if (type->type != ResourceType::kRaw &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 (util::EndsWith(src_path, ".xml.flat") ||
479 util::EndsWith(src_path, ".xml"))) {
480 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 << "failed to open file");
484 return false;
485 }
486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 file_op.xml_to_flatten =
488 xml::Inflate(data->data(), data->size(),
489 context_->GetDiagnostics(), file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 if (!file_op.xml_to_flatten) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 return false;
493 }
494
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 file_op.xml_to_flatten->file.config = config_value->config;
496 file_op.xml_to_flatten->file.source = file_ref->GetSource();
497 file_op.xml_to_flatten->file.name =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 ResourceName(pkg->name, type->type, entry->name);
499
500 // Enqueue the XML files to be processed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700501 file_operations.push(std::move(file_op));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700503 file_op.file_to_copy = file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504
505 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 // else we end up copying the string in the std::make_pair() method,
507 // then creating a StringPiece from the copy, which would cause us
508 // to end up referencing garbage in the map.
509 const StringPiece entry_name(entry->name);
510 config_sorted_files[std::make_pair(
511 config_value->config, entry_name)] = std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 }
513 }
514 }
515
516 // Now process the XML queue
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700517 for (; !file_operations.empty(); file_operations.pop()) {
518 FileOperation& file_op = file_operations.front();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700519
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 if (!LinkAndVersionXmlFile(table, &file_op, &file_operations)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 error = true;
522 continue;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700523 }
524
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 // NOTE(adamlesinski): Explicitly construct a StringPiece here, or else
526 // we end up copying the string in the std::make_pair() method, then
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 // creating a StringPiece from the copy, which would cause us to end up
528 // referencing garbage in the map.
529 const StringPiece entry_name(file_op.entry->name);
530 config_sorted_files[std::make_pair(file_op.config, entry_name)] =
531 std::move(file_op);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 }
533
534 if (error) {
535 return false;
536 }
537
538 // Now flatten the sorted values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700539 for (auto& map_entry : config_sorted_files) {
540 const ConfigDescription& config = map_entry.first.first;
541 const FileOperation& file_op = map_entry.second;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 if (file_op.xml_to_flatten) {
544 Maybe<size_t> max_sdk_level;
545 if (!options_.no_auto_version && !file_op.skip_version) {
546 max_sdk_level =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 std::max<size_t>(std::max<size_t>(config.sdkVersion, 1u),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 }
550
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 bool result = FlattenXml(
552 file_op.xml_to_flatten.get(), file_op.dst_path, max_sdk_level,
553 options_.keep_raw_values, archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 if (!result) {
555 error = true;
556 }
557 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 bool result = CopyFileToArchive(
559 file_op.file_to_copy, file_op.dst_path,
560 GetCompressionFlags(file_op.dst_path), archive_writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 if (!result) {
562 error = true;
563 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700564 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700566 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 }
568 return !error;
569}
570
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571static bool WriteStableIdMapToPath(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 IDiagnostics* diag,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700573 const std::unordered_map<ResourceName, ResourceId>& id_map,
574 const std::string& id_map_path) {
575 std::ofstream fout(id_map_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 diag->Error(DiagMessage(id_map_path) << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 return false;
579 }
580
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 for (const auto& entry : id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 const ResourceName& name = entry.first;
583 const ResourceId& id = entry.second;
584 fout << name << " = " << id << "\n";
585 }
586
587 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 diag->Error(DiagMessage(id_map_path)
589 << "failed writing to file: "
590 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 return false;
592 }
593
594 return true;
595}
596
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597static bool LoadStableIdMap(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 IDiagnostics* diag, const std::string& path,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 std::unordered_map<ResourceName, ResourceId>* out_id_map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 std::string content;
601 if (!android::base::ReadFileToString(path, &content)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602 diag->Error(DiagMessage(path) << "failed reading stable ID file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 return false;
604 }
605
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606 out_id_map->clear();
607 size_t line_no = 0;
608 for (StringPiece line : util::Tokenize(content, '\n')) {
609 line_no++;
610 line = util::TrimWhitespace(line);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700611 if (line.empty()) {
612 continue;
613 }
614
615 auto iter = std::find(line.begin(), line.end(), '=');
616 if (iter == line.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700617 diag->Error(DiagMessage(Source(path, line_no)) << "missing '='");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 return false;
619 }
620
621 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622 StringPiece res_name_str =
623 util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter)));
624 if (!ResourceUtils::ParseResourceName(res_name_str, &name)) {
625 diag->Error(DiagMessage(Source(path, line_no))
626 << "invalid resource name '" << res_name_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627 return false;
628 }
629
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630 const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1;
631 const size_t res_id_str_len = line.size() - res_id_start_idx;
632 StringPiece res_id_str =
633 util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700634
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700635 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
636 if (!maybe_id) {
637 diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '"
638 << res_id_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700639 return false;
640 }
641
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700642 (*out_id_map)[name.ToResourceName()] = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 }
644 return true;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700645}
646
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647static bool ParseSplitParameter(const StringPiece& arg, IDiagnostics* diag,
648 std::string* out_path,
649 SplitConstraints* out_split) {
650 std::vector<std::string> parts = util::Split(arg, ':');
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 if (parts.size() != 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652 diag->Error(DiagMessage() << "invalid split parameter '" << arg << "'");
653 diag->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700654 DiagMessage()
655 << "should be --split path/to/output.apk:<config>[,<config>...]");
656 return false;
657 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700658 *out_path = parts[0];
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 std::vector<ConfigDescription> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 for (const StringPiece& config_str : util::Tokenize(parts[1], ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 configs.push_back({});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700662 if (!ConfigDescription::Parse(config_str, &configs.back())) {
663 diag->Error(DiagMessage() << "invalid config '" << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 << "' in split parameter '" << arg << "'");
665 return false;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700666 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668 out_split->configs.insert(configs.begin(), configs.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 return true;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700670}
671
Adam Lesinskifb48d292015-11-07 15:52:13 -0800672class LinkCommand {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673 public:
674 LinkCommand(LinkContext* context, const LinkOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 : options_(options),
676 context_(context),
677 final_table_(),
678 file_collection_(util::make_unique<io::FileCollection>()) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700679
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 /**
681 * Creates a SymbolTable that loads symbols from the various APKs and caches
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700682 * the results for faster lookup.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700684 bool LoadSymbolsFromIncludePaths() {
685 std::unique_ptr<AssetManagerSymbolSource> asset_source =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700686 util::make_unique<AssetManagerSymbolSource>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700687 for (const std::string& path : options_.include_paths) {
688 if (context_->IsVerbose()) {
689 context_->GetDiagnostics()->Note(DiagMessage(path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 << "loading include path");
691 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700692
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700693 // First try to load the file as a static lib.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700694 std::string error_str;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800695 std::unique_ptr<ResourceTable> include_static = LoadStaticLibrary(path, &error_str);
696 if (include_static) {
697 if (options_.package_type != PackageType::kStaticLib) {
698 // Can't include static libraries when not building a static library (they have no IDs
699 // assigned).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700700 context_->GetDiagnostics()->Error(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800701 DiagMessage(path) << "can't include static library when not building a static lib");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 return false;
703 }
704
705 // If we are using --no-static-lib-packages, we need to rename the
706 // package of this
707 // table to our compilation package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700708 if (options_.no_static_lib_packages) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800709 if (ResourceTablePackage* pkg = include_static->FindPackageById(0x7f)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700710 pkg->name = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 }
712 }
713
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700714 context_->GetExternalSymbols()->AppendSource(
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800715 util::make_unique<ResourceTableSymbolSource>(include_static.get()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800717 static_table_includes_.push_back(std::move(include_static));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719 } else if (!error_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720 // We had an error with reading, so fail.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700721 context_->GetDiagnostics()->Error(DiagMessage(path) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 return false;
723 }
724
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700725 if (!asset_source->AddAssetPath(path)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800726 context_->GetDiagnostics()->Error(DiagMessage(path) << "failed to load include path");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 return false;
728 }
729 }
730
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800731 // Capture the shared libraries so that the final resource table can be properly flattened
732 // with support for shared libraries.
733 for (auto& entry : asset_source->GetAssignedPackageIds()) {
734 if (entry.first > 0x01 && entry.first < 0x7f) {
735 final_table_.included_packages_[entry.first] = entry.second;
736 }
737 }
738
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700739 context_->GetExternalSymbols()->AppendSource(std::move(asset_source));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 return true;
741 }
742
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700743 Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700744 IDiagnostics* diag) {
745 // Make sure the first element is <manifest> with package attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700746 if (xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get())) {
747 AppInfo app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700749 if (!manifest_el->namespace_uri.empty() ||
750 manifest_el->name != "manifest") {
751 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700752 << "root tag must be <manifest>");
753 return {};
754 }
755
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700756 xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package");
757 if (!package_attr) {
758 diag->Error(DiagMessage(xml_res->file.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700759 << "<manifest> must have a 'package' attribute");
760 return {};
761 }
762
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700763 app_info.package = package_attr->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700764
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700765 if (xml::Attribute* version_code_attr =
766 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
767 Maybe<uint32_t> maybe_code =
768 ResourceUtils::ParseInt(version_code_attr->value);
769 if (!maybe_code) {
770 diag->Error(DiagMessage(xml_res->file.source.WithLine(
771 manifest_el->line_number))
772 << "invalid android:versionCode '"
773 << version_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700774 return {};
775 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776 app_info.version_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700777 }
778
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700779 if (xml::Attribute* revision_code_attr =
780 manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
781 Maybe<uint32_t> maybe_code =
782 ResourceUtils::ParseInt(revision_code_attr->value);
783 if (!maybe_code) {
784 diag->Error(DiagMessage(xml_res->file.source.WithLine(
785 manifest_el->line_number))
786 << "invalid android:revisionCode '"
787 << revision_code_attr->value << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700788 return {};
789 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700790 app_info.revision_code = maybe_code.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700791 }
792
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700793 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
794 if (xml::Attribute* min_sdk = uses_sdk_el->FindAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700795 xml::kSchemaAndroid, "minSdkVersion")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700796 app_info.min_sdk_version = min_sdk->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700797 }
798 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700799 return app_info;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700800 }
801 return {};
802 }
803
804 /**
805 * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it
806 * linked.
807 * Postcondition: ResourceTable has only one package left. All others are
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700808 * stripped, or there is an error and false is returned.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700809 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700810 bool VerifyNoExternalPackages() {
811 auto is_ext_package_func =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700812 [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700813 return context_->GetCompilationPackage() != pkg->name || !pkg->id ||
814 pkg->id.value() != context_->GetPackageId();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700815 };
816
817 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700818 for (const auto& package : final_table_.packages) {
819 if (is_ext_package_func(package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700820 // We have a package that is not related to the one we're building!
821 for (const auto& type : package->types) {
822 for (const auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700823 ResourceNameRef res_name(package->name, type->type, entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700824
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700825 for (const auto& config_value : entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700826 // Special case the occurrence of an ID that is being generated
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700827 // for the 'android' package. This is due to legacy reasons.
828 if (ValueCast<Id>(config_value->value.get()) &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 package->name == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700830 context_->GetDiagnostics()->Warn(
831 DiagMessage(config_value->value->GetSource())
832 << "generated id '" << res_name
833 << "' for external package '" << package->name << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700834 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700835 context_->GetDiagnostics()->Error(
836 DiagMessage(config_value->value->GetSource())
837 << "defined resource '" << res_name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838 << "' for external package '" << package->name << "'");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700839 error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700841 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700842 }
843 }
844 }
845 }
846
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700847 auto new_end_iter =
848 std::remove_if(final_table_.packages.begin(),
849 final_table_.packages.end(), is_ext_package_func);
850 final_table_.packages.erase(new_end_iter, final_table_.packages.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700851 return !error;
852 }
853
854 /**
855 * Returns true if no IDs have been set, false otherwise.
856 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857 bool VerifyNoIdsSet() {
858 for (const auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 for (const auto& type : package->types) {
860 if (type->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700861 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700862 DiagMessage() << "type " << type->type << " has ID " << std::hex
863 << (int)type->id.value() << std::dec
864 << " assigned");
865 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700866 }
867
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700868 for (const auto& entry : type->entries) {
869 if (entry->id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700870 ResourceNameRef res_name(package->name, type->type, entry->name);
871 context_->GetDiagnostics()->Error(
872 DiagMessage() << "entry " << res_name << " has ID " << std::hex
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700873 << (int)entry->id.value() << std::dec
874 << " assigned");
875 return false;
876 }
877 }
878 }
879 }
880 return true;
881 }
882
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700883 std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) {
884 if (options_.output_to_directory) {
885 return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700886 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700887 return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888 }
889 }
890
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700891 bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700892 BigBuffer buffer(1024);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800893 TableFlattener flattener(options_.table_flattener_options, &buffer);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700894 if (!flattener.Consume(context_, table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700895 return false;
896 }
897
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700898 if (writer->StartEntry("resources.arsc", ArchiveEntry::kAlign)) {
899 if (writer->WriteEntry(buffer)) {
900 if (writer->FinishEntry()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 return true;
902 }
903 }
904 }
905
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700906 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 DiagMessage() << "failed to write resources.arsc to archive");
908 return false;
909 }
910
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700911 bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700912 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700913 if (!writer->StartEntry("resources.arsc.flat", 0)) {
914 context_->GetDiagnostics()->Error(DiagMessage() << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700915 return false;
916 }
917
918 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700919 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700920 {
921 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700922 // ZeroCopyOutputStream interface.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700923 CopyingOutputStreamAdaptor adaptor(writer);
924
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700925 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
926 if (!pb_table->SerializeToZeroCopyStream(&adaptor)) {
927 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700928 return false;
929 }
930 }
931
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700932 if (!writer->FinishEntry()) {
933 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700934 << "failed to finish entry");
935 return false;
936 }
937 return true;
938 }
939
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700940 bool WriteJavaFile(ResourceTable* table,
941 const StringPiece& package_name_to_generate,
942 const StringPiece& out_package,
943 const JavaClassGeneratorOptions& java_options) {
944 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700945 return true;
946 }
947
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700948 std::string out_path = options_.generate_java_class_path.value();
949 file::AppendPath(&out_path, file::PackageToPath(out_package));
950 if (!file::mkdirs(out_path)) {
951 context_->GetDiagnostics()->Error(
952 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953 return false;
954 }
955
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700956 file::AppendPath(&out_path, "R.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700957
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700958 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700959 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700960 context_->GetDiagnostics()->Error(
961 DiagMessage() << "failed writing to '" << out_path << "': "
962 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700963 return false;
964 }
965
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700966 JavaClassGenerator generator(context_, table, java_options);
967 if (!generator.Generate(package_name_to_generate, out_package, &fout)) {
968 context_->GetDiagnostics()->Error(DiagMessage(out_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700969 << generator.getError());
970 return false;
971 }
972
973 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700974 context_->GetDiagnostics()->Error(
975 DiagMessage() << "failed writing to '" << out_path << "': "
976 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 }
978 return true;
979 }
980
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700981 bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
982 if (!options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700983 return true;
984 }
985
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700986 std::unique_ptr<ClassDefinition> manifest_class =
987 GenerateManifestClass(context_->GetDiagnostics(), manifest_xml);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700988
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700989 if (!manifest_class) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700990 // Something bad happened, but we already logged it, so exit.
991 return false;
992 }
993
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700994 if (manifest_class->empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700995 // Empty Manifest class, no need to generate it.
996 return true;
997 }
998
999 // Add any JavaDoc annotations to the generated class.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001000 for (const std::string& annotation : options_.javadoc_annotations) {
1001 std::string proper_annotation = "@";
1002 proper_annotation += annotation;
1003 manifest_class->GetCommentBuilder()->AppendComment(proper_annotation);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001004 }
1005
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001006 const std::string& package_utf8 = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001008 std::string out_path = options_.generate_java_class_path.value();
1009 file::AppendPath(&out_path, file::PackageToPath(package_utf8));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001011 if (!file::mkdirs(out_path)) {
1012 context_->GetDiagnostics()->Error(
1013 DiagMessage() << "failed to create directory '" << out_path << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001014 return false;
1015 }
1016
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017 file::AppendPath(&out_path, "Manifest.java");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001018
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001019 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001020 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001021 context_->GetDiagnostics()->Error(
1022 DiagMessage() << "failed writing to '" << out_path << "': "
1023 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001024 return false;
1025 }
1026
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8,
1028 true, &fout)) {
1029 context_->GetDiagnostics()->Error(
1030 DiagMessage() << "failed writing to '" << out_path << "': "
1031 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001032 return false;
1033 }
1034 return true;
1035 }
1036
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001037 bool WriteProguardFile(const Maybe<std::string>& out,
1038 const proguard::KeepSet& keep_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039 if (!out) {
1040 return true;
1041 }
1042
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001043 const std::string& out_path = out.value();
1044 std::ofstream fout(out_path, std::ofstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001045 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001046 context_->GetDiagnostics()->Error(
1047 DiagMessage() << "failed to open '" << out_path << "': "
1048 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001049 return false;
1050 }
1051
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001052 proguard::WriteKeepSet(&fout, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001053 if (!fout) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001054 context_->GetDiagnostics()->Error(
1055 DiagMessage() << "failed writing to '" << out_path << "': "
1056 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001057 return false;
1058 }
1059 return true;
1060 }
1061
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001062 std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input,
1063 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001064 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001065 io::ZipFileCollection::Create(input, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001066 if (!collection) {
1067 return {};
1068 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001069 return LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001070 }
1071
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001072 std::unique_ptr<ResourceTable> LoadTablePbFromCollection(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001073 io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001074 io::IFile* file = collection->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075 if (!file) {
1076 return {};
1077 }
1078
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001079 std::unique_ptr<io::IData> data = file->OpenAsData();
1080 return LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1081 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001082 }
1083
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001084 bool MergeStaticLibrary(const std::string& input, bool override) {
1085 if (context_->IsVerbose()) {
1086 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001087 << "merging static library " << input);
1088 }
1089
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001090 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001092 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001093 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001094 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001095 return false;
1096 }
1097
1098 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001099 LoadTablePbFromCollection(collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001100 if (!table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001101 context_->GetDiagnostics()->Error(DiagMessage(input)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001102 << "invalid static library");
1103 return false;
1104 }
1105
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001106 ResourceTablePackage* pkg = table->FindPackageById(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001107 if (!pkg) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001108 context_->GetDiagnostics()->Error(DiagMessage(input)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 << "static library has no package");
1110 return false;
1111 }
1112
1113 bool result;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001114 if (options_.no_static_lib_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001115 // Merge all resources as if they were in the compilation package. This is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001116 // the old behavior of aapt.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001117
1118 // Add the package to the set of --extra-packages so we emit an R.java for
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001119 // each library package.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001120 if (!pkg->name.empty()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001121 options_.extra_java_packages.insert(pkg->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001122 }
1123
1124 pkg->name = "";
1125 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001126 result = table_merger_->MergeOverlay(Source(input), table.get(),
1127 collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001128 } else {
1129 result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001130 table_merger_->Merge(Source(input), table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001131 }
1132
1133 } else {
1134 // This is the proper way to merge libraries, where the package name is
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001135 // preserved and resource names are mangled.
1136 result = table_merger_->MergeAndMangle(Source(input), pkg->name,
1137 table.get(), collection.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001138 }
1139
1140 if (!result) {
1141 return false;
1142 }
1143
1144 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001145 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001146 return true;
1147 }
1148
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001149 bool MergeResourceTable(io::IFile* file, bool override) {
1150 if (context_->IsVerbose()) {
1151 context_->GetDiagnostics()->Note(
1152 DiagMessage() << "merging resource table " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001153 }
1154
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001155 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001156 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001157 context_->GetDiagnostics()->Error(DiagMessage(file->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001158 << "failed to open file");
1159 return false;
1160 }
1161
1162 std::unique_ptr<ResourceTable> table =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001163 LoadTableFromPb(file->GetSource(), data->data(), data->size(),
1164 context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001165 if (!table) {
1166 return false;
1167 }
1168
1169 bool result = false;
1170 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001171 result = table_merger_->MergeOverlay(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001172 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001173 result = table_merger_->Merge(file->GetSource(), table.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001174 }
1175 return result;
1176 }
1177
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001178 bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001179 bool override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001180 if (context_->IsVerbose()) {
1181 context_->GetDiagnostics()->Note(
1182 DiagMessage() << "merging '" << file_desc->name
1183 << "' from compiled file " << file->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001184 }
1185
1186 bool result = false;
1187 if (override) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001188 result = table_merger_->MergeFileOverlay(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001189 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001190 result = table_merger_->MergeFile(*file_desc, file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001191 }
1192
1193 if (!result) {
1194 return false;
1195 }
1196
1197 // Add the exports of this file to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001198 for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) {
1199 if (exported_symbol.name.package.empty()) {
1200 exported_symbol.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001201 }
1202
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001203 ResourceNameRef res_name = exported_symbol.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001204
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001205 Maybe<ResourceName> mangled_name =
1206 context_->GetNameMangler()->MangleName(exported_symbol.name);
1207 if (mangled_name) {
1208 res_name = mangled_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001209 }
1210
1211 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001212 id->SetSource(file_desc->source.WithLine(exported_symbol.line));
1213 bool result = final_table_.AddResourceAllowMangled(
1214 res_name, ConfigDescription::DefaultConfig(), std::string(),
1215 std::move(id), context_->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001216 if (!result) {
1217 return false;
1218 }
1219 }
1220 return true;
1221 }
1222
1223 /**
1224 * Takes a path to load as a ZIP file and merges the files within into the
1225 * master ResourceTable.
1226 * If override is true, conflicting resources are allowed to override each
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001227 * other, in order of last seen.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001228 *
1229 * An io::IFileCollection is created from the ZIP file and added to the set of
1230 * io::IFileCollections that are open.
1231 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001232 bool MergeArchive(const std::string& input, bool override) {
1233 if (context_->IsVerbose()) {
1234 context_->GetDiagnostics()->Note(DiagMessage() << "merging archive "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001235 << input);
1236 }
1237
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001238 std::string error_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001239 std::unique_ptr<io::ZipFileCollection> collection =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001240 io::ZipFileCollection::Create(input, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001241 if (!collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001242 context_->GetDiagnostics()->Error(DiagMessage(input) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001243 return false;
1244 }
1245
1246 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001247 for (auto iter = collection->Iterator(); iter->HasNext();) {
1248 if (!MergeFile(iter->Next(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001249 error = true;
1250 }
1251 }
1252
1253 // Make sure to move the collection into the set of IFileCollections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001254 collections_.push_back(std::move(collection));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001255 return !error;
1256 }
1257
1258 /**
1259 * Takes a path to load and merge into the master ResourceTable. If override
1260 * is true,
1261 * conflicting resources are allowed to override each other, in order of last
1262 * seen.
1263 *
1264 * If the file path ends with .flata, .jar, .jack, or .zip the file is treated
1265 * as ZIP archive
1266 * and the files within are merged individually.
1267 *
1268 * Otherwise the files is processed on its own.
1269 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001270 bool MergePath(const std::string& path, bool override) {
1271 if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") ||
1272 util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) {
1273 return MergeArchive(path, override);
1274 } else if (util::EndsWith(path, ".apk")) {
1275 return MergeStaticLibrary(path, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001276 }
1277
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001278 io::IFile* file = file_collection_->InsertFile(path);
1279 return MergeFile(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001280 }
1281
1282 /**
1283 * Takes a file to load and merge into the master ResourceTable. If override
1284 * is true,
1285 * conflicting resources are allowed to override each other, in order of last
1286 * seen.
1287 *
1288 * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and
1289 * merged into the
1290 * master ResourceTable. If the file ends with .flat, then it is treated like
1291 * a compiled file
1292 * and the header data is read and merged into the final ResourceTable.
1293 *
1294 * All other file types are ignored. This is because these files could be
1295 * coming from a zip,
1296 * where we could have other files like classes.dex.
1297 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001298 bool MergeFile(io::IFile* file, bool override) {
1299 const Source& src = file->GetSource();
1300 if (util::EndsWith(src.path, ".arsc.flat")) {
1301 return MergeResourceTable(file, override);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001302
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001303 } else if (util::EndsWith(src.path, ".flat")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001304 // Try opening the file and looking for an Export header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001305 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001307 context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001308 return false;
1309 }
1310
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001311 CompiledFileInputStream input_stream(data->data(), data->size());
1312 uint32_t num_files = 0;
1313 if (!input_stream.ReadLittleEndian32(&num_files)) {
1314 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001315 << "failed read num files");
1316 return false;
1317 }
1318
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001319 for (uint32_t i = 0; i < num_files; i++) {
1320 pb::CompiledFile compiled_file;
1321 if (!input_stream.ReadCompiledFile(&compiled_file)) {
1322 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001323 DiagMessage(src) << "failed to read compiled file header");
1324 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -08001325 }
1326
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001327 uint64_t offset, len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001328 if (!input_stream.ReadDataMetaData(&offset, &len)) {
1329 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001330 << "failed to read data meta data");
1331 return false;
1332 }
1333
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001334 std::unique_ptr<ResourceFile> resource_file =
1335 DeserializeCompiledFileFromPb(compiled_file, file->GetSource(),
1336 context_->GetDiagnostics());
1337 if (!resource_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001338 return false;
1339 }
1340
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001341 if (!MergeCompiledFile(file->CreateFileSegment(offset, len),
1342 resource_file.get(), override)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001343 return false;
1344 }
1345 }
1346 return true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001347 } else if (util::EndsWith(src.path, ".xml") ||
1348 util::EndsWith(src.path, ".png")) {
Adam Lesinski6a396c12016-10-20 14:38:23 -07001349 // Since AAPT compiles these file types and appends .flat to them, seeing
1350 // their raw extensions is a sign that they weren't compiled.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001351 const StringPiece file_type =
1352 util::EndsWith(src.path, ".xml") ? "XML" : "PNG";
1353 context_->GetDiagnostics()->Error(DiagMessage(src)
1354 << "uncompiled " << file_type
Adam Lesinski6a396c12016-10-20 14:38:23 -07001355 << " file passed as argument. Must be "
1356 "compiled first into .flat file.");
1357 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001358 }
1359
1360 // Ignore non .flat files. This could be classes.dex or something else that
1361 // happens
1362 // to be in an archive.
1363 return true;
1364 }
1365
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001366 std::unique_ptr<xml::XmlResource> GenerateSplitManifest(
1367 const AppInfo& app_info, const SplitConstraints& constraints) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001368 std::unique_ptr<xml::XmlResource> doc =
1369 util::make_unique<xml::XmlResource>();
1370
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001371 std::unique_ptr<xml::Namespace> namespace_android =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001372 util::make_unique<xml::Namespace>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001373 namespace_android->namespace_uri = xml::kSchemaAndroid;
1374 namespace_android->namespace_prefix = "android";
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001375
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001376 std::unique_ptr<xml::Element> manifest_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001377 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001378 manifest_el->name = "manifest";
1379 manifest_el->attributes.push_back(
1380 xml::Attribute{"", "package", app_info.package});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001381
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001382 if (app_info.version_code) {
1383 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001384 xml::Attribute{xml::kSchemaAndroid, "versionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001385 std::to_string(app_info.version_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001386 }
1387
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001388 if (app_info.revision_code) {
1389 manifest_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001390 xml::Attribute{xml::kSchemaAndroid, "revisionCode",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001391 std::to_string(app_info.revision_code.value())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001392 }
1393
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001394 std::stringstream split_name;
1395 split_name << "config." << util::Joiner(constraints.configs, "_");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001396
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001397 manifest_el->attributes.push_back(
1398 xml::Attribute{"", "split", split_name.str()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001399
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400 std::unique_ptr<xml::Element> application_el =
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001401 util::make_unique<xml::Element>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001402 application_el->name = "application";
1403 application_el->attributes.push_back(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001404 xml::Attribute{xml::kSchemaAndroid, "hasCode", "false"});
1405
Adam Lesinskie343eb12016-10-27 16:31:58 -07001406 manifest_el->AppendChild(std::move(application_el));
1407 namespace_android->AppendChild(std::move(manifest_el));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001408 doc->root = std::move(namespace_android);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001409 return doc;
1410 }
1411
1412 /**
1413 * Writes the AndroidManifest, ResourceTable, and all XML files referenced by
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001414 * the ResourceTable to the IArchiveWriter.
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001415 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001416 bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001417 xml::XmlResource* manifest, ResourceTable* table) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001418 const bool keep_raw_values = options_.package_type == PackageType::kStaticLib;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001419 bool result = FlattenXml(manifest, "AndroidManifest.xml", {},
1420 keep_raw_values, writer, context_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001421 if (!result) {
1422 return false;
1423 }
1424
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001425 ResourceFileFlattenerOptions file_flattener_options;
1426 file_flattener_options.keep_raw_values = keep_raw_values;
1427 file_flattener_options.do_not_compress_anything =
1428 options_.do_not_compress_anything;
1429 file_flattener_options.extensions_to_not_compress =
1430 options_.extensions_to_not_compress;
1431 file_flattener_options.no_auto_version = options_.no_auto_version;
1432 file_flattener_options.no_version_vectors = options_.no_version_vectors;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001433 file_flattener_options.no_version_transitions = options_.no_version_transitions;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001434 file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces;
1435 file_flattener_options.update_proguard_spec =
1436 static_cast<bool>(options_.generate_proguard_rules_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001437
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001438 ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001440 if (!file_flattener.Flatten(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001441 context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001442 return false;
1443 }
1444
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001445 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001446 if (!FlattenTableToPb(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001447 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001448 return false;
1449 }
1450 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001451 if (!FlattenTable(table, writer)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001452 context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001453 return false;
1454 }
1455 }
1456 return true;
1457 }
1458
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001459 int Run(const std::vector<std::string>& input_files) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001460 // Load the AndroidManifest.xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001461 std::unique_ptr<xml::XmlResource> manifest_xml =
1462 LoadXml(options_.manifest_path, context_->GetDiagnostics());
1463 if (!manifest_xml) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001464 return 1;
1465 }
1466
1467 // First extract the Package name without modifying it (via
1468 // --rename-manifest-package).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001469 if (Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1470 manifest_xml.get(), context_->GetDiagnostics())) {
1471 const AppInfo& app_info = maybe_app_info.value();
1472 context_->SetCompilationPackage(app_info.package);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001473 }
1474
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001475 ManifestFixer manifest_fixer(options_.manifest_fixer_options);
1476 if (!manifest_fixer.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001477 return 1;
1478 }
1479
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001480 Maybe<AppInfo> maybe_app_info = ExtractAppInfoFromManifest(
1481 manifest_xml.get(), context_->GetDiagnostics());
1482 if (!maybe_app_info) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001483 return 1;
1484 }
1485
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001486 const AppInfo& app_info = maybe_app_info.value();
1487 if (app_info.min_sdk_version) {
1488 if (Maybe<int> maybe_min_sdk_version = ResourceUtils::ParseSdkVersion(
1489 app_info.min_sdk_version.value())) {
1490 context_->SetMinSdkVersion(maybe_min_sdk_version.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001491 }
1492 }
1493
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001494 context_->SetNameManglerPolicy(
1495 NameManglerPolicy{context_->GetCompilationPackage()});
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001496 if (options_.package_type == PackageType::kSharedLib) {
1497 context_->SetPackageId(0x00);
1498 } else if (context_->GetCompilationPackage() == "android") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001499 context_->SetPackageId(0x01);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001500 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001501 context_->SetPackageId(0x7f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001502 }
1503
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001504 if (!LoadSymbolsFromIncludePaths()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001505 return 1;
1506 }
1507
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001508 TableMergerOptions table_merger_options;
1509 table_merger_options.auto_add_overlay = options_.auto_add_overlay;
1510 table_merger_ = util::make_unique<TableMerger>(context_, &final_table_,
1511 table_merger_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001512
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001513 if (context_->IsVerbose()) {
1514 context_->GetDiagnostics()->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001515 << "linking package '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001516 << context_->GetCompilationPackage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001517 << "' with package ID " << std::hex
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001518 << (int)context_->GetPackageId());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001519 }
1520
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001521 for (const std::string& input : input_files) {
1522 if (!MergePath(input, false)) {
1523 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001524 << "failed parsing input");
1525 return 1;
1526 }
1527 }
1528
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001529 for (const std::string& input : options_.overlay_files) {
1530 if (!MergePath(input, true)) {
1531 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001532 << "failed parsing overlays");
1533 return 1;
1534 }
1535 }
1536
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001537 if (!VerifyNoExternalPackages()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001538 return 1;
1539 }
1540
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001541 if (options_.package_type != PackageType::kStaticLib) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001542 PrivateAttributeMover mover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001543 if (!mover.Consume(context_, &final_table_)) {
1544 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001545 DiagMessage() << "failed moving private attributes");
1546 return 1;
1547 }
1548
1549 // Assign IDs if we are building a regular app.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001550 IdAssigner id_assigner(&options_.stable_id_map);
1551 if (!id_assigner.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001552 context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001553 return 1;
1554 }
1555
1556 // Now grab each ID and emit it as a file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001557 if (options_.resource_id_map_path) {
1558 for (auto& package : final_table_.packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001559 for (auto& type : package->types) {
1560 for (auto& entry : type->entries) {
1561 ResourceName name(package->name, type->type, entry->name);
1562 // The IDs are guaranteed to exist.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001563 options_.stable_id_map[std::move(name)] = ResourceId(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001564 package->id.value(), type->id.value(), entry->id.value());
1565 }
1566 }
1567 }
1568
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001569 if (!WriteStableIdMapToPath(context_->GetDiagnostics(),
1570 options_.stable_id_map,
1571 options_.resource_id_map_path.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001572 return 1;
1573 }
1574 }
1575 } else {
1576 // Static libs are merged with other apps, and ID collisions are bad, so
1577 // verify that
1578 // no IDs have been set.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001579 if (!VerifyNoIdsSet()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001580 return 1;
1581 }
1582 }
1583
1584 // Add the names to mangle based on our source merge earlier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001585 context_->SetNameManglerPolicy(NameManglerPolicy{
1586 context_->GetCompilationPackage(), table_merger_->merged_packages()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001587
1588 // Add our table to the symbol table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001589 context_->GetExternalSymbols()->PrependSource(
1590 util::make_unique<ResourceTableSymbolSource>(&final_table_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001591
1592 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001593 if (!linker.Consume(context_, &final_table_)) {
1594 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001595 << "failed linking references");
1596 return 1;
1597 }
1598
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001599 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001600 if (!options_.products.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001601 context_->GetDiagnostics()->Warn(DiagMessage()
1602 << "can't select products when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001603 }
1604 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001605 ProductFilter product_filter(options_.products);
1606 if (!product_filter.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001607 context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001608 return 1;
1609 }
1610 }
1611
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001612 if (!options_.no_auto_version) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001613 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001614 if (!versioner.Consume(context_, &final_table_)) {
1615 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001616 << "failed versioning styles");
1617 return 1;
1618 }
1619 }
1620
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001621 if (options_.package_type != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001622 if (context_->IsVerbose()) {
1623 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001624 DiagMessage() << "collapsing resource versions for minimum SDK "
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001625 << context_->GetMinSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001626 }
1627
1628 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001629 if (!collapser.Consume(context_, &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001630 return 1;
1631 }
1632 }
1633
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001634 if (!options_.no_resource_deduping) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001635 ResourceDeduper deduper;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001636 if (!deduper.Consume(context_, &final_table_)) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001637 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001638 return 1;
1639 }
1640 }
1641
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001642 proguard::KeepSet proguard_keep_set;
1643 proguard::KeepSet proguard_main_dex_keep_set;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001644
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001645 if (options_.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001646 if (options_.table_splitter_options.config_filter != nullptr ||
Pierre Lecesne672384b2017-02-06 10:29:02 +00001647 !options_.table_splitter_options.preferred_densities.empty()) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001648 context_->GetDiagnostics()->Warn(DiagMessage()
1649 << "can't strip resources when building static library");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001650 }
1651 } else {
1652 // Adjust the SplitConstraints so that their SDK version is stripped if it
1653 // is less
1654 // than or equal to the minSdk. Otherwise the resources that have had
1655 // their SDK version
1656 // stripped due to minSdk won't ever match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001657 std::vector<SplitConstraints> adjusted_constraints_list;
1658 adjusted_constraints_list.reserve(options_.split_constraints.size());
1659 for (const SplitConstraints& constraints : options_.split_constraints) {
1660 SplitConstraints adjusted_constraints;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001661 for (const ConfigDescription& config : constraints.configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001662 if (config.sdkVersion <= context_->GetMinSdkVersion()) {
1663 adjusted_constraints.configs.insert(config.CopyWithoutSdkVersion());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001664 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001665 adjusted_constraints.configs.insert(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001666 }
1667 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001668 adjusted_constraints_list.push_back(std::move(adjusted_constraints));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001669 }
1670
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001671 TableSplitter table_splitter(adjusted_constraints_list,
1672 options_.table_splitter_options);
1673 if (!table_splitter.VerifySplitConstraints(context_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001674 return 1;
1675 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001676 table_splitter.SplitTable(&final_table_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001677
1678 // Now we need to write out the Split APKs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001679 auto path_iter = options_.split_paths.begin();
1680 auto split_constraints_iter = adjusted_constraints_list.begin();
1681 for (std::unique_ptr<ResourceTable>& split_table :
1682 table_splitter.splits()) {
1683 if (context_->IsVerbose()) {
1684 context_->GetDiagnostics()->Note(
1685 DiagMessage(*path_iter)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001686 << "generating split with configurations '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001687 << util::Joiner(split_constraints_iter->configs, ", ") << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001688 }
1689
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001690 std::unique_ptr<IArchiveWriter> archive_writer =
1691 MakeArchiveWriter(*path_iter);
1692 if (!archive_writer) {
1693 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001694 << "failed to create archive");
1695 return 1;
1696 }
1697
1698 // Generate an AndroidManifest.xml for each split.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001699 std::unique_ptr<xml::XmlResource> split_manifest =
1700 GenerateSplitManifest(app_info, *split_constraints_iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001701
1702 XmlReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001703 if (!linker.Consume(context_, split_manifest.get())) {
1704 context_->GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001705 DiagMessage() << "failed to create Split AndroidManifest.xml");
1706 return 1;
1707 }
1708
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001709 if (!WriteApk(archive_writer.get(), &proguard_keep_set,
1710 split_manifest.get(), split_table.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001711 return 1;
1712 }
1713
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001714 ++path_iter;
1715 ++split_constraints_iter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001716 }
1717 }
1718
1719 // Start writing the base APK.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001720 std::unique_ptr<IArchiveWriter> archive_writer =
1721 MakeArchiveWriter(options_.output_path);
1722 if (!archive_writer) {
1723 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001724 << "failed to create archive");
1725 return 1;
1726 }
1727
1728 bool error = false;
1729 {
1730 // AndroidManifest.xml has no resource name, but the CallSite is built
1731 // from the name
1732 // (aka, which package the AndroidManifest.xml is coming from).
1733 // So we give it a package name so it can see local resources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001734 manifest_xml->file.name.package = context_->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001735
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001736 XmlReferenceLinker manifest_linker;
1737 if (manifest_linker.Consume(context_, manifest_xml.get())) {
1738 if (options_.generate_proguard_rules_path &&
1739 !proguard::CollectProguardRulesForManifest(
1740 Source(options_.manifest_path), manifest_xml.get(),
1741 &proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001742 error = true;
1743 }
1744
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001745 if (options_.generate_main_dex_proguard_rules_path &&
1746 !proguard::CollectProguardRulesForManifest(
1747 Source(options_.manifest_path), manifest_xml.get(),
1748 &proguard_main_dex_keep_set, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001749 error = true;
Alexandria Cornwall637b4822016-08-11 09:53:16 -07001750 }
1751
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001752 if (options_.generate_java_class_path) {
1753 if (!WriteManifestJavaFile(manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001754 error = true;
1755 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001756 }
1757
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001758 if (options_.no_xml_namespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001759 // PackageParser will fail if URIs are removed from
1760 // AndroidManifest.xml.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001761 XmlNamespaceRemover namespace_remover(true /* keepUris */);
1762 if (!namespace_remover.Consume(context_, manifest_xml.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001763 error = true;
1764 }
Rohit Agrawale49bb302016-04-22 12:27:55 -07001765 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001766 } else {
1767 error = true;
1768 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001769 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001770
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001771 if (error) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001772 context_->GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001773 << "failed processing manifest");
1774 return 1;
1775 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001776
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001777 if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(),
1778 &final_table_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001779 return 1;
1780 }
Adam Lesinskifb48d292015-11-07 15:52:13 -08001781
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001782 if (options_.generate_java_class_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001783 JavaClassGeneratorOptions options;
1784 options.types = JavaClassGeneratorOptions::SymbolTypes::kAll;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001785 options.javadoc_annotations = options_.javadoc_annotations;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001786
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001787 if (options_.package_type == PackageType::kStaticLib || options_.generate_non_final_ids) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001788 options.use_final = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001789 }
Adam Lesinski64587af2016-02-18 18:33:06 -08001790
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001791 if (options_.package_type == PackageType::kSharedLib) {
1792 options.use_final = false;
1793 options.generate_rewrite_callback = true;
1794 }
1795
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001796 const StringPiece actual_package = context_->GetCompilationPackage();
1797 StringPiece output_package = context_->GetCompilationPackage();
1798 if (options_.custom_java_package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001799 // Override the output java package to the custom one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001800 output_package = options_.custom_java_package.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001801 }
1802
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001803 if (options_.private_symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001804 // If we defined a private symbols package, we only emit Public symbols
1805 // to the original package, and private and public symbols to the
1806 // private package.
1807
1808 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001809 if (!WriteJavaFile(&final_table_, context_->GetCompilationPackage(),
1810 output_package, options)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001811 return 1;
1812 }
1813
1814 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001815 output_package = options_.private_symbols.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001816 }
1817
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001818 if (!WriteJavaFile(&final_table_, actual_package, output_package,
1819 options)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001820 return 1;
1821 }
1822
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001823 for (const std::string& extra_package : options_.extra_java_packages) {
1824 if (!WriteJavaFile(&final_table_, actual_package, extra_package,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001825 options)) {
1826 return 1;
1827 }
1828 }
1829 }
1830
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001831 if (!WriteProguardFile(options_.generate_proguard_rules_path,
1832 proguard_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001833 return 1;
1834 }
1835
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001836 if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path,
1837 proguard_main_dex_keep_set)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001838 return 1;
1839 }
1840
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001841 if (context_->IsVerbose()) {
1842 DebugPrintTableOptions debug_print_table_options;
1843 debug_print_table_options.show_sources = true;
1844 Debug::PrintTable(&final_table_, debug_print_table_options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001845 }
1846 return 0;
1847 }
1848
1849 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001850 LinkOptions options_;
1851 LinkContext* context_;
1852 ResourceTable final_table_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001853
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001854 std::unique_ptr<TableMerger> table_merger_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001855
1856 // A pointer to the FileCollection representing the filesystem (not archives).
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001857 std::unique_ptr<io::FileCollection> file_collection_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001858
1859 // A vector of IFileCollections. This is mainly here to keep ownership of the
1860 // collections.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001861 std::vector<std::unique_ptr<io::IFileCollection>> collections_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001862
1863 // A vector of ResourceTables. This is here to retain ownership, so that the
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001864 // SymbolTable can use these.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001865 std::vector<std::unique_ptr<ResourceTable>> static_table_includes_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001866
1867 // The set of shared libraries being used, mapping their assigned package ID to package name.
1868 std::map<size_t, std::string> shared_libs_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001869};
1870
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001871int Link(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001872 LinkContext context;
1873 LinkOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001874 std::vector<std::string> overlay_arg_list;
1875 std::vector<std::string> extra_java_packages;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001876 Maybe<std::string> configs;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001877 Maybe<std::string> preferred_density;
1878 Maybe<std::string> product_list;
1879 bool legacy_x_flag = false;
1880 bool require_localization = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001881 bool verbose = false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001882 bool shared_lib = false;
1883 bool static_lib = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001884 Maybe<std::string> stable_id_file_path;
1885 std::vector<std::string> split_args;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001886 Flags flags =
1887 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001888 .RequiredFlag("-o", "Output path", &options.output_path)
1889 .RequiredFlag("--manifest", "Path to the Android manifest to build",
1890 &options.manifest_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001891 .OptionalFlagList("-I", "Adds an Android APK to link against", &options.include_paths)
1892 .OptionalFlagList("-R",
1893 "Compilation unit to link, using `overlay` semantics.\n"
1894 "The last conflicting resource given takes precedence.",
1895 &overlay_arg_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001896 .OptionalFlag("--java", "Directory in which to generate R.java",
1897 &options.generate_java_class_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001898 .OptionalFlag("--proguard", "Output file for generated Proguard rules",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001899 &options.generate_proguard_rules_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001900 .OptionalFlag("--proguard-main-dex",
1901 "Output file for generated Proguard rules for the main dex",
1902 &options.generate_main_dex_proguard_rules_path)
1903 .OptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001904 &options.no_auto_version)
1905 .OptionalSwitch("--no-version-vectors",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001906 "Disables automatic versioning of vector drawables. "
1907 "Use this only\n"
1908 "when building with vector drawable support library",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001909 &options.no_version_vectors)
Yuichi Araki4d35cca2017-01-18 20:42:17 +09001910 .OptionalSwitch("--no-version-transitions",
1911 "Disables automatic versioning of transition resources. "
1912 "Use this only\n"
1913 "when building with transition support library",
1914 &options.no_version_transitions)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001915 .OptionalSwitch("--no-resource-deduping",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001916 "Disables automatic deduping of resources with\n"
1917 "identical values across compatible configurations.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001918 &options.no_resource_deduping)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001919 .OptionalSwitch("--enable-sparse-encoding",
1920 "Enables encoding sparse entries using a binary search tree.\n"
1921 "This decreases APK size at the cost of resource retrieval performance.",
1922 &options.table_flattener_options.use_sparse_entries)
1923 .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01",
1924 &legacy_x_flag)
1925 .OptionalSwitch("-z", "Require localization of strings marked 'suggested'",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001926 &require_localization)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001927 .OptionalFlag("-c",
1928 "Comma separated list of configurations to include. The default\n"
1929 "is all configurations",
1930 &configs)
1931 .OptionalFlag("--preferred-density",
1932 "Selects the closest matching density and strips out all others.",
1933 &preferred_density)
1934 .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001935 .OptionalSwitch("--output-to-dir",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001936 "Outputs the APK contents to a directory specified "
1937 "by -o",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001938 &options.output_to_directory)
1939 .OptionalSwitch("--no-xml-namespaces",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001940 "Removes XML namespace prefix and URI "
1941 "information from AndroidManifest.xml\nand XML "
1942 "binaries in res/*.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001943 &options.no_xml_namespaces)
1944 .OptionalFlag("--min-sdk-version",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001945 "Default minimum SDK version to use for "
1946 "AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001947 &options.manifest_fixer_options.min_sdk_version_default)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001948 .OptionalFlag("--target-sdk-version",
1949 "Default target SDK version to use for "
1950 "AndroidManifest.xml",
1951 &options.manifest_fixer_options.target_sdk_version_default)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001952 .OptionalFlag("--version-code",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001953 "Version code (integer) to inject into the "
1954 "AndroidManifest.xml if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001955 &options.manifest_fixer_options.version_code_default)
1956 .OptionalFlag("--version-name",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001957 "Version name to inject into the AndroidManifest.xml "
1958 "if none is present",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001959 &options.manifest_fixer_options.version_name_default)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08001960 .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library", &shared_lib)
1961 .OptionalSwitch("--static-lib", "Generate a static Android library", &static_lib)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001962 .OptionalSwitch("--no-static-lib-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001963 "Merge all library resources under the app's package",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001964 &options.no_static_lib_packages)
1965 .OptionalSwitch("--non-final-ids",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001966 "Generates R.java without the final modifier.\n"
1967 "This is implied when --static-lib is specified.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001968 &options.generate_non_final_ids)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001969 .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001970 &stable_id_file_path)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001971 .OptionalFlag("--emit-ids",
1972 "Emit a file at the given path with a list of name to ID\n"
1973 "mappings, suitable for use with --stable-ids.",
1974 &options.resource_id_map_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001975 .OptionalFlag("--private-symbols",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001976 "Package name to use when generating R.java for "
1977 "private symbols.\n"
1978 "If not specified, public and private symbols will use "
1979 "the application's "
1980 "package name",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001981 &options.private_symbols)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001982 .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001983 &options.custom_java_package)
1984 .OptionalFlagList("--extra-packages",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001985 "Generate the same R.java but with different "
1986 "package names",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001987 &extra_java_packages)
1988 .OptionalFlagList("--add-javadoc-annotation",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001989 "Adds a JavaDoc annotation to all "
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001990 "generated Java classes",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001991 &options.javadoc_annotations)
1992 .OptionalSwitch("--auto-add-overlay",
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001993 "Allows the addition of new resources in "
1994 "overlays without <add-resource> tags",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001995 &options.auto_add_overlay)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001996 .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml",
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001997 &options.manifest_fixer_options.rename_manifest_package)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08001998 .OptionalFlag("--rename-instrumentation-target-package",
1999 "Changes the name of the target package for instrumentation. "
2000 "Most useful "
2001 "when used\nin conjunction with --rename-manifest-package",
2002 &options.manifest_fixer_options.rename_instrumentation_target_package)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002003 .OptionalFlagList("-0", "File extensions not to compress",
2004 &options.extensions_to_not_compress)
Adam Lesinskic8f71aa2017-02-08 07:03:50 -08002005 .OptionalFlagList("--split",
2006 "Split resources matching a set of configs out to a "
2007 "Split APK.\nSyntax: path/to/output.apk:<config>[,<config>[...]]",
2008 &split_args)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002009 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002010
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002011 if (!flags.Parse("aapt2 link", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002012 return 1;
2013 }
2014
2015 // Expand all argument-files passed into the command line. These start with
2016 // '@'.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002017 std::vector<std::string> arg_list;
2018 for (const std::string& arg : flags.GetArgs()) {
2019 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002020 const std::string path = arg.substr(1, arg.size() - 1);
2021 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002022 if (!file::AppendArgsFromFile(path, &arg_list, &error)) {
2023 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002024 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002025 }
2026 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002027 arg_list.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002028 }
2029 }
2030
2031 // Expand all argument-files passed to -R.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002032 for (const std::string& arg : overlay_arg_list) {
2033 if (util::StartsWith(arg, "@")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002034 const std::string path = arg.substr(1, arg.size() - 1);
2035 std::string error;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002036 if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) {
2037 context.GetDiagnostics()->Error(DiagMessage(path) << error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002038 return 1;
2039 }
2040 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002041 options.overlay_files.push_back(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002042 }
2043 }
2044
2045 if (verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002046 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002047 }
2048
2049 // Populate the set of extra packages for which to generate R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002050 for (std::string& extra_package : extra_java_packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002051 // A given package can actually be a colon separated list of packages.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002052 for (StringPiece package : util::Split(extra_package, ':')) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002053 options.extra_java_packages.insert(package.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002054 }
2055 }
2056
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002057 if (product_list) {
2058 for (StringPiece product : util::Tokenize(product_list.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002059 if (product != "" && product != "default") {
Adam Lesinskid5083f62017-01-16 15:07:21 -08002060 options.products.insert(product.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002061 }
2062 }
2063 }
2064
2065 AxisConfigFilter filter;
2066 if (configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002067 for (const StringPiece& config_str : util::Tokenize(configs.value(), ',')) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002068 ConfigDescription config;
2069 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002070 if (lv.InitFromFilterString(config_str)) {
2071 lv.WriteTo(&config);
2072 } else if (!ConfigDescription::Parse(config_str, &config)) {
2073 context.GetDiagnostics()->Error(DiagMessage() << "invalid config '"
2074 << config_str
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002075 << "' for -c option");
2076 return 1;
2077 }
2078
2079 if (config.density != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002080 context.GetDiagnostics()->Warn(DiagMessage() << "ignoring density '"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002081 << config
2082 << "' for -c option");
2083 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002084 filter.AddConfig(config);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002085 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002086 }
2087
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002088 options.table_splitter_options.config_filter = &filter;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002089 }
2090
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002091 if (preferred_density) {
2092 ConfigDescription preferred_density_config;
2093 if (!ConfigDescription::Parse(preferred_density.value(),
2094 &preferred_density_config)) {
2095 context.GetDiagnostics()->Error(
2096 DiagMessage() << "invalid density '" << preferred_density.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002097 << "' for --preferred-density option");
2098 return 1;
Adam Lesinskic51562c2016-04-28 11:12:38 -07002099 }
2100
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002101 // Clear the version that can be automatically added.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002102 preferred_density_config.sdkVersion = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002103
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002104 if (preferred_density_config.diff(ConfigDescription::DefaultConfig()) !=
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002105 ConfigDescription::CONFIG_DENSITY) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002106 context.GetDiagnostics()->Error(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002107 DiagMessage() << "invalid preferred density '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002108 << preferred_density.value() << "'. "
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002109 << "Preferred density must only be a density value");
2110 return 1;
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002111 }
Pierre Lecesne672384b2017-02-06 10:29:02 +00002112 options.table_splitter_options.preferred_densities.push_back(preferred_density_config.density);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002113 }
Adam Lesinski1e21ff02016-06-24 14:57:58 -07002114
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002115 if (shared_lib && static_lib) {
2116 context.GetDiagnostics()->Error(DiagMessage()
2117 << "only one of --shared-lib and --static-lib can be defined");
2118 return 1;
2119 }
2120
2121 if (shared_lib) {
2122 options.package_type = PackageType::kSharedLib;
2123 } else if (static_lib) {
2124 options.package_type = PackageType::kStaticLib;
2125 }
2126
2127 if (options.package_type != PackageType::kStaticLib && stable_id_file_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002128 if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(),
2129 &options.stable_id_map)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002130 return 1;
Adam Lesinski64587af2016-02-18 18:33:06 -08002131 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002132 }
Adam Lesinski64587af2016-02-18 18:33:06 -08002133
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002134 // Populate some default no-compress extensions that are already compressed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002135 options.extensions_to_not_compress.insert(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002136 {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg",
2137 ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl",
2138 ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2",
2139 ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"});
2140
2141 // Parse the split parameters.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002142 for (const std::string& split_arg : split_args) {
2143 options.split_paths.push_back({});
2144 options.split_constraints.push_back({});
2145 if (!ParseSplitParameter(split_arg, context.GetDiagnostics(),
2146 &options.split_paths.back(),
2147 &options.split_constraints.back())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002148 return 1;
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002149 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002150 }
Adam Lesinskifc9570e62015-11-16 15:07:54 -08002151
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002152 // Turn off auto versioning for static-libs.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -08002153 if (options.package_type == PackageType::kStaticLib) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002154 options.no_auto_version = true;
2155 options.no_version_vectors = true;
Yuichi Araki4d35cca2017-01-18 20:42:17 +09002156 options.no_version_transitions = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002157 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -08002158
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002159 LinkCommand cmd(&context, options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07002160 return cmd.Run(arg_list);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07002161}
2162
Adam Lesinskicacb28f2016-10-19 12:18:14 -07002163} // namespace aapt