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