Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #include <memory> |
| 18 | #include <vector> |
| 19 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 20 | #include "android-base/stringprintf.h" |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 21 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 22 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 23 | #include "androidfw/StringPiece.h" |
| 24 | |
| 25 | #include "Diagnostics.h" |
| 26 | #include "Flags.h" |
| 27 | #include "LoadedApk.h" |
| 28 | #include "ResourceUtils.h" |
| 29 | #include "SdkConstants.h" |
| 30 | #include "ValueVisitor.h" |
| 31 | #include "cmd/Util.h" |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 32 | #include "configuration/ConfigurationParser.h" |
| 33 | #include "filter/AbiFilter.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 34 | #include "format/binary/TableFlattener.h" |
| 35 | #include "format/binary/XmlFlattener.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 36 | #include "io/BigBufferStream.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 37 | #include "io/Util.h" |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 38 | #include "optimize/MultiApkGenerator.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 39 | #include "optimize/ResourceDeduper.h" |
| 40 | #include "optimize/VersionCollapser.h" |
| 41 | #include "split/TableSplitter.h" |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 42 | #include "util/Files.h" |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 43 | #include "util/Util.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 44 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 45 | using ::aapt::configuration::Abi; |
| 46 | using ::aapt::configuration::Artifact; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 47 | using ::aapt::configuration::PostProcessingConfiguration; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 48 | using ::android::ResTable_config; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 49 | using ::android::StringPiece; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 50 | using ::android::base::StringAppendF; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 51 | using ::android::base::StringPrintf; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 52 | |
| 53 | namespace aapt { |
| 54 | |
| 55 | struct OptimizeOptions { |
| 56 | // Path to the output APK. |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 57 | Maybe<std::string> output_path; |
| 58 | // Path to the output APK directory for splits. |
| 59 | Maybe<std::string> output_dir; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 60 | |
| 61 | // Details of the app extracted from the AndroidManifest.xml |
| 62 | AppInfo app_info; |
| 63 | |
| 64 | // Split APK options. |
| 65 | TableSplitterOptions table_splitter_options; |
| 66 | |
| 67 | // List of output split paths. These are in the same order as `split_constraints`. |
| 68 | std::vector<std::string> split_paths; |
| 69 | |
| 70 | // List of SplitConstraints governing what resources go into each split. Ordered by `split_paths`. |
| 71 | std::vector<SplitConstraints> split_constraints; |
| 72 | |
| 73 | TableFlattenerOptions table_flattener_options; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 74 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 75 | Maybe<PostProcessingConfiguration> configuration; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | class OptimizeContext : public IAaptContext { |
| 79 | public: |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 80 | OptimizeContext() = default; |
| 81 | |
| 82 | PackageType GetPackageType() override { |
| 83 | // Not important here. Using anything other than kApp adds EXTRA validation, which we want to |
| 84 | // avoid. |
| 85 | return PackageType::kApp; |
| 86 | } |
| 87 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 88 | IDiagnostics* GetDiagnostics() override { |
| 89 | return &diagnostics_; |
| 90 | } |
| 91 | |
| 92 | NameMangler* GetNameMangler() override { |
| 93 | UNIMPLEMENTED(FATAL); |
| 94 | return nullptr; |
| 95 | } |
| 96 | |
| 97 | const std::string& GetCompilationPackage() override { |
| 98 | static std::string empty; |
| 99 | return empty; |
| 100 | } |
| 101 | |
| 102 | uint8_t GetPackageId() override { |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | SymbolTable* GetExternalSymbols() override { |
| 107 | UNIMPLEMENTED(FATAL); |
| 108 | return nullptr; |
| 109 | } |
| 110 | |
| 111 | bool IsVerbose() override { |
| 112 | return verbose_; |
| 113 | } |
| 114 | |
| 115 | void SetVerbose(bool val) { |
| 116 | verbose_ = val; |
| 117 | } |
| 118 | |
| 119 | void SetMinSdkVersion(int sdk_version) { |
| 120 | sdk_version_ = sdk_version; |
| 121 | } |
| 122 | |
| 123 | int GetMinSdkVersion() override { |
| 124 | return sdk_version_; |
| 125 | } |
| 126 | |
| 127 | private: |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 128 | DISALLOW_COPY_AND_ASSIGN(OptimizeContext); |
| 129 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 130 | StdErrDiagnostics diagnostics_; |
| 131 | bool verbose_ = false; |
| 132 | int sdk_version_ = 0; |
| 133 | }; |
| 134 | |
| 135 | class OptimizeCommand { |
| 136 | public: |
| 137 | OptimizeCommand(OptimizeContext* context, const OptimizeOptions& options) |
| 138 | : options_(options), context_(context) { |
| 139 | } |
| 140 | |
| 141 | int Run(std::unique_ptr<LoadedApk> apk) { |
| 142 | if (context_->IsVerbose()) { |
| 143 | context_->GetDiagnostics()->Note(DiagMessage() << "Optimizing APK..."); |
| 144 | } |
| 145 | |
| 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())) { |
| 153 | context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources"); |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or |
| 158 | // equal to the minSdk. |
| 159 | options_.split_constraints = |
| 160 | AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints); |
| 161 | |
| 162 | // Stripping the APK using the TableSplitter. The resource table is modified in place in the |
| 163 | // LoadedApk. |
| 164 | TableSplitter splitter(options_.split_constraints, options_.table_splitter_options); |
| 165 | if (!splitter.VerifySplitConstraints(context_)) { |
| 166 | return 1; |
| 167 | } |
| 168 | splitter.SplitTable(apk->GetResourceTable()); |
| 169 | |
| 170 | auto path_iter = options_.split_paths.begin(); |
| 171 | auto split_constraints_iter = options_.split_constraints.begin(); |
| 172 | for (std::unique_ptr<ResourceTable>& split_table : splitter.splits()) { |
| 173 | if (context_->IsVerbose()) { |
| 174 | context_->GetDiagnostics()->Note( |
| 175 | DiagMessage(*path_iter) << "generating split with configurations '" |
| 176 | << util::Joiner(split_constraints_iter->configs, ", ") << "'"); |
| 177 | } |
| 178 | |
| 179 | // Generate an AndroidManifest.xml for each split. |
| 180 | std::unique_ptr<xml::XmlResource> split_manifest = |
| 181 | GenerateSplitManifest(options_.app_info, *split_constraints_iter); |
| 182 | std::unique_ptr<IArchiveWriter> split_writer = |
| 183 | CreateZipFileArchiveWriter(context_->GetDiagnostics(), *path_iter); |
| 184 | if (!split_writer) { |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | if (!WriteSplitApk(split_table.get(), split_manifest.get(), split_writer.get())) { |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | ++path_iter; |
| 193 | ++split_constraints_iter; |
| 194 | } |
| 195 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 196 | if (options_.configuration && options_.output_dir) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 197 | MultiApkGenerator generator{apk.get(), context_}; |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 198 | MultiApkGeneratorOptions generator_options = {options_.output_dir.value(), |
| 199 | options_.configuration.value(), |
| 200 | options_.table_flattener_options}; |
| 201 | if (!generator.FromBaseApk(generator_options)) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 202 | return 1; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | if (options_.output_path) { |
| 207 | std::unique_ptr<IArchiveWriter> writer = |
| 208 | CreateZipFileArchiveWriter(context_->GetDiagnostics(), options_.output_path.value()); |
| 209 | if (!apk->WriteToArchive(context_, options_.table_flattener_options, writer.get())) { |
| 210 | return 1; |
| 211 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | private: |
| 218 | bool WriteSplitApk(ResourceTable* table, xml::XmlResource* manifest, IArchiveWriter* writer) { |
| 219 | BigBuffer manifest_buffer(4096); |
| 220 | XmlFlattener xml_flattener(&manifest_buffer, {}); |
| 221 | if (!xml_flattener.Consume(context_, manifest)) { |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | io::BigBufferInputStream manifest_buffer_in(&manifest_buffer); |
| 226 | if (!io::CopyInputStreamToArchive(context_, &manifest_buffer_in, "AndroidManifest.xml", |
| 227 | ArchiveEntry::kCompress, writer)) { |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | std::map<std::pair<ConfigDescription, StringPiece>, FileReference*> config_sorted_files; |
| 232 | for (auto& pkg : table->packages) { |
| 233 | for (auto& type : pkg->types) { |
| 234 | // Sort by config and name, so that we get better locality in the zip file. |
| 235 | config_sorted_files.clear(); |
| 236 | |
| 237 | for (auto& entry : type->entries) { |
| 238 | for (auto& config_value : entry->values) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 239 | auto* file_ref = ValueCast<FileReference>(config_value->value.get()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 240 | if (file_ref == nullptr) { |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | if (file_ref->file == nullptr) { |
| 245 | ResourceNameRef name(pkg->name, type->type, entry->name); |
Adam Lesinski | 742888f | 2017-04-28 15:34:52 -0700 | [diff] [blame] | 246 | context_->GetDiagnostics()->Warn(DiagMessage(file_ref->GetSource()) |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 247 | << "file for resource " << name << " with config '" |
| 248 | << config_value->config << "' not found"); |
Adam Lesinski | 742888f | 2017-04-28 15:34:52 -0700 | [diff] [blame] | 249 | continue; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | const StringPiece entry_name = entry->name; |
| 253 | config_sorted_files[std::make_pair(config_value->config, entry_name)] = file_ref; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | for (auto& entry : config_sorted_files) { |
| 258 | FileReference* file_ref = entry.second; |
| 259 | uint32_t compression_flags = |
| 260 | file_ref->file->WasCompressed() ? ArchiveEntry::kCompress : 0u; |
| 261 | if (!io::CopyFileToArchive(context_, file_ref->file, *file_ref->path, compression_flags, |
| 262 | writer)) { |
| 263 | return false; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | BigBuffer table_buffer(4096); |
| 270 | TableFlattener table_flattener(options_.table_flattener_options, &table_buffer); |
| 271 | if (!table_flattener.Consume(context_, table)) { |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | io::BigBufferInputStream table_buffer_in(&table_buffer); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 276 | return io::CopyInputStreamToArchive(context_, &table_buffer_in, "resources.arsc", |
| 277 | ArchiveEntry::kAlign, writer); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | OptimizeOptions options_; |
| 281 | OptimizeContext* context_; |
| 282 | }; |
| 283 | |
| 284 | bool ExtractAppDataFromManifest(OptimizeContext* context, LoadedApk* apk, |
| 285 | OptimizeOptions* out_options) { |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 286 | std::unique_ptr<xml::XmlResource> manifest = apk->InflateManifest(context); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 287 | if (manifest == nullptr) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | |
| 291 | Maybe<AppInfo> app_info = |
| 292 | ExtractAppInfoFromBinaryManifest(manifest.get(), context->GetDiagnostics()); |
| 293 | if (!app_info) { |
| 294 | context->GetDiagnostics()->Error(DiagMessage() |
| 295 | << "failed to extract data from AndroidManifest.xml"); |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | out_options->app_info = std::move(app_info.value()); |
| 300 | context->SetMinSdkVersion(out_options->app_info.min_sdk_version.value_or_default(0)); |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | int Optimize(const std::vector<StringPiece>& args) { |
| 305 | OptimizeContext context; |
| 306 | OptimizeOptions options; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 307 | Maybe<std::string> config_path; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 308 | Maybe<std::string> target_densities; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 309 | Maybe<std::string> target_abis; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 310 | std::vector<std::string> configs; |
| 311 | std::vector<std::string> split_args; |
| 312 | bool verbose = false; |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 313 | bool print_only = false; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 314 | Flags flags = |
| 315 | Flags() |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 316 | .OptionalFlag("-o", "Path to the output APK.", &options.output_path) |
| 317 | .OptionalFlag("-d", "Path to the output directory (for splits).", &options.output_dir) |
| 318 | .OptionalFlag("-x", "Path to XML configuration file.", &config_path) |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 319 | .OptionalSwitch("-p", "Print the multi APK artifacts and exit.", &print_only) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 320 | .OptionalFlag( |
| 321 | "--target-densities", |
| 322 | "Comma separated list of the screen densities that the APK will be optimized for.\n" |
| 323 | "All the resources that would be unused on devices of the given densities will be \n" |
| 324 | "removed from the APK.", |
| 325 | &target_densities) |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 326 | .OptionalFlag( |
| 327 | "--target-abis", |
| 328 | "Comma separated list of the CPU ABIs that the APK will be optimized for.\n" |
| 329 | "All the native libraries that would be unused on devices of the given ABIs will \n" |
| 330 | "be removed from the APK.", |
| 331 | &target_abis) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 332 | .OptionalFlagList("-c", |
| 333 | "Comma separated list of configurations to include. The default\n" |
| 334 | "is all configurations.", |
| 335 | &configs) |
| 336 | .OptionalFlagList("--split", |
| 337 | "Split resources matching a set of configs out to a " |
Adam Lesinski | db09157 | 2017-04-13 12:48:56 -0700 | [diff] [blame] | 338 | "Split APK.\nSyntax: path/to/output.apk;<config>[,<config>[...]].\n" |
| 339 | "On Windows, use a semicolon ';' separator instead.", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 340 | &split_args) |
| 341 | .OptionalSwitch("--enable-sparse-encoding", |
| 342 | "Enables encoding sparse entries using a binary search tree.\n" |
| 343 | "This decreases APK size at the cost of resource retrieval performance.", |
| 344 | &options.table_flattener_options.use_sparse_entries) |
| 345 | .OptionalSwitch("-v", "Enables verbose logging", &verbose); |
| 346 | |
| 347 | if (!flags.Parse("aapt2 optimize", args, &std::cerr)) { |
| 348 | return 1; |
| 349 | } |
| 350 | |
| 351 | if (flags.GetArgs().size() != 1u) { |
| 352 | std::cerr << "must have one APK as argument.\n\n"; |
| 353 | flags.Usage("aapt2 optimize", &std::cerr); |
| 354 | return 1; |
| 355 | } |
| 356 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 357 | const std::string& apk_path = flags.GetArgs()[0]; |
| 358 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(&context, apk_path); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 359 | if (!apk) { |
| 360 | return 1; |
| 361 | } |
| 362 | |
| 363 | context.SetVerbose(verbose); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 364 | IDiagnostics* diag = context.GetDiagnostics(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 365 | |
| 366 | if (target_densities) { |
| 367 | // Parse the target screen densities. |
| 368 | for (const StringPiece& config_str : util::Tokenize(target_densities.value(), ',')) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 369 | Maybe<uint16_t> target_density = ParseTargetDensityParameter(config_str, diag); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 370 | if (!target_density) { |
| 371 | return 1; |
| 372 | } |
| 373 | options.table_splitter_options.preferred_densities.push_back(target_density.value()); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | std::unique_ptr<IConfigFilter> filter; |
| 378 | if (!configs.empty()) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 379 | filter = ParseConfigFilterParameters(configs, diag); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 380 | if (filter == nullptr) { |
| 381 | return 1; |
| 382 | } |
| 383 | options.table_splitter_options.config_filter = filter.get(); |
| 384 | } |
| 385 | |
| 386 | // Parse the split parameters. |
| 387 | for (const std::string& split_arg : split_args) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 388 | options.split_paths.emplace_back(); |
| 389 | options.split_constraints.emplace_back(); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 390 | if (!ParseSplitParameter(split_arg, diag, &options.split_paths.back(), |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 391 | &options.split_constraints.back())) { |
| 392 | return 1; |
| 393 | } |
| 394 | } |
| 395 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 396 | if (config_path) { |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 397 | std::string& path = config_path.value(); |
| 398 | Maybe<ConfigurationParser> for_path = ConfigurationParser::ForPath(path); |
| 399 | if (for_path) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 400 | options.configuration = for_path.value().WithDiagnostics(diag).Parse(); |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 401 | } else { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 402 | diag->Error(DiagMessage() << "Could not parse config file " << path); |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 403 | return 1; |
| 404 | } |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 405 | |
| 406 | if (print_only) { |
| 407 | std::vector<std::string> names; |
| 408 | const PostProcessingConfiguration& config = options.configuration.value(); |
| 409 | if (!config.AllArtifactNames(file::GetFilename(apk_path), &names, diag)) { |
| 410 | diag->Error(DiagMessage() << "Failed to generate output artifact list"); |
| 411 | return 1; |
| 412 | } |
| 413 | |
| 414 | for (const auto& name : names) { |
| 415 | std::cout << name << std::endl; |
| 416 | } |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | // Since we know that we are going to process the APK (not just print targets), make sure we |
| 421 | // have somewhere to write them to. |
| 422 | if (!options.output_dir) { |
| 423 | diag->Error(DiagMessage() << "Output directory is required when using a configuration file"); |
| 424 | return 1; |
| 425 | } |
| 426 | } else if (print_only) { |
| 427 | diag->Error(DiagMessage() << "Asked to print artifacts without providing a configurations"); |
| 428 | return 1; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 431 | if (!ExtractAppDataFromManifest(&context, apk.get(), &options)) { |
| 432 | return 1; |
| 433 | } |
| 434 | |
| 435 | OptimizeCommand cmd(&context, options); |
| 436 | return cmd.Run(std::move(apk)); |
| 437 | } |
| 438 | |
| 439 | } // namespace aapt |