blob: 4033983f4c0f2be8899f10f3a699f9595fa49f11 [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
Adam Lesinskid0f492d2017-04-03 18:12:45 -070019#include <memory>
20#include <vector>
21
Adam Lesinskid0f492d2017-04-03 18:12:45 -070022#include "Diagnostics.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070023#include "LoadedApk.h"
24#include "ResourceUtils.h"
25#include "SdkConstants.h"
26#include "ValueVisitor.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000027#include "android-base/file.h"
28#include "android-base/stringprintf.h"
29#include "androidfw/ConfigDescription.h"
30#include "androidfw/IDiagnostics.h"
31#include "androidfw/ResourceTypes.h"
32#include "androidfw/StringPiece.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070033#include "cmd/Util.h"
Shane Farmer57669432017-06-19 12:52:04 -070034#include "configuration/ConfigurationParser.h"
35#include "filter/AbiFilter.h"
Adam Lesinski46708052017-09-29 14:49:15 -070036#include "format/binary/TableFlattener.h"
37#include "format/binary/XmlFlattener.h"
Adam Lesinski00451162017-10-03 07:44:08 -070038#include "io/BigBufferStream.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070039#include "io/Util.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070040#include "optimize/MultiApkGenerator.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070041#include "optimize/ResourceDeduper.h"
Mohamed Heikald3c5fb62018-01-12 11:37:26 -050042#include "optimize/ResourceFilter.h"
Mohamed Heikalc7694032018-11-07 16:49:02 -050043#include "optimize/ResourcePathShortener.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070044#include "optimize/VersionCollapser.h"
45#include "split/TableSplitter.h"
Shane Farmer57669432017-06-19 12:52:04 -070046#include "util/Files.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070047#include "util/Util.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070048
Shane Farmer57669432017-06-19 12:52:04 -070049using ::aapt::configuration::Abi;
Shane Farmercb6c3f92017-11-27 13:19:36 -080050using ::aapt::configuration::OutputArtifact;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020051using ::android::ConfigDescription;
Shane Farmer0a5b2012017-06-22 12:24:12 -070052using ::android::ResTable_config;
Shane Farmer57669432017-06-19 12:52:04 -070053using ::android::StringPiece;
Luke Nicholsonb0643302017-12-01 15:29:03 -080054using ::android::base::ReadFileToString;
Shane Farmer0a5b2012017-06-22 12:24:12 -070055using ::android::base::StringAppendF;
Shane Farmer57669432017-06-19 12:52:04 -070056using ::android::base::StringPrintf;
Brian Changdcef8312019-09-13 11:38:32 -070057using ::android::base::WriteStringToFile;
Adam Lesinskid0f492d2017-04-03 18:12:45 -070058
59namespace aapt {
60
Adam Lesinskid0f492d2017-04-03 18:12:45 -070061class OptimizeContext : public IAaptContext {
62 public:
Adam Lesinskib522f042017-04-21 16:57:59 -070063 OptimizeContext() = default;
64
65 PackageType GetPackageType() override {
66 // Not important here. Using anything other than kApp adds EXTRA validation, which we want to
67 // avoid.
68 return PackageType::kApp;
69 }
70
Jeremy Meyer56f36e82022-05-20 20:35:42 +000071 android::IDiagnostics* GetDiagnostics() override {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070072 return &diagnostics_;
73 }
74
75 NameMangler* GetNameMangler() override {
76 UNIMPLEMENTED(FATAL);
77 return nullptr;
78 }
79
80 const std::string& GetCompilationPackage() override {
81 static std::string empty;
82 return empty;
83 }
84
85 uint8_t GetPackageId() override {
86 return 0;
87 }
88
89 SymbolTable* GetExternalSymbols() override {
90 UNIMPLEMENTED(FATAL);
91 return nullptr;
92 }
93
94 bool IsVerbose() override {
95 return verbose_;
96 }
97
98 void SetVerbose(bool val) {
99 verbose_ = val;
100 }
101
102 void SetMinSdkVersion(int sdk_version) {
103 sdk_version_ = sdk_version;
104 }
105
106 int GetMinSdkVersion() override {
107 return sdk_version_;
108 }
109
Udam Sainib228df32019-06-18 16:50:34 -0700110 const std::set<std::string>& GetSplitNameDependencies() override {
111 UNIMPLEMENTED(FATAL) << "Split Name Dependencies should not be necessary";
112 static std::set<std::string> empty;
113 return empty;
114 }
115
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700116 private:
Adam Lesinskib522f042017-04-21 16:57:59 -0700117 DISALLOW_COPY_AND_ASSIGN(OptimizeContext);
118
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700119 StdErrDiagnostics diagnostics_;
120 bool verbose_ = false;
121 int sdk_version_ = 0;
122};
123
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700124class Optimizer {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700125 public:
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700126 Optimizer(OptimizeContext* context, const OptimizeOptions& options)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700127 : options_(options), context_(context) {
128 }
129
130 int Run(std::unique_ptr<LoadedApk> apk) {
131 if (context_->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000132 context_->GetDiagnostics()->Note(android::DiagMessage() << "Optimizing APK...");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700133 }
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700134 if (!options_.resources_exclude_list.empty()) {
135 ResourceFilter filter(options_.resources_exclude_list);
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500136 if (!filter.Consume(context_, apk->GetResourceTable())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000137 context_->GetDiagnostics()->Error(android::DiagMessage() << "failed filtering resources");
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500138 return 1;
139 }
140 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700141
142 VersionCollapser collapser;
143 if (!collapser.Consume(context_, apk->GetResourceTable())) {
144 return 1;
145 }
146
147 ResourceDeduper deduper;
148 if (!deduper.Consume(context_, apk->GetResourceTable())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000149 context_->GetDiagnostics()->Error(android::DiagMessage() << "failed deduping resources");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700150 return 1;
151 }
152
Mohamed Heikalc7694032018-11-07 16:49:02 -0500153 if (options_.shorten_resource_paths) {
154 ResourcePathShortener shortener(options_.table_flattener_options.shortened_path_map);
155 if (!shortener.Consume(context_, apk->GetResourceTable())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000156 context_->GetDiagnostics()->Error(android::DiagMessage()
157 << "failed shortening resource paths");
Mohamed Heikalc7694032018-11-07 16:49:02 -0500158 return 1;
159 }
160 if (options_.shortened_paths_map_path
161 && !WriteShortenedPathsMap(options_.table_flattener_options.shortened_path_map,
162 options_.shortened_paths_map_path.value())) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000163 context_->GetDiagnostics()->Error(android::DiagMessage()
Mohamed Heikalc7694032018-11-07 16:49:02 -0500164 << "failed to write shortened resource paths to file");
165 return 1;
166 }
167 }
168
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700169 // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or
170 // equal to the minSdk.
171 options_.split_constraints =
172 AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints);
173
174 // Stripping the APK using the TableSplitter. The resource table is modified in place in the
175 // LoadedApk.
176 TableSplitter splitter(options_.split_constraints, options_.table_splitter_options);
177 if (!splitter.VerifySplitConstraints(context_)) {
178 return 1;
179 }
180 splitter.SplitTable(apk->GetResourceTable());
181
182 auto path_iter = options_.split_paths.begin();
183 auto split_constraints_iter = options_.split_constraints.begin();
184 for (std::unique_ptr<ResourceTable>& split_table : splitter.splits()) {
185 if (context_->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000186 context_->GetDiagnostics()->Note(android::DiagMessage(*path_iter)
187 << "generating split with configurations '"
188 << util::Joiner(split_constraints_iter->configs, ", ")
189 << "'");
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700190 }
191
192 // Generate an AndroidManifest.xml for each split.
193 std::unique_ptr<xml::XmlResource> split_manifest =
194 GenerateSplitManifest(options_.app_info, *split_constraints_iter);
195 std::unique_ptr<IArchiveWriter> split_writer =
196 CreateZipFileArchiveWriter(context_->GetDiagnostics(), *path_iter);
197 if (!split_writer) {
198 return 1;
199 }
200
201 if (!WriteSplitApk(split_table.get(), split_manifest.get(), split_writer.get())) {
202 return 1;
203 }
204
205 ++path_iter;
206 ++split_constraints_iter;
207 }
208
Shane Farmercb6c3f92017-11-27 13:19:36 -0800209 if (options_.apk_artifacts && options_.output_dir) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700210 MultiApkGenerator generator{apk.get(), context_};
Shane Farmer666de342017-11-29 16:07:51 -0800211 MultiApkGeneratorOptions generator_options = {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800212 options_.output_dir.value(), options_.apk_artifacts.value(),
213 options_.table_flattener_options, options_.kept_artifacts};
Shane Farmerefe45392017-08-21 14:39:28 -0700214 if (!generator.FromBaseApk(generator_options)) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700215 return 1;
Shane Farmer57669432017-06-19 12:52:04 -0700216 }
217 }
218
219 if (options_.output_path) {
220 std::unique_ptr<IArchiveWriter> writer =
221 CreateZipFileArchiveWriter(context_->GetDiagnostics(), options_.output_path.value());
222 if (!apk->WriteToArchive(context_, options_.table_flattener_options, writer.get())) {
223 return 1;
224 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700225 }
226
227 return 0;
228 }
229
230 private:
231 bool WriteSplitApk(ResourceTable* table, xml::XmlResource* manifest, IArchiveWriter* writer) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000232 android::BigBuffer manifest_buffer(4096);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700233 XmlFlattener xml_flattener(&manifest_buffer, {});
234 if (!xml_flattener.Consume(context_, manifest)) {
235 return false;
236 }
237
238 io::BigBufferInputStream manifest_buffer_in(&manifest_buffer);
239 if (!io::CopyInputStreamToArchive(context_, &manifest_buffer_in, "AndroidManifest.xml",
240 ArchiveEntry::kCompress, writer)) {
241 return false;
242 }
243
244 std::map<std::pair<ConfigDescription, StringPiece>, FileReference*> config_sorted_files;
245 for (auto& pkg : table->packages) {
246 for (auto& type : pkg->types) {
247 // Sort by config and name, so that we get better locality in the zip file.
248 config_sorted_files.clear();
249
250 for (auto& entry : type->entries) {
251 for (auto& config_value : entry->values) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700252 auto* file_ref = ValueCast<FileReference>(config_value->value.get());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700253 if (file_ref == nullptr) {
254 continue;
255 }
256
257 if (file_ref->file == nullptr) {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000258 ResourceNameRef name(pkg->name, type->named_type, entry->name);
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000259 context_->GetDiagnostics()->Warn(android::DiagMessage(file_ref->GetSource())
Shane Farmer57669432017-06-19 12:52:04 -0700260 << "file for resource " << name << " with config '"
261 << config_value->config << "' not found");
Adam Lesinski742888f2017-04-28 15:34:52 -0700262 continue;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700263 }
264
265 const StringPiece entry_name = entry->name;
266 config_sorted_files[std::make_pair(config_value->config, entry_name)] = file_ref;
267 }
268 }
269
270 for (auto& entry : config_sorted_files) {
271 FileReference* file_ref = entry.second;
Pierre Lecesned55bef72017-11-10 22:31:01 +0000272 if (!io::CopyFileToArchivePreserveCompression(context_, file_ref->file, *file_ref->path,
273 writer)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700274 return false;
275 }
276 }
277 }
278 }
279
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000280 android::BigBuffer table_buffer(4096);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700281 TableFlattener table_flattener(options_.table_flattener_options, &table_buffer);
282 if (!table_flattener.Consume(context_, table)) {
283 return false;
284 }
285
286 io::BigBufferInputStream table_buffer_in(&table_buffer);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700287 return io::CopyInputStreamToArchive(context_, &table_buffer_in, "resources.arsc",
288 ArchiveEntry::kAlign, writer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700289 }
290
Mohamed Heikalc7694032018-11-07 16:49:02 -0500291 bool WriteShortenedPathsMap(const std::map<std::string, std::string> &path_map,
292 const std::string &file_path) {
293 std::stringstream ss;
294 for (auto it = path_map.cbegin(); it != path_map.cend(); ++it) {
295 ss << it->first << " -> " << it->second << "\n";
296 }
297 return WriteStringToFile(ss.str(), file_path);
298 }
299
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700300 OptimizeOptions options_;
301 OptimizeContext* context_;
302};
303
Brian Changdcef8312019-09-13 11:38:32 -0700304bool ParseConfig(const std::string& content, IAaptContext* context, OptimizeOptions* options) {
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500305 size_t line_no = 0;
306 for (StringPiece line : util::Tokenize(content, '\n')) {
307 line_no++;
308 line = util::TrimWhitespace(line);
309 if (line.empty()) {
310 continue;
311 }
312
313 auto split_line = util::Split(line, '#');
314 if (split_line.size() < 2) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000315 context->GetDiagnostics()->Error(android::DiagMessage(line) << "No # found in line");
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500316 return false;
317 }
318 StringPiece resource_string = split_line[0];
319 StringPiece directives = split_line[1];
320 ResourceNameRef resource_name;
321 if (!ResourceUtils::ParseResourceName(resource_string, &resource_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000322 context->GetDiagnostics()->Error(android::DiagMessage(line) << "Malformed resource name");
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500323 return false;
324 }
325 if (!resource_name.package.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000326 context->GetDiagnostics()->Error(android::DiagMessage(line)
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500327 << "Package set for resource. Only use type/name");
328 return false;
329 }
330 for (StringPiece directive : util::Tokenize(directives, ',')) {
331 if (directive == "remove") {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700332 options->resources_exclude_list.insert(resource_name.ToResourceName());
Brian Changdcef8312019-09-13 11:38:32 -0700333 } else if (directive == "no_collapse" || directive == "no_obfuscate") {
334 options->table_flattener_options.name_collapse_exemptions.insert(
335 resource_name.ToResourceName());
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500336 }
337 }
Luke Nicholsonb0643302017-12-01 15:29:03 -0800338 }
339 return true;
340}
341
Brian Changdcef8312019-09-13 11:38:32 -0700342bool ExtractConfig(const std::string& path, IAaptContext* context, OptimizeOptions* options) {
343 std::string content;
344 if (!android::base::ReadFileToString(path, &content, true /*follow_symlinks*/)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000345 context->GetDiagnostics()->Error(android::DiagMessage(path) << "failed reading config file");
Brian Changdcef8312019-09-13 11:38:32 -0700346 return false;
347 }
348 return ParseConfig(content, context, options);
349}
350
Adam Lesinski8780eb62017-10-31 17:44:39 -0700351bool ExtractAppDataFromManifest(OptimizeContext* context, const LoadedApk* apk,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700352 OptimizeOptions* out_options) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700353 const xml::XmlResource* manifest = apk->GetManifest();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700354 if (manifest == nullptr) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700355 return false;
356 }
357
Ryan Mitchell4382e442021-07-14 12:53:01 -0700358 auto app_info = ExtractAppInfoFromBinaryManifest(*manifest, context->GetDiagnostics());
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700359 if (!app_info) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000360 context->GetDiagnostics()->Error(android::DiagMessage()
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700361 << "failed to extract data from AndroidManifest.xml");
362 return false;
363 }
364
365 out_options->app_info = std::move(app_info.value());
Ryan Mitchell4382e442021-07-14 12:53:01 -0700366 context->SetMinSdkVersion(out_options->app_info.min_sdk_version.value_or(0));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700367 return true;
368}
369
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700370int OptimizeCommand::Action(const std::vector<std::string>& args) {
371 if (args.size() != 1u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700372 std::cerr << "must have one APK as argument.\n\n";
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700373 Usage(&std::cerr);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700374 return 1;
375 }
376
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700377 const std::string& apk_path = args[0];
378 OptimizeContext context;
379 context.SetVerbose(verbose_);
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000380 android::IDiagnostics* diag = context.GetDiagnostics();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700381
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700382 if (config_path_) {
383 std::string& path = config_path_.value();
Ryan Mitchell4382e442021-07-14 12:53:01 -0700384 std::optional<ConfigurationParser> for_path = ConfigurationParser::ForPath(path);
Shane Farmer57669432017-06-19 12:52:04 -0700385 if (for_path) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700386 options_.apk_artifacts = for_path.value().WithDiagnostics(diag).Parse(apk_path);
387 if (!options_.apk_artifacts) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000388 diag->Error(android::DiagMessage() << "Failed to parse the output artifact list");
Shane Farmercb6c3f92017-11-27 13:19:36 -0800389 return 1;
390 }
391
Shane Farmer57669432017-06-19 12:52:04 -0700392 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000393 diag->Error(android::DiagMessage() << "Could not parse config file " << path);
Shane Farmer57669432017-06-19 12:52:04 -0700394 return 1;
395 }
Shane Farmer9ecc0752017-08-24 15:55:36 -0700396
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700397 if (print_only_) {
398 for (const OutputArtifact& artifact : options_.apk_artifacts.value()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800399 std::cout << artifact.name << std::endl;
Shane Farmer9ecc0752017-08-24 15:55:36 -0700400 }
401 return 0;
402 }
403
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700404 if (!kept_artifacts_.empty()) {
405 for (const std::string& artifact_str : kept_artifacts_) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800406 for (const StringPiece& artifact : util::Tokenize(artifact_str, ',')) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700407 options_.kept_artifacts.insert(artifact.to_string());
Shane Farmer666de342017-11-29 16:07:51 -0800408 }
409 }
410 }
411
Shane Farmer9ecc0752017-08-24 15:55:36 -0700412 // Since we know that we are going to process the APK (not just print targets), make sure we
413 // have somewhere to write them to.
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700414 if (!options_.output_dir) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000415 diag->Error(android::DiagMessage()
416 << "Output directory is required when using a configuration file");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700417 return 1;
418 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700419 } else if (print_only_) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000420 diag->Error(android::DiagMessage()
421 << "Asked to print artifacts without providing a configurations");
Shane Farmer9ecc0752017-08-24 15:55:36 -0700422 return 1;
Shane Farmer57669432017-06-19 12:52:04 -0700423 }
424
Shane Farmer2c122412017-12-15 16:55:54 -0800425 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(apk_path, context.GetDiagnostics());
426 if (!apk) {
427 return 1;
428 }
429
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700430 if (target_densities_) {
Shane Farmer2c122412017-12-15 16:55:54 -0800431 // Parse the target screen densities.
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700432 for (const StringPiece& config_str : util::Tokenize(target_densities_.value(), ',')) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700433 std::optional<uint16_t> target_density = ParseTargetDensityParameter(config_str, diag);
Shane Farmer2c122412017-12-15 16:55:54 -0800434 if (!target_density) {
435 return 1;
436 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700437 options_.table_splitter_options.preferred_densities.push_back(target_density.value());
Shane Farmer2c122412017-12-15 16:55:54 -0800438 }
439 }
440
441 std::unique_ptr<IConfigFilter> filter;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700442 if (!configs_.empty()) {
443 filter = ParseConfigFilterParameters(configs_, diag);
Shane Farmer2c122412017-12-15 16:55:54 -0800444 if (filter == nullptr) {
445 return 1;
446 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700447 options_.table_splitter_options.config_filter = filter.get();
Shane Farmer2c122412017-12-15 16:55:54 -0800448 }
449
450 // Parse the split parameters.
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700451 for (const std::string& split_arg : split_args_) {
452 options_.split_paths.emplace_back();
453 options_.split_constraints.emplace_back();
454 if (!ParseSplitParameter(split_arg, diag, &options_.split_paths.back(),
455 &options_.split_constraints.back())) {
Shane Farmer2c122412017-12-15 16:55:54 -0800456 return 1;
457 }
458 }
459
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700460 if (resources_config_path_) {
461 std::string& path = resources_config_path_.value();
462 if (!ExtractConfig(path, &context, &options_)) {
Mohamed Heikald3c5fb62018-01-12 11:37:26 -0500463 return 1;
464 }
465 }
466
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700467 if (!ExtractAppDataFromManifest(&context, apk.get(), &options_)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700468 return 1;
469 }
470
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700471 Optimizer cmd(&context, options_);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700472 return cmd.Run(std::move(apk));
473}
474
475} // namespace aapt