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 | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 26 | #include "ValueVisitor.h" |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 27 | #include "configuration/ConfigurationParser.h" |
| 28 | #include "filter/AbiFilter.h" |
| 29 | #include "filter/Filter.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 30 | #include "format/Archive.h" |
| 31 | #include "format/binary/XmlFlattener.h" |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 32 | #include "optimize/VersionCollapser.h" |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 33 | #include "process/IResourceTableConsumer.h" |
| 34 | #include "split/TableSplitter.h" |
| 35 | #include "util/Files.h" |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 36 | #include "xml/XmlDom.h" |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 37 | #include "xml/XmlUtil.h" |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 38 | |
| 39 | namespace aapt { |
| 40 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 41 | using ::aapt::configuration::AndroidSdk; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 42 | using ::aapt::configuration::Artifact; |
| 43 | using ::aapt::configuration::PostProcessingConfiguration; |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 44 | using ::aapt::xml::kSchemaAndroid; |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 45 | using ::aapt::xml::XmlResource; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 46 | using ::android::StringPiece; |
| 47 | |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 48 | namespace { |
| 49 | |
| 50 | Maybe<AndroidSdk> GetAndroidSdk(const Artifact& artifact, const PostProcessingConfiguration& config, |
| 51 | IDiagnostics* diag) { |
| 52 | if (!artifact.android_sdk_group) { |
| 53 | return {}; |
| 54 | } |
| 55 | |
| 56 | const std::string& group_name = artifact.android_sdk_group.value(); |
| 57 | auto group = config.android_sdk_groups.find(group_name); |
| 58 | // TODO: Remove validation when configuration parser ensures referential integrity. |
| 59 | if (group == config.android_sdk_groups.end()) { |
| 60 | diag->Error(DiagMessage() << "could not find referenced group '" << group_name << "'"); |
| 61 | return {}; |
| 62 | } |
| 63 | |
| 64 | return group->second; |
| 65 | } |
| 66 | |
| 67 | } // namespace |
| 68 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Context wrapper that allows the min Android SDK value to be overridden. |
| 71 | */ |
| 72 | class ContextWrapper : public IAaptContext { |
| 73 | public: |
| 74 | explicit ContextWrapper(IAaptContext* context) |
| 75 | : context_(context), min_sdk_(context_->GetMinSdkVersion()) { |
| 76 | } |
| 77 | |
| 78 | PackageType GetPackageType() override { |
| 79 | return context_->GetPackageType(); |
| 80 | } |
| 81 | |
| 82 | SymbolTable* GetExternalSymbols() override { |
| 83 | return context_->GetExternalSymbols(); |
| 84 | } |
| 85 | |
| 86 | IDiagnostics* GetDiagnostics() override { |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 87 | if (source_diag_) { |
| 88 | return source_diag_.get(); |
| 89 | } |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 90 | return context_->GetDiagnostics(); |
| 91 | } |
| 92 | |
| 93 | const std::string& GetCompilationPackage() override { |
| 94 | return context_->GetCompilationPackage(); |
| 95 | } |
| 96 | |
| 97 | uint8_t GetPackageId() override { |
| 98 | return context_->GetPackageId(); |
| 99 | } |
| 100 | |
| 101 | NameMangler* GetNameMangler() override { |
| 102 | return context_->GetNameMangler(); |
| 103 | } |
| 104 | |
| 105 | bool IsVerbose() override { |
| 106 | return context_->IsVerbose(); |
| 107 | } |
| 108 | |
| 109 | int GetMinSdkVersion() override { |
| 110 | return min_sdk_; |
| 111 | } |
| 112 | |
| 113 | void SetMinSdkVersion(int min_sdk) { |
| 114 | min_sdk_ = min_sdk; |
| 115 | } |
| 116 | |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 117 | void SetSource(const Source& source) { |
| 118 | source_diag_ = util::make_unique<SourcePathDiagnostics>(source, context_->GetDiagnostics()); |
| 119 | } |
| 120 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 121 | private: |
| 122 | IAaptContext* context_; |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 123 | std::unique_ptr<SourcePathDiagnostics> source_diag_; |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 124 | |
| 125 | int min_sdk_ = -1; |
| 126 | }; |
| 127 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 128 | MultiApkGenerator::MultiApkGenerator(LoadedApk* apk, IAaptContext* context) |
| 129 | : apk_(apk), context_(context) { |
| 130 | } |
| 131 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 132 | bool MultiApkGenerator::FromBaseApk(const MultiApkGeneratorOptions& options) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 133 | // TODO(safarmer): Handle APK version codes for the generated APKs. |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 134 | const PostProcessingConfiguration& config = options.config; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 135 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 136 | const std::string& apk_name = file::GetFilename(apk_->GetSource().path).to_string(); |
| 137 | const StringPiece ext = file::GetExtension(apk_name); |
| 138 | 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] | 139 | |
Shane Farmer | 666de34 | 2017-11-29 16:07:51 -0800 | [diff] [blame] | 140 | std::unordered_set<std::string> artifacts_to_keep = options.kept_artifacts; |
| 141 | std::unordered_set<std::string> filtered_artifacts; |
| 142 | std::unordered_set<std::string> kept_artifacts; |
| 143 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 144 | // For now, just write out the stripped APK since ABI splitting doesn't modify anything else. |
| 145 | for (const Artifact& artifact : config.artifacts) { |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 146 | SourcePathDiagnostics diag{{apk_name}, context_->GetDiagnostics()}; |
| 147 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 148 | FilterChain filters; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 149 | |
| 150 | if (!artifact.name && !config.artifact_format) { |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 151 | diag.Error( |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 152 | DiagMessage() << "Artifact does not have a name and no global name template defined"); |
| 153 | return false; |
| 154 | } |
| 155 | |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 156 | Maybe<std::string> maybe_artifact_name = |
| 157 | (artifact.name) ? artifact.Name(apk_name, &diag) |
| 158 | : artifact.ToArtifactName(config.artifact_format.value(), apk_name, &diag); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 159 | |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 160 | if (!maybe_artifact_name) { |
| 161 | diag.Error(DiagMessage() << "Could not determine split APK artifact name"); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 165 | const std::string& artifact_name = maybe_artifact_name.value(); |
| 166 | |
| 167 | ContextWrapper wrapped_context{context_}; |
| 168 | wrapped_context.SetSource({artifact_name}); |
| 169 | |
Shane Farmer | 666de34 | 2017-11-29 16:07:51 -0800 | [diff] [blame] | 170 | if (!options.kept_artifacts.empty()) { |
| 171 | const auto& it = artifacts_to_keep.find(artifact_name); |
| 172 | if (it == artifacts_to_keep.end()) { |
| 173 | filtered_artifacts.insert(artifact_name); |
| 174 | if (context_->IsVerbose()) { |
| 175 | context_->GetDiagnostics()->Note(DiagMessage(artifact_name) << "skipping artifact"); |
| 176 | } |
| 177 | continue; |
| 178 | } else { |
| 179 | artifacts_to_keep.erase(it); |
| 180 | kept_artifacts.insert(artifact_name); |
| 181 | } |
| 182 | } |
| 183 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 184 | std::unique_ptr<ResourceTable> table = |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 185 | FilterTable(artifact, config, *apk_->GetResourceTable(), &wrapped_context, &filters); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 186 | if (!table) { |
| 187 | return false; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 190 | std::unique_ptr<XmlResource> manifest; |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 191 | if (!UpdateManifest(artifact, config, &manifest, &diag)) { |
| 192 | diag.Error(DiagMessage() << "could not update AndroidManifest.xml for output artifact"); |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 193 | return false; |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 196 | std::string out = options.out_dir; |
Shane Farmer | 3d66afe | 2017-08-24 11:04:35 -0700 | [diff] [blame] | 197 | if (!file::mkdirs(out)) { |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 198 | diag.Warn(DiagMessage() << "could not create out dir: " << out); |
Shane Farmer | 3d66afe | 2017-08-24 11:04:35 -0700 | [diff] [blame] | 199 | } |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 200 | file::AppendPath(&out, artifact_name); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 201 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 202 | if (context_->IsVerbose()) { |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 203 | diag.Note(DiagMessage() << "Generating split: " << out); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 206 | std::unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(&diag, out); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 207 | |
| 208 | if (context_->IsVerbose()) { |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 209 | diag.Note(DiagMessage() << "Writing output: " << out); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 212 | if (!apk_->WriteToArchive(&wrapped_context, table.get(), options.table_flattener_options, |
| 213 | &filters, writer.get(), manifest.get())) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 214 | return false; |
| 215 | } |
| 216 | } |
| 217 | |
Shane Farmer | 666de34 | 2017-11-29 16:07:51 -0800 | [diff] [blame] | 218 | // Make sure all of the requested artifacts were valid. If there are any kept artifacts left, |
| 219 | // either the config or the command line was wrong. |
| 220 | if (!artifacts_to_keep.empty()) { |
| 221 | context_->GetDiagnostics()->Error( |
| 222 | DiagMessage() << "The configuration and command line to filter artifacts do not match"); |
| 223 | |
| 224 | context_->GetDiagnostics()->Error(DiagMessage() << kept_artifacts.size() << " kept:"); |
| 225 | for (const auto& artifact : kept_artifacts) { |
| 226 | context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact); |
| 227 | } |
| 228 | |
| 229 | context_->GetDiagnostics()->Error(DiagMessage() << filtered_artifacts.size() << " filtered:"); |
| 230 | for (const auto& artifact : filtered_artifacts) { |
| 231 | context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact); |
| 232 | } |
| 233 | |
| 234 | context_->GetDiagnostics()->Error(DiagMessage() << artifacts_to_keep.size() << " missing:"); |
| 235 | for (const auto& artifact : artifacts_to_keep) { |
| 236 | context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact); |
| 237 | } |
| 238 | |
| 239 | return false; |
| 240 | } |
| 241 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 242 | return true; |
| 243 | } |
| 244 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 245 | std::unique_ptr<ResourceTable> MultiApkGenerator::FilterTable( |
| 246 | const configuration::Artifact& artifact, |
| 247 | const configuration::PostProcessingConfiguration& config, const ResourceTable& old_table, |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 248 | IAaptContext* context, FilterChain* filters) { |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 249 | TableSplitterOptions splits; |
| 250 | AxisConfigFilter axis_filter; |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 251 | ContextWrapper wrapped_context{context}; |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 252 | |
| 253 | if (artifact.abi_group) { |
| 254 | const std::string& group_name = artifact.abi_group.value(); |
| 255 | |
| 256 | auto group = config.abi_groups.find(group_name); |
| 257 | // TODO: Remove validation when configuration parser ensures referential integrity. |
| 258 | if (group == config.abi_groups.end()) { |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 259 | context->GetDiagnostics()->Error(DiagMessage() << "could not find referenced ABI group '" |
| 260 | << group_name << "'"); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 261 | return {}; |
| 262 | } |
| 263 | filters->AddFilter(AbiFilter::FromAbiList(group->second)); |
| 264 | } |
| 265 | |
| 266 | if (artifact.screen_density_group) { |
| 267 | const std::string& group_name = artifact.screen_density_group.value(); |
| 268 | |
| 269 | auto group = config.screen_density_groups.find(group_name); |
| 270 | // TODO: Remove validation when configuration parser ensures referential integrity. |
| 271 | if (group == config.screen_density_groups.end()) { |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 272 | context->GetDiagnostics()->Error(DiagMessage() |
| 273 | << "could not find referenced group '" << group_name << "'"); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 274 | return {}; |
| 275 | } |
| 276 | |
| 277 | const std::vector<ConfigDescription>& densities = group->second; |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 278 | for (const auto& density_config : densities) { |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 279 | splits.preferred_densities.push_back(density_config.density); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if (artifact.locale_group) { |
| 284 | const std::string& group_name = artifact.locale_group.value(); |
| 285 | auto group = config.locale_groups.find(group_name); |
| 286 | // TODO: Remove validation when configuration parser ensures referential integrity. |
| 287 | if (group == config.locale_groups.end()) { |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 288 | context->GetDiagnostics()->Error(DiagMessage() |
| 289 | << "could not find referenced group '" << group_name << "'"); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 290 | return {}; |
| 291 | } |
| 292 | |
| 293 | const std::vector<ConfigDescription>& locales = group->second; |
| 294 | for (const auto& locale : locales) { |
| 295 | axis_filter.AddConfig(locale); |
| 296 | } |
| 297 | splits.config_filter = &axis_filter; |
| 298 | } |
| 299 | |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 300 | Maybe<AndroidSdk> sdk = GetAndroidSdk(artifact, config, context->GetDiagnostics()); |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 301 | if (sdk && sdk.value().min_sdk_version) { |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 302 | wrapped_context.SetMinSdkVersion(sdk.value().min_sdk_version.value()); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | std::unique_ptr<ResourceTable> table = old_table.Clone(); |
| 306 | |
| 307 | VersionCollapser collapser; |
Shane Farmer | 20ac034 | 2017-11-29 16:55:05 -0800 | [diff] [blame] | 308 | if (!collapser.Consume(&wrapped_context, table.get())) { |
| 309 | context->GetDiagnostics()->Error(DiagMessage() << "Failed to strip versioned resources"); |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 310 | return {}; |
| 311 | } |
| 312 | |
| 313 | TableSplitter splitter{{}, splits}; |
| 314 | splitter.SplitTable(table.get()); |
| 315 | return table; |
| 316 | } |
| 317 | |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 318 | bool MultiApkGenerator::UpdateManifest(const Artifact& artifact, |
| 319 | const PostProcessingConfiguration& config, |
| 320 | std::unique_ptr<XmlResource>* updated_manifest, |
| 321 | IDiagnostics* diag) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 322 | const xml::XmlResource* apk_manifest = apk_->GetManifest(); |
| 323 | if (apk_manifest == nullptr) { |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 324 | return false; |
| 325 | } |
| 326 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 327 | *updated_manifest = apk_manifest->Clone(); |
| 328 | XmlResource* manifest = updated_manifest->get(); |
| 329 | |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 330 | // Make sure the first element is <manifest> with package attribute. |
| 331 | xml::Element* manifest_el = manifest->root.get(); |
| 332 | if (manifest_el == nullptr) { |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") { |
| 337 | diag->Error(DiagMessage(manifest->file.source) << "root tag must be <manifest>"); |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | // Update the versionCode attribute. |
| 342 | xml::Attribute* versionCode = manifest_el->FindAttribute(kSchemaAndroid, "versionCode"); |
| 343 | if (versionCode == nullptr) { |
| 344 | diag->Error(DiagMessage(manifest->file.source) << "manifest must have a versionCode attribute"); |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | auto* compiled_version = ValueCast<BinaryPrimitive>(versionCode->compiled_value.get()); |
| 349 | if (compiled_version == nullptr) { |
| 350 | diag->Error(DiagMessage(manifest->file.source) << "versionCode is invalid"); |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | int new_version = compiled_version->value.data + artifact.version; |
| 355 | versionCode->compiled_value = ResourceUtils::TryParseInt(std::to_string(new_version)); |
| 356 | |
| 357 | // Check to see if the minSdkVersion needs to be updated. |
| 358 | Maybe<AndroidSdk> maybe_sdk = GetAndroidSdk(artifact, config, diag); |
| 359 | if (maybe_sdk) { |
| 360 | // TODO(safarmer): Handle the rest of the Android SDK. |
| 361 | const AndroidSdk& android_sdk = maybe_sdk.value(); |
| 362 | |
| 363 | if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) { |
| 364 | if (xml::Attribute* min_sdk_attr = |
| 365 | uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) { |
| 366 | // Populate with a pre-compiles attribute to we don't need to relink etc. |
| 367 | const std::string& min_sdk_str = std::to_string(android_sdk.min_sdk_version.value()); |
| 368 | min_sdk_attr->compiled_value = ResourceUtils::TryParseInt(min_sdk_str); |
| 369 | } else { |
| 370 | // There was no minSdkVersion. This is strange since at this point we should have been |
| 371 | // through the manifest fixer which sets the default minSdkVersion. |
| 372 | diag->Error(DiagMessage(manifest->file.source) << "missing minSdkVersion from <uses-sdk>"); |
| 373 | return false; |
| 374 | } |
| 375 | } else { |
| 376 | // No uses-sdk present. This is strange since at this point we should have been |
| 377 | // through the manifest fixer which should have added it. |
| 378 | diag->Error(DiagMessage(manifest->file.source) << "missing <uses-sdk> from <manifest>"); |
| 379 | return false; |
| 380 | } |
| 381 | } |
| 382 | |
Shane Farmer | e179935 | 2017-09-25 14:19:03 -0700 | [diff] [blame] | 383 | if (artifact.screen_density_group) { |
| 384 | auto densities = config.screen_density_groups.find(artifact.screen_density_group.value()); |
| 385 | CHECK(densities != config.screen_density_groups.end()) << "Missing density group"; |
| 386 | |
| 387 | xml::Element* screens_el = manifest_el->FindChild({}, "compatible-screens"); |
| 388 | if (!screens_el) { |
| 389 | // create a new element. |
| 390 | std::unique_ptr<xml::Element> new_screens_el = util::make_unique<xml::Element>(); |
| 391 | new_screens_el->name = "compatible-screens"; |
| 392 | screens_el = new_screens_el.get(); |
| 393 | manifest_el->InsertChild(0, std::move(new_screens_el)); |
| 394 | } else { |
| 395 | // clear out the old element. |
| 396 | screens_el->GetChildElements().clear(); |
| 397 | } |
| 398 | |
| 399 | for (const auto& density : densities->second) { |
| 400 | std::unique_ptr<xml::Element> screen_el = util::make_unique<xml::Element>(); |
| 401 | screen_el->name = "screen"; |
| 402 | const char* density_str = density.toString().string(); |
| 403 | screen_el->attributes.push_back(xml::Attribute{kSchemaAndroid, "screenDensity", density_str}); |
| 404 | screens_el->AppendChild(std::move(screen_el)); |
| 405 | } |
| 406 | } |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 407 | |
| 408 | return true; |
| 409 | } |
| 410 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 411 | } // namespace aapt |