blob: c34cfbf3580e3560a3cee7932fd77f406316b7f1 [file] [log] [blame]
Pierre Lecesne8a7b4cb2017-01-31 23:58:27 +00001/*
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"
Pierre Lecesne2968cbf2017-02-02 22:38:40 +000025#include "split/TableSplitter.h"
Pierre Lecesne8a7b4cb2017-01-31 23:58:27 +000026
27using android::StringPiece;
28
29namespace aapt {
30
31struct StripOptions {
32 /** Path to the output APK. */
33 std::string output_path;
34
35 /** List of screen density configurations the APK will be optimized for. */
36 std::vector<ConfigDescription> target_configs;
37};
38
39class StripContext : public IAaptContext {
40 public:
41 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
42
43 NameMangler* GetNameMangler() override {
44 abort();
45 return nullptr;
46 }
47
48 const std::string& GetCompilationPackage() override {
49 static std::string empty;
50 return empty;
51 }
52
53 uint8_t GetPackageId() override { return 0; }
54
55 SymbolTable* GetExternalSymbols() override {
56 abort();
57 return nullptr;
58 }
59
60 bool IsVerbose() override { return verbose_; }
61
62 void SetVerbose(bool val) { verbose_ = val; }
63
64 int GetMinSdkVersion() override { return 0; }
65
66 private:
67 StdErrDiagnostics diagnostics_;
68 bool verbose_ = false;
69};
70
71class StripCommand {
72 public:
73 StripCommand(StripContext* context, const StripOptions& options)
74 : options_(options),
75 context_(context) {}
76
77 int Run(std::unique_ptr<LoadedApk> apk) {
78 if (context_->IsVerbose()) {
79 context_->GetDiagnostics()->Note(DiagMessage() << "Stripping APK...");
80 }
81
Pierre Lecesne2968cbf2017-02-02 22:38:40 +000082 // TODO(lecesne): Add support for more than one density.
83 if (options_.target_configs.size() > 1) {
84 context_->GetDiagnostics()->Error(DiagMessage()
85 << "Multiple densities not supported at the moment");
86 return 1;
87 }
88
89 // Stripping the APK using the TableSplitter with no splits and the target
90 // density as the preferred density. The resource table is modified in
91 // place in the LoadedApk.
92 TableSplitterOptions splitter_options;
93 splitter_options.preferred_density = options_.target_configs[0].density;
94 std::vector<SplitConstraints> splits;
95 TableSplitter splitter(splits, splitter_options);
96 splitter.SplitTable(apk->GetResourceTable());
Pierre Lecesne8a7b4cb2017-01-31 23:58:27 +000097
Pierre Lecesne2599aa42017-02-01 22:47:03 +000098 std::unique_ptr<IArchiveWriter> writer =
99 CreateZipFileArchiveWriter(context_->GetDiagnostics(), options_.output_path);
100 if (!apk->WriteToArchive(context_, writer.get())) {
101 return 1;
102 }
103
Pierre Lecesne8a7b4cb2017-01-31 23:58:27 +0000104 return 0;
105 }
106
107 private:
108 StripOptions options_;
109 StripContext* context_;
110};
111
112int Strip(const std::vector<StringPiece>& args) {
113 StripContext context;
114 StripOptions options;
115 std::string target_densities;
116 bool verbose = false;
117 Flags flags =
118 Flags()
119 .RequiredFlag("-o", "Path to the output APK.", &options.output_path)
120 .RequiredFlag(
121 "--target-densities",
122 "Comma separated list of the screen densities that the APK will "
123 "be optimized for. All the resources that would be unused on "
124 "devices of the given densities will be removed from the APK.",
125 &target_densities)
126 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
127
128 if (!flags.Parse("aapt2 strip", args, &std::cerr)) {
129 return 1;
130 }
131
132 if (flags.GetArgs().size() != 1u) {
133 std::cerr << "must have one APK as argument.\n\n";
134 flags.Usage("aapt2 strip", &std::cerr);
135 return 1;
136 }
137
138 std::unique_ptr<LoadedApk> apk =
139 LoadedApk::LoadApkFromPath(&context, flags.GetArgs()[0]);
140 if (!apk) {
141 return 1;
142 }
143
144 if (verbose) {
145 context.SetVerbose(verbose);
146 }
147
148 // Parse the target screen densities.
149 for (const StringPiece& config_str : util::Tokenize(target_densities, ',')) {
150 ConfigDescription config;
151 if (!ConfigDescription::Parse(config_str, &config) || config.density == 0) {
152 context.GetDiagnostics()->Error(
153 DiagMessage() << "invalid density '" << config_str
154 << "' for --target-densities option");
155 return 1;
156 }
157
158 // Clear the version that can be automatically added.
159 config.sdkVersion = 0;
160
161 if (config.diff(ConfigDescription::DefaultConfig()) !=
162 ConfigDescription::CONFIG_DENSITY) {
163 context.GetDiagnostics()->Error(
164 DiagMessage() << "invalid density '" << config_str
165 << "' for --target-densities option. Must be only a "
166 << "density value.");
167 return 1;
168 }
169
170 options.target_configs.push_back(config);
171 }
172
173 StripCommand cmd(&context, options);
174 return cmd.Run(std::move(apk));
175}
176
177} // namespace aapt