Pierre Lecesne | 8a7b4cb | 2017-01-31 23:58:27 +0000 | [diff] [blame] | 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 <memory> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "androidfw/StringPiece.h" |
| 21 | |
| 22 | #include "Diagnostics.h" |
| 23 | #include "Flags.h" |
| 24 | #include "LoadedApk.h" |
Pierre Lecesne | 2968cbf | 2017-02-02 22:38:40 +0000 | [diff] [blame] | 25 | #include "split/TableSplitter.h" |
Pierre Lecesne | 8a7b4cb | 2017-01-31 23:58:27 +0000 | [diff] [blame] | 26 | |
| 27 | using android::StringPiece; |
| 28 | |
| 29 | namespace aapt { |
| 30 | |
| 31 | struct 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 | |
| 39 | class 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 | |
| 71 | class 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 Lecesne | 2968cbf | 2017-02-02 22:38:40 +0000 | [diff] [blame] | 82 | // Stripping the APK using the TableSplitter with no splits and the target |
Pierre Lecesne | 672384b | 2017-02-06 10:29:02 +0000 | [diff] [blame] | 83 | // densities as the preferred densities. The resource table is modified in |
Pierre Lecesne | 2968cbf | 2017-02-02 22:38:40 +0000 | [diff] [blame] | 84 | // place in the LoadedApk. |
| 85 | TableSplitterOptions splitter_options; |
Pierre Lecesne | 672384b | 2017-02-06 10:29:02 +0000 | [diff] [blame] | 86 | for (auto& config : options_.target_configs) { |
| 87 | splitter_options.preferred_densities.push_back(config.density); |
| 88 | } |
Pierre Lecesne | 2968cbf | 2017-02-02 22:38:40 +0000 | [diff] [blame] | 89 | std::vector<SplitConstraints> splits; |
| 90 | TableSplitter splitter(splits, splitter_options); |
| 91 | splitter.SplitTable(apk->GetResourceTable()); |
Pierre Lecesne | 8a7b4cb | 2017-01-31 23:58:27 +0000 | [diff] [blame] | 92 | |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 93 | 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 Lecesne | 8a7b4cb | 2017-01-31 23:58:27 +0000 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | StripOptions options_; |
| 104 | StripContext* context_; |
| 105 | }; |
| 106 | |
| 107 | int 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 |