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