Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #ifndef AAPT2_OPTIMIZE_H |
| 18 | #define AAPT2_OPTIMIZE_H |
| 19 | |
| 20 | #include "AppInfo.h" |
| 21 | #include "Command.h" |
| 22 | #include "configuration/ConfigurationParser.h" |
| 23 | #include "format/binary/TableFlattener.h" |
| 24 | #include "split/TableSplitter.h" |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
| 28 | struct OptimizeOptions { |
| 29 | friend class OptimizeCommand; |
| 30 | |
| 31 | // Path to the output APK. |
| 32 | Maybe<std::string> output_path; |
| 33 | // Path to the output APK directory for splits. |
| 34 | Maybe<std::string> output_dir; |
| 35 | |
| 36 | // Details of the app extracted from the AndroidManifest.xml |
| 37 | AppInfo app_info; |
| 38 | |
| 39 | // Blacklist of unused resources that should be removed from the apk. |
| 40 | std::unordered_set<ResourceName> resources_blacklist; |
| 41 | |
| 42 | // Split APK options. |
| 43 | TableSplitterOptions table_splitter_options; |
| 44 | |
| 45 | // List of output split paths. These are in the same order as `split_constraints`. |
| 46 | std::vector<std::string> split_paths; |
| 47 | |
| 48 | // List of SplitConstraints governing what resources go into each split. Ordered by `split_paths`. |
| 49 | std::vector<SplitConstraints> split_constraints; |
| 50 | |
| 51 | TableFlattenerOptions table_flattener_options; |
| 52 | |
| 53 | Maybe<std::vector<aapt::configuration::OutputArtifact>> apk_artifacts; |
| 54 | |
| 55 | // Set of artifacts to keep when generating multi-APK splits. If the list is empty, all artifacts |
| 56 | // are kept and will be written as output. |
| 57 | std::unordered_set<std::string> kept_artifacts; |
| 58 | }; |
| 59 | |
| 60 | class OptimizeCommand : public Command { |
| 61 | public: |
| 62 | explicit OptimizeCommand() : Command("optimize") { |
| 63 | SetDescription("Preforms resource optimizations on an apk."); |
| 64 | AddOptionalFlag("-o", "Path to the output APK.", &options_.output_path); |
| 65 | AddOptionalFlag("-d", "Path to the output directory (for splits).", &options_.output_dir); |
| 66 | AddOptionalFlag("-x", "Path to XML configuration file.", &config_path_); |
| 67 | AddOptionalSwitch("-p", "Print the multi APK artifacts and exit.", &print_only_); |
| 68 | AddOptionalFlag( |
| 69 | "--target-densities", |
| 70 | "Comma separated list of the screen densities that the APK will be optimized for.\n" |
| 71 | "All the resources that would be unused on devices of the given densities will be \n" |
| 72 | "removed from the APK.", |
| 73 | &target_densities_); |
| 74 | AddOptionalFlag("--whitelist-path", |
| 75 | "Path to the whitelist.cfg file containing whitelisted resources \n" |
| 76 | "whose names should not be altered in final resource tables.", |
| 77 | &whitelist_path_); |
| 78 | AddOptionalFlag("--resources-config-path", |
| 79 | "Path to the resources.cfg file containing the list of resources and \n" |
| 80 | "directives to each resource. \n" |
| 81 | "Format: type/resource_name#[directive][,directive]", |
| 82 | &resources_config_path_); |
| 83 | AddOptionalFlagList("-c", |
| 84 | "Comma separated list of configurations to include. The default\n" |
| 85 | "is all configurations.", |
| 86 | &configs_); |
| 87 | AddOptionalFlagList("--split", |
| 88 | "Split resources matching a set of configs out to a " |
| 89 | "Split APK.\nSyntax: path/to/output.apk;<config>[,<config>[...]].\n" |
| 90 | "On Windows, use a semicolon ';' separator instead.", |
| 91 | &split_args_); |
| 92 | AddOptionalFlagList("--keep-artifacts", |
| 93 | "Comma separated list of artifacts to keep. If none are specified,\n" |
| 94 | "all artifacts will be kept.", |
| 95 | &kept_artifacts_); |
| 96 | AddOptionalSwitch("--enable-sparse-encoding", |
| 97 | "Enables encoding sparse entries using a binary search tree.\n" |
| 98 | "This decreases APK size at the cost of resource retrieval performance.", |
| 99 | &options_.table_flattener_options.use_sparse_entries); |
| 100 | AddOptionalSwitch("--enable-resource-obfuscation", |
| 101 | "Enables obfuscation of key string pool to single value", |
| 102 | &options_.table_flattener_options.collapse_key_stringpool); |
| 103 | AddOptionalSwitch("-v", "Enables verbose logging", &verbose_); |
| 104 | } |
| 105 | |
| 106 | int Action(const std::vector<std::string>& args) override; |
| 107 | |
| 108 | private: |
| 109 | OptimizeOptions options_; |
| 110 | |
| 111 | Maybe<std::string> config_path_; |
| 112 | Maybe<std::string> whitelist_path_; |
| 113 | Maybe<std::string> resources_config_path_; |
| 114 | Maybe<std::string> target_densities_; |
| 115 | std::vector<std::string> configs_; |
| 116 | std::vector<std::string> split_args_; |
| 117 | std::unordered_set<std::string> kept_artifacts_; |
| 118 | bool print_only_ = false; |
| 119 | bool verbose_ = false; |
| 120 | }; |
| 121 | |
| 122 | }// namespace aapt |
| 123 | |
| 124 | #endif //AAPT2_OPTIMIZE_H |