blob: da3b8792be69441c3d773640638a0ee37fcee96c [file] [log] [blame]
Shane Farmer0a5b2012017-06-22 12:24:12 -07001/*
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 Farmer3edd4722017-09-01 14:34:22 -070025#include "ResourceUtils.h"
Shane Farmer810fd182017-09-21 14:37:44 -070026#include "ValueVisitor.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070027#include "configuration/ConfigurationParser.h"
28#include "filter/AbiFilter.h"
29#include "filter/Filter.h"
Adam Lesinski46708052017-09-29 14:49:15 -070030#include "format/Archive.h"
31#include "format/binary/XmlFlattener.h"
Shane Farmerefe45392017-08-21 14:39:28 -070032#include "optimize/VersionCollapser.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070033#include "process/IResourceTableConsumer.h"
34#include "split/TableSplitter.h"
35#include "util/Files.h"
Shane Farmer3edd4722017-09-01 14:34:22 -070036#include "xml/XmlDom.h"
Shane Farmer810fd182017-09-21 14:37:44 -070037#include "xml/XmlUtil.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070038
39namespace aapt {
40
Shane Farmerefe45392017-08-21 14:39:28 -070041using ::aapt::configuration::AndroidSdk;
Shane Farmer0a5b2012017-06-22 12:24:12 -070042using ::aapt::configuration::Artifact;
43using ::aapt::configuration::PostProcessingConfiguration;
Shane Farmer810fd182017-09-21 14:37:44 -070044using ::aapt::xml::kSchemaAndroid;
Shane Farmer3edd4722017-09-01 14:34:22 -070045using ::aapt::xml::XmlResource;
Shane Farmer0a5b2012017-06-22 12:24:12 -070046using ::android::StringPiece;
47
Shane Farmer3edd4722017-09-01 14:34:22 -070048namespace {
49
50Maybe<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 Farmerefe45392017-08-21 14:39:28 -070069/**
70 * Context wrapper that allows the min Android SDK value to be overridden.
71 */
72class 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 Farmer20ac0342017-11-29 16:55:05 -080087 if (source_diag_) {
88 return source_diag_.get();
89 }
Shane Farmerefe45392017-08-21 14:39:28 -070090 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 Farmer20ac0342017-11-29 16:55:05 -0800117 void SetSource(const Source& source) {
118 source_diag_ = util::make_unique<SourcePathDiagnostics>(source, context_->GetDiagnostics());
119 }
120
Shane Farmerefe45392017-08-21 14:39:28 -0700121 private:
122 IAaptContext* context_;
Shane Farmer20ac0342017-11-29 16:55:05 -0800123 std::unique_ptr<SourcePathDiagnostics> source_diag_;
Shane Farmerefe45392017-08-21 14:39:28 -0700124
125 int min_sdk_ = -1;
126};
127
Shane Farmer0a5b2012017-06-22 12:24:12 -0700128MultiApkGenerator::MultiApkGenerator(LoadedApk* apk, IAaptContext* context)
129 : apk_(apk), context_(context) {
130}
131
Shane Farmerefe45392017-08-21 14:39:28 -0700132bool MultiApkGenerator::FromBaseApk(const MultiApkGeneratorOptions& options) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700133 // TODO(safarmer): Handle APK version codes for the generated APKs.
Shane Farmerefe45392017-08-21 14:39:28 -0700134 const PostProcessingConfiguration& config = options.config;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700135
Shane Farmer9ecc0752017-08-24 15:55:36 -0700136 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 Farmer0a5b2012017-06-22 12:24:12 -0700139
Shane Farmer666de342017-11-29 16:07:51 -0800140 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 Farmer0a5b2012017-06-22 12:24:12 -0700144 // For now, just write out the stripped APK since ABI splitting doesn't modify anything else.
145 for (const Artifact& artifact : config.artifacts) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800146 SourcePathDiagnostics diag{{apk_name}, context_->GetDiagnostics()};
147
Shane Farmer0a5b2012017-06-22 12:24:12 -0700148 FilterChain filters;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700149
150 if (!artifact.name && !config.artifact_format) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800151 diag.Error(
Shane Farmer0a5b2012017-06-22 12:24:12 -0700152 DiagMessage() << "Artifact does not have a name and no global name template defined");
153 return false;
154 }
155
Shane Farmer20ac0342017-11-29 16:55:05 -0800156 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 Farmer0a5b2012017-06-22 12:24:12 -0700159
Shane Farmer20ac0342017-11-29 16:55:05 -0800160 if (!maybe_artifact_name) {
161 diag.Error(DiagMessage() << "Could not determine split APK artifact name");
Shane Farmer0a5b2012017-06-22 12:24:12 -0700162 return false;
163 }
164
Shane Farmer20ac0342017-11-29 16:55:05 -0800165 const std::string& artifact_name = maybe_artifact_name.value();
166
167 ContextWrapper wrapped_context{context_};
168 wrapped_context.SetSource({artifact_name});
169
Shane Farmer666de342017-11-29 16:07:51 -0800170 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 Farmerefe45392017-08-21 14:39:28 -0700184 std::unique_ptr<ResourceTable> table =
Shane Farmer20ac0342017-11-29 16:55:05 -0800185 FilterTable(artifact, config, *apk_->GetResourceTable(), &wrapped_context, &filters);
Shane Farmerefe45392017-08-21 14:39:28 -0700186 if (!table) {
187 return false;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700188 }
189
Shane Farmer3edd4722017-09-01 14:34:22 -0700190 std::unique_ptr<XmlResource> manifest;
Shane Farmer20ac0342017-11-29 16:55:05 -0800191 if (!UpdateManifest(artifact, config, &manifest, &diag)) {
192 diag.Error(DiagMessage() << "could not update AndroidManifest.xml for output artifact");
Shane Farmer810fd182017-09-21 14:37:44 -0700193 return false;
Shane Farmer3edd4722017-09-01 14:34:22 -0700194 }
195
Shane Farmerefe45392017-08-21 14:39:28 -0700196 std::string out = options.out_dir;
Shane Farmer3d66afe2017-08-24 11:04:35 -0700197 if (!file::mkdirs(out)) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800198 diag.Warn(DiagMessage() << "could not create out dir: " << out);
Shane Farmer3d66afe2017-08-24 11:04:35 -0700199 }
Shane Farmer20ac0342017-11-29 16:55:05 -0800200 file::AppendPath(&out, artifact_name);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700201
Shane Farmerefe45392017-08-21 14:39:28 -0700202 if (context_->IsVerbose()) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800203 diag.Note(DiagMessage() << "Generating split: " << out);
Shane Farmerefe45392017-08-21 14:39:28 -0700204 }
205
Shane Farmer20ac0342017-11-29 16:55:05 -0800206 std::unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(&diag, out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700207
208 if (context_->IsVerbose()) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800209 diag.Note(DiagMessage() << "Writing output: " << out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700210 }
211
Shane Farmer20ac0342017-11-29 16:55:05 -0800212 if (!apk_->WriteToArchive(&wrapped_context, table.get(), options.table_flattener_options,
213 &filters, writer.get(), manifest.get())) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700214 return false;
215 }
216 }
217
Shane Farmer666de342017-11-29 16:07:51 -0800218 // 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 Farmer0a5b2012017-06-22 12:24:12 -0700242 return true;
243}
244
Shane Farmerefe45392017-08-21 14:39:28 -0700245std::unique_ptr<ResourceTable> MultiApkGenerator::FilterTable(
246 const configuration::Artifact& artifact,
247 const configuration::PostProcessingConfiguration& config, const ResourceTable& old_table,
Shane Farmer20ac0342017-11-29 16:55:05 -0800248 IAaptContext* context, FilterChain* filters) {
Shane Farmerefe45392017-08-21 14:39:28 -0700249 TableSplitterOptions splits;
250 AxisConfigFilter axis_filter;
Shane Farmer20ac0342017-11-29 16:55:05 -0800251 ContextWrapper wrapped_context{context};
Shane Farmerefe45392017-08-21 14:39:28 -0700252
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 Farmer20ac0342017-11-29 16:55:05 -0800259 context->GetDiagnostics()->Error(DiagMessage() << "could not find referenced ABI group '"
260 << group_name << "'");
Shane Farmerefe45392017-08-21 14:39:28 -0700261 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 Farmer20ac0342017-11-29 16:55:05 -0800272 context->GetDiagnostics()->Error(DiagMessage()
273 << "could not find referenced group '" << group_name << "'");
Shane Farmerefe45392017-08-21 14:39:28 -0700274 return {};
275 }
276
277 const std::vector<ConfigDescription>& densities = group->second;
Shane Farmer20ac0342017-11-29 16:55:05 -0800278 for (const auto& density_config : densities) {
Shane Farmerefe45392017-08-21 14:39:28 -0700279 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 Farmer20ac0342017-11-29 16:55:05 -0800288 context->GetDiagnostics()->Error(DiagMessage()
289 << "could not find referenced group '" << group_name << "'");
Shane Farmerefe45392017-08-21 14:39:28 -0700290 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 Farmer20ac0342017-11-29 16:55:05 -0800300 Maybe<AndroidSdk> sdk = GetAndroidSdk(artifact, config, context->GetDiagnostics());
Shane Farmer3edd4722017-09-01 14:34:22 -0700301 if (sdk && sdk.value().min_sdk_version) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800302 wrapped_context.SetMinSdkVersion(sdk.value().min_sdk_version.value());
Shane Farmerefe45392017-08-21 14:39:28 -0700303 }
304
305 std::unique_ptr<ResourceTable> table = old_table.Clone();
306
307 VersionCollapser collapser;
Shane Farmer20ac0342017-11-29 16:55:05 -0800308 if (!collapser.Consume(&wrapped_context, table.get())) {
309 context->GetDiagnostics()->Error(DiagMessage() << "Failed to strip versioned resources");
Shane Farmerefe45392017-08-21 14:39:28 -0700310 return {};
311 }
312
313 TableSplitter splitter{{}, splits};
314 splitter.SplitTable(table.get());
315 return table;
316}
317
Shane Farmer810fd182017-09-21 14:37:44 -0700318bool MultiApkGenerator::UpdateManifest(const Artifact& artifact,
319 const PostProcessingConfiguration& config,
320 std::unique_ptr<XmlResource>* updated_manifest,
321 IDiagnostics* diag) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700322 const xml::XmlResource* apk_manifest = apk_->GetManifest();
323 if (apk_manifest == nullptr) {
Shane Farmer810fd182017-09-21 14:37:44 -0700324 return false;
325 }
326
Adam Lesinski8780eb62017-10-31 17:44:39 -0700327 *updated_manifest = apk_manifest->Clone();
328 XmlResource* manifest = updated_manifest->get();
329
Shane Farmer810fd182017-09-21 14:37:44 -0700330 // 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 Farmere1799352017-09-25 14:19:03 -0700383 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 Farmer810fd182017-09-21 14:37:44 -0700407
408 return true;
409}
410
Shane Farmer0a5b2012017-06-22 12:24:12 -0700411} // namespace aapt