blob: 96159622e653a8f942652d720f38fe67dcbda871 [file] [log] [blame]
Adam Lesinskid48944a2017-02-21 14:22:30 -08001/*
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
20#include "androidfw/StringPiece.h"
21
22#include "Diagnostics.h"
23#include "Flags.h"
24#include "LoadedApk.h"
25#include "SdkConstants.h"
26#include "flatten/TableFlattener.h"
27#include "optimize/ResourceDeduper.h"
28#include "optimize/VersionCollapser.h"
29#include "split/TableSplitter.h"
30
31using android::StringPiece;
32
33namespace aapt {
34
35struct OptimizeOptions {
36 // Path to the output APK.
37 std::string output_path;
38
39 // List of screen density configurations the APK will be optimized for.
40 std::vector<ConfigDescription> target_configs;
41
42 TableFlattenerOptions table_flattener_options;
43};
44
45class OptimizeContext : public IAaptContext {
46 public:
47 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
48
49 NameMangler* GetNameMangler() override {
50 abort();
51 return nullptr;
52 }
53
54 const std::string& GetCompilationPackage() override {
55 static std::string empty;
56 return empty;
57 }
58
59 uint8_t GetPackageId() override { return 0; }
60
61 SymbolTable* GetExternalSymbols() override {
62 abort();
63 return nullptr;
64 }
65
66 bool IsVerbose() override { return verbose_; }
67
68 void SetVerbose(bool val) { verbose_ = val; }
69
70 void SetMinSdkVersion(int sdk_version) { sdk_version_ = sdk_version; }
71
72 int GetMinSdkVersion() override { return sdk_version_; }
73
74 private:
75 StdErrDiagnostics diagnostics_;
76 bool verbose_ = false;
77 int sdk_version_ = 0;
78};
79
80class OptimizeCommand {
81 public:
82 OptimizeCommand(OptimizeContext* context, const OptimizeOptions& options)
83 : options_(options),
84 context_(context) {}
85
86 int Run(std::unique_ptr<LoadedApk> apk) {
87 if (context_->IsVerbose()) {
88 context_->GetDiagnostics()->Note(DiagMessage() << "Optimizing APK...");
89 }
90
91 VersionCollapser collapser;
92 if (!collapser.Consume(context_, apk->GetResourceTable())) {
93 return 1;
94 }
95
96 ResourceDeduper deduper;
97 if (!deduper.Consume(context_, apk->GetResourceTable())) {
98 context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources");
99 return 1;
100 }
101
102 // Stripping the APK using the TableSplitter with no splits and the target
103 // densities as the preferred densities. The resource table is modified in
104 // place in the LoadedApk.
105 TableSplitterOptions splitter_options;
106 for (auto& config : options_.target_configs) {
107 splitter_options.preferred_densities.push_back(config.density);
108 }
109 std::vector<SplitConstraints> splits;
110 TableSplitter splitter(splits, splitter_options);
111 splitter.SplitTable(apk->GetResourceTable());
112
113 std::unique_ptr<IArchiveWriter> writer =
114 CreateZipFileArchiveWriter(context_->GetDiagnostics(), options_.output_path);
115 if (!apk->WriteToArchive(context_, options_.table_flattener_options, writer.get())) {
116 return 1;
117 }
118
119 return 0;
120 }
121
122 private:
123 OptimizeOptions options_;
124 OptimizeContext* context_;
125};
126
127int Optimize(const std::vector<StringPiece>& args) {
128 OptimizeContext context;
129 OptimizeOptions options;
130 Maybe<std::string> target_densities;
131 bool verbose = false;
132 Flags flags =
133 Flags()
134 .RequiredFlag("-o", "Path to the output APK.", &options.output_path)
135 .OptionalFlag(
136 "--target-densities",
137 "Comma separated list of the screen densities that the APK will "
138 "be optimized for. All the resources that would be unused on "
139 "devices of the given densities will be removed from the APK.",
140 &target_densities)
141 .OptionalSwitch("--enable-sparse-encoding",
142 "Enables encoding sparse entries using a binary search tree.\n"
143 "This decreases APK size at the cost of resource retrieval performance.",
144 &options.table_flattener_options.use_sparse_entries)
145 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
146
147 if (!flags.Parse("aapt2 optimize", args, &std::cerr)) {
148 return 1;
149 }
150
151 if (flags.GetArgs().size() != 1u) {
152 std::cerr << "must have one APK as argument.\n\n";
153 flags.Usage("aapt2 optimize", &std::cerr);
154 return 1;
155 }
156
157 std::unique_ptr<LoadedApk> apk =
158 LoadedApk::LoadApkFromPath(&context, flags.GetArgs()[0]);
159 if (!apk) {
160 return 1;
161 }
162
163 if (verbose) {
164 context.SetVerbose(verbose);
165 }
166
167 if (target_densities) {
168 // Parse the target screen densities.
169 for (const StringPiece& config_str : util::Tokenize(target_densities.value(), ',')) {
170 ConfigDescription config;
171 if (!ConfigDescription::Parse(config_str, &config) || config.density == 0) {
172 context.GetDiagnostics()->Error(
173 DiagMessage() << "invalid density '" << config_str
174 << "' for --target-densities option");
175 return 1;
176 }
177
178 // Clear the version that can be automatically added.
179 config.sdkVersion = 0;
180
181 if (config.diff(ConfigDescription::DefaultConfig()) !=
182 ConfigDescription::CONFIG_DENSITY) {
183 context.GetDiagnostics()->Error(
184 DiagMessage() << "invalid density '" << config_str
185 << "' for --target-densities option. Must be only a "
186 << "density value.");
187 return 1;
188 }
189
190 options.target_configs.push_back(config);
191 }
192 }
193
194 // TODO(adamlesinski): Read manfiest and set the proper minSdkVersion.
195 // context.SetMinSdkVersion(SDK_O);
196
197 OptimizeCommand cmd(&context, options);
198 return cmd.Run(std::move(apk));
199}
200
201} // namespace aapt