blob: 588b3316e6fa25839e48b5f7884f6fe5d114dc68 [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"
29#include "filter/AbiFilter.h"
30#include "filter/Filter.h"
Adam Lesinski46708052017-09-29 14:49:15 -070031#include "format/Archive.h"
32#include "format/binary/XmlFlattener.h"
Shane Farmerefe45392017-08-21 14:39:28 -070033#include "optimize/VersionCollapser.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070034#include "process/IResourceTableConsumer.h"
35#include "split/TableSplitter.h"
36#include "util/Files.h"
Shane Farmer3edd4722017-09-01 14:34:22 -070037#include "xml/XmlDom.h"
Shane Farmer810fd182017-09-21 14:37:44 -070038#include "xml/XmlUtil.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070039
40namespace aapt {
41
Shane Farmerefe45392017-08-21 14:39:28 -070042using ::aapt::configuration::AndroidSdk;
Shane Farmercb6c3f92017-11-27 13:19:36 -080043using ::aapt::configuration::OutputArtifact;
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 Farmerefe45392017-08-21 14:39:28 -070048/**
49 * Context wrapper that allows the min Android SDK value to be overridden.
50 */
51class ContextWrapper : public IAaptContext {
52 public:
53 explicit ContextWrapper(IAaptContext* context)
54 : context_(context), min_sdk_(context_->GetMinSdkVersion()) {
55 }
56
57 PackageType GetPackageType() override {
58 return context_->GetPackageType();
59 }
60
61 SymbolTable* GetExternalSymbols() override {
62 return context_->GetExternalSymbols();
63 }
64
65 IDiagnostics* GetDiagnostics() override {
Shane Farmer20ac0342017-11-29 16:55:05 -080066 if (source_diag_) {
67 return source_diag_.get();
68 }
Shane Farmerefe45392017-08-21 14:39:28 -070069 return context_->GetDiagnostics();
70 }
71
72 const std::string& GetCompilationPackage() override {
73 return context_->GetCompilationPackage();
74 }
75
76 uint8_t GetPackageId() override {
77 return context_->GetPackageId();
78 }
79
80 NameMangler* GetNameMangler() override {
81 return context_->GetNameMangler();
82 }
83
84 bool IsVerbose() override {
85 return context_->IsVerbose();
86 }
87
88 int GetMinSdkVersion() override {
89 return min_sdk_;
90 }
91
92 void SetMinSdkVersion(int min_sdk) {
93 min_sdk_ = min_sdk;
94 }
95
Shane Farmercb6c3f92017-11-27 13:19:36 -080096 void SetSource(const std::string& source) {
97 source_diag_ =
98 util::make_unique<SourcePathDiagnostics>(Source{source}, context_->GetDiagnostics());
Shane Farmer20ac0342017-11-29 16:55:05 -080099 }
100
Shane Farmerefe45392017-08-21 14:39:28 -0700101 private:
102 IAaptContext* context_;
Shane Farmer20ac0342017-11-29 16:55:05 -0800103 std::unique_ptr<SourcePathDiagnostics> source_diag_;
Shane Farmerefe45392017-08-21 14:39:28 -0700104
105 int min_sdk_ = -1;
106};
107
Shane Farmer3ff44432017-09-29 11:59:25 -0700108class SignatureFilter : public IPathFilter {
109 bool Keep(const std::string& path) override {
110 static std::regex signature_regex(R"regex(^META-INF/.*\.(RSA|DSA|EC|SF)$)regex");
111 if (std::regex_search(path, signature_regex)) {
112 return false;
113 }
114 return !(path == "META-INF/MANIFEST.MF");
115 }
116};
117
Shane Farmer0a5b2012017-06-22 12:24:12 -0700118MultiApkGenerator::MultiApkGenerator(LoadedApk* apk, IAaptContext* context)
119 : apk_(apk), context_(context) {
120}
121
Shane Farmerefe45392017-08-21 14:39:28 -0700122bool MultiApkGenerator::FromBaseApk(const MultiApkGeneratorOptions& options) {
Shane Farmer666de342017-11-29 16:07:51 -0800123 std::unordered_set<std::string> artifacts_to_keep = options.kept_artifacts;
124 std::unordered_set<std::string> filtered_artifacts;
125 std::unordered_set<std::string> kept_artifacts;
126
Shane Farmer0a5b2012017-06-22 12:24:12 -0700127 // For now, just write out the stripped APK since ABI splitting doesn't modify anything else.
Shane Farmercb6c3f92017-11-27 13:19:36 -0800128 for (const OutputArtifact& artifact : options.apk_artifacts) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700129 FilterChain filters;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700130
Shane Farmer20ac0342017-11-29 16:55:05 -0800131 ContextWrapper wrapped_context{context_};
Shane Farmercb6c3f92017-11-27 13:19:36 -0800132 wrapped_context.SetSource(artifact.name);
Shane Farmer20ac0342017-11-29 16:55:05 -0800133
Shane Farmer666de342017-11-29 16:07:51 -0800134 if (!options.kept_artifacts.empty()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800135 const auto& it = artifacts_to_keep.find(artifact.name);
Shane Farmer666de342017-11-29 16:07:51 -0800136 if (it == artifacts_to_keep.end()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800137 filtered_artifacts.insert(artifact.name);
Shane Farmer666de342017-11-29 16:07:51 -0800138 if (context_->IsVerbose()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800139 context_->GetDiagnostics()->Note(DiagMessage(artifact.name) << "skipping artifact");
Shane Farmer666de342017-11-29 16:07:51 -0800140 }
141 continue;
142 } else {
143 artifacts_to_keep.erase(it);
Shane Farmercb6c3f92017-11-27 13:19:36 -0800144 kept_artifacts.insert(artifact.name);
Shane Farmer666de342017-11-29 16:07:51 -0800145 }
146 }
147
Shane Farmerefe45392017-08-21 14:39:28 -0700148 std::unique_ptr<ResourceTable> table =
Shane Farmercb6c3f92017-11-27 13:19:36 -0800149 FilterTable(context_, artifact, *apk_->GetResourceTable(), &filters);
Shane Farmerefe45392017-08-21 14:39:28 -0700150 if (!table) {
151 return false;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700152 }
153
Shane Farmercb6c3f92017-11-27 13:19:36 -0800154 IDiagnostics* diag = wrapped_context.GetDiagnostics();
155
Shane Farmer3edd4722017-09-01 14:34:22 -0700156 std::unique_ptr<XmlResource> manifest;
Shane Farmercb6c3f92017-11-27 13:19:36 -0800157 if (!UpdateManifest(artifact, &manifest, diag)) {
158 diag->Error(DiagMessage() << "could not update AndroidManifest.xml for output artifact");
Shane Farmer810fd182017-09-21 14:37:44 -0700159 return false;
Shane Farmer3edd4722017-09-01 14:34:22 -0700160 }
161
Shane Farmerefe45392017-08-21 14:39:28 -0700162 std::string out = options.out_dir;
Shane Farmer3d66afe2017-08-24 11:04:35 -0700163 if (!file::mkdirs(out)) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800164 diag->Warn(DiagMessage() << "could not create out dir: " << out);
Shane Farmer3d66afe2017-08-24 11:04:35 -0700165 }
Shane Farmercb6c3f92017-11-27 13:19:36 -0800166 file::AppendPath(&out, artifact.name);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700167
Shane Farmerefe45392017-08-21 14:39:28 -0700168 if (context_->IsVerbose()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800169 diag->Note(DiagMessage() << "Generating split: " << out);
Shane Farmerefe45392017-08-21 14:39:28 -0700170 }
171
Shane Farmercb6c3f92017-11-27 13:19:36 -0800172 std::unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(diag, out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700173
174 if (context_->IsVerbose()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800175 diag->Note(DiagMessage() << "Writing output: " << out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700176 }
177
Shane Farmer3ff44432017-09-29 11:59:25 -0700178 filters.AddFilter(util::make_unique<SignatureFilter>());
Shane Farmer20ac0342017-11-29 16:55:05 -0800179 if (!apk_->WriteToArchive(&wrapped_context, table.get(), options.table_flattener_options,
180 &filters, writer.get(), manifest.get())) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700181 return false;
182 }
183 }
184
Shane Farmer666de342017-11-29 16:07:51 -0800185 // Make sure all of the requested artifacts were valid. If there are any kept artifacts left,
186 // either the config or the command line was wrong.
187 if (!artifacts_to_keep.empty()) {
188 context_->GetDiagnostics()->Error(
189 DiagMessage() << "The configuration and command line to filter artifacts do not match");
190
191 context_->GetDiagnostics()->Error(DiagMessage() << kept_artifacts.size() << " kept:");
192 for (const auto& artifact : kept_artifacts) {
193 context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact);
194 }
195
196 context_->GetDiagnostics()->Error(DiagMessage() << filtered_artifacts.size() << " filtered:");
197 for (const auto& artifact : filtered_artifacts) {
198 context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact);
199 }
200
201 context_->GetDiagnostics()->Error(DiagMessage() << artifacts_to_keep.size() << " missing:");
202 for (const auto& artifact : artifacts_to_keep) {
203 context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact);
204 }
205
206 return false;
207 }
208
Shane Farmer0a5b2012017-06-22 12:24:12 -0700209 return true;
210}
211
Shane Farmercb6c3f92017-11-27 13:19:36 -0800212std::unique_ptr<ResourceTable> MultiApkGenerator::FilterTable(IAaptContext* context,
213 const OutputArtifact& artifact,
214 const ResourceTable& old_table,
215 FilterChain* filters) {
Shane Farmerefe45392017-08-21 14:39:28 -0700216 TableSplitterOptions splits;
217 AxisConfigFilter axis_filter;
Shane Farmer20ac0342017-11-29 16:55:05 -0800218 ContextWrapper wrapped_context{context};
Shane Farmercb6c3f92017-11-27 13:19:36 -0800219 wrapped_context.SetSource(artifact.name);
Shane Farmerefe45392017-08-21 14:39:28 -0700220
Shane Farmercb6c3f92017-11-27 13:19:36 -0800221 if (!artifact.abis.empty()) {
222 filters->AddFilter(AbiFilter::FromAbiList(artifact.abis));
Shane Farmerefe45392017-08-21 14:39:28 -0700223 }
224
Shane Farmercb6c3f92017-11-27 13:19:36 -0800225 if (!artifact.screen_densities.empty()) {
226 for (const auto& density_config : artifact.screen_densities) {
Shane Farmerefe45392017-08-21 14:39:28 -0700227 splits.preferred_densities.push_back(density_config.density);
228 }
229 }
230
Shane Farmercb6c3f92017-11-27 13:19:36 -0800231 if (!artifact.locales.empty()) {
232 for (const auto& locale : artifact.locales) {
Shane Farmerefe45392017-08-21 14:39:28 -0700233 axis_filter.AddConfig(locale);
234 }
235 splits.config_filter = &axis_filter;
236 }
237
Shane Farmer78c43d72017-12-04 09:08:38 -0800238 if (artifact.android_sdk) {
239 wrapped_context.SetMinSdkVersion(artifact.android_sdk.value().min_sdk_version);
Shane Farmerefe45392017-08-21 14:39:28 -0700240 }
241
242 std::unique_ptr<ResourceTable> table = old_table.Clone();
243
244 VersionCollapser collapser;
Shane Farmer20ac0342017-11-29 16:55:05 -0800245 if (!collapser.Consume(&wrapped_context, table.get())) {
246 context->GetDiagnostics()->Error(DiagMessage() << "Failed to strip versioned resources");
Shane Farmerefe45392017-08-21 14:39:28 -0700247 return {};
248 }
249
250 TableSplitter splitter{{}, splits};
251 splitter.SplitTable(table.get());
252 return table;
253}
254
Shane Farmercb6c3f92017-11-27 13:19:36 -0800255bool MultiApkGenerator::UpdateManifest(const OutputArtifact& artifact,
Shane Farmer810fd182017-09-21 14:37:44 -0700256 std::unique_ptr<XmlResource>* updated_manifest,
257 IDiagnostics* diag) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700258 const xml::XmlResource* apk_manifest = apk_->GetManifest();
259 if (apk_manifest == nullptr) {
Shane Farmer810fd182017-09-21 14:37:44 -0700260 return false;
261 }
262
Adam Lesinski8780eb62017-10-31 17:44:39 -0700263 *updated_manifest = apk_manifest->Clone();
264 XmlResource* manifest = updated_manifest->get();
265
Shane Farmer810fd182017-09-21 14:37:44 -0700266 // Make sure the first element is <manifest> with package attribute.
267 xml::Element* manifest_el = manifest->root.get();
268 if (manifest_el == nullptr) {
269 return false;
270 }
271
272 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
273 diag->Error(DiagMessage(manifest->file.source) << "root tag must be <manifest>");
274 return false;
275 }
276
277 // Update the versionCode attribute.
278 xml::Attribute* versionCode = manifest_el->FindAttribute(kSchemaAndroid, "versionCode");
279 if (versionCode == nullptr) {
280 diag->Error(DiagMessage(manifest->file.source) << "manifest must have a versionCode attribute");
281 return false;
282 }
283
284 auto* compiled_version = ValueCast<BinaryPrimitive>(versionCode->compiled_value.get());
285 if (compiled_version == nullptr) {
286 diag->Error(DiagMessage(manifest->file.source) << "versionCode is invalid");
287 return false;
288 }
289
290 int new_version = compiled_version->value.data + artifact.version;
291 versionCode->compiled_value = ResourceUtils::TryParseInt(std::to_string(new_version));
292
293 // Check to see if the minSdkVersion needs to be updated.
Shane Farmercb6c3f92017-11-27 13:19:36 -0800294 if (artifact.android_sdk) {
Shane Farmer810fd182017-09-21 14:37:44 -0700295 // TODO(safarmer): Handle the rest of the Android SDK.
Shane Farmercb6c3f92017-11-27 13:19:36 -0800296 const AndroidSdk& android_sdk = artifact.android_sdk.value();
Shane Farmer810fd182017-09-21 14:37:44 -0700297
298 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
299 if (xml::Attribute* min_sdk_attr =
300 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
301 // Populate with a pre-compiles attribute to we don't need to relink etc.
Shane Farmer78c43d72017-12-04 09:08:38 -0800302 const std::string& min_sdk_str = std::to_string(android_sdk.min_sdk_version);
Shane Farmer810fd182017-09-21 14:37:44 -0700303 min_sdk_attr->compiled_value = ResourceUtils::TryParseInt(min_sdk_str);
304 } else {
305 // There was no minSdkVersion. This is strange since at this point we should have been
306 // through the manifest fixer which sets the default minSdkVersion.
307 diag->Error(DiagMessage(manifest->file.source) << "missing minSdkVersion from <uses-sdk>");
308 return false;
309 }
310 } else {
311 // No uses-sdk present. This is strange since at this point we should have been
312 // through the manifest fixer which should have added it.
313 diag->Error(DiagMessage(manifest->file.source) << "missing <uses-sdk> from <manifest>");
314 return false;
315 }
316 }
317
Shane Farmercb6c3f92017-11-27 13:19:36 -0800318 if (!artifact.screen_densities.empty()) {
Shane Farmere1799352017-09-25 14:19:03 -0700319 xml::Element* screens_el = manifest_el->FindChild({}, "compatible-screens");
320 if (!screens_el) {
321 // create a new element.
322 std::unique_ptr<xml::Element> new_screens_el = util::make_unique<xml::Element>();
323 new_screens_el->name = "compatible-screens";
324 screens_el = new_screens_el.get();
Shane Farmerd05b9132018-02-14 15:40:35 -0800325 manifest_el->AppendChild(std::move(new_screens_el));
Shane Farmere1799352017-09-25 14:19:03 -0700326 } else {
327 // clear out the old element.
328 screens_el->GetChildElements().clear();
329 }
330
Shane Farmercb6c3f92017-11-27 13:19:36 -0800331 for (const auto& density : artifact.screen_densities) {
Shane Farmerd05b9132018-02-14 15:40:35 -0800332 AddScreens(density, screens_el);
Shane Farmere1799352017-09-25 14:19:03 -0700333 }
334 }
Shane Farmer810fd182017-09-21 14:37:44 -0700335
336 return true;
337}
338
Shane Farmerd05b9132018-02-14 15:40:35 -0800339/**
340 * Adds a screen element with both screenSize and screenDensity set. Since we only know the density
341 * we add it for all screen sizes.
342 *
343 * This requires the resource IDs for the attributes from the framework library. Since these IDs are
344 * a part of the public API (and in public.xml) we hard code the values.
345 *
346 * The excert from the framework is as follows:
347 * <public type="attr" name="screenSize" id="0x010102ca" />
348 * <public type="attr" name="screenDensity" id="0x010102cb" />
349 */
350void MultiApkGenerator::AddScreens(const ConfigDescription& config, xml::Element* parent) {
351 // Hard coded integer representation of the supported screen sizes:
352 // small = 200
353 // normal = 300
354 // large = 400
355 // xlarge = 500
356 constexpr const uint32_t kScreenSizes[4] = {200, 300, 400, 500,};
357 constexpr const uint32_t kScreenSizeResourceId = 0x010102ca;
358 constexpr const uint32_t kScreenDensityResourceId = 0x010102cb;
359
360 for (uint32_t screen_size : kScreenSizes) {
361 std::unique_ptr<xml::Element> screen = util::make_unique<xml::Element>();
362 screen->name = "screen";
363
364 xml::Attribute* size = screen->FindOrCreateAttribute(kSchemaAndroid, "screenSize");
365 size->compiled_attribute = xml::AaptAttribute(Attribute(), {kScreenSizeResourceId});
366 size->compiled_value = ResourceUtils::MakeInt(screen_size);
367
368 xml::Attribute* density = screen->FindOrCreateAttribute(kSchemaAndroid, "screenDensity");
369 density->compiled_attribute = xml::AaptAttribute(Attribute(), {kScreenDensityResourceId});
370 density->compiled_value = ResourceUtils::MakeInt(config.density);
371
372
373 parent->AppendChild(std::move(screen));
374 }
375}
376
Shane Farmer0a5b2012017-06-22 12:24:12 -0700377} // namespace aapt