Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [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 | |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 20 | #include "android-base/file.h" |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 22 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 23 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 24 | #include "androidfw/StringPiece.h" |
| 25 | |
| 26 | #include "Diagnostics.h" |
| 27 | #include "Flags.h" |
| 28 | #include "LoadedApk.h" |
| 29 | #include "ResourceUtils.h" |
| 30 | #include "SdkConstants.h" |
| 31 | #include "ValueVisitor.h" |
| 32 | #include "cmd/Util.h" |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 33 | #include "configuration/ConfigurationParser.h" |
| 34 | #include "filter/AbiFilter.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 35 | #include "format/binary/TableFlattener.h" |
| 36 | #include "format/binary/XmlFlattener.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 37 | #include "io/BigBufferStream.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 38 | #include "io/Util.h" |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 39 | #include "optimize/MultiApkGenerator.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 40 | #include "optimize/ResourceDeduper.h" |
Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame^] | 41 | #include "optimize/ResourceFilter.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 42 | #include "optimize/VersionCollapser.h" |
| 43 | #include "split/TableSplitter.h" |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 44 | #include "util/Files.h" |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 45 | #include "util/Util.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 46 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 47 | using ::aapt::configuration::Abi; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 48 | using ::aapt::configuration::OutputArtifact; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 49 | using ::android::ResTable_config; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 50 | using ::android::StringPiece; |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 51 | using ::android::base::ReadFileToString; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 52 | using ::android::base::StringAppendF; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 53 | using ::android::base::StringPrintf; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 54 | |
| 55 | namespace aapt { |
| 56 | |
| 57 | struct OptimizeOptions { |
| 58 | // Path to the output APK. |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 59 | Maybe<std::string> output_path; |
| 60 | // Path to the output APK directory for splits. |
| 61 | Maybe<std::string> output_dir; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 62 | |
| 63 | // Details of the app extracted from the AndroidManifest.xml |
| 64 | AppInfo app_info; |
| 65 | |
Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame^] | 66 | // Blacklist of unused resources that should be removed from the apk. |
| 67 | std::unordered_set<ResourceName> resources_blacklist; |
| 68 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 69 | // Split APK options. |
| 70 | TableSplitterOptions table_splitter_options; |
| 71 | |
| 72 | // List of output split paths. These are in the same order as `split_constraints`. |
| 73 | std::vector<std::string> split_paths; |
| 74 | |
| 75 | // List of SplitConstraints governing what resources go into each split. Ordered by `split_paths`. |
| 76 | std::vector<SplitConstraints> split_constraints; |
| 77 | |
| 78 | TableFlattenerOptions table_flattener_options; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 79 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 80 | Maybe<std::vector<OutputArtifact>> apk_artifacts; |
Shane Farmer | 666de34 | 2017-11-29 16:07:51 -0800 | [diff] [blame] | 81 | |
| 82 | // Set of artifacts to keep when generating multi-APK splits. If the list is empty, all artifacts |
| 83 | // are kept and will be written as output. |
| 84 | std::unordered_set<std::string> kept_artifacts; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | class OptimizeContext : public IAaptContext { |
| 88 | public: |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 89 | OptimizeContext() = default; |
| 90 | |
| 91 | PackageType GetPackageType() override { |
| 92 | // Not important here. Using anything other than kApp adds EXTRA validation, which we want to |
| 93 | // avoid. |
| 94 | return PackageType::kApp; |
| 95 | } |
| 96 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 97 | IDiagnostics* GetDiagnostics() override { |
| 98 | return &diagnostics_; |
| 99 | } |
| 100 | |
| 101 | NameMangler* GetNameMangler() override { |
| 102 | UNIMPLEMENTED(FATAL); |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
| 106 | const std::string& GetCompilationPackage() override { |
| 107 | static std::string empty; |
| 108 | return empty; |
| 109 | } |
| 110 | |
| 111 | uint8_t GetPackageId() override { |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | SymbolTable* GetExternalSymbols() override { |
| 116 | UNIMPLEMENTED(FATAL); |
| 117 | return nullptr; |
| 118 | } |
| 119 | |
| 120 | bool IsVerbose() override { |
| 121 | return verbose_; |
| 122 | } |
| 123 | |
| 124 | void SetVerbose(bool val) { |
| 125 | verbose_ = val; |
| 126 | } |
| 127 | |
| 128 | void SetMinSdkVersion(int sdk_version) { |
| 129 | sdk_version_ = sdk_version; |
| 130 | } |
| 131 | |
| 132 | int GetMinSdkVersion() override { |
| 133 | return sdk_version_; |
| 134 | } |
| 135 | |
| 136 | private: |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 137 | DISALLOW_COPY_AND_ASSIGN(OptimizeContext); |
| 138 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 139 | StdErrDiagnostics diagnostics_; |
| 140 | bool verbose_ = false; |
| 141 | int sdk_version_ = 0; |
| 142 | }; |
| 143 | |
| 144 | class OptimizeCommand { |
| 145 | public: |
| 146 | OptimizeCommand(OptimizeContext* context, const OptimizeOptions& options) |
| 147 | : options_(options), context_(context) { |
| 148 | } |
| 149 | |
| 150 | int Run(std::unique_ptr<LoadedApk> apk) { |
| 151 | if (context_->IsVerbose()) { |
| 152 | context_->GetDiagnostics()->Note(DiagMessage() << "Optimizing APK..."); |
| 153 | } |
Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame^] | 154 | if (!options_.resources_blacklist.empty()) { |
| 155 | ResourceFilter filter(options_.resources_blacklist); |
| 156 | if (!filter.Consume(context_, apk->GetResourceTable())) { |
| 157 | context_->GetDiagnostics()->Error(DiagMessage() << "failed filtering resources"); |
| 158 | return 1; |
| 159 | } |
| 160 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 161 | |
| 162 | VersionCollapser collapser; |
| 163 | if (!collapser.Consume(context_, apk->GetResourceTable())) { |
| 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | ResourceDeduper deduper; |
| 168 | if (!deduper.Consume(context_, apk->GetResourceTable())) { |
| 169 | context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources"); |
| 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or |
| 174 | // equal to the minSdk. |
| 175 | options_.split_constraints = |
| 176 | AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints); |
| 177 | |
| 178 | // Stripping the APK using the TableSplitter. The resource table is modified in place in the |
| 179 | // LoadedApk. |
| 180 | TableSplitter splitter(options_.split_constraints, options_.table_splitter_options); |
| 181 | if (!splitter.VerifySplitConstraints(context_)) { |
| 182 | return 1; |
| 183 | } |
| 184 | splitter.SplitTable(apk->GetResourceTable()); |
| 185 | |
| 186 | auto path_iter = options_.split_paths.begin(); |
| 187 | auto split_constraints_iter = options_.split_constraints.begin(); |
| 188 | for (std::unique_ptr<ResourceTable>& split_table : splitter.splits()) { |
| 189 | if (context_->IsVerbose()) { |
| 190 | context_->GetDiagnostics()->Note( |
| 191 | DiagMessage(*path_iter) << "generating split with configurations '" |
| 192 | << util::Joiner(split_constraints_iter->configs, ", ") << "'"); |
| 193 | } |
| 194 | |
| 195 | // Generate an AndroidManifest.xml for each split. |
| 196 | std::unique_ptr<xml::XmlResource> split_manifest = |
| 197 | GenerateSplitManifest(options_.app_info, *split_constraints_iter); |
| 198 | std::unique_ptr<IArchiveWriter> split_writer = |
| 199 | CreateZipFileArchiveWriter(context_->GetDiagnostics(), *path_iter); |
| 200 | if (!split_writer) { |
| 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | if (!WriteSplitApk(split_table.get(), split_manifest.get(), split_writer.get())) { |
| 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | ++path_iter; |
| 209 | ++split_constraints_iter; |
| 210 | } |
| 211 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 212 | if (options_.apk_artifacts && options_.output_dir) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 213 | MultiApkGenerator generator{apk.get(), context_}; |
Shane Farmer | 666de34 | 2017-11-29 16:07:51 -0800 | [diff] [blame] | 214 | MultiApkGeneratorOptions generator_options = { |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 215 | options_.output_dir.value(), options_.apk_artifacts.value(), |
| 216 | options_.table_flattener_options, options_.kept_artifacts}; |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 217 | if (!generator.FromBaseApk(generator_options)) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 218 | return 1; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | if (options_.output_path) { |
| 223 | std::unique_ptr<IArchiveWriter> writer = |
| 224 | CreateZipFileArchiveWriter(context_->GetDiagnostics(), options_.output_path.value()); |
| 225 | if (!apk->WriteToArchive(context_, options_.table_flattener_options, writer.get())) { |
| 226 | return 1; |
| 227 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | private: |
| 234 | bool WriteSplitApk(ResourceTable* table, xml::XmlResource* manifest, IArchiveWriter* writer) { |
| 235 | BigBuffer manifest_buffer(4096); |
| 236 | XmlFlattener xml_flattener(&manifest_buffer, {}); |
| 237 | if (!xml_flattener.Consume(context_, manifest)) { |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | io::BigBufferInputStream manifest_buffer_in(&manifest_buffer); |
| 242 | if (!io::CopyInputStreamToArchive(context_, &manifest_buffer_in, "AndroidManifest.xml", |
| 243 | ArchiveEntry::kCompress, writer)) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | std::map<std::pair<ConfigDescription, StringPiece>, FileReference*> config_sorted_files; |
| 248 | for (auto& pkg : table->packages) { |
| 249 | for (auto& type : pkg->types) { |
| 250 | // Sort by config and name, so that we get better locality in the zip file. |
| 251 | config_sorted_files.clear(); |
| 252 | |
| 253 | for (auto& entry : type->entries) { |
| 254 | for (auto& config_value : entry->values) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 255 | auto* file_ref = ValueCast<FileReference>(config_value->value.get()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 256 | if (file_ref == nullptr) { |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | if (file_ref->file == nullptr) { |
| 261 | ResourceNameRef name(pkg->name, type->type, entry->name); |
Adam Lesinski | 742888f | 2017-04-28 15:34:52 -0700 | [diff] [blame] | 262 | context_->GetDiagnostics()->Warn(DiagMessage(file_ref->GetSource()) |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 263 | << "file for resource " << name << " with config '" |
| 264 | << config_value->config << "' not found"); |
Adam Lesinski | 742888f | 2017-04-28 15:34:52 -0700 | [diff] [blame] | 265 | continue; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | const StringPiece entry_name = entry->name; |
| 269 | config_sorted_files[std::make_pair(config_value->config, entry_name)] = file_ref; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | for (auto& entry : config_sorted_files) { |
| 274 | FileReference* file_ref = entry.second; |
Pierre Lecesne | d55bef7 | 2017-11-10 22:31:01 +0000 | [diff] [blame] | 275 | if (!io::CopyFileToArchivePreserveCompression(context_, file_ref->file, *file_ref->path, |
| 276 | writer)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | BigBuffer table_buffer(4096); |
| 284 | TableFlattener table_flattener(options_.table_flattener_options, &table_buffer); |
| 285 | if (!table_flattener.Consume(context_, table)) { |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | io::BigBufferInputStream table_buffer_in(&table_buffer); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 290 | return io::CopyInputStreamToArchive(context_, &table_buffer_in, "resources.arsc", |
| 291 | ArchiveEntry::kAlign, writer); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | OptimizeOptions options_; |
| 295 | OptimizeContext* context_; |
| 296 | }; |
| 297 | |
Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame^] | 298 | bool ExtractObfuscationWhitelistFromConfig(const std::string& path, OptimizeContext* context, |
| 299 | OptimizeOptions* options) { |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 300 | std::string contents; |
| 301 | if (!ReadFileToString(path, &contents, true)) { |
| 302 | context->GetDiagnostics()->Error(DiagMessage() |
| 303 | << "failed to parse whitelist from config file: " << path); |
| 304 | return false; |
| 305 | } |
Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame^] | 306 | for (StringPiece resource_name : util::Tokenize(contents, ',')) { |
| 307 | options->table_flattener_options.whitelisted_resources.insert( |
| 308 | resource_name.to_string()); |
| 309 | } |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | bool ExtractConfig(const std::string& path, OptimizeContext* context, |
| 314 | OptimizeOptions* options) { |
| 315 | std::string content; |
| 316 | if (!android::base::ReadFileToString(path, &content, true /*follow_symlinks*/)) { |
| 317 | context->GetDiagnostics()->Error(DiagMessage(path) << "failed reading whitelist"); |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | size_t line_no = 0; |
| 322 | for (StringPiece line : util::Tokenize(content, '\n')) { |
| 323 | line_no++; |
| 324 | line = util::TrimWhitespace(line); |
| 325 | if (line.empty()) { |
| 326 | continue; |
| 327 | } |
| 328 | |
| 329 | auto split_line = util::Split(line, '#'); |
| 330 | if (split_line.size() < 2) { |
| 331 | context->GetDiagnostics()->Error(DiagMessage(line) << "No # found in line"); |
| 332 | return false; |
| 333 | } |
| 334 | StringPiece resource_string = split_line[0]; |
| 335 | StringPiece directives = split_line[1]; |
| 336 | ResourceNameRef resource_name; |
| 337 | if (!ResourceUtils::ParseResourceName(resource_string, &resource_name)) { |
| 338 | context->GetDiagnostics()->Error(DiagMessage(line) << "Malformed resource name"); |
| 339 | return false; |
| 340 | } |
| 341 | if (!resource_name.package.empty()) { |
| 342 | context->GetDiagnostics()->Error(DiagMessage(line) |
| 343 | << "Package set for resource. Only use type/name"); |
| 344 | return false; |
| 345 | } |
| 346 | for (StringPiece directive : util::Tokenize(directives, ',')) { |
| 347 | if (directive == "remove") { |
| 348 | options->resources_blacklist.insert(resource_name.ToResourceName()); |
| 349 | } else if (directive == "no_obfuscate") { |
| 350 | options->table_flattener_options.whitelisted_resources.insert( |
| 351 | resource_name.entry.to_string()); |
| 352 | } |
| 353 | } |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 354 | } |
| 355 | return true; |
| 356 | } |
| 357 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 358 | bool ExtractAppDataFromManifest(OptimizeContext* context, const LoadedApk* apk, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 359 | OptimizeOptions* out_options) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 360 | const xml::XmlResource* manifest = apk->GetManifest(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 361 | if (manifest == nullptr) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 362 | return false; |
| 363 | } |
| 364 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 365 | Maybe<AppInfo> app_info = ExtractAppInfoFromBinaryManifest(*manifest, context->GetDiagnostics()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 366 | if (!app_info) { |
| 367 | context->GetDiagnostics()->Error(DiagMessage() |
| 368 | << "failed to extract data from AndroidManifest.xml"); |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | out_options->app_info = std::move(app_info.value()); |
| 373 | context->SetMinSdkVersion(out_options->app_info.min_sdk_version.value_or_default(0)); |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | int Optimize(const std::vector<StringPiece>& args) { |
| 378 | OptimizeContext context; |
| 379 | OptimizeOptions options; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 380 | Maybe<std::string> config_path; |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 381 | Maybe<std::string> whitelist_path; |
Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame^] | 382 | Maybe<std::string> resources_config_path; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 383 | Maybe<std::string> target_densities; |
| 384 | std::vector<std::string> configs; |
| 385 | std::vector<std::string> split_args; |
Shane Farmer | 666de34 | 2017-11-29 16:07:51 -0800 | [diff] [blame] | 386 | std::unordered_set<std::string> kept_artifacts; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 387 | bool verbose = false; |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 388 | bool print_only = false; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 389 | Flags flags = |
| 390 | Flags() |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 391 | .OptionalFlag("-o", "Path to the output APK.", &options.output_path) |
| 392 | .OptionalFlag("-d", "Path to the output directory (for splits).", &options.output_dir) |
| 393 | .OptionalFlag("-x", "Path to XML configuration file.", &config_path) |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 394 | .OptionalSwitch("-p", "Print the multi APK artifacts and exit.", &print_only) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 395 | .OptionalFlag( |
| 396 | "--target-densities", |
| 397 | "Comma separated list of the screen densities that the APK will be optimized for.\n" |
| 398 | "All the resources that would be unused on devices of the given densities will be \n" |
| 399 | "removed from the APK.", |
| 400 | &target_densities) |
Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame^] | 401 | .OptionalFlag("--whitelist-path", |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 402 | "Path to the whitelist.cfg file containing whitelisted resources \n" |
| 403 | "whose names should not be altered in final resource tables.", |
| 404 | &whitelist_path) |
Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame^] | 405 | .OptionalFlag("--resources-config-path", |
| 406 | "Path to the resources.cfg file containing the list of resources and \n" |
| 407 | "directives to each resource. \n" |
| 408 | "Format: type/resource_name#[directive][,directive]", |
| 409 | &resources_config_path) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 410 | .OptionalFlagList("-c", |
| 411 | "Comma separated list of configurations to include. The default\n" |
| 412 | "is all configurations.", |
| 413 | &configs) |
| 414 | .OptionalFlagList("--split", |
| 415 | "Split resources matching a set of configs out to a " |
Adam Lesinski | db09157 | 2017-04-13 12:48:56 -0700 | [diff] [blame] | 416 | "Split APK.\nSyntax: path/to/output.apk;<config>[,<config>[...]].\n" |
| 417 | "On Windows, use a semicolon ';' separator instead.", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 418 | &split_args) |
Shane Farmer | 666de34 | 2017-11-29 16:07:51 -0800 | [diff] [blame] | 419 | .OptionalFlagList("--keep-artifacts", |
| 420 | "Comma separated list of artifacts to keep. If none are specified,\n" |
| 421 | "all artifacts will be kept.", |
| 422 | &kept_artifacts) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 423 | .OptionalSwitch("--enable-sparse-encoding", |
| 424 | "Enables encoding sparse entries using a binary search tree.\n" |
| 425 | "This decreases APK size at the cost of resource retrieval performance.", |
| 426 | &options.table_flattener_options.use_sparse_entries) |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 427 | .OptionalSwitch("--enable-resource-obfuscation", |
| 428 | "Enables obfuscation of key string pool to single value", |
| 429 | &options.table_flattener_options.collapse_key_stringpool) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 430 | .OptionalSwitch("-v", "Enables verbose logging", &verbose); |
| 431 | |
| 432 | if (!flags.Parse("aapt2 optimize", args, &std::cerr)) { |
| 433 | return 1; |
| 434 | } |
| 435 | |
| 436 | if (flags.GetArgs().size() != 1u) { |
| 437 | std::cerr << "must have one APK as argument.\n\n"; |
| 438 | flags.Usage("aapt2 optimize", &std::cerr); |
| 439 | return 1; |
| 440 | } |
| 441 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 442 | const std::string& apk_path = flags.GetArgs()[0]; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 443 | |
| 444 | context.SetVerbose(verbose); |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 445 | IDiagnostics* diag = context.GetDiagnostics(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 446 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 447 | if (config_path) { |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 448 | std::string& path = config_path.value(); |
| 449 | Maybe<ConfigurationParser> for_path = ConfigurationParser::ForPath(path); |
| 450 | if (for_path) { |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 451 | options.apk_artifacts = for_path.value().WithDiagnostics(diag).Parse(apk_path); |
| 452 | if (!options.apk_artifacts) { |
| 453 | diag->Error(DiagMessage() << "Failed to parse the output artifact list"); |
| 454 | return 1; |
| 455 | } |
| 456 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 457 | } else { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 458 | diag->Error(DiagMessage() << "Could not parse config file " << path); |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 459 | return 1; |
| 460 | } |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 461 | |
| 462 | if (print_only) { |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 463 | for (const OutputArtifact& artifact : options.apk_artifacts.value()) { |
| 464 | std::cout << artifact.name << std::endl; |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | |
Shane Farmer | 666de34 | 2017-11-29 16:07:51 -0800 | [diff] [blame] | 469 | if (!kept_artifacts.empty()) { |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 470 | for (const std::string& artifact_str : kept_artifacts) { |
| 471 | for (const StringPiece& artifact : util::Tokenize(artifact_str, ',')) { |
Shane Farmer | 666de34 | 2017-11-29 16:07:51 -0800 | [diff] [blame] | 472 | options.kept_artifacts.insert(artifact.to_string()); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 477 | // Since we know that we are going to process the APK (not just print targets), make sure we |
| 478 | // have somewhere to write them to. |
| 479 | if (!options.output_dir) { |
| 480 | diag->Error(DiagMessage() << "Output directory is required when using a configuration file"); |
| 481 | return 1; |
| 482 | } |
| 483 | } else if (print_only) { |
| 484 | diag->Error(DiagMessage() << "Asked to print artifacts without providing a configurations"); |
| 485 | return 1; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Shane Farmer | 2c12241 | 2017-12-15 16:55:54 -0800 | [diff] [blame] | 488 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(apk_path, context.GetDiagnostics()); |
| 489 | if (!apk) { |
| 490 | return 1; |
| 491 | } |
| 492 | |
| 493 | if (target_densities) { |
| 494 | // Parse the target screen densities. |
| 495 | for (const StringPiece& config_str : util::Tokenize(target_densities.value(), ',')) { |
| 496 | Maybe<uint16_t> target_density = ParseTargetDensityParameter(config_str, diag); |
| 497 | if (!target_density) { |
| 498 | return 1; |
| 499 | } |
| 500 | options.table_splitter_options.preferred_densities.push_back(target_density.value()); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | std::unique_ptr<IConfigFilter> filter; |
| 505 | if (!configs.empty()) { |
| 506 | filter = ParseConfigFilterParameters(configs, diag); |
| 507 | if (filter == nullptr) { |
| 508 | return 1; |
| 509 | } |
| 510 | options.table_splitter_options.config_filter = filter.get(); |
| 511 | } |
| 512 | |
| 513 | // Parse the split parameters. |
| 514 | for (const std::string& split_arg : split_args) { |
| 515 | options.split_paths.emplace_back(); |
| 516 | options.split_constraints.emplace_back(); |
| 517 | if (!ParseSplitParameter(split_arg, diag, &options.split_paths.back(), |
| 518 | &options.split_constraints.back())) { |
| 519 | return 1; |
| 520 | } |
| 521 | } |
| 522 | |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 523 | if (options.table_flattener_options.collapse_key_stringpool) { |
| 524 | if (whitelist_path) { |
| 525 | std::string& path = whitelist_path.value(); |
Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame^] | 526 | if (!ExtractObfuscationWhitelistFromConfig(path, &context, &options)) { |
Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 527 | return 1; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame^] | 532 | if (resources_config_path) { |
| 533 | std::string& path = resources_config_path.value(); |
| 534 | if (!ExtractConfig(path, &context, &options)) { |
| 535 | return 1; |
| 536 | } |
| 537 | } |
| 538 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 539 | if (!ExtractAppDataFromManifest(&context, apk.get(), &options)) { |
| 540 | return 1; |
| 541 | } |
| 542 | |
| 543 | OptimizeCommand cmd(&context, options); |
| 544 | return cmd.Run(std::move(apk)); |
| 545 | } |
| 546 | |
| 547 | } // namespace aapt |