blob: f045dad6d11a9c1a4f26daf9ad1a64c89ad0d495 [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;
Brandon Liu48d229d2023-05-04 23:54:03 +0000104 diagnostics_.SetVerbose(val);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700105 }
106
107 void SetMinSdkVersion(int sdk_version) {
108 sdk_version_ = sdk_version;
109 }
110
111 int GetMinSdkVersion() override {
112 return sdk_version_;
113 }
114
Udam Sainib228df32019-06-18 16:50:34 -0700115 const std::set<std::string>& GetSplitNameDependencies() override {
116 UNIMPLEMENTED(FATAL) << "Split Name Dependencies should not be necessary";
117 static std::set<std::string> empty;
118 return empty;
119 }
120
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700121 private:
122 StdErrDiagnostics diagnostics_;
123 bool verbose_ = false;
124 int sdk_version_ = 0;
felkachang4bdd3ac2022-09-13 10:58:49 +0800125
126 DISALLOW_COPY_AND_ASSIGN(OptimizeContext);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700127};
128
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700129class Optimizer {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700130 public:
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700131 Optimizer(OptimizeContext* context, const OptimizeOptions& options)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700132 : options_(options), context_(context) {
133 }
134
135 int Run(std::unique_ptr<LoadedApk> apk) {
136 if (context_->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000137 context_->GetDiagnostics()->Note(android::DiagMessage() << "Optimizing APK...");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700138 }
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700139 if (!options_.resources_exclude_list.empty()) {
140 ResourceFilter filter(options_.resources_exclude_list);
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500141 if (!filter.Consume(context_, apk->GetResourceTable())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000142 context_->GetDiagnostics()->Error(android::DiagMessage() << "failed filtering resources");
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500143 return 1;
144 }
145 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700146
147 VersionCollapser collapser;
148 if (!collapser.Consume(context_, apk->GetResourceTable())) {
149 return 1;
150 }
151
152 ResourceDeduper deduper;
153 if (!deduper.Consume(context_, apk->GetResourceTable())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000154 context_->GetDiagnostics()->Error(android::DiagMessage() << "failed deduping resources");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700155 return 1;
156 }
157
felkachang8ceb39c2022-09-13 10:58:49 +0800158 Obfuscator obfuscator(options_);
159 if (obfuscator.IsEnabled()) {
felkachang4bdd3ac2022-09-13 10:58:49 +0800160 if (!obfuscator.Consume(context_, apk->GetResourceTable())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000161 context_->GetDiagnostics()->Error(android::DiagMessage()
162 << "failed shortening resource paths");
Mohamed Heikalc7694032018-11-07 16:49:02 -0500163 return 1;
164 }
felkachang78a8d372022-09-14 15:17:29 +0800165
166 if (options_.obfuscation_map_path &&
167 !obfuscator.WriteObfuscationMap(options_.obfuscation_map_path.value())) {
168 context_->GetDiagnostics()->Error(android::DiagMessage()
169 << "failed to write the obfuscation map to file");
170 return 1;
171 }
172
173 // TODO(b/246489170): keep the old option and format until transform to the new one
Mohamed Heikalc7694032018-11-07 16:49:02 -0500174 if (options_.shortened_paths_map_path
175 && !WriteShortenedPathsMap(options_.table_flattener_options.shortened_path_map,
176 options_.shortened_paths_map_path.value())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000177 context_->GetDiagnostics()->Error(android::DiagMessage()
Mohamed Heikalc7694032018-11-07 16:49:02 -0500178 << "failed to write shortened resource paths to file");
179 return 1;
180 }
181 }
182
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700183 // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or
184 // equal to the minSdk.
185 options_.split_constraints =
186 AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints);
187
188 // Stripping the APK using the TableSplitter. The resource table is modified in place in the
189 // LoadedApk.
190 TableSplitter splitter(options_.split_constraints, options_.table_splitter_options);
191 if (!splitter.VerifySplitConstraints(context_)) {
192 return 1;
193 }
194 splitter.SplitTable(apk->GetResourceTable());
195
196 auto path_iter = options_.split_paths.begin();
197 auto split_constraints_iter = options_.split_constraints.begin();
198 for (std::unique_ptr<ResourceTable>& split_table : splitter.splits()) {
199 if (context_->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000200 context_->GetDiagnostics()->Note(android::DiagMessage(*path_iter)
201 << "generating split with configurations '"
202 << util::Joiner(split_constraints_iter->configs, ", ")
203 << "'");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700204 }
205
206 // Generate an AndroidManifest.xml for each split.
207 std::unique_ptr<xml::XmlResource> split_manifest =
208 GenerateSplitManifest(options_.app_info, *split_constraints_iter);
209 std::unique_ptr<IArchiveWriter> split_writer =
210 CreateZipFileArchiveWriter(context_->GetDiagnostics(), *path_iter);
211 if (!split_writer) {
212 return 1;
213 }
214
215 if (!WriteSplitApk(split_table.get(), split_manifest.get(), split_writer.get())) {
216 return 1;
217 }
218
219 ++path_iter;
220 ++split_constraints_iter;
221 }
222
Shane Farmercb6c3f92017-11-27 13:19:36 -0800223 if (options_.apk_artifacts && options_.output_dir) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700224 MultiApkGenerator generator{apk.get(), context_};
Shane Farmer666de342017-11-29 16:07:51 -0800225 MultiApkGeneratorOptions generator_options = {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800226 options_.output_dir.value(), options_.apk_artifacts.value(),
227 options_.table_flattener_options, options_.kept_artifacts};
Shane Farmerefe45392017-08-21 14:39:28 -0700228 if (!generator.FromBaseApk(generator_options)) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700229 return 1;
Shane Farmer57669432017-06-19 12:52:04 -0700230 }
231 }
232
233 if (options_.output_path) {
234 std::unique_ptr<IArchiveWriter> writer =
235 CreateZipFileArchiveWriter(context_->GetDiagnostics(), options_.output_path.value());
236 if (!apk->WriteToArchive(context_, options_.table_flattener_options, writer.get())) {
237 return 1;
238 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700239 }
240
241 return 0;
242 }
243
244 private:
245 bool WriteSplitApk(ResourceTable* table, xml::XmlResource* manifest, IArchiveWriter* writer) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000246 android::BigBuffer manifest_buffer(4096);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700247 XmlFlattener xml_flattener(&manifest_buffer, {});
248 if (!xml_flattener.Consume(context_, manifest)) {
249 return false;
250 }
251
252 io::BigBufferInputStream manifest_buffer_in(&manifest_buffer);
253 if (!io::CopyInputStreamToArchive(context_, &manifest_buffer_in, "AndroidManifest.xml",
254 ArchiveEntry::kCompress, writer)) {
255 return false;
256 }
257
258 std::map<std::pair<ConfigDescription, StringPiece>, FileReference*> config_sorted_files;
259 for (auto& pkg : table->packages) {
260 for (auto& type : pkg->types) {
261 // Sort by config and name, so that we get better locality in the zip file.
262 config_sorted_files.clear();
263
264 for (auto& entry : type->entries) {
265 for (auto& config_value : entry->values) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700266 auto* file_ref = ValueCast<FileReference>(config_value->value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700267 if (file_ref == nullptr) {
268 continue;
269 }
270
271 if (file_ref->file == nullptr) {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000272 ResourceNameRef name(pkg->name, type->named_type, entry->name);
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000273 context_->GetDiagnostics()->Warn(android::DiagMessage(file_ref->GetSource())
Shane Farmer57669432017-06-19 12:52:04 -0700274 << "file for resource " << name << " with config '"
275 << config_value->config << "' not found");
Adam Lesinski742888f2017-04-28 15:34:52 -0700276 continue;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700277 }
278
279 const StringPiece entry_name = entry->name;
280 config_sorted_files[std::make_pair(config_value->config, entry_name)] = file_ref;
281 }
282 }
283
284 for (auto& entry : config_sorted_files) {
285 FileReference* file_ref = entry.second;
Pierre Lecesned55bef72017-11-10 22:31:01 +0000286 if (!io::CopyFileToArchivePreserveCompression(context_, file_ref->file, *file_ref->path,
287 writer)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700288 return false;
289 }
290 }
291 }
292 }
293
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000294 android::BigBuffer table_buffer(4096);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700295 TableFlattener table_flattener(options_.table_flattener_options, &table_buffer);
296 if (!table_flattener.Consume(context_, table)) {
297 return false;
298 }
299
300 io::BigBufferInputStream table_buffer_in(&table_buffer);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700301 return io::CopyInputStreamToArchive(context_, &table_buffer_in, "resources.arsc",
302 ArchiveEntry::kAlign, writer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700303 }
304
felkachang78a8d372022-09-14 15:17:29 +0800305 // TODO(b/246489170): keep the old option and format until transform to the new one
Mohamed Heikalc7694032018-11-07 16:49:02 -0500306 bool WriteShortenedPathsMap(const std::map<std::string, std::string> &path_map,
307 const std::string &file_path) {
308 std::stringstream ss;
309 for (auto it = path_map.cbegin(); it != path_map.cend(); ++it) {
310 ss << it->first << " -> " << it->second << "\n";
311 }
312 return WriteStringToFile(ss.str(), file_path);
313 }
314
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700315 OptimizeOptions options_;
316 OptimizeContext* context_;
317};
318
Brian Changdcef8312019-09-13 11:38:32 -0700319bool ExtractConfig(const std::string& path, IAaptContext* context, OptimizeOptions* options) {
320 std::string content;
321 if (!android::base::ReadFileToString(path, &content, true /*follow_symlinks*/)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000322 context->GetDiagnostics()->Error(android::DiagMessage(path) << "failed reading config file");
Brian Changdcef8312019-09-13 11:38:32 -0700323 return false;
324 }
Iurii Makhno054e4332022-10-12 16:03:03 +0000325 return ParseResourceConfig(content, context, options->resources_exclude_list,
branliuf1ed5232022-12-16 19:02:29 +0800326 options->table_flattener_options.name_collapse_exemptions,
327 options->table_flattener_options.path_shorten_exemptions);
Brian Changdcef8312019-09-13 11:38:32 -0700328}
329
Adam Lesinski8780eb62017-10-31 17:44:39 -0700330bool ExtractAppDataFromManifest(OptimizeContext* context, const LoadedApk* apk,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700331 OptimizeOptions* out_options) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700332 const xml::XmlResource* manifest = apk->GetManifest();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700333 if (manifest == nullptr) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700334 return false;
335 }
336
Ryan Mitchell4382e442021-07-14 12:53:01 -0700337 auto app_info = ExtractAppInfoFromBinaryManifest(*manifest, context->GetDiagnostics());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700338 if (!app_info) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000339 context->GetDiagnostics()->Error(android::DiagMessage()
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700340 << "failed to extract data from AndroidManifest.xml");
341 return false;
342 }
343
344 out_options->app_info = std::move(app_info.value());
Ryan Mitchell4382e442021-07-14 12:53:01 -0700345 context->SetMinSdkVersion(out_options->app_info.min_sdk_version.value_or(0));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700346 return true;
347}
348
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700349int OptimizeCommand::Action(const std::vector<std::string>& args) {
350 if (args.size() != 1u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700351 std::cerr << "must have one APK as argument.\n\n";
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700352 Usage(&std::cerr);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700353 return 1;
354 }
355
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700356 const std::string& apk_path = args[0];
357 OptimizeContext context;
358 context.SetVerbose(verbose_);
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000359 android::IDiagnostics* diag = context.GetDiagnostics();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700360
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700361 if (config_path_) {
362 std::string& path = config_path_.value();
Ryan Mitchell4382e442021-07-14 12:53:01 -0700363 std::optional<ConfigurationParser> for_path = ConfigurationParser::ForPath(path);
Shane Farmer57669432017-06-19 12:52:04 -0700364 if (for_path) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700365 options_.apk_artifacts = for_path.value().WithDiagnostics(diag).Parse(apk_path);
366 if (!options_.apk_artifacts) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000367 diag->Error(android::DiagMessage() << "Failed to parse the output artifact list");
Shane Farmercb6c3f92017-11-27 13:19:36 -0800368 return 1;
369 }
370
Shane Farmer57669432017-06-19 12:52:04 -0700371 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000372 diag->Error(android::DiagMessage() << "Could not parse config file " << path);
Shane Farmer57669432017-06-19 12:52:04 -0700373 return 1;
374 }
Shane Farmer9ecc0752017-08-24 15:55:36 -0700375
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700376 if (print_only_) {
377 for (const OutputArtifact& artifact : options_.apk_artifacts.value()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800378 std::cout << artifact.name << std::endl;
Shane Farmer9ecc0752017-08-24 15:55:36 -0700379 }
380 return 0;
381 }
382
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700383 if (!kept_artifacts_.empty()) {
384 for (const std::string& artifact_str : kept_artifacts_) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700385 for (StringPiece artifact : util::Tokenize(artifact_str, ',')) {
386 options_.kept_artifacts.emplace(artifact);
Shane Farmer666de342017-11-29 16:07:51 -0800387 }
388 }
389 }
390
Shane Farmer9ecc0752017-08-24 15:55:36 -0700391 // Since we know that we are going to process the APK (not just print targets), make sure we
392 // have somewhere to write them to.
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700393 if (!options_.output_dir) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000394 diag->Error(android::DiagMessage()
395 << "Output directory is required when using a configuration file");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700396 return 1;
397 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700398 } else if (print_only_) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000399 diag->Error(android::DiagMessage()
400 << "Asked to print artifacts without providing a configurations");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700401 return 1;
Shane Farmer57669432017-06-19 12:52:04 -0700402 }
403
Shane Farmer2c122412017-12-15 16:55:54 -0800404 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(apk_path, context.GetDiagnostics());
405 if (!apk) {
406 return 1;
407 }
408
Iurii Makhnoda06e4d2022-08-24 14:17:32 +0000409 if (options_.enable_sparse_encoding) {
410 options_.table_flattener_options.sparse_entries = SparseEntriesMode::Enabled;
411 }
412 if (options_.force_sparse_encoding) {
413 options_.table_flattener_options.sparse_entries = SparseEntriesMode::Forced;
414 }
415
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700416 if (target_densities_) {
Shane Farmer2c122412017-12-15 16:55:54 -0800417 // Parse the target screen densities.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700418 for (StringPiece config_str : util::Tokenize(target_densities_.value(), ',')) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700419 std::optional<uint16_t> target_density = ParseTargetDensityParameter(config_str, diag);
Shane Farmer2c122412017-12-15 16:55:54 -0800420 if (!target_density) {
421 return 1;
422 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700423 options_.table_splitter_options.preferred_densities.push_back(target_density.value());
Shane Farmer2c122412017-12-15 16:55:54 -0800424 }
425 }
426
427 std::unique_ptr<IConfigFilter> filter;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700428 if (!configs_.empty()) {
429 filter = ParseConfigFilterParameters(configs_, diag);
Shane Farmer2c122412017-12-15 16:55:54 -0800430 if (filter == nullptr) {
431 return 1;
432 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700433 options_.table_splitter_options.config_filter = filter.get();
Shane Farmer2c122412017-12-15 16:55:54 -0800434 }
435
436 // Parse the split parameters.
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700437 for (const std::string& split_arg : split_args_) {
438 options_.split_paths.emplace_back();
439 options_.split_constraints.emplace_back();
440 if (!ParseSplitParameter(split_arg, diag, &options_.split_paths.back(),
441 &options_.split_constraints.back())) {
Shane Farmer2c122412017-12-15 16:55:54 -0800442 return 1;
443 }
444 }
445
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700446 if (resources_config_path_) {
447 std::string& path = resources_config_path_.value();
448 if (!ExtractConfig(path, &context, &options_)) {
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500449 return 1;
450 }
451 }
452
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700453 if (!ExtractAppDataFromManifest(&context, apk.get(), &options_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700454 return 1;
455 }
456
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700457 Optimizer cmd(&context, options_);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700458 return cmd.Run(std::move(apk));
459}
460
461} // namespace aapt