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" |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 29 | #include "optimize/VersionCollapser.h" |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 30 | #include "process/IResourceTableConsumer.h" |
| 31 | #include "split/TableSplitter.h" |
| 32 | #include "util/Files.h" |
| 33 | |
| 34 | namespace aapt { |
| 35 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 36 | using ::aapt::configuration::AndroidSdk; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 37 | using ::aapt::configuration::Artifact; |
| 38 | using ::aapt::configuration::PostProcessingConfiguration; |
| 39 | using ::android::StringPiece; |
| 40 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Context wrapper that allows the min Android SDK value to be overridden. |
| 43 | */ |
| 44 | class ContextWrapper : public IAaptContext { |
| 45 | public: |
| 46 | explicit ContextWrapper(IAaptContext* context) |
| 47 | : context_(context), min_sdk_(context_->GetMinSdkVersion()) { |
| 48 | } |
| 49 | |
| 50 | PackageType GetPackageType() override { |
| 51 | return context_->GetPackageType(); |
| 52 | } |
| 53 | |
| 54 | SymbolTable* GetExternalSymbols() override { |
| 55 | return context_->GetExternalSymbols(); |
| 56 | } |
| 57 | |
| 58 | IDiagnostics* GetDiagnostics() override { |
| 59 | return context_->GetDiagnostics(); |
| 60 | } |
| 61 | |
| 62 | const std::string& GetCompilationPackage() override { |
| 63 | return context_->GetCompilationPackage(); |
| 64 | } |
| 65 | |
| 66 | uint8_t GetPackageId() override { |
| 67 | return context_->GetPackageId(); |
| 68 | } |
| 69 | |
| 70 | NameMangler* GetNameMangler() override { |
| 71 | return context_->GetNameMangler(); |
| 72 | } |
| 73 | |
| 74 | bool IsVerbose() override { |
| 75 | return context_->IsVerbose(); |
| 76 | } |
| 77 | |
| 78 | int GetMinSdkVersion() override { |
| 79 | return min_sdk_; |
| 80 | } |
| 81 | |
| 82 | void SetMinSdkVersion(int min_sdk) { |
| 83 | min_sdk_ = min_sdk; |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | IAaptContext* context_; |
| 88 | |
| 89 | int min_sdk_ = -1; |
| 90 | }; |
| 91 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 92 | MultiApkGenerator::MultiApkGenerator(LoadedApk* apk, IAaptContext* context) |
| 93 | : apk_(apk), context_(context) { |
| 94 | } |
| 95 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 96 | bool MultiApkGenerator::FromBaseApk(const MultiApkGeneratorOptions& options) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 97 | // TODO(safarmer): Handle APK version codes for the generated APKs. |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 98 | IDiagnostics* diag = context_->GetDiagnostics(); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 99 | const PostProcessingConfiguration& config = options.config; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 100 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 101 | const std::string& apk_name = file::GetFilename(apk_->GetSource().path).to_string(); |
| 102 | const StringPiece ext = file::GetExtension(apk_name); |
| 103 | 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] | 104 | |
| 105 | // For now, just write out the stripped APK since ABI splitting doesn't modify anything else. |
| 106 | for (const Artifact& artifact : config.artifacts) { |
| 107 | FilterChain filters; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 108 | |
| 109 | if (!artifact.name && !config.artifact_format) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 110 | diag->Error( |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 111 | DiagMessage() << "Artifact does not have a name and no global name template defined"); |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | Maybe<std::string> artifact_name = |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 116 | (artifact.name) ? artifact.Name(apk_name, diag) |
| 117 | : artifact.ToArtifactName(config.artifact_format.value(), apk_name, diag); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 118 | |
| 119 | if (!artifact_name) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 120 | diag->Error(DiagMessage() << "Could not determine split APK artifact name"); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 124 | std::unique_ptr<ResourceTable> table = |
| 125 | FilterTable(artifact, config, *apk_->GetResourceTable(), &filters); |
| 126 | if (!table) { |
| 127 | return false; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 130 | std::string out = options.out_dir; |
Shane Farmer | 3d66afe | 2017-08-24 11:04:35 -0700 | [diff] [blame] | 131 | if (!file::mkdirs(out)) { |
| 132 | context_->GetDiagnostics()->Warn(DiagMessage() << "could not create out dir: " << out); |
| 133 | } |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 134 | file::AppendPath(&out, artifact_name.value()); |
| 135 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 136 | if (context_->IsVerbose()) { |
| 137 | context_->GetDiagnostics()->Note(DiagMessage() << "Generating split: " << out); |
| 138 | } |
| 139 | |
| 140 | std::unique_ptr<IArchiveWriter> writer = |
| 141 | CreateZipFileArchiveWriter(context_->GetDiagnostics(), out); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 142 | |
| 143 | if (context_->IsVerbose()) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 144 | diag->Note(DiagMessage() << "Writing output: " << out); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 147 | if (!apk_->WriteToArchive(context_, table.get(), options.table_flattener_options, &filters, |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 148 | writer.get())) { |
| 149 | return false; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 156 | std::unique_ptr<ResourceTable> MultiApkGenerator::FilterTable( |
| 157 | const configuration::Artifact& artifact, |
| 158 | const configuration::PostProcessingConfiguration& config, const ResourceTable& old_table, |
| 159 | FilterChain* filters) { |
| 160 | TableSplitterOptions splits; |
| 161 | AxisConfigFilter axis_filter; |
| 162 | ContextWrapper wrappedContext{context_}; |
| 163 | |
| 164 | if (artifact.abi_group) { |
| 165 | const std::string& group_name = artifact.abi_group.value(); |
| 166 | |
| 167 | auto group = config.abi_groups.find(group_name); |
| 168 | // TODO: Remove validation when configuration parser ensures referential integrity. |
| 169 | if (group == config.abi_groups.end()) { |
| 170 | context_->GetDiagnostics()->Error(DiagMessage() << "could not find referenced ABI group '" |
| 171 | << group_name << "'"); |
| 172 | return {}; |
| 173 | } |
| 174 | filters->AddFilter(AbiFilter::FromAbiList(group->second)); |
| 175 | } |
| 176 | |
| 177 | if (artifact.screen_density_group) { |
| 178 | const std::string& group_name = artifact.screen_density_group.value(); |
| 179 | |
| 180 | auto group = config.screen_density_groups.find(group_name); |
| 181 | // TODO: Remove validation when configuration parser ensures referential integrity. |
| 182 | if (group == config.screen_density_groups.end()) { |
| 183 | context_->GetDiagnostics()->Error(DiagMessage() << "could not find referenced group '" |
| 184 | << group_name << "'"); |
| 185 | return {}; |
| 186 | } |
| 187 | |
| 188 | const std::vector<ConfigDescription>& densities = group->second; |
| 189 | for(const auto& density_config : densities) { |
| 190 | splits.preferred_densities.push_back(density_config.density); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (artifact.locale_group) { |
| 195 | const std::string& group_name = artifact.locale_group.value(); |
| 196 | auto group = config.locale_groups.find(group_name); |
| 197 | // TODO: Remove validation when configuration parser ensures referential integrity. |
| 198 | if (group == config.locale_groups.end()) { |
| 199 | context_->GetDiagnostics()->Error(DiagMessage() << "could not find referenced group '" |
| 200 | << group_name << "'"); |
| 201 | return {}; |
| 202 | } |
| 203 | |
| 204 | const std::vector<ConfigDescription>& locales = group->second; |
| 205 | for (const auto& locale : locales) { |
| 206 | axis_filter.AddConfig(locale); |
| 207 | } |
| 208 | splits.config_filter = &axis_filter; |
| 209 | } |
| 210 | |
| 211 | if (artifact.android_sdk_group) { |
| 212 | const std::string& group_name = artifact.android_sdk_group.value(); |
| 213 | auto group = config.android_sdk_groups.find(group_name); |
| 214 | // TODO: Remove validation when configuration parser ensures referential integrity. |
| 215 | if (group == config.android_sdk_groups.end()) { |
| 216 | context_->GetDiagnostics()->Error(DiagMessage() << "could not find referenced group '" |
| 217 | << group_name << "'"); |
| 218 | return {}; |
| 219 | } |
| 220 | |
| 221 | const AndroidSdk& sdk = group->second; |
| 222 | if (!sdk.min_sdk_version) { |
| 223 | context_->GetDiagnostics()->Error(DiagMessage() |
| 224 | << "skipping SDK version. No min SDK: " << group_name); |
| 225 | return {}; |
| 226 | } |
| 227 | |
| 228 | ConfigDescription c; |
| 229 | const std::string& version = sdk.min_sdk_version.value(); |
| 230 | if (!ConfigDescription::Parse(version, &c)) { |
| 231 | context_->GetDiagnostics()->Error(DiagMessage() << "could not parse min SDK: " << version); |
| 232 | return {}; |
| 233 | } |
| 234 | |
| 235 | wrappedContext.SetMinSdkVersion(c.sdkVersion); |
| 236 | } |
| 237 | |
| 238 | std::unique_ptr<ResourceTable> table = old_table.Clone(); |
| 239 | |
| 240 | VersionCollapser collapser; |
| 241 | if (!collapser.Consume(context_, table.get())) { |
| 242 | context_->GetDiagnostics()->Error(DiagMessage() << "Failed to strip versioned resources"); |
| 243 | return {}; |
| 244 | } |
| 245 | |
| 246 | TableSplitter splitter{{}, splits}; |
| 247 | splitter.SplitTable(table.get()); |
| 248 | return table; |
| 249 | } |
| 250 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 251 | } // namespace aapt |