| 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; | 
| Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 56 | using ::android::base::StringAppendF; | 
| Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 57 | using ::android::base::StringPrintf; | 
| Brian Chang | dcef831 | 2019-09-13 11:38:32 -0700 | [diff] [blame] | 58 | using ::android::base::WriteStringToFile; | 
| 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 | } | 
| Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 135 | if (!options_.resources_exclude_list.empty()) { | 
|  | 136 | ResourceFilter filter(options_.resources_exclude_list); | 
| Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame] | 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 |  | 
| Brian Chang | dcef831 | 2019-09-13 11:38:32 -0700 | [diff] [blame] | 303 | bool ParseConfig(const std::string& content, IAaptContext* context, OptimizeOptions* options) { | 
| Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame] | 304 | size_t line_no = 0; | 
|  | 305 | for (StringPiece line : util::Tokenize(content, '\n')) { | 
|  | 306 | line_no++; | 
|  | 307 | line = util::TrimWhitespace(line); | 
|  | 308 | if (line.empty()) { | 
|  | 309 | continue; | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | auto split_line = util::Split(line, '#'); | 
|  | 313 | if (split_line.size() < 2) { | 
|  | 314 | context->GetDiagnostics()->Error(DiagMessage(line) << "No # found in line"); | 
|  | 315 | return false; | 
|  | 316 | } | 
|  | 317 | StringPiece resource_string = split_line[0]; | 
|  | 318 | StringPiece directives = split_line[1]; | 
|  | 319 | ResourceNameRef resource_name; | 
|  | 320 | if (!ResourceUtils::ParseResourceName(resource_string, &resource_name)) { | 
|  | 321 | context->GetDiagnostics()->Error(DiagMessage(line) << "Malformed resource name"); | 
|  | 322 | return false; | 
|  | 323 | } | 
|  | 324 | if (!resource_name.package.empty()) { | 
|  | 325 | context->GetDiagnostics()->Error(DiagMessage(line) | 
|  | 326 | << "Package set for resource. Only use type/name"); | 
|  | 327 | return false; | 
|  | 328 | } | 
|  | 329 | for (StringPiece directive : util::Tokenize(directives, ',')) { | 
|  | 330 | if (directive == "remove") { | 
| Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 331 | options->resources_exclude_list.insert(resource_name.ToResourceName()); | 
| Brian Chang | dcef831 | 2019-09-13 11:38:32 -0700 | [diff] [blame] | 332 | } else if (directive == "no_collapse" || directive == "no_obfuscate") { | 
|  | 333 | options->table_flattener_options.name_collapse_exemptions.insert( | 
|  | 334 | resource_name.ToResourceName()); | 
| Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame] | 335 | } | 
|  | 336 | } | 
| Luke Nicholson | b064330 | 2017-12-01 15:29:03 -0800 | [diff] [blame] | 337 | } | 
|  | 338 | return true; | 
|  | 339 | } | 
|  | 340 |  | 
| Brian Chang | dcef831 | 2019-09-13 11:38:32 -0700 | [diff] [blame] | 341 | bool ExtractConfig(const std::string& path, IAaptContext* context, OptimizeOptions* options) { | 
|  | 342 | std::string content; | 
|  | 343 | if (!android::base::ReadFileToString(path, &content, true /*follow_symlinks*/)) { | 
|  | 344 | context->GetDiagnostics()->Error(DiagMessage(path) << "failed reading config file"); | 
|  | 345 | return false; | 
|  | 346 | } | 
|  | 347 | return ParseConfig(content, context, options); | 
|  | 348 | } | 
|  | 349 |  | 
| Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 350 | bool ExtractAppDataFromManifest(OptimizeContext* context, const LoadedApk* apk, | 
| Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 351 | OptimizeOptions* out_options) { | 
| Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 352 | const xml::XmlResource* manifest = apk->GetManifest(); | 
| Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 353 | if (manifest == nullptr) { | 
| Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 354 | return false; | 
|  | 355 | } | 
|  | 356 |  | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 357 | auto app_info = ExtractAppInfoFromBinaryManifest(*manifest, context->GetDiagnostics()); | 
| Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 358 | if (!app_info) { | 
|  | 359 | context->GetDiagnostics()->Error(DiagMessage() | 
|  | 360 | << "failed to extract data from AndroidManifest.xml"); | 
|  | 361 | return false; | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | out_options->app_info = std::move(app_info.value()); | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 365 | context->SetMinSdkVersion(out_options->app_info.min_sdk_version.value_or(0)); | 
| Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 366 | return true; | 
|  | 367 | } | 
|  | 368 |  | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 369 | int OptimizeCommand::Action(const std::vector<std::string>& args) { | 
|  | 370 | if (args.size() != 1u) { | 
| Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 371 | std::cerr << "must have one APK as argument.\n\n"; | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 372 | Usage(&std::cerr); | 
| Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 373 | return 1; | 
|  | 374 | } | 
|  | 375 |  | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 376 | const std::string& apk_path = args[0]; | 
|  | 377 | OptimizeContext context; | 
|  | 378 | context.SetVerbose(verbose_); | 
| Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 379 | IDiagnostics* diag = context.GetDiagnostics(); | 
| Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 380 |  | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 381 | if (config_path_) { | 
|  | 382 | std::string& path = config_path_.value(); | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 383 | std::optional<ConfigurationParser> for_path = ConfigurationParser::ForPath(path); | 
| Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 384 | if (for_path) { | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 385 | options_.apk_artifacts = for_path.value().WithDiagnostics(diag).Parse(apk_path); | 
|  | 386 | if (!options_.apk_artifacts) { | 
| Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 387 | diag->Error(DiagMessage() << "Failed to parse the output artifact list"); | 
|  | 388 | return 1; | 
|  | 389 | } | 
|  | 390 |  | 
| Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 391 | } else { | 
| Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 392 | diag->Error(DiagMessage() << "Could not parse config file " << path); | 
| Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 393 | return 1; | 
|  | 394 | } | 
| Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 395 |  | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 396 | if (print_only_) { | 
|  | 397 | for (const OutputArtifact& artifact : options_.apk_artifacts.value()) { | 
| Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 398 | std::cout << artifact.name << std::endl; | 
| Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 399 | } | 
|  | 400 | return 0; | 
|  | 401 | } | 
|  | 402 |  | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 403 | if (!kept_artifacts_.empty()) { | 
|  | 404 | for (const std::string& artifact_str : kept_artifacts_) { | 
| Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 405 | for (const StringPiece& artifact : util::Tokenize(artifact_str, ',')) { | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 406 | options_.kept_artifacts.insert(artifact.to_string()); | 
| Shane Farmer | 666de34 | 2017-11-29 16:07:51 -0800 | [diff] [blame] | 407 | } | 
|  | 408 | } | 
|  | 409 | } | 
|  | 410 |  | 
| Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 411 | // Since we know that we are going to process the APK (not just print targets), make sure we | 
|  | 412 | // have somewhere to write them to. | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 413 | if (!options_.output_dir) { | 
| Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 414 | diag->Error(DiagMessage() << "Output directory is required when using a configuration file"); | 
|  | 415 | return 1; | 
|  | 416 | } | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 417 | } else if (print_only_) { | 
| Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 418 | diag->Error(DiagMessage() << "Asked to print artifacts without providing a configurations"); | 
|  | 419 | return 1; | 
| Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 420 | } | 
|  | 421 |  | 
| Shane Farmer | 2c12241 | 2017-12-15 16:55:54 -0800 | [diff] [blame] | 422 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(apk_path, context.GetDiagnostics()); | 
|  | 423 | if (!apk) { | 
|  | 424 | return 1; | 
|  | 425 | } | 
|  | 426 |  | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 427 | if (target_densities_) { | 
| Shane Farmer | 2c12241 | 2017-12-15 16:55:54 -0800 | [diff] [blame] | 428 | // Parse the target screen densities. | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 429 | for (const StringPiece& config_str : util::Tokenize(target_densities_.value(), ',')) { | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 430 | std::optional<uint16_t> target_density = ParseTargetDensityParameter(config_str, diag); | 
| Shane Farmer | 2c12241 | 2017-12-15 16:55:54 -0800 | [diff] [blame] | 431 | if (!target_density) { | 
|  | 432 | return 1; | 
|  | 433 | } | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 434 | options_.table_splitter_options.preferred_densities.push_back(target_density.value()); | 
| Shane Farmer | 2c12241 | 2017-12-15 16:55:54 -0800 | [diff] [blame] | 435 | } | 
|  | 436 | } | 
|  | 437 |  | 
|  | 438 | std::unique_ptr<IConfigFilter> filter; | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 439 | if (!configs_.empty()) { | 
|  | 440 | filter = ParseConfigFilterParameters(configs_, diag); | 
| Shane Farmer | 2c12241 | 2017-12-15 16:55:54 -0800 | [diff] [blame] | 441 | if (filter == nullptr) { | 
|  | 442 | return 1; | 
|  | 443 | } | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 444 | options_.table_splitter_options.config_filter = filter.get(); | 
| Shane Farmer | 2c12241 | 2017-12-15 16:55:54 -0800 | [diff] [blame] | 445 | } | 
|  | 446 |  | 
|  | 447 | // Parse the split parameters. | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 448 | for (const std::string& split_arg : split_args_) { | 
|  | 449 | options_.split_paths.emplace_back(); | 
|  | 450 | options_.split_constraints.emplace_back(); | 
|  | 451 | if (!ParseSplitParameter(split_arg, diag, &options_.split_paths.back(), | 
|  | 452 | &options_.split_constraints.back())) { | 
| Shane Farmer | 2c12241 | 2017-12-15 16:55:54 -0800 | [diff] [blame] | 453 | return 1; | 
|  | 454 | } | 
|  | 455 | } | 
|  | 456 |  | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 457 | if (resources_config_path_) { | 
|  | 458 | std::string& path = resources_config_path_.value(); | 
|  | 459 | if (!ExtractConfig(path, &context, &options_)) { | 
| Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame] | 460 | return 1; | 
|  | 461 | } | 
|  | 462 | } | 
|  | 463 |  | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 464 | if (!ExtractAppDataFromManifest(&context, apk.get(), &options_)) { | 
| Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 465 | return 1; | 
|  | 466 | } | 
|  | 467 |  | 
| Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 468 | Optimizer cmd(&context, options_); | 
| Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 469 | return cmd.Run(std::move(apk)); | 
|  | 470 | } | 
|  | 471 |  | 
|  | 472 | }  // namespace aapt |