Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -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 "MultiApkGenerator.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <string> |
| 21 | |
| 22 | #include "androidfw/StringPiece.h" |
| 23 | |
| 24 | #include "LoadedApk.h" |
| 25 | #include "configuration/ConfigurationParser.h" |
| 26 | #include "filter/AbiFilter.h" |
| 27 | #include "filter/Filter.h" |
| 28 | #include "flatten/Archive.h" |
| 29 | #include "process/IResourceTableConsumer.h" |
| 30 | #include "split/TableSplitter.h" |
| 31 | #include "util/Files.h" |
| 32 | |
| 33 | namespace aapt { |
| 34 | |
| 35 | using ::aapt::configuration::Artifact; |
| 36 | using ::aapt::configuration::PostProcessingConfiguration; |
| 37 | using ::android::StringPiece; |
| 38 | |
| 39 | MultiApkGenerator::MultiApkGenerator(LoadedApk* apk, IAaptContext* context) |
| 40 | : apk_(apk), context_(context) { |
| 41 | } |
| 42 | |
| 43 | bool MultiApkGenerator::FromBaseApk(const std::string& out_dir, |
| 44 | const PostProcessingConfiguration& config, |
| 45 | const TableFlattenerOptions& table_flattener_options) { |
| 46 | // TODO(safarmer): Handle APK version codes for the generated APKs. |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 47 | IDiagnostics* diag = context_->GetDiagnostics(); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 48 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 49 | const std::string& apk_name = file::GetFilename(apk_->GetSource().path).to_string(); |
| 50 | const StringPiece ext = file::GetExtension(apk_name); |
| 51 | const std::string base_name = apk_name.substr(0, apk_name.rfind(ext.to_string())); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 52 | |
| 53 | // For now, just write out the stripped APK since ABI splitting doesn't modify anything else. |
| 54 | for (const Artifact& artifact : config.artifacts) { |
| 55 | FilterChain filters; |
| 56 | TableSplitterOptions splits; |
| 57 | AxisConfigFilter axis_filter; |
| 58 | |
| 59 | if (!artifact.name && !config.artifact_format) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 60 | diag->Error( |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 61 | DiagMessage() << "Artifact does not have a name and no global name template defined"); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | Maybe<std::string> artifact_name = |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 66 | (artifact.name) ? artifact.Name(apk_name, diag) |
| 67 | : artifact.ToArtifactName(config.artifact_format.value(), apk_name, diag); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 68 | |
| 69 | if (!artifact_name) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 70 | diag->Error(DiagMessage() << "Could not determine split APK artifact name"); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if (artifact.abi_group) { |
| 75 | const std::string& group_name = artifact.abi_group.value(); |
| 76 | |
| 77 | auto group = config.abi_groups.find(group_name); |
| 78 | // TODO: Remove validation when configuration parser ensures referential integrity. |
| 79 | if (group == config.abi_groups.end()) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 80 | diag->Error(DiagMessage() << "could not find referenced ABI group '" << group_name << "'"); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | filters.AddFilter(AbiFilter::FromAbiList(group->second)); |
| 84 | } |
| 85 | |
| 86 | if (artifact.screen_density_group) { |
| 87 | const std::string& group_name = artifact.screen_density_group.value(); |
| 88 | |
| 89 | auto group = config.screen_density_groups.find(group_name); |
| 90 | // TODO: Remove validation when configuration parser ensures referential integrity. |
| 91 | if (group == config.screen_density_groups.end()) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 92 | diag->Error(DiagMessage() << "could not find referenced group '" << group_name << "'"); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | |
| 96 | const std::vector<ConfigDescription>& densities = group->second; |
| 97 | std::for_each(densities.begin(), densities.end(), [&](const ConfigDescription& c) { |
| 98 | splits.preferred_densities.push_back(c.density); |
| 99 | }); |
| 100 | } |
| 101 | |
| 102 | if (artifact.locale_group) { |
| 103 | const std::string& group_name = artifact.locale_group.value(); |
| 104 | auto group = config.locale_groups.find(group_name); |
| 105 | // TODO: Remove validation when configuration parser ensures referential integrity. |
| 106 | if (group == config.locale_groups.end()) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 107 | diag->Error(DiagMessage() << "could not find referenced group '" << group_name << "'"); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 108 | return false; |
| 109 | } |
| 110 | |
| 111 | const std::vector<ConfigDescription>& locales = group->second; |
| 112 | std::for_each(locales.begin(), locales.end(), |
| 113 | [&](const ConfigDescription& c) { axis_filter.AddConfig(c); }); |
| 114 | splits.config_filter = &axis_filter; |
| 115 | } |
| 116 | |
| 117 | std::unique_ptr<ResourceTable> table = apk_->GetResourceTable()->Clone(); |
| 118 | |
Shane Farmer | 3d66afe | 2017-08-24 11:04:35 -0700 | [diff] [blame] | 119 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 120 | TableSplitter splitter{{}, splits}; |
| 121 | splitter.SplitTable(table.get()); |
| 122 | |
| 123 | std::string out = out_dir; |
Shane Farmer | 3d66afe | 2017-08-24 11:04:35 -0700 | [diff] [blame] | 124 | if (!file::mkdirs(out)) { |
| 125 | context_->GetDiagnostics()->Warn(DiagMessage() << "could not create out dir: " << out); |
| 126 | } |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 127 | file::AppendPath(&out, artifact_name.value()); |
| 128 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 129 | std::unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(diag, out); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 130 | |
| 131 | if (context_->IsVerbose()) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 132 | diag->Note(DiagMessage() << "Writing output: " << out); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if (!apk_->WriteToArchive(context_, table.get(), table_flattener_options, &filters, |
| 136 | writer.get())) { |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | } // namespace aapt |