blob: b3787ec69f083f887047037af90d46cfa1ca8395 [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
83 return 0;
84 }
85
86 private:
87 StripOptions options_;
88 StripContext* context_;
89};
90
91int Strip(const std::vector<StringPiece>& args) {
92 StripContext context;
93 StripOptions options;
94 std::string target_densities;
95 bool verbose = false;
96 Flags flags =
97 Flags()
98 .RequiredFlag("-o", "Path to the output APK.", &options.output_path)
99 .RequiredFlag(
100 "--target-densities",
101 "Comma separated list of the screen densities that the APK will "
102 "be optimized for. All the resources that would be unused on "
103 "devices of the given densities will be removed from the APK.",
104 &target_densities)
105 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
106
107 if (!flags.Parse("aapt2 strip", args, &std::cerr)) {
108 return 1;
109 }
110
111 if (flags.GetArgs().size() != 1u) {
112 std::cerr << "must have one APK as argument.\n\n";
113 flags.Usage("aapt2 strip", &std::cerr);
114 return 1;
115 }
116
117 std::unique_ptr<LoadedApk> apk =
118 LoadedApk::LoadApkFromPath(&context, flags.GetArgs()[0]);
119 if (!apk) {
120 return 1;
121 }
122
123 if (verbose) {
124 context.SetVerbose(verbose);
125 }
126
127 // Parse the target screen densities.
128 for (const StringPiece& config_str : util::Tokenize(target_densities, ',')) {
129 ConfigDescription config;
130 if (!ConfigDescription::Parse(config_str, &config) || config.density == 0) {
131 context.GetDiagnostics()->Error(
132 DiagMessage() << "invalid density '" << config_str
133 << "' for --target-densities option");
134 return 1;
135 }
136
137 // Clear the version that can be automatically added.
138 config.sdkVersion = 0;
139
140 if (config.diff(ConfigDescription::DefaultConfig()) !=
141 ConfigDescription::CONFIG_DENSITY) {
142 context.GetDiagnostics()->Error(
143 DiagMessage() << "invalid density '" << config_str
144 << "' for --target-densities option. Must be only a "
145 << "density value.");
146 return 1;
147 }
148
149 options.target_configs.push_back(config);
150 }
151
152 StripCommand cmd(&context, options);
153 return cmd.Run(std::move(apk));
154}
155
156} // namespace aapt