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_CONVERT_H |
| 18 | #define AAPT2_CONVERT_H |
| 19 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 20 | #include <optional> |
| 21 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 22 | #include "Command.h" |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 23 | #include "LoadedApk.h" |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 24 | #include "format/binary/TableFlattener.h" |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 25 | #include "format/binary/XmlFlattener.h" |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 26 | |
| 27 | namespace aapt { |
| 28 | |
| 29 | class ConvertCommand : public Command { |
| 30 | public: |
| 31 | explicit ConvertCommand() : Command("convert") { |
| 32 | SetDescription("Converts an apk between binary and proto formats."); |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 33 | AddRequiredFlag("-o", "Output path", &output_path_, Command::kPath); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 34 | AddOptionalFlag("--output-format", android::base::StringPrintf("Format of the output. " |
| 35 | "Accepted values are '%s' and '%s'. When not set, defaults to '%s'.", |
| 36 | kOutputFormatProto, kOutputFormatBinary, kOutputFormatBinary), &output_format_); |
Iurii Makhno | da06e4d | 2022-08-24 14:17:32 +0000 | [diff] [blame] | 37 | AddOptionalSwitch( |
| 38 | "--enable-sparse-encoding", |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 39 | "Enables encoding sparse entries using a binary search tree.\n" |
Iurii Makhno | da06e4d | 2022-08-24 14:17:32 +0000 | [diff] [blame] | 40 | "This decreases APK size at the cost of resource retrieval performance.\n" |
| 41 | "Only applies sparse encoding to Android O+ resources or all resources if minSdk of " |
| 42 | "the APK is O+", |
| 43 | &enable_sparse_encoding_); |
| 44 | AddOptionalSwitch("--force-sparse-encoding", |
| 45 | "Enables encoding sparse entries using a binary search tree.\n" |
| 46 | "This decreases APK size at the cost of resource retrieval performance.\n" |
| 47 | "Applies sparse encoding to all resources regardless of minSdk.", |
| 48 | &force_sparse_encoding_); |
Rico Wind | ec629dd | 2023-08-01 14:11:45 +0200 | [diff] [blame] | 49 | AddOptionalSwitch( |
| 50 | "--enable-compact-entries", |
| 51 | "This decreases APK size by using compact resource entries for simple data types.", |
| 52 | &enable_compact_entries_); |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 53 | AddOptionalSwitch("--keep-raw-values", |
| 54 | android::base::StringPrintf("Preserve raw attribute values in xml files when using the" |
| 55 | " '%s' output format", kOutputFormatBinary), |
| 56 | &xml_flattener_options_.keep_raw_values); |
Iurii Makhno | 054e433 | 2022-10-12 16:03:03 +0000 | [diff] [blame] | 57 | AddOptionalFlag("--resources-config-path", |
| 58 | "Path to the resources.cfg file containing the list of resources and \n" |
| 59 | "directives to each resource. \n" |
| 60 | "Format: type/resource_name#[directive][,directive]", |
| 61 | &resources_config_path_); |
| 62 | AddOptionalSwitch( |
| 63 | "--collapse-resource-names", |
| 64 | "Collapses resource names to a single value in the key string pool. Resources can \n" |
| 65 | "be exempted using the \"no_collapse\" directive in a file specified by " |
| 66 | "--resources-config-path.", |
| 67 | &table_flattener_options_.collapse_key_stringpool); |
| 68 | AddOptionalSwitch( |
| 69 | "--deduplicate-entry-values", |
| 70 | "Whether to deduplicate pairs of resource entry and value for simple resources.\n" |
| 71 | "This is recommended to be used together with '--collapse-resource-names' flag or for\n" |
| 72 | "APKs where resource names are manually collapsed. For such APKs this flag allows to\n" |
| 73 | "store the same resource value only once in resource table which decreases APK size.\n" |
| 74 | "Has no effect on APKs where resource names are kept.", |
| 75 | &table_flattener_options_.deduplicate_entry_values); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 76 | AddOptionalSwitch("-v", "Enables verbose logging", &verbose_); |
| 77 | } |
| 78 | |
| 79 | int Action(const std::vector<std::string>& args) override; |
| 80 | |
| 81 | private: |
| 82 | const static char* kOutputFormatProto; |
| 83 | const static char* kOutputFormatBinary; |
| 84 | |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 85 | TableFlattenerOptions table_flattener_options_; |
| 86 | XmlFlattenerOptions xml_flattener_options_; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 87 | std::string output_path_; |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 88 | std::optional<std::string> output_format_; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 89 | bool verbose_ = false; |
Iurii Makhno | da06e4d | 2022-08-24 14:17:32 +0000 | [diff] [blame] | 90 | bool enable_sparse_encoding_ = false; |
| 91 | bool force_sparse_encoding_ = false; |
Rico Wind | ec629dd | 2023-08-01 14:11:45 +0200 | [diff] [blame] | 92 | bool enable_compact_entries_ = false; |
Iurii Makhno | 054e433 | 2022-10-12 16:03:03 +0000 | [diff] [blame] | 93 | std::optional<std::string> resources_config_path_; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 96 | int Convert(IAaptContext* context, LoadedApk* input, IArchiveWriter* output_writer, |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 97 | ApkFormat output_format,TableFlattenerOptions table_flattener_options, |
| 98 | XmlFlattenerOptions xml_flattener_options); |
Ryan Mitchell | 4e9a922 | 2018-11-13 10:40:07 -0800 | [diff] [blame] | 99 | |
| 100 | } // namespace aapt |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 101 | |
| 102 | #endif //AAPT2_CONVERT_H |