blob: 7260649c26d48d0e535d5f415904ac8f61c8ee46 [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 // Stripping the APK using the TableSplitter with no splits and the target
Pierre Lecesne672384b2017-02-06 10:29:02 +000083 // densities as the preferred densities. The resource table is modified in
Pierre Lecesne2968cbf2017-02-02 22:38:40 +000084 // place in the LoadedApk.
85 TableSplitterOptions splitter_options;
Pierre Lecesne672384b2017-02-06 10:29:02 +000086 for (auto& config : options_.target_configs) {
87 splitter_options.preferred_densities.push_back(config.density);
88 }
Pierre Lecesne2968cbf2017-02-02 22:38:40 +000089 std::vector<SplitConstraints> splits;
90 TableSplitter splitter(splits, splitter_options);
91 splitter.SplitTable(apk->GetResourceTable());
Pierre Lecesne8a7b4cb2017-01-31 23:58:27 +000092
Pierre Lecesne2599aa42017-02-01 22:47:03 +000093 std::unique_ptr<IArchiveWriter> writer =
94 CreateZipFileArchiveWriter(context_->GetDiagnostics(), options_.output_path);
95 if (!apk->WriteToArchive(context_, writer.get())) {
96 return 1;
97 }
98
Pierre Lecesne8a7b4cb2017-01-31 23:58:27 +000099 return 0;
100 }
101
102 private:
103 StripOptions options_;
104 StripContext* context_;
105};
106
107int Strip(const std::vector<StringPiece>& args) {
108 StripContext context;
109 StripOptions options;
110 std::string target_densities;
111 bool verbose = false;
112 Flags flags =
113 Flags()
114 .RequiredFlag("-o", "Path to the output APK.", &options.output_path)
115 .RequiredFlag(
116 "--target-densities",
117 "Comma separated list of the screen densities that the APK will "
118 "be optimized for. All the resources that would be unused on "
119 "devices of the given densities will be removed from the APK.",
120 &target_densities)
121 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
122
123 if (!flags.Parse("aapt2 strip", args, &std::cerr)) {
124 return 1;
125 }
126
127 if (flags.GetArgs().size() != 1u) {
128 std::cerr << "must have one APK as argument.\n\n";
129 flags.Usage("aapt2 strip", &std::cerr);
130 return 1;
131 }
132
133 std::unique_ptr<LoadedApk> apk =
134 LoadedApk::LoadApkFromPath(&context, flags.GetArgs()[0]);
135 if (!apk) {
136 return 1;
137 }
138
139 if (verbose) {
140 context.SetVerbose(verbose);
141 }
142
143 // Parse the target screen densities.
144 for (const StringPiece& config_str : util::Tokenize(target_densities, ',')) {
145 ConfigDescription config;
146 if (!ConfigDescription::Parse(config_str, &config) || config.density == 0) {
147 context.GetDiagnostics()->Error(
148 DiagMessage() << "invalid density '" << config_str
149 << "' for --target-densities option");
150 return 1;
151 }
152
153 // Clear the version that can be automatically added.
154 config.sdkVersion = 0;
155
156 if (config.diff(ConfigDescription::DefaultConfig()) !=
157 ConfigDescription::CONFIG_DENSITY) {
158 context.GetDiagnostics()->Error(
159 DiagMessage() << "invalid density '" << config_str
160 << "' for --target-densities option. Must be only a "
161 << "density value.");
162 return 1;
163 }
164
165 options.target_configs.push_back(config);
166 }
167
168 StripCommand cmd(&context, options);
169 return cmd.Run(std::move(apk));
170}
171
172} // namespace aapt