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