blob: 16898d6f3e03033791460fdf0cd0be2acd8ae679 [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 Farmer0a5b2012017-06-22 12:24:12 -0700123 // TODO(safarmer): Handle APK version codes for the generated APKs.
Shane Farmer0a5b2012017-06-22 12:24:12 -0700124
Shane Farmer666de342017-11-29 16:07:51 -0800125 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 Farmer0a5b2012017-06-22 12:24:12 -0700129 // For now, just write out the stripped APK since ABI splitting doesn't modify anything else.
Shane Farmercb6c3f92017-11-27 13:19:36 -0800130 for (const OutputArtifact& artifact : options.apk_artifacts) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700131 FilterChain filters;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700132
Shane Farmer20ac0342017-11-29 16:55:05 -0800133 ContextWrapper wrapped_context{context_};
Shane Farmercb6c3f92017-11-27 13:19:36 -0800134 wrapped_context.SetSource(artifact.name);
Shane Farmer20ac0342017-11-29 16:55:05 -0800135
Shane Farmer666de342017-11-29 16:07:51 -0800136 if (!options.kept_artifacts.empty()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800137 const auto& it = artifacts_to_keep.find(artifact.name);
Shane Farmer666de342017-11-29 16:07:51 -0800138 if (it == artifacts_to_keep.end()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800139 filtered_artifacts.insert(artifact.name);
Shane Farmer666de342017-11-29 16:07:51 -0800140 if (context_->IsVerbose()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800141 context_->GetDiagnostics()->Note(DiagMessage(artifact.name) << "skipping artifact");
Shane Farmer666de342017-11-29 16:07:51 -0800142 }
143 continue;
144 } else {
145 artifacts_to_keep.erase(it);
Shane Farmercb6c3f92017-11-27 13:19:36 -0800146 kept_artifacts.insert(artifact.name);
Shane Farmer666de342017-11-29 16:07:51 -0800147 }
148 }
149
Shane Farmerefe45392017-08-21 14:39:28 -0700150 std::unique_ptr<ResourceTable> table =
Shane Farmercb6c3f92017-11-27 13:19:36 -0800151 FilterTable(context_, artifact, *apk_->GetResourceTable(), &filters);
Shane Farmerefe45392017-08-21 14:39:28 -0700152 if (!table) {
153 return false;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700154 }
155
Shane Farmercb6c3f92017-11-27 13:19:36 -0800156 IDiagnostics* diag = wrapped_context.GetDiagnostics();
157
Shane Farmer3edd4722017-09-01 14:34:22 -0700158 std::unique_ptr<XmlResource> manifest;
Shane Farmercb6c3f92017-11-27 13:19:36 -0800159 if (!UpdateManifest(artifact, &manifest, diag)) {
160 diag->Error(DiagMessage() << "could not update AndroidManifest.xml for output artifact");
Shane Farmer810fd182017-09-21 14:37:44 -0700161 return false;
Shane Farmer3edd4722017-09-01 14:34:22 -0700162 }
163
Shane Farmerefe45392017-08-21 14:39:28 -0700164 std::string out = options.out_dir;
Shane Farmer3d66afe2017-08-24 11:04:35 -0700165 if (!file::mkdirs(out)) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800166 diag->Warn(DiagMessage() << "could not create out dir: " << out);
Shane Farmer3d66afe2017-08-24 11:04:35 -0700167 }
Shane Farmercb6c3f92017-11-27 13:19:36 -0800168 file::AppendPath(&out, artifact.name);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700169
Shane Farmerefe45392017-08-21 14:39:28 -0700170 if (context_->IsVerbose()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800171 diag->Note(DiagMessage() << "Generating split: " << out);
Shane Farmerefe45392017-08-21 14:39:28 -0700172 }
173
Shane Farmercb6c3f92017-11-27 13:19:36 -0800174 std::unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(diag, out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700175
176 if (context_->IsVerbose()) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800177 diag->Note(DiagMessage() << "Writing output: " << out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700178 }
179
Shane Farmer3ff44432017-09-29 11:59:25 -0700180 filters.AddFilter(util::make_unique<SignatureFilter>());
Shane Farmer20ac0342017-11-29 16:55:05 -0800181 if (!apk_->WriteToArchive(&wrapped_context, table.get(), options.table_flattener_options,
182 &filters, writer.get(), manifest.get())) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700183 return false;
184 }
185 }
186
Shane Farmer666de342017-11-29 16:07:51 -0800187 // 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 Farmer0a5b2012017-06-22 12:24:12 -0700211 return true;
212}
213
Shane Farmercb6c3f92017-11-27 13:19:36 -0800214std::unique_ptr<ResourceTable> MultiApkGenerator::FilterTable(IAaptContext* context,
215 const OutputArtifact& artifact,
216 const ResourceTable& old_table,
217 FilterChain* filters) {
Shane Farmerefe45392017-08-21 14:39:28 -0700218 TableSplitterOptions splits;
219 AxisConfigFilter axis_filter;
Shane Farmer20ac0342017-11-29 16:55:05 -0800220 ContextWrapper wrapped_context{context};
Shane Farmercb6c3f92017-11-27 13:19:36 -0800221 wrapped_context.SetSource(artifact.name);
Shane Farmerefe45392017-08-21 14:39:28 -0700222
Shane Farmercb6c3f92017-11-27 13:19:36 -0800223 if (!artifact.abis.empty()) {
224 filters->AddFilter(AbiFilter::FromAbiList(artifact.abis));
Shane Farmerefe45392017-08-21 14:39:28 -0700225 }
226
Shane Farmercb6c3f92017-11-27 13:19:36 -0800227 if (!artifact.screen_densities.empty()) {
228 for (const auto& density_config : artifact.screen_densities) {
Shane Farmerefe45392017-08-21 14:39:28 -0700229 splits.preferred_densities.push_back(density_config.density);
230 }
231 }
232
Shane Farmercb6c3f92017-11-27 13:19:36 -0800233 if (!artifact.locales.empty()) {
234 for (const auto& locale : artifact.locales) {
Shane Farmerefe45392017-08-21 14:39:28 -0700235 axis_filter.AddConfig(locale);
236 }
237 splits.config_filter = &axis_filter;
238 }
239
Shane Farmercb6c3f92017-11-27 13:19:36 -0800240 if (artifact.android_sdk && artifact.android_sdk.value().min_sdk_version) {
241 wrapped_context.SetMinSdkVersion(artifact.android_sdk.value().min_sdk_version.value());
Shane Farmerefe45392017-08-21 14:39:28 -0700242 }
243
244 std::unique_ptr<ResourceTable> table = old_table.Clone();
245
246 VersionCollapser collapser;
Shane Farmer20ac0342017-11-29 16:55:05 -0800247 if (!collapser.Consume(&wrapped_context, table.get())) {
248 context->GetDiagnostics()->Error(DiagMessage() << "Failed to strip versioned resources");
Shane Farmerefe45392017-08-21 14:39:28 -0700249 return {};
250 }
251
252 TableSplitter splitter{{}, splits};
253 splitter.SplitTable(table.get());
254 return table;
255}
256
Shane Farmercb6c3f92017-11-27 13:19:36 -0800257bool MultiApkGenerator::UpdateManifest(const OutputArtifact& artifact,
Shane Farmer810fd182017-09-21 14:37:44 -0700258 std::unique_ptr<XmlResource>* updated_manifest,
259 IDiagnostics* diag) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700260 const xml::XmlResource* apk_manifest = apk_->GetManifest();
261 if (apk_manifest == nullptr) {
Shane Farmer810fd182017-09-21 14:37:44 -0700262 return false;
263 }
264
Adam Lesinski8780eb62017-10-31 17:44:39 -0700265 *updated_manifest = apk_manifest->Clone();
266 XmlResource* manifest = updated_manifest->get();
267
Shane Farmer810fd182017-09-21 14:37:44 -0700268 // 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 Farmercb6c3f92017-11-27 13:19:36 -0800296 if (artifact.android_sdk) {
Shane Farmer810fd182017-09-21 14:37:44 -0700297 // TODO(safarmer): Handle the rest of the Android SDK.
Shane Farmercb6c3f92017-11-27 13:19:36 -0800298 const AndroidSdk& android_sdk = artifact.android_sdk.value();
Shane Farmer810fd182017-09-21 14:37:44 -0700299
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.
304 const std::string& min_sdk_str = std::to_string(android_sdk.min_sdk_version.value());
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 Farmercb6c3f92017-11-27 13:19:36 -0800320 if (!artifact.screen_densities.empty()) {
Shane Farmere1799352017-09-25 14:19:03 -0700321 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();
327 manifest_el->InsertChild(0, std::move(new_screens_el));
328 } else {
329 // clear out the old element.
330 screens_el->GetChildElements().clear();
331 }
332
Shane Farmercb6c3f92017-11-27 13:19:36 -0800333 for (const auto& density : artifact.screen_densities) {
Shane Farmere1799352017-09-25 14:19:03 -0700334 std::unique_ptr<xml::Element> screen_el = util::make_unique<xml::Element>();
335 screen_el->name = "screen";
336 const char* density_str = density.toString().string();
337 screen_el->attributes.push_back(xml::Attribute{kSchemaAndroid, "screenDensity", density_str});
338 screens_el->AppendChild(std::move(screen_el));
339 }
340 }
Shane Farmer810fd182017-09-21 14:37:44 -0700341
342 return true;
343}
344
Shane Farmer0a5b2012017-06-22 12:24:12 -0700345} // namespace aapt