blob: a931343281bc1009bd50fccd38c8a00cfd0a2355 [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>
Shane Farmer3ff44432017-09-29 11:59:25 -070020#include <regex>
Shane Farmer0a5b2012017-06-22 12:24:12 -070021#include <string>
22
23#include "androidfw/StringPiece.h"
24
25#include "LoadedApk.h"
Shane Farmer3edd4722017-09-01 14:34:22 -070026#include "ResourceUtils.h"
Shane Farmer810fd182017-09-21 14:37:44 -070027#include "ValueVisitor.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070028#include "configuration/ConfigurationParser.h"
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -070029#include "cmd/Util.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070030#include "filter/AbiFilter.h"
31#include "filter/Filter.h"
Adam Lesinski46708052017-09-29 14:49:15 -070032#include "format/Archive.h"
33#include "format/binary/XmlFlattener.h"
Shane Farmerefe45392017-08-21 14:39:28 -070034#include "optimize/VersionCollapser.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070035#include "process/IResourceTableConsumer.h"
36#include "split/TableSplitter.h"
37#include "util/Files.h"
Shane Farmer3edd4722017-09-01 14:34:22 -070038#include "xml/XmlDom.h"
Shane Farmer810fd182017-09-21 14:37:44 -070039#include "xml/XmlUtil.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070040
41namespace aapt {
42
Shane Farmerefe45392017-08-21 14:39:28 -070043using ::aapt::configuration::AndroidSdk;
Shane Farmercb6c3f92017-11-27 13:19:36 -080044using ::aapt::configuration::OutputArtifact;
Shane Farmer810fd182017-09-21 14:37:44 -070045using ::aapt::xml::kSchemaAndroid;
Shane Farmer3edd4722017-09-01 14:34:22 -070046using ::aapt::xml::XmlResource;
Shane Farmer0a5b2012017-06-22 12:24:12 -070047using ::android::StringPiece;
48
Shane Farmerefe45392017-08-21 14:39:28 -070049/**
50 * Context wrapper that allows the min Android SDK value to be overridden.
51 */
52class 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 Farmer20ac0342017-11-29 16:55:05 -080067 if (source_diag_) {
68 return source_diag_.get();
69 }
Shane Farmerefe45392017-08-21 14:39:28 -070070 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 Farmercb6c3f92017-11-27 13:19:36 -080097 void SetSource(const std::string& source) {
98 source_diag_ =
99 util::make_unique<SourcePathDiagnostics>(Source{source}, context_->GetDiagnostics());
Shane Farmer20ac0342017-11-29 16:55:05 -0800100 }
101
Chris Warrington481f0272018-02-06 14:03:39 +0000102 bool IsAutoNamespace() override {
103 return context_->IsAutoNamespace();
104 }
105
Shane Farmerefe45392017-08-21 14:39:28 -0700106 private:
107 IAaptContext* context_;
Shane Farmer20ac0342017-11-29 16:55:05 -0800108 std::unique_ptr<SourcePathDiagnostics> source_diag_;
Shane Farmerefe45392017-08-21 14:39:28 -0700109
110 int min_sdk_ = -1;
111};
112
Shane Farmer3ff44432017-09-29 11:59:25 -0700113class SignatureFilter : public IPathFilter {
114 bool Keep(const std::string& path) override {
115 static std::regex signature_regex(R"regex(^META-INF/.*\.(RSA|DSA|EC|SF)$)regex");
116 if (std::regex_search(path, signature_regex)) {
117 return false;
118 }
119 return !(path == "META-INF/MANIFEST.MF");
120 }
121};
122
Shane Farmer0a5b2012017-06-22 12:24:12 -0700123MultiApkGenerator::MultiApkGenerator(LoadedApk* apk, IAaptContext* context)
124 : apk_(apk), context_(context) {
125}
126
Shane Farmerefe45392017-08-21 14:39:28 -0700127bool MultiApkGenerator::FromBaseApk(const MultiApkGeneratorOptions& options) {
Shane Farmer666de342017-11-29 16:07:51 -0800128 std::unordered_set<std::string> artifacts_to_keep = options.kept_artifacts;
129 std::unordered_set<std::string> filtered_artifacts;
130 std::unordered_set<std::string> kept_artifacts;
131
Shane Farmer0a5b2012017-06-22 12:24:12 -0700132 // For now, just write out the stripped APK since ABI splitting doesn't modify anything else.
Shane Farmercb6c3f92017-11-27 13:19:36 -0800133 for (const OutputArtifact& artifact : options.apk_artifacts) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700134 FilterChain filters;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700135
Shane Farmer20ac0342017-11-29 16:55:05 -0800136 ContextWrapper wrapped_context{context_};
Shane Farmercb6c3f92017-11-27 13:19:36 -0800137 wrapped_context.SetSource(artifact.name);
Shane Farmer20ac0342017-11-29 16:55:05 -0800138
Shane Farmer666de342017-11-29 16:07:51 -0800139 if (!options.kept_artifacts.empty()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800140 const auto& it = artifacts_to_keep.find(artifact.name);
Shane Farmer666de342017-11-29 16:07:51 -0800141 if (it == artifacts_to_keep.end()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800142 filtered_artifacts.insert(artifact.name);
Shane Farmer666de342017-11-29 16:07:51 -0800143 if (context_->IsVerbose()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800144 context_->GetDiagnostics()->Note(DiagMessage(artifact.name) << "skipping artifact");
Shane Farmer666de342017-11-29 16:07:51 -0800145 }
146 continue;
147 } else {
148 artifacts_to_keep.erase(it);
Shane Farmercb6c3f92017-11-27 13:19:36 -0800149 kept_artifacts.insert(artifact.name);
Shane Farmer666de342017-11-29 16:07:51 -0800150 }
151 }
152
Shane Farmerefe45392017-08-21 14:39:28 -0700153 std::unique_ptr<ResourceTable> table =
Shane Farmercb6c3f92017-11-27 13:19:36 -0800154 FilterTable(context_, artifact, *apk_->GetResourceTable(), &filters);
Shane Farmerefe45392017-08-21 14:39:28 -0700155 if (!table) {
156 return false;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700157 }
158
Shane Farmercb6c3f92017-11-27 13:19:36 -0800159 IDiagnostics* diag = wrapped_context.GetDiagnostics();
160
Shane Farmer3edd4722017-09-01 14:34:22 -0700161 std::unique_ptr<XmlResource> manifest;
Shane Farmercb6c3f92017-11-27 13:19:36 -0800162 if (!UpdateManifest(artifact, &manifest, diag)) {
163 diag->Error(DiagMessage() << "could not update AndroidManifest.xml for output artifact");
Shane Farmer810fd182017-09-21 14:37:44 -0700164 return false;
Shane Farmer3edd4722017-09-01 14:34:22 -0700165 }
166
Shane Farmerefe45392017-08-21 14:39:28 -0700167 std::string out = options.out_dir;
Shane Farmer3d66afe2017-08-24 11:04:35 -0700168 if (!file::mkdirs(out)) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800169 diag->Warn(DiagMessage() << "could not create out dir: " << out);
Shane Farmer3d66afe2017-08-24 11:04:35 -0700170 }
Shane Farmercb6c3f92017-11-27 13:19:36 -0800171 file::AppendPath(&out, artifact.name);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700172
Shane Farmerefe45392017-08-21 14:39:28 -0700173 if (context_->IsVerbose()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800174 diag->Note(DiagMessage() << "Generating split: " << out);
Shane Farmerefe45392017-08-21 14:39:28 -0700175 }
176
Shane Farmercb6c3f92017-11-27 13:19:36 -0800177 std::unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(diag, out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700178
179 if (context_->IsVerbose()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800180 diag->Note(DiagMessage() << "Writing output: " << out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700181 }
182
Shane Farmer3ff44432017-09-29 11:59:25 -0700183 filters.AddFilter(util::make_unique<SignatureFilter>());
Shane Farmer20ac0342017-11-29 16:55:05 -0800184 if (!apk_->WriteToArchive(&wrapped_context, table.get(), options.table_flattener_options,
185 &filters, writer.get(), manifest.get())) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700186 return false;
187 }
188 }
189
Shane Farmer666de342017-11-29 16:07:51 -0800190 // Make sure all of the requested artifacts were valid. If there are any kept artifacts left,
191 // either the config or the command line was wrong.
192 if (!artifacts_to_keep.empty()) {
193 context_->GetDiagnostics()->Error(
194 DiagMessage() << "The configuration and command line to filter artifacts do not match");
195
196 context_->GetDiagnostics()->Error(DiagMessage() << kept_artifacts.size() << " kept:");
197 for (const auto& artifact : kept_artifacts) {
198 context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact);
199 }
200
201 context_->GetDiagnostics()->Error(DiagMessage() << filtered_artifacts.size() << " filtered:");
202 for (const auto& artifact : filtered_artifacts) {
203 context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact);
204 }
205
206 context_->GetDiagnostics()->Error(DiagMessage() << artifacts_to_keep.size() << " missing:");
207 for (const auto& artifact : artifacts_to_keep) {
208 context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact);
209 }
210
211 return false;
212 }
213
Shane Farmer0a5b2012017-06-22 12:24:12 -0700214 return true;
215}
216
Shane Farmercb6c3f92017-11-27 13:19:36 -0800217std::unique_ptr<ResourceTable> MultiApkGenerator::FilterTable(IAaptContext* context,
218 const OutputArtifact& artifact,
219 const ResourceTable& old_table,
220 FilterChain* filters) {
Shane Farmerefe45392017-08-21 14:39:28 -0700221 TableSplitterOptions splits;
222 AxisConfigFilter axis_filter;
Shane Farmer20ac0342017-11-29 16:55:05 -0800223 ContextWrapper wrapped_context{context};
Shane Farmercb6c3f92017-11-27 13:19:36 -0800224 wrapped_context.SetSource(artifact.name);
Shane Farmerefe45392017-08-21 14:39:28 -0700225
Shane Farmercb6c3f92017-11-27 13:19:36 -0800226 if (!artifact.abis.empty()) {
227 filters->AddFilter(AbiFilter::FromAbiList(artifact.abis));
Shane Farmerefe45392017-08-21 14:39:28 -0700228 }
229
Shane Farmercb6c3f92017-11-27 13:19:36 -0800230 if (!artifact.screen_densities.empty()) {
231 for (const auto& density_config : artifact.screen_densities) {
Shane Farmerefe45392017-08-21 14:39:28 -0700232 splits.preferred_densities.push_back(density_config.density);
233 }
234 }
235
Shane Farmercb6c3f92017-11-27 13:19:36 -0800236 if (!artifact.locales.empty()) {
237 for (const auto& locale : artifact.locales) {
Shane Farmerefe45392017-08-21 14:39:28 -0700238 axis_filter.AddConfig(locale);
239 }
240 splits.config_filter = &axis_filter;
241 }
242
Shane Farmer78c43d72017-12-04 09:08:38 -0800243 if (artifact.android_sdk) {
244 wrapped_context.SetMinSdkVersion(artifact.android_sdk.value().min_sdk_version);
Shane Farmerefe45392017-08-21 14:39:28 -0700245 }
246
247 std::unique_ptr<ResourceTable> table = old_table.Clone();
248
249 VersionCollapser collapser;
Shane Farmer20ac0342017-11-29 16:55:05 -0800250 if (!collapser.Consume(&wrapped_context, table.get())) {
251 context->GetDiagnostics()->Error(DiagMessage() << "Failed to strip versioned resources");
Shane Farmerefe45392017-08-21 14:39:28 -0700252 return {};
253 }
254
255 TableSplitter splitter{{}, splits};
256 splitter.SplitTable(table.get());
257 return table;
258}
259
Shane Farmercb6c3f92017-11-27 13:19:36 -0800260bool MultiApkGenerator::UpdateManifest(const OutputArtifact& artifact,
Shane Farmer810fd182017-09-21 14:37:44 -0700261 std::unique_ptr<XmlResource>* updated_manifest,
262 IDiagnostics* diag) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700263 const xml::XmlResource* apk_manifest = apk_->GetManifest();
264 if (apk_manifest == nullptr) {
Shane Farmer810fd182017-09-21 14:37:44 -0700265 return false;
266 }
267
Adam Lesinski8780eb62017-10-31 17:44:39 -0700268 *updated_manifest = apk_manifest->Clone();
269 XmlResource* manifest = updated_manifest->get();
270
Shane Farmer810fd182017-09-21 14:37:44 -0700271 // Make sure the first element is <manifest> with package attribute.
272 xml::Element* manifest_el = manifest->root.get();
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700273 if (!manifest_el) {
Shane Farmer810fd182017-09-21 14:37:44 -0700274 return false;
275 }
276
277 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
278 diag->Error(DiagMessage(manifest->file.source) << "root tag must be <manifest>");
279 return false;
280 }
281
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700282 // Retrieve the versionCode attribute.
283 auto version_code = manifest_el->FindAttribute(kSchemaAndroid, "versionCode");
284 if (!version_code) {
Shane Farmer810fd182017-09-21 14:37:44 -0700285 diag->Error(DiagMessage(manifest->file.source) << "manifest must have a versionCode attribute");
286 return false;
287 }
288
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700289 auto version_code_value = ValueCast<BinaryPrimitive>(version_code->compiled_value.get());
290 if (!version_code_value) {
Shane Farmer810fd182017-09-21 14:37:44 -0700291 diag->Error(DiagMessage(manifest->file.source) << "versionCode is invalid");
292 return false;
293 }
294
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700295 // Retrieve the versionCodeMajor attribute.
296 auto version_code_major = manifest_el->FindAttribute(kSchemaAndroid, "versionCodeMajor");
297 BinaryPrimitive* version_code_major_value = nullptr;
298 if (version_code_major) {
299 version_code_major_value = ValueCast<BinaryPrimitive>(version_code_major->compiled_value.get());
300 if (!version_code_major_value) {
301 diag->Error(DiagMessage(manifest->file.source) << "versionCodeMajor is invalid");
302 return false;
303 }
304 }
305
306 // Calculate and set the updated version code
307 uint64_t major = (version_code_major_value)
308 ? ((uint64_t) version_code_major_value->value.data) << 32 : 0;
309 uint64_t new_version = (major | version_code_value->value.data) + artifact.version;
310 SetLongVersionCode(manifest_el, new_version);
Shane Farmer810fd182017-09-21 14:37:44 -0700311
312 // Check to see if the minSdkVersion needs to be updated.
Shane Farmercb6c3f92017-11-27 13:19:36 -0800313 if (artifact.android_sdk) {
Shane Farmer810fd182017-09-21 14:37:44 -0700314 // TODO(safarmer): Handle the rest of the Android SDK.
Shane Farmercb6c3f92017-11-27 13:19:36 -0800315 const AndroidSdk& android_sdk = artifact.android_sdk.value();
Shane Farmer810fd182017-09-21 14:37:44 -0700316
317 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
318 if (xml::Attribute* min_sdk_attr =
319 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
320 // Populate with a pre-compiles attribute to we don't need to relink etc.
Shane Farmer78c43d72017-12-04 09:08:38 -0800321 const std::string& min_sdk_str = std::to_string(android_sdk.min_sdk_version);
Shane Farmer810fd182017-09-21 14:37:44 -0700322 min_sdk_attr->compiled_value = ResourceUtils::TryParseInt(min_sdk_str);
323 } else {
324 // There was no minSdkVersion. This is strange since at this point we should have been
325 // through the manifest fixer which sets the default minSdkVersion.
326 diag->Error(DiagMessage(manifest->file.source) << "missing minSdkVersion from <uses-sdk>");
327 return false;
328 }
329 } else {
330 // No uses-sdk present. This is strange since at this point we should have been
331 // through the manifest fixer which should have added it.
332 diag->Error(DiagMessage(manifest->file.source) << "missing <uses-sdk> from <manifest>");
333 return false;
334 }
335 }
336
Shane Farmercb6c3f92017-11-27 13:19:36 -0800337 if (!artifact.screen_densities.empty()) {
Shane Farmere1799352017-09-25 14:19:03 -0700338 xml::Element* screens_el = manifest_el->FindChild({}, "compatible-screens");
339 if (!screens_el) {
340 // create a new element.
341 std::unique_ptr<xml::Element> new_screens_el = util::make_unique<xml::Element>();
342 new_screens_el->name = "compatible-screens";
343 screens_el = new_screens_el.get();
Shane Farmerd05b9132018-02-14 15:40:35 -0800344 manifest_el->AppendChild(std::move(new_screens_el));
Shane Farmere1799352017-09-25 14:19:03 -0700345 } else {
346 // clear out the old element.
347 screens_el->GetChildElements().clear();
348 }
349
Shane Farmercb6c3f92017-11-27 13:19:36 -0800350 for (const auto& density : artifact.screen_densities) {
Shane Farmerd05b9132018-02-14 15:40:35 -0800351 AddScreens(density, screens_el);
Shane Farmere1799352017-09-25 14:19:03 -0700352 }
353 }
Shane Farmer810fd182017-09-21 14:37:44 -0700354
355 return true;
356}
357
Shane Farmerd05b9132018-02-14 15:40:35 -0800358/**
359 * Adds a screen element with both screenSize and screenDensity set. Since we only know the density
360 * we add it for all screen sizes.
361 *
362 * This requires the resource IDs for the attributes from the framework library. Since these IDs are
363 * a part of the public API (and in public.xml) we hard code the values.
364 *
365 * The excert from the framework is as follows:
366 * <public type="attr" name="screenSize" id="0x010102ca" />
367 * <public type="attr" name="screenDensity" id="0x010102cb" />
368 */
369void MultiApkGenerator::AddScreens(const ConfigDescription& config, xml::Element* parent) {
370 // Hard coded integer representation of the supported screen sizes:
371 // small = 200
372 // normal = 300
373 // large = 400
374 // xlarge = 500
375 constexpr const uint32_t kScreenSizes[4] = {200, 300, 400, 500,};
376 constexpr const uint32_t kScreenSizeResourceId = 0x010102ca;
377 constexpr const uint32_t kScreenDensityResourceId = 0x010102cb;
378
379 for (uint32_t screen_size : kScreenSizes) {
380 std::unique_ptr<xml::Element> screen = util::make_unique<xml::Element>();
381 screen->name = "screen";
382
383 xml::Attribute* size = screen->FindOrCreateAttribute(kSchemaAndroid, "screenSize");
384 size->compiled_attribute = xml::AaptAttribute(Attribute(), {kScreenSizeResourceId});
385 size->compiled_value = ResourceUtils::MakeInt(screen_size);
386
387 xml::Attribute* density = screen->FindOrCreateAttribute(kSchemaAndroid, "screenDensity");
388 density->compiled_attribute = xml::AaptAttribute(Attribute(), {kScreenDensityResourceId});
389 density->compiled_value = ResourceUtils::MakeInt(config.density);
390
391
392 parent->AppendChild(std::move(screen));
393 }
394}
395
Shane Farmer0a5b2012017-06-22 12:24:12 -0700396} // namespace aapt