blob: 042926c1943a0b47ffe577c5320b118159dacafb [file] [log] [blame]
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001/*
2 * Copyright (C) 2017 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
Ryan Mitchell833a1a62018-07-10 13:51:36 -070017#include "Optimize.h"
18
felkachang4bdd3ac2022-09-13 10:58:49 +080019#include <map>
Adam Lesinskid0f492d2017-04-03 18:12:45 -070020#include <memory>
felkachang4bdd3ac2022-09-13 10:58:49 +080021#include <set>
22#include <string>
23#include <utility>
Adam Lesinskid0f492d2017-04-03 18:12:45 -070024#include <vector>
25
Adam Lesinskid0f492d2017-04-03 18:12:45 -070026#include "Diagnostics.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070027#include "LoadedApk.h"
28#include "ResourceUtils.h"
29#include "SdkConstants.h"
30#include "ValueVisitor.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000031#include "android-base/file.h"
32#include "android-base/stringprintf.h"
33#include "androidfw/ConfigDescription.h"
34#include "androidfw/IDiagnostics.h"
35#include "androidfw/ResourceTypes.h"
36#include "androidfw/StringPiece.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070037#include "cmd/Util.h"
Shane Farmer57669432017-06-19 12:52:04 -070038#include "configuration/ConfigurationParser.h"
39#include "filter/AbiFilter.h"
Adam Lesinski46708052017-09-29 14:49:15 -070040#include "format/binary/TableFlattener.h"
41#include "format/binary/XmlFlattener.h"
Adam Lesinski00451162017-10-03 07:44:08 -070042#include "io/BigBufferStream.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070043#include "io/Util.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070044#include "optimize/MultiApkGenerator.h"
felkachang4bdd3ac2022-09-13 10:58:49 +080045#include "optimize/Obfuscator.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070046#include "optimize/ResourceDeduper.h"
Mohamed Heikald3c5fb62018-01-12 11:37:26 -050047#include "optimize/ResourceFilter.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070048#include "optimize/VersionCollapser.h"
49#include "split/TableSplitter.h"
Shane Farmer57669432017-06-19 12:52:04 -070050#include "util/Files.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070051#include "util/Util.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070052
Shane Farmer57669432017-06-19 12:52:04 -070053using ::aapt::configuration::Abi;
Shane Farmercb6c3f92017-11-27 13:19:36 -080054using ::aapt::configuration::OutputArtifact;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020055using ::android::ConfigDescription;
Shane Farmer0a5b2012017-06-22 12:24:12 -070056using ::android::ResTable_config;
Shane Farmer57669432017-06-19 12:52:04 -070057using ::android::StringPiece;
Luke Nicholsonb0643302017-12-01 15:29:03 -080058using ::android::base::ReadFileToString;
Shane Farmer0a5b2012017-06-22 12:24:12 -070059using ::android::base::StringAppendF;
Shane Farmer57669432017-06-19 12:52:04 -070060using ::android::base::StringPrintf;
Brian Changdcef8312019-09-13 11:38:32 -070061using ::android::base::WriteStringToFile;
Adam Lesinskid0f492d2017-04-03 18:12:45 -070062
63namespace aapt {
64
Adam Lesinskid0f492d2017-04-03 18:12:45 -070065class OptimizeContext : public IAaptContext {
66 public:
Adam Lesinskib522f042017-04-21 16:57:59 -070067 OptimizeContext() = default;
68
69 PackageType GetPackageType() override {
70 // Not important here. Using anything other than kApp adds EXTRA validation, which we want to
71 // avoid.
72 return PackageType::kApp;
73 }
74
Jeremy Meyer56f36e82022-05-20 20:35:42 +000075 android::IDiagnostics* GetDiagnostics() override {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070076 return &diagnostics_;
77 }
78
79 NameMangler* GetNameMangler() override {
80 UNIMPLEMENTED(FATAL);
81 return nullptr;
82 }
83
84 const std::string& GetCompilationPackage() override {
85 static std::string empty;
86 return empty;
87 }
88
89 uint8_t GetPackageId() override {
90 return 0;
91 }
92
93 SymbolTable* GetExternalSymbols() override {
94 UNIMPLEMENTED(FATAL);
95 return nullptr;
96 }
97
98 bool IsVerbose() override {
99 return verbose_;
100 }
101
102 void SetVerbose(bool val) {
103 verbose_ = val;
104 }
105
106 void SetMinSdkVersion(int sdk_version) {
107 sdk_version_ = sdk_version;
108 }
109
110 int GetMinSdkVersion() override {
111 return sdk_version_;
112 }
113
Udam Sainib228df32019-06-18 16:50:34 -0700114 const std::set<std::string>& GetSplitNameDependencies() override {
115 UNIMPLEMENTED(FATAL) << "Split Name Dependencies should not be necessary";
116 static std::set<std::string> empty;
117 return empty;
118 }
119
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700120 private:
121 StdErrDiagnostics diagnostics_;
122 bool verbose_ = false;
123 int sdk_version_ = 0;
felkachang4bdd3ac2022-09-13 10:58:49 +0800124
125 DISALLOW_COPY_AND_ASSIGN(OptimizeContext);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700126};
127
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700128class Optimizer {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700129 public:
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700130 Optimizer(OptimizeContext* context, const OptimizeOptions& options)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700131 : options_(options), context_(context) {
132 }
133
134 int Run(std::unique_ptr<LoadedApk> apk) {
135 if (context_->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000136 context_->GetDiagnostics()->Note(android::DiagMessage() << "Optimizing APK...");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700137 }
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700138 if (!options_.resources_exclude_list.empty()) {
139 ResourceFilter filter(options_.resources_exclude_list);
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500140 if (!filter.Consume(context_, apk->GetResourceTable())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000141 context_->GetDiagnostics()->Error(android::DiagMessage() << "failed filtering resources");
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500142 return 1;
143 }
144 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700145
146 VersionCollapser collapser;
147 if (!collapser.Consume(context_, apk->GetResourceTable())) {
148 return 1;
149 }
150
151 ResourceDeduper deduper;
152 if (!deduper.Consume(context_, apk->GetResourceTable())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000153 context_->GetDiagnostics()->Error(android::DiagMessage() << "failed deduping resources");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700154 return 1;
155 }
156
Mohamed Heikalc7694032018-11-07 16:49:02 -0500157 if (options_.shorten_resource_paths) {
felkachang4bdd3ac2022-09-13 10:58:49 +0800158 Obfuscator obfuscator(options_.table_flattener_options.shortened_path_map);
159 if (!obfuscator.Consume(context_, apk->GetResourceTable())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000160 context_->GetDiagnostics()->Error(android::DiagMessage()
161 << "failed shortening resource paths");
Mohamed Heikalc7694032018-11-07 16:49:02 -0500162 return 1;
163 }
164 if (options_.shortened_paths_map_path
165 && !WriteShortenedPathsMap(options_.table_flattener_options.shortened_path_map,
166 options_.shortened_paths_map_path.value())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000167 context_->GetDiagnostics()->Error(android::DiagMessage()
Mohamed Heikalc7694032018-11-07 16:49:02 -0500168 << "failed to write shortened resource paths to file");
169 return 1;
170 }
171 }
172
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700173 // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or
174 // equal to the minSdk.
175 options_.split_constraints =
176 AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints);
177
178 // Stripping the APK using the TableSplitter. The resource table is modified in place in the
179 // LoadedApk.
180 TableSplitter splitter(options_.split_constraints, options_.table_splitter_options);
181 if (!splitter.VerifySplitConstraints(context_)) {
182 return 1;
183 }
184 splitter.SplitTable(apk->GetResourceTable());
185
186 auto path_iter = options_.split_paths.begin();
187 auto split_constraints_iter = options_.split_constraints.begin();
188 for (std::unique_ptr<ResourceTable>& split_table : splitter.splits()) {
189 if (context_->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000190 context_->GetDiagnostics()->Note(android::DiagMessage(*path_iter)
191 << "generating split with configurations '"
192 << util::Joiner(split_constraints_iter->configs, ", ")
193 << "'");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700194 }
195
196 // Generate an AndroidManifest.xml for each split.
197 std::unique_ptr<xml::XmlResource> split_manifest =
198 GenerateSplitManifest(options_.app_info, *split_constraints_iter);
199 std::unique_ptr<IArchiveWriter> split_writer =
200 CreateZipFileArchiveWriter(context_->GetDiagnostics(), *path_iter);
201 if (!split_writer) {
202 return 1;
203 }
204
205 if (!WriteSplitApk(split_table.get(), split_manifest.get(), split_writer.get())) {
206 return 1;
207 }
208
209 ++path_iter;
210 ++split_constraints_iter;
211 }
212
Shane Farmercb6c3f92017-11-27 13:19:36 -0800213 if (options_.apk_artifacts && options_.output_dir) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700214 MultiApkGenerator generator{apk.get(), context_};
Shane Farmer666de342017-11-29 16:07:51 -0800215 MultiApkGeneratorOptions generator_options = {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800216 options_.output_dir.value(), options_.apk_artifacts.value(),
217 options_.table_flattener_options, options_.kept_artifacts};
Shane Farmerefe45392017-08-21 14:39:28 -0700218 if (!generator.FromBaseApk(generator_options)) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700219 return 1;
Shane Farmer57669432017-06-19 12:52:04 -0700220 }
221 }
222
223 if (options_.output_path) {
224 std::unique_ptr<IArchiveWriter> writer =
225 CreateZipFileArchiveWriter(context_->GetDiagnostics(), options_.output_path.value());
226 if (!apk->WriteToArchive(context_, options_.table_flattener_options, writer.get())) {
227 return 1;
228 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700229 }
230
231 return 0;
232 }
233
234 private:
235 bool WriteSplitApk(ResourceTable* table, xml::XmlResource* manifest, IArchiveWriter* writer) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000236 android::BigBuffer manifest_buffer(4096);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700237 XmlFlattener xml_flattener(&manifest_buffer, {});
238 if (!xml_flattener.Consume(context_, manifest)) {
239 return false;
240 }
241
242 io::BigBufferInputStream manifest_buffer_in(&manifest_buffer);
243 if (!io::CopyInputStreamToArchive(context_, &manifest_buffer_in, "AndroidManifest.xml",
244 ArchiveEntry::kCompress, writer)) {
245 return false;
246 }
247
248 std::map<std::pair<ConfigDescription, StringPiece>, FileReference*> config_sorted_files;
249 for (auto& pkg : table->packages) {
250 for (auto& type : pkg->types) {
251 // Sort by config and name, so that we get better locality in the zip file.
252 config_sorted_files.clear();
253
254 for (auto& entry : type->entries) {
255 for (auto& config_value : entry->values) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700256 auto* file_ref = ValueCast<FileReference>(config_value->value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700257 if (file_ref == nullptr) {
258 continue;
259 }
260
261 if (file_ref->file == nullptr) {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000262 ResourceNameRef name(pkg->name, type->named_type, entry->name);
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000263 context_->GetDiagnostics()->Warn(android::DiagMessage(file_ref->GetSource())
Shane Farmer57669432017-06-19 12:52:04 -0700264 << "file for resource " << name << " with config '"
265 << config_value->config << "' not found");
Adam Lesinski742888f2017-04-28 15:34:52 -0700266 continue;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700267 }
268
269 const StringPiece entry_name = entry->name;
270 config_sorted_files[std::make_pair(config_value->config, entry_name)] = file_ref;
271 }
272 }
273
274 for (auto& entry : config_sorted_files) {
275 FileReference* file_ref = entry.second;
Pierre Lecesned55bef72017-11-10 22:31:01 +0000276 if (!io::CopyFileToArchivePreserveCompression(context_, file_ref->file, *file_ref->path,
277 writer)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700278 return false;
279 }
280 }
281 }
282 }
283
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000284 android::BigBuffer table_buffer(4096);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700285 TableFlattener table_flattener(options_.table_flattener_options, &table_buffer);
286 if (!table_flattener.Consume(context_, table)) {
287 return false;
288 }
289
290 io::BigBufferInputStream table_buffer_in(&table_buffer);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700291 return io::CopyInputStreamToArchive(context_, &table_buffer_in, "resources.arsc",
292 ArchiveEntry::kAlign, writer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700293 }
294
Mohamed Heikalc7694032018-11-07 16:49:02 -0500295 bool WriteShortenedPathsMap(const std::map<std::string, std::string> &path_map,
296 const std::string &file_path) {
297 std::stringstream ss;
298 for (auto it = path_map.cbegin(); it != path_map.cend(); ++it) {
299 ss << it->first << " -> " << it->second << "\n";
300 }
301 return WriteStringToFile(ss.str(), file_path);
302 }
303
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700304 OptimizeOptions options_;
305 OptimizeContext* context_;
306};
307
Brian Changdcef8312019-09-13 11:38:32 -0700308bool ExtractConfig(const std::string& path, IAaptContext* context, OptimizeOptions* options) {
309 std::string content;
310 if (!android::base::ReadFileToString(path, &content, true /*follow_symlinks*/)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000311 context->GetDiagnostics()->Error(android::DiagMessage(path) << "failed reading config file");
Brian Changdcef8312019-09-13 11:38:32 -0700312 return false;
313 }
Iurii Makhno054e4332022-10-12 16:03:03 +0000314 return ParseResourceConfig(content, context, options->resources_exclude_list,
315 options->table_flattener_options.name_collapse_exemptions);
Brian Changdcef8312019-09-13 11:38:32 -0700316}
317
Adam Lesinski8780eb62017-10-31 17:44:39 -0700318bool ExtractAppDataFromManifest(OptimizeContext* context, const LoadedApk* apk,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700319 OptimizeOptions* out_options) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700320 const xml::XmlResource* manifest = apk->GetManifest();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700321 if (manifest == nullptr) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700322 return false;
323 }
324
Ryan Mitchell4382e442021-07-14 12:53:01 -0700325 auto app_info = ExtractAppInfoFromBinaryManifest(*manifest, context->GetDiagnostics());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700326 if (!app_info) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000327 context->GetDiagnostics()->Error(android::DiagMessage()
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700328 << "failed to extract data from AndroidManifest.xml");
329 return false;
330 }
331
332 out_options->app_info = std::move(app_info.value());
Ryan Mitchell4382e442021-07-14 12:53:01 -0700333 context->SetMinSdkVersion(out_options->app_info.min_sdk_version.value_or(0));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700334 return true;
335}
336
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700337int OptimizeCommand::Action(const std::vector<std::string>& args) {
338 if (args.size() != 1u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700339 std::cerr << "must have one APK as argument.\n\n";
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700340 Usage(&std::cerr);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700341 return 1;
342 }
343
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700344 const std::string& apk_path = args[0];
345 OptimizeContext context;
346 context.SetVerbose(verbose_);
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000347 android::IDiagnostics* diag = context.GetDiagnostics();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700348
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700349 if (config_path_) {
350 std::string& path = config_path_.value();
Ryan Mitchell4382e442021-07-14 12:53:01 -0700351 std::optional<ConfigurationParser> for_path = ConfigurationParser::ForPath(path);
Shane Farmer57669432017-06-19 12:52:04 -0700352 if (for_path) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700353 options_.apk_artifacts = for_path.value().WithDiagnostics(diag).Parse(apk_path);
354 if (!options_.apk_artifacts) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000355 diag->Error(android::DiagMessage() << "Failed to parse the output artifact list");
Shane Farmercb6c3f92017-11-27 13:19:36 -0800356 return 1;
357 }
358
Shane Farmer57669432017-06-19 12:52:04 -0700359 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000360 diag->Error(android::DiagMessage() << "Could not parse config file " << path);
Shane Farmer57669432017-06-19 12:52:04 -0700361 return 1;
362 }
Shane Farmer9ecc0752017-08-24 15:55:36 -0700363
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700364 if (print_only_) {
365 for (const OutputArtifact& artifact : options_.apk_artifacts.value()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800366 std::cout << artifact.name << std::endl;
Shane Farmer9ecc0752017-08-24 15:55:36 -0700367 }
368 return 0;
369 }
370
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700371 if (!kept_artifacts_.empty()) {
372 for (const std::string& artifact_str : kept_artifacts_) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800373 for (const StringPiece& artifact : util::Tokenize(artifact_str, ',')) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700374 options_.kept_artifacts.insert(artifact.to_string());
Shane Farmer666de342017-11-29 16:07:51 -0800375 }
376 }
377 }
378
Shane Farmer9ecc0752017-08-24 15:55:36 -0700379 // Since we know that we are going to process the APK (not just print targets), make sure we
380 // have somewhere to write them to.
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700381 if (!options_.output_dir) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000382 diag->Error(android::DiagMessage()
383 << "Output directory is required when using a configuration file");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700384 return 1;
385 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700386 } else if (print_only_) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000387 diag->Error(android::DiagMessage()
388 << "Asked to print artifacts without providing a configurations");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700389 return 1;
Shane Farmer57669432017-06-19 12:52:04 -0700390 }
391
Shane Farmer2c122412017-12-15 16:55:54 -0800392 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(apk_path, context.GetDiagnostics());
393 if (!apk) {
394 return 1;
395 }
396
Iurii Makhnoda06e4d2022-08-24 14:17:32 +0000397 if (options_.enable_sparse_encoding) {
398 options_.table_flattener_options.sparse_entries = SparseEntriesMode::Enabled;
399 }
400 if (options_.force_sparse_encoding) {
401 options_.table_flattener_options.sparse_entries = SparseEntriesMode::Forced;
402 }
403
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700404 if (target_densities_) {
Shane Farmer2c122412017-12-15 16:55:54 -0800405 // Parse the target screen densities.
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700406 for (const StringPiece& config_str : util::Tokenize(target_densities_.value(), ',')) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700407 std::optional<uint16_t> target_density = ParseTargetDensityParameter(config_str, diag);
Shane Farmer2c122412017-12-15 16:55:54 -0800408 if (!target_density) {
409 return 1;
410 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700411 options_.table_splitter_options.preferred_densities.push_back(target_density.value());
Shane Farmer2c122412017-12-15 16:55:54 -0800412 }
413 }
414
415 std::unique_ptr<IConfigFilter> filter;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700416 if (!configs_.empty()) {
417 filter = ParseConfigFilterParameters(configs_, diag);
Shane Farmer2c122412017-12-15 16:55:54 -0800418 if (filter == nullptr) {
419 return 1;
420 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700421 options_.table_splitter_options.config_filter = filter.get();
Shane Farmer2c122412017-12-15 16:55:54 -0800422 }
423
424 // Parse the split parameters.
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700425 for (const std::string& split_arg : split_args_) {
426 options_.split_paths.emplace_back();
427 options_.split_constraints.emplace_back();
428 if (!ParseSplitParameter(split_arg, diag, &options_.split_paths.back(),
429 &options_.split_constraints.back())) {
Shane Farmer2c122412017-12-15 16:55:54 -0800430 return 1;
431 }
432 }
433
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700434 if (resources_config_path_) {
435 std::string& path = resources_config_path_.value();
436 if (!ExtractConfig(path, &context, &options_)) {
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500437 return 1;
438 }
439 }
440
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700441 if (!ExtractAppDataFromManifest(&context, apk.get(), &options_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700442 return 1;
443 }
444
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700445 Optimizer cmd(&context, options_);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700446 return cmd.Run(std::move(apk));
447}
448
449} // namespace aapt