1 /*
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 "Optimize.h"
18
19 #include <map>
20 #include <memory>
21 #include <set>
22 #include <string>
23 #include <utility>
24 #include <vector>
25
26 #include "Diagnostics.h"
27 #include "LoadedApk.h"
28 #include "ResourceUtils.h"
29 #include "SdkConstants.h"
30 #include "ValueVisitor.h"
31 #include "android-base/file.h"
32 #include "android-base/stringprintf.h"
33 #include "androidfw/BigBufferStream.h"
34 #include "androidfw/ConfigDescription.h"
35 #include "androidfw/IDiagnostics.h"
36 #include "androidfw/ResourceTypes.h"
37 #include "androidfw/StringPiece.h"
38 #include "cmd/Util.h"
39 #include "configuration/ConfigurationParser.h"
40 #include "filter/AbiFilter.h"
41 #include "format/binary/TableFlattener.h"
42 #include "format/binary/XmlFlattener.h"
43 #include "io/Util.h"
44 #include "optimize/MultiApkGenerator.h"
45 #include "optimize/Obfuscator.h"
46 #include "optimize/ResourceDeduper.h"
47 #include "optimize/ResourceFilter.h"
48 #include "optimize/VersionCollapser.h"
49 #include "split/TableSplitter.h"
50 #include "util/Files.h"
51 #include "util/Util.h"
52
53 using ::aapt::configuration::Abi;
54 using ::aapt::configuration::OutputArtifact;
55 using ::android::ConfigDescription;
56 using ::android::ResTable_config;
57 using ::android::StringPiece;
58 using ::android::base::ReadFileToString;
59 using ::android::base::StringAppendF;
60 using ::android::base::StringPrintf;
61 using ::android::base::WriteStringToFile;
62
63 namespace aapt {
64
65 class OptimizeContext : public IAaptContext {
66 public:
67 OptimizeContext() = default;
68
GetPackageType()69 PackageType GetPackageType() override {
70 // Not important here. Using anything other than kApp adds EXTRA validation, which we want to
71 // avoid.
72 return PackageType::kApp;
73 }
74
GetDiagnostics()75 android::IDiagnostics* GetDiagnostics() override {
76 return &diagnostics_;
77 }
78
GetNameMangler()79 NameMangler* GetNameMangler() override {
80 UNIMPLEMENTED(FATAL);
81 return nullptr;
82 }
83
GetCompilationPackage()84 const std::string& GetCompilationPackage() override {
85 static std::string empty;
86 return empty;
87 }
88
GetPackageId()89 uint8_t GetPackageId() override {
90 return 0;
91 }
92
GetExternalSymbols()93 SymbolTable* GetExternalSymbols() override {
94 UNIMPLEMENTED(FATAL);
95 return nullptr;
96 }
97
IsVerbose()98 bool IsVerbose() override {
99 return verbose_;
100 }
101
SetVerbose(bool val)102 void SetVerbose(bool val) {
103 verbose_ = val;
104 diagnostics_.SetVerbose(val);
105 }
106
SetMinSdkVersion(int sdk_version)107 void SetMinSdkVersion(int sdk_version) {
108 sdk_version_ = sdk_version;
109 }
110
GetMinSdkVersion()111 int GetMinSdkVersion() override {
112 return sdk_version_;
113 }
114
GetSplitNameDependencies()115 const std::set<std::string>& GetSplitNameDependencies() override {
116 UNIMPLEMENTED(FATAL) << "Split Name Dependencies should not be necessary";
117 static std::set<std::string> empty;
118 return empty;
119 }
120
121 private:
122 StdErrDiagnostics diagnostics_;
123 bool verbose_ = false;
124 int sdk_version_ = 0;
125
126 DISALLOW_COPY_AND_ASSIGN(OptimizeContext);
127 };
128
129 class Optimizer {
130 public:
Optimizer(OptimizeContext * context,const OptimizeOptions & options)131 Optimizer(OptimizeContext* context, const OptimizeOptions& options)
132 : options_(options), context_(context) {
133 }
134
Run(std::unique_ptr<LoadedApk> apk)135 int Run(std::unique_ptr<LoadedApk> apk) {
136 if (context_->IsVerbose()) {
137 context_->GetDiagnostics()->Note(android::DiagMessage() << "Optimizing APK...");
138 }
139 if (!options_.resources_exclude_list.empty()) {
140 ResourceFilter filter(options_.resources_exclude_list);
141 if (!filter.Consume(context_, apk->GetResourceTable())) {
142 context_->GetDiagnostics()->Error(android::DiagMessage() << "failed filtering resources");
143 return 1;
144 }
145 }
146
147 VersionCollapser collapser;
148 if (!collapser.Consume(context_, apk->GetResourceTable())) {
149 return 1;
150 }
151
152 ResourceDeduper deduper;
153 if (!deduper.Consume(context_, apk->GetResourceTable())) {
154 context_->GetDiagnostics()->Error(android::DiagMessage() << "failed deduping resources");
155 return 1;
156 }
157
158 Obfuscator obfuscator(options_);
159 if (obfuscator.IsEnabled()) {
160 if (!obfuscator.Consume(context_, apk->GetResourceTable())) {
161 context_->GetDiagnostics()->Error(android::DiagMessage()
162 << "failed shortening resource paths");
163 return 1;
164 }
165
166 if (options_.obfuscation_map_path &&
167 !obfuscator.WriteObfuscationMap(options_.obfuscation_map_path.value())) {
168 context_->GetDiagnostics()->Error(android::DiagMessage()
169 << "failed to write the obfuscation map to file");
170 return 1;
171 }
172
173 // TODO(b/246489170): keep the old option and format until transform to the new one
174 if (options_.shortened_paths_map_path
175 && !WriteShortenedPathsMap(options_.table_flattener_options.shortened_path_map,
176 options_.shortened_paths_map_path.value())) {
177 context_->GetDiagnostics()->Error(android::DiagMessage()
178 << "failed to write shortened resource paths to file");
179 return 1;
180 }
181 }
182
183 // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or
184 // equal to the minSdk.
185 options_.split_constraints =
186 AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints);
187
188 // Stripping the APK using the TableSplitter. The resource table is modified in place in the
189 // LoadedApk.
190 TableSplitter splitter(options_.split_constraints, options_.table_splitter_options);
191 if (!splitter.VerifySplitConstraints(context_)) {
192 return 1;
193 }
194 splitter.SplitTable(apk->GetResourceTable());
195
196 auto path_iter = options_.split_paths.begin();
197 auto split_constraints_iter = options_.split_constraints.begin();
198 for (std::unique_ptr<ResourceTable>& split_table : splitter.splits()) {
199 if (context_->IsVerbose()) {
200 context_->GetDiagnostics()->Note(android::DiagMessage(*path_iter)
201 << "generating split with configurations '"
202 << util::Joiner(split_constraints_iter->configs, ", ")
203 << "'");
204 }
205
206 // Generate an AndroidManifest.xml for each split.
207 std::unique_ptr<xml::XmlResource> split_manifest =
208 GenerateSplitManifest(options_.app_info, *split_constraints_iter);
209 std::unique_ptr<IArchiveWriter> split_writer =
210 CreateZipFileArchiveWriter(context_->GetDiagnostics(), *path_iter);
211 if (!split_writer) {
212 return 1;
213 }
214
215 if (!WriteSplitApk(split_table.get(), split_manifest.get(), split_writer.get())) {
216 return 1;
217 }
218
219 ++path_iter;
220 ++split_constraints_iter;
221 }
222
223 if (options_.apk_artifacts && options_.output_dir) {
224 MultiApkGenerator generator{apk.get(), context_};
225 MultiApkGeneratorOptions generator_options = {
226 options_.output_dir.value(), options_.apk_artifacts.value(),
227 options_.table_flattener_options, options_.kept_artifacts};
228 if (!generator.FromBaseApk(generator_options)) {
229 return 1;
230 }
231 }
232
233 if (options_.output_path) {
234 std::unique_ptr<IArchiveWriter> writer =
235 CreateZipFileArchiveWriter(context_->GetDiagnostics(), options_.output_path.value());
236 if (!apk->WriteToArchive(context_, options_.table_flattener_options, writer.get())) {
237 return 1;
238 }
239 }
240
241 return 0;
242 }
243
244 private:
WriteSplitApk(ResourceTable * table,xml::XmlResource * manifest,IArchiveWriter * writer)245 bool WriteSplitApk(ResourceTable* table, xml::XmlResource* manifest, IArchiveWriter* writer) {
246 android::BigBuffer manifest_buffer(4096);
247 XmlFlattener xml_flattener(&manifest_buffer, {});
248 if (!xml_flattener.Consume(context_, manifest)) {
249 return false;
250 }
251
252 android::BigBufferInputStream manifest_buffer_in(&manifest_buffer);
253 if (!io::CopyInputStreamToArchive(context_, &manifest_buffer_in, "AndroidManifest.xml",
254 ArchiveEntry::kCompress, writer)) {
255 return false;
256 }
257
258 std::map<std::pair<ConfigDescription, StringPiece>, FileReference*> config_sorted_files;
259 for (auto& pkg : table->packages) {
260 for (auto& type : pkg->types) {
261 // Sort by config and name, so that we get better locality in the zip file.
262 config_sorted_files.clear();
263
264 for (auto& entry : type->entries) {
265 for (auto& config_value : entry->values) {
266 auto* file_ref = ValueCast<FileReference>(config_value->value.get());
267 if (file_ref == nullptr) {
268 continue;
269 }
270
271 if (file_ref->file == nullptr) {
272 ResourceNameRef name(pkg->name, type->named_type, entry->name);
273 context_->GetDiagnostics()->Warn(android::DiagMessage(file_ref->GetSource())
274 << "file for resource " << name << " with config '"
275 << config_value->config << "' not found");
276 continue;
277 }
278
279 const StringPiece entry_name = entry->name;
280 config_sorted_files[std::make_pair(config_value->config, entry_name)] = file_ref;
281 }
282 }
283
284 for (auto& entry : config_sorted_files) {
285 FileReference* file_ref = entry.second;
286 if (!io::CopyFileToArchivePreserveCompression(context_, file_ref->file, *file_ref->path,
287 writer)) {
288 return false;
289 }
290 }
291 }
292 }
293
294 android::BigBuffer table_buffer(4096);
295 TableFlattener table_flattener(options_.table_flattener_options, &table_buffer);
296 if (!table_flattener.Consume(context_, table)) {
297 return false;
298 }
299
300 android::BigBufferInputStream table_buffer_in(&table_buffer);
301 return io::CopyInputStreamToArchive(context_, &table_buffer_in, "resources.arsc",
302 ArchiveEntry::kAlign, writer);
303 }
304
305 // TODO(b/246489170): keep the old option and format until transform to the new one
WriteShortenedPathsMap(const std::map<std::string,std::string> & path_map,const std::string & file_path)306 bool WriteShortenedPathsMap(const std::map<std::string, std::string> &path_map,
307 const std::string &file_path) {
308 std::stringstream ss;
309 for (auto it = path_map.cbegin(); it != path_map.cend(); ++it) {
310 ss << it->first << " -> " << it->second << "\n";
311 }
312 return WriteStringToFile(ss.str(), file_path);
313 }
314
315 OptimizeOptions options_;
316 OptimizeContext* context_;
317 };
318
ExtractConfig(const std::string & path,IAaptContext * context,OptimizeOptions * options)319 bool ExtractConfig(const std::string& path, IAaptContext* context, OptimizeOptions* options) {
320 std::string content;
321 if (!android::base::ReadFileToString(path, &content, true /*follow_symlinks*/)) {
322 context->GetDiagnostics()->Error(android::DiagMessage(path) << "failed reading config file");
323 return false;
324 }
325 return ParseResourceConfig(content, context, options->resources_exclude_list,
326 options->table_flattener_options.name_collapse_exemptions,
327 options->table_flattener_options.path_shorten_exemptions);
328 }
329
ExtractAppDataFromManifest(OptimizeContext * context,const LoadedApk * apk,OptimizeOptions * out_options)330 bool ExtractAppDataFromManifest(OptimizeContext* context, const LoadedApk* apk,
331 OptimizeOptions* out_options) {
332 const xml::XmlResource* manifest = apk->GetManifest();
333 if (manifest == nullptr) {
334 return false;
335 }
336
337 auto app_info = ExtractAppInfoFromBinaryManifest(*manifest, context->GetDiagnostics());
338 if (!app_info) {
339 context->GetDiagnostics()->Error(android::DiagMessage()
340 << "failed to extract data from AndroidManifest.xml");
341 return false;
342 }
343
344 out_options->app_info = std::move(app_info.value());
345 context->SetMinSdkVersion(out_options->app_info.min_sdk_version.value_or(0));
346 return true;
347 }
348
Action(const std::vector<std::string> & args)349 int OptimizeCommand::Action(const std::vector<std::string>& args) {
350 if (args.size() != 1u) {
351 std::cerr << "must have one APK as argument.\n\n";
352 Usage(&std::cerr);
353 return 1;
354 }
355
356 const std::string& apk_path = args[0];
357 OptimizeContext context;
358 context.SetVerbose(verbose_);
359 android::IDiagnostics* diag = context.GetDiagnostics();
360
361 if (config_path_) {
362 std::string& path = config_path_.value();
363 std::optional<ConfigurationParser> for_path = ConfigurationParser::ForPath(path);
364 if (for_path) {
365 options_.apk_artifacts = for_path.value().WithDiagnostics(diag).Parse(apk_path);
366 if (!options_.apk_artifacts) {
367 diag->Error(android::DiagMessage() << "Failed to parse the output artifact list");
368 return 1;
369 }
370
371 } else {
372 diag->Error(android::DiagMessage() << "Could not parse config file " << path);
373 return 1;
374 }
375
376 if (print_only_) {
377 for (const OutputArtifact& artifact : options_.apk_artifacts.value()) {
378 std::cout << artifact.name << std::endl;
379 }
380 return 0;
381 }
382
383 if (!kept_artifacts_.empty()) {
384 for (const std::string& artifact_str : kept_artifacts_) {
385 for (StringPiece artifact : util::Tokenize(artifact_str, ',')) {
386 options_.kept_artifacts.emplace(artifact);
387 }
388 }
389 }
390
391 // Since we know that we are going to process the APK (not just print targets), make sure we
392 // have somewhere to write them to.
393 if (!options_.output_dir) {
394 diag->Error(android::DiagMessage()
395 << "Output directory is required when using a configuration file");
396 return 1;
397 }
398 } else if (print_only_) {
399 diag->Error(android::DiagMessage()
400 << "Asked to print artifacts without providing a configurations");
401 return 1;
402 }
403
404 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(apk_path, context.GetDiagnostics());
405 if (!apk) {
406 return 1;
407 }
408
409 if (options_.enable_sparse_encoding) {
410 options_.table_flattener_options.sparse_entries = SparseEntriesMode::Enabled;
411 }
412 if (options_.force_sparse_encoding) {
413 options_.table_flattener_options.sparse_entries = SparseEntriesMode::Forced;
414 }
415
416 if (target_densities_) {
417 // Parse the target screen densities.
418 for (StringPiece config_str : util::Tokenize(target_densities_.value(), ',')) {
419 std::optional<uint16_t> target_density = ParseTargetDensityParameter(config_str, diag);
420 if (!target_density) {
421 return 1;
422 }
423 options_.table_splitter_options.preferred_densities.push_back(target_density.value());
424 }
425 }
426
427 std::unique_ptr<IConfigFilter> filter;
428 if (!configs_.empty()) {
429 filter = ParseConfigFilterParameters(configs_, diag);
430 if (filter == nullptr) {
431 return 1;
432 }
433 options_.table_splitter_options.config_filter = filter.get();
434 }
435
436 // Parse the split parameters.
437 for (const std::string& split_arg : split_args_) {
438 options_.split_paths.emplace_back();
439 options_.split_constraints.emplace_back();
440 if (!ParseSplitParameter(split_arg, diag, &options_.split_paths.back(),
441 &options_.split_constraints.back())) {
442 return 1;
443 }
444 }
445
446 if (resources_config_path_) {
447 std::string& path = resources_config_path_.value();
448 if (!ExtractConfig(path, &context, &options_)) {
449 return 1;
450 }
451 }
452
453 if (!ExtractAppDataFromManifest(&context, apk.get(), &options_)) {
454 return 1;
455 }
456
457 Optimizer cmd(&context, options_);
458 return cmd.Run(std::move(apk));
459 }
460
461 } // namespace aapt
462