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