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