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