blob: dbe79701bf5cbf7e2e6ad1902942e023bb0e954f [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
felkachang8ceb39c2022-09-13 10:58:49 +0800157 Obfuscator obfuscator(options_);
158 if (obfuscator.IsEnabled()) {
felkachang4bdd3ac2022-09-13 10:58:49 +0800159 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 }
felkachang78a8d372022-09-14 15:17:29 +0800164
165 if (options_.obfuscation_map_path &&
166 !obfuscator.WriteObfuscationMap(options_.obfuscation_map_path.value())) {
167 context_->GetDiagnostics()->Error(android::DiagMessage()
168 << "failed to write the obfuscation map to file");
169 return 1;
170 }
171
172 // TODO(b/246489170): keep the old option and format until transform to the new one
Mohamed Heikalc7694032018-11-07 16:49:02 -0500173 if (options_.shortened_paths_map_path
174 && !WriteShortenedPathsMap(options_.table_flattener_options.shortened_path_map,
175 options_.shortened_paths_map_path.value())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000176 context_->GetDiagnostics()->Error(android::DiagMessage()
Mohamed Heikalc7694032018-11-07 16:49:02 -0500177 << "failed to write shortened resource paths to file");
178 return 1;
179 }
180 }
181
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700182 // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or
183 // equal to the minSdk.
184 options_.split_constraints =
185 AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints);
186
187 // Stripping the APK using the TableSplitter. The resource table is modified in place in the
188 // LoadedApk.
189 TableSplitter splitter(options_.split_constraints, options_.table_splitter_options);
190 if (!splitter.VerifySplitConstraints(context_)) {
191 return 1;
192 }
193 splitter.SplitTable(apk->GetResourceTable());
194
195 auto path_iter = options_.split_paths.begin();
196 auto split_constraints_iter = options_.split_constraints.begin();
197 for (std::unique_ptr<ResourceTable>& split_table : splitter.splits()) {
198 if (context_->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000199 context_->GetDiagnostics()->Note(android::DiagMessage(*path_iter)
200 << "generating split with configurations '"
201 << util::Joiner(split_constraints_iter->configs, ", ")
202 << "'");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700203 }
204
205 // Generate an AndroidManifest.xml for each split.
206 std::unique_ptr<xml::XmlResource> split_manifest =
207 GenerateSplitManifest(options_.app_info, *split_constraints_iter);
208 std::unique_ptr<IArchiveWriter> split_writer =
209 CreateZipFileArchiveWriter(context_->GetDiagnostics(), *path_iter);
210 if (!split_writer) {
211 return 1;
212 }
213
214 if (!WriteSplitApk(split_table.get(), split_manifest.get(), split_writer.get())) {
215 return 1;
216 }
217
218 ++path_iter;
219 ++split_constraints_iter;
220 }
221
Shane Farmercb6c3f92017-11-27 13:19:36 -0800222 if (options_.apk_artifacts && options_.output_dir) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700223 MultiApkGenerator generator{apk.get(), context_};
Shane Farmer666de342017-11-29 16:07:51 -0800224 MultiApkGeneratorOptions generator_options = {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800225 options_.output_dir.value(), options_.apk_artifacts.value(),
226 options_.table_flattener_options, options_.kept_artifacts};
Shane Farmerefe45392017-08-21 14:39:28 -0700227 if (!generator.FromBaseApk(generator_options)) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700228 return 1;
Shane Farmer57669432017-06-19 12:52:04 -0700229 }
230 }
231
232 if (options_.output_path) {
233 std::unique_ptr<IArchiveWriter> writer =
234 CreateZipFileArchiveWriter(context_->GetDiagnostics(), options_.output_path.value());
235 if (!apk->WriteToArchive(context_, options_.table_flattener_options, writer.get())) {
236 return 1;
237 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700238 }
239
240 return 0;
241 }
242
243 private:
244 bool WriteSplitApk(ResourceTable* table, xml::XmlResource* manifest, IArchiveWriter* writer) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000245 android::BigBuffer manifest_buffer(4096);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700246 XmlFlattener xml_flattener(&manifest_buffer, {});
247 if (!xml_flattener.Consume(context_, manifest)) {
248 return false;
249 }
250
251 io::BigBufferInputStream manifest_buffer_in(&manifest_buffer);
252 if (!io::CopyInputStreamToArchive(context_, &manifest_buffer_in, "AndroidManifest.xml",
253 ArchiveEntry::kCompress, writer)) {
254 return false;
255 }
256
257 std::map<std::pair<ConfigDescription, StringPiece>, FileReference*> config_sorted_files;
258 for (auto& pkg : table->packages) {
259 for (auto& type : pkg->types) {
260 // Sort by config and name, so that we get better locality in the zip file.
261 config_sorted_files.clear();
262
263 for (auto& entry : type->entries) {
264 for (auto& config_value : entry->values) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700265 auto* file_ref = ValueCast<FileReference>(config_value->value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700266 if (file_ref == nullptr) {
267 continue;
268 }
269
270 if (file_ref->file == nullptr) {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000271 ResourceNameRef name(pkg->name, type->named_type, entry->name);
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000272 context_->GetDiagnostics()->Warn(android::DiagMessage(file_ref->GetSource())
Shane Farmer57669432017-06-19 12:52:04 -0700273 << "file for resource " << name << " with config '"
274 << config_value->config << "' not found");
Adam Lesinski742888f2017-04-28 15:34:52 -0700275 continue;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700276 }
277
278 const StringPiece entry_name = entry->name;
279 config_sorted_files[std::make_pair(config_value->config, entry_name)] = file_ref;
280 }
281 }
282
283 for (auto& entry : config_sorted_files) {
284 FileReference* file_ref = entry.second;
Pierre Lecesned55bef72017-11-10 22:31:01 +0000285 if (!io::CopyFileToArchivePreserveCompression(context_, file_ref->file, *file_ref->path,
286 writer)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700287 return false;
288 }
289 }
290 }
291 }
292
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000293 android::BigBuffer table_buffer(4096);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700294 TableFlattener table_flattener(options_.table_flattener_options, &table_buffer);
295 if (!table_flattener.Consume(context_, table)) {
296 return false;
297 }
298
299 io::BigBufferInputStream table_buffer_in(&table_buffer);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700300 return io::CopyInputStreamToArchive(context_, &table_buffer_in, "resources.arsc",
301 ArchiveEntry::kAlign, writer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700302 }
303
felkachang78a8d372022-09-14 15:17:29 +0800304 // TODO(b/246489170): keep the old option and format until transform to the new one
Mohamed Heikalc7694032018-11-07 16:49:02 -0500305 bool WriteShortenedPathsMap(const std::map<std::string, std::string> &path_map,
306 const std::string &file_path) {
307 std::stringstream ss;
308 for (auto it = path_map.cbegin(); it != path_map.cend(); ++it) {
309 ss << it->first << " -> " << it->second << "\n";
310 }
311 return WriteStringToFile(ss.str(), file_path);
312 }
313
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700314 OptimizeOptions options_;
315 OptimizeContext* context_;
316};
317
Brian Changdcef8312019-09-13 11:38:32 -0700318bool ExtractConfig(const std::string& path, IAaptContext* context, OptimizeOptions* options) {
319 std::string content;
320 if (!android::base::ReadFileToString(path, &content, true /*follow_symlinks*/)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000321 context->GetDiagnostics()->Error(android::DiagMessage(path) << "failed reading config file");
Brian Changdcef8312019-09-13 11:38:32 -0700322 return false;
323 }
Iurii Makhno054e4332022-10-12 16:03:03 +0000324 return ParseResourceConfig(content, context, options->resources_exclude_list,
branliuf1ed5232022-12-16 19:02:29 +0800325 options->table_flattener_options.name_collapse_exemptions,
326 options->table_flattener_options.path_shorten_exemptions);
Brian Changdcef8312019-09-13 11:38:32 -0700327}
328
Adam Lesinski8780eb62017-10-31 17:44:39 -0700329bool ExtractAppDataFromManifest(OptimizeContext* context, const LoadedApk* apk,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700330 OptimizeOptions* out_options) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700331 const xml::XmlResource* manifest = apk->GetManifest();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700332 if (manifest == nullptr) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700333 return false;
334 }
335
Ryan Mitchell4382e442021-07-14 12:53:01 -0700336 auto app_info = ExtractAppInfoFromBinaryManifest(*manifest, context->GetDiagnostics());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700337 if (!app_info) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000338 context->GetDiagnostics()->Error(android::DiagMessage()
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700339 << "failed to extract data from AndroidManifest.xml");
340 return false;
341 }
342
343 out_options->app_info = std::move(app_info.value());
Ryan Mitchell4382e442021-07-14 12:53:01 -0700344 context->SetMinSdkVersion(out_options->app_info.min_sdk_version.value_or(0));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700345 return true;
346}
347
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700348int OptimizeCommand::Action(const std::vector<std::string>& args) {
349 if (args.size() != 1u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700350 std::cerr << "must have one APK as argument.\n\n";
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700351 Usage(&std::cerr);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700352 return 1;
353 }
354
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700355 const std::string& apk_path = args[0];
356 OptimizeContext context;
357 context.SetVerbose(verbose_);
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000358 android::IDiagnostics* diag = context.GetDiagnostics();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700359
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700360 if (config_path_) {
361 std::string& path = config_path_.value();
Ryan Mitchell4382e442021-07-14 12:53:01 -0700362 std::optional<ConfigurationParser> for_path = ConfigurationParser::ForPath(path);
Shane Farmer57669432017-06-19 12:52:04 -0700363 if (for_path) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700364 options_.apk_artifacts = for_path.value().WithDiagnostics(diag).Parse(apk_path);
365 if (!options_.apk_artifacts) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000366 diag->Error(android::DiagMessage() << "Failed to parse the output artifact list");
Shane Farmercb6c3f92017-11-27 13:19:36 -0800367 return 1;
368 }
369
Shane Farmer57669432017-06-19 12:52:04 -0700370 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000371 diag->Error(android::DiagMessage() << "Could not parse config file " << path);
Shane Farmer57669432017-06-19 12:52:04 -0700372 return 1;
373 }
Shane Farmer9ecc0752017-08-24 15:55:36 -0700374
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700375 if (print_only_) {
376 for (const OutputArtifact& artifact : options_.apk_artifacts.value()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800377 std::cout << artifact.name << std::endl;
Shane Farmer9ecc0752017-08-24 15:55:36 -0700378 }
379 return 0;
380 }
381
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700382 if (!kept_artifacts_.empty()) {
383 for (const std::string& artifact_str : kept_artifacts_) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700384 for (StringPiece artifact : util::Tokenize(artifact_str, ',')) {
385 options_.kept_artifacts.emplace(artifact);
Shane Farmer666de342017-11-29 16:07:51 -0800386 }
387 }
388 }
389
Shane Farmer9ecc0752017-08-24 15:55:36 -0700390 // Since we know that we are going to process the APK (not just print targets), make sure we
391 // have somewhere to write them to.
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700392 if (!options_.output_dir) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000393 diag->Error(android::DiagMessage()
394 << "Output directory is required when using a configuration file");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700395 return 1;
396 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700397 } else if (print_only_) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000398 diag->Error(android::DiagMessage()
399 << "Asked to print artifacts without providing a configurations");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700400 return 1;
Shane Farmer57669432017-06-19 12:52:04 -0700401 }
402
Shane Farmer2c122412017-12-15 16:55:54 -0800403 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(apk_path, context.GetDiagnostics());
404 if (!apk) {
405 return 1;
406 }
407
Iurii Makhnoda06e4d2022-08-24 14:17:32 +0000408 if (options_.enable_sparse_encoding) {
409 options_.table_flattener_options.sparse_entries = SparseEntriesMode::Enabled;
410 }
411 if (options_.force_sparse_encoding) {
412 options_.table_flattener_options.sparse_entries = SparseEntriesMode::Forced;
413 }
414
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700415 if (target_densities_) {
Shane Farmer2c122412017-12-15 16:55:54 -0800416 // Parse the target screen densities.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700417 for (StringPiece config_str : util::Tokenize(target_densities_.value(), ',')) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700418 std::optional<uint16_t> target_density = ParseTargetDensityParameter(config_str, diag);
Shane Farmer2c122412017-12-15 16:55:54 -0800419 if (!target_density) {
420 return 1;
421 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700422 options_.table_splitter_options.preferred_densities.push_back(target_density.value());
Shane Farmer2c122412017-12-15 16:55:54 -0800423 }
424 }
425
426 std::unique_ptr<IConfigFilter> filter;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700427 if (!configs_.empty()) {
428 filter = ParseConfigFilterParameters(configs_, diag);
Shane Farmer2c122412017-12-15 16:55:54 -0800429 if (filter == nullptr) {
430 return 1;
431 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700432 options_.table_splitter_options.config_filter = filter.get();
Shane Farmer2c122412017-12-15 16:55:54 -0800433 }
434
435 // Parse the split parameters.
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700436 for (const std::string& split_arg : split_args_) {
437 options_.split_paths.emplace_back();
438 options_.split_constraints.emplace_back();
439 if (!ParseSplitParameter(split_arg, diag, &options_.split_paths.back(),
440 &options_.split_constraints.back())) {
Shane Farmer2c122412017-12-15 16:55:54 -0800441 return 1;
442 }
443 }
444
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700445 if (resources_config_path_) {
446 std::string& path = resources_config_path_.value();
447 if (!ExtractConfig(path, &context, &options_)) {
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500448 return 1;
449 }
450 }
451
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700452 if (!ExtractAppDataFromManifest(&context, apk.get(), &options_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700453 return 1;
454 }
455
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700456 Optimizer cmd(&context, options_);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700457 return cmd.Run(std::move(apk));
458}
459
460} // namespace aapt