blob: 6c096496a1ea54a62c09f5beebb73259b8e169a1 [file] [log] [blame]
Ryan Mitchell833a1a62018-07-10 13:51:36 -07001/*
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 Mitchell4382e442021-07-14 12:53:01 -070020#include <optional>
21
Ryan Mitchell833a1a62018-07-10 13:51:36 -070022#include "Command.h"
Ryan Mitchell4e9a9222018-11-13 10:40:07 -080023#include "LoadedApk.h"
Ryan Mitchell833a1a62018-07-10 13:51:36 -070024#include "format/binary/TableFlattener.h"
Ryan Mitchell479fa392019-01-02 17:15:39 -080025#include "format/binary/XmlFlattener.h"
Ryan Mitchell833a1a62018-07-10 13:51:36 -070026
27namespace aapt {
28
29class ConvertCommand : public Command {
30 public:
31 explicit ConvertCommand() : Command("convert") {
32 SetDescription("Converts an apk between binary and proto formats.");
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080033 AddRequiredFlag("-o", "Output path", &output_path_, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070034 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 Makhnoda06e4d2022-08-24 14:17:32 +000037 AddOptionalSwitch(
38 "--enable-sparse-encoding",
Ryan Mitchell833a1a62018-07-10 13:51:36 -070039 "Enables encoding sparse entries using a binary search tree.\n"
Iurii Makhnoda06e4d2022-08-24 14:17:32 +000040 "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_);
Ryan Mitchell479fa392019-01-02 17:15:39 -080049 AddOptionalSwitch("--keep-raw-values",
50 android::base::StringPrintf("Preserve raw attribute values in xml files when using the"
51 " '%s' output format", kOutputFormatBinary),
52 &xml_flattener_options_.keep_raw_values);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070053 AddOptionalSwitch("-v", "Enables verbose logging", &verbose_);
54 }
55
56 int Action(const std::vector<std::string>& args) override;
57
58 private:
59 const static char* kOutputFormatProto;
60 const static char* kOutputFormatBinary;
61
Ryan Mitchell479fa392019-01-02 17:15:39 -080062 TableFlattenerOptions table_flattener_options_;
63 XmlFlattenerOptions xml_flattener_options_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070064 std::string output_path_;
Ryan Mitchell4382e442021-07-14 12:53:01 -070065 std::optional<std::string> output_format_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070066 bool verbose_ = false;
Iurii Makhnoda06e4d2022-08-24 14:17:32 +000067 bool enable_sparse_encoding_ = false;
68 bool force_sparse_encoding_ = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070069};
70
Ryan Mitchell4e9a9222018-11-13 10:40:07 -080071int Convert(IAaptContext* context, LoadedApk* input, IArchiveWriter* output_writer,
Ryan Mitchell479fa392019-01-02 17:15:39 -080072 ApkFormat output_format,TableFlattenerOptions table_flattener_options,
73 XmlFlattenerOptions xml_flattener_options);
Ryan Mitchell4e9a9222018-11-13 10:40:07 -080074
75} // namespace aapt
Ryan Mitchell833a1a62018-07-10 13:51:36 -070076
77#endif //AAPT2_CONVERT_H