blob: 3c96344d8602b0c9bd7f3c0690a1fb33f435e265 [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
140 // For now, just write out the stripped APK since ABI splitting doesn't modify anything else.
141 for (const Artifact& artifact : config.artifacts) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800142 SourcePathDiagnostics diag{{apk_name}, context_->GetDiagnostics()};
143
Shane Farmer0a5b2012017-06-22 12:24:12 -0700144 FilterChain filters;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700145
146 if (!artifact.name && !config.artifact_format) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800147 diag.Error(
Shane Farmer0a5b2012017-06-22 12:24:12 -0700148 DiagMessage() << "Artifact does not have a name and no global name template defined");
149 return false;
150 }
151
Shane Farmer20ac0342017-11-29 16:55:05 -0800152 Maybe<std::string> maybe_artifact_name =
153 (artifact.name) ? artifact.Name(apk_name, &diag)
154 : artifact.ToArtifactName(config.artifact_format.value(), apk_name, &diag);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700155
Shane Farmer20ac0342017-11-29 16:55:05 -0800156 if (!maybe_artifact_name) {
157 diag.Error(DiagMessage() << "Could not determine split APK artifact name");
Shane Farmer0a5b2012017-06-22 12:24:12 -0700158 return false;
159 }
160
Shane Farmer20ac0342017-11-29 16:55:05 -0800161 const std::string& artifact_name = maybe_artifact_name.value();
162
163 ContextWrapper wrapped_context{context_};
164 wrapped_context.SetSource({artifact_name});
165
Shane Farmerefe45392017-08-21 14:39:28 -0700166 std::unique_ptr<ResourceTable> table =
Shane Farmer20ac0342017-11-29 16:55:05 -0800167 FilterTable(artifact, config, *apk_->GetResourceTable(), &wrapped_context, &filters);
Shane Farmerefe45392017-08-21 14:39:28 -0700168 if (!table) {
169 return false;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700170 }
171
Shane Farmer3edd4722017-09-01 14:34:22 -0700172 std::unique_ptr<XmlResource> manifest;
Shane Farmer20ac0342017-11-29 16:55:05 -0800173 if (!UpdateManifest(artifact, config, &manifest, &diag)) {
174 diag.Error(DiagMessage() << "could not update AndroidManifest.xml for output artifact");
Shane Farmer810fd182017-09-21 14:37:44 -0700175 return false;
Shane Farmer3edd4722017-09-01 14:34:22 -0700176 }
177
Shane Farmerefe45392017-08-21 14:39:28 -0700178 std::string out = options.out_dir;
Shane Farmer3d66afe2017-08-24 11:04:35 -0700179 if (!file::mkdirs(out)) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800180 diag.Warn(DiagMessage() << "could not create out dir: " << out);
Shane Farmer3d66afe2017-08-24 11:04:35 -0700181 }
Shane Farmer20ac0342017-11-29 16:55:05 -0800182 file::AppendPath(&out, artifact_name);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700183
Shane Farmerefe45392017-08-21 14:39:28 -0700184 if (context_->IsVerbose()) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800185 diag.Note(DiagMessage() << "Generating split: " << out);
Shane Farmerefe45392017-08-21 14:39:28 -0700186 }
187
Shane Farmer20ac0342017-11-29 16:55:05 -0800188 std::unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(&diag, out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700189
190 if (context_->IsVerbose()) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800191 diag.Note(DiagMessage() << "Writing output: " << out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700192 }
193
Shane Farmer20ac0342017-11-29 16:55:05 -0800194 if (!apk_->WriteToArchive(&wrapped_context, table.get(), options.table_flattener_options,
195 &filters, writer.get(), manifest.get())) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700196 return false;
197 }
198 }
199
200 return true;
201}
202
Shane Farmerefe45392017-08-21 14:39:28 -0700203std::unique_ptr<ResourceTable> MultiApkGenerator::FilterTable(
204 const configuration::Artifact& artifact,
205 const configuration::PostProcessingConfiguration& config, const ResourceTable& old_table,
Shane Farmer20ac0342017-11-29 16:55:05 -0800206 IAaptContext* context, FilterChain* filters) {
Shane Farmerefe45392017-08-21 14:39:28 -0700207 TableSplitterOptions splits;
208 AxisConfigFilter axis_filter;
Shane Farmer20ac0342017-11-29 16:55:05 -0800209 ContextWrapper wrapped_context{context};
Shane Farmerefe45392017-08-21 14:39:28 -0700210
211 if (artifact.abi_group) {
212 const std::string& group_name = artifact.abi_group.value();
213
214 auto group = config.abi_groups.find(group_name);
215 // TODO: Remove validation when configuration parser ensures referential integrity.
216 if (group == config.abi_groups.end()) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800217 context->GetDiagnostics()->Error(DiagMessage() << "could not find referenced ABI group '"
218 << group_name << "'");
Shane Farmerefe45392017-08-21 14:39:28 -0700219 return {};
220 }
221 filters->AddFilter(AbiFilter::FromAbiList(group->second));
222 }
223
224 if (artifact.screen_density_group) {
225 const std::string& group_name = artifact.screen_density_group.value();
226
227 auto group = config.screen_density_groups.find(group_name);
228 // TODO: Remove validation when configuration parser ensures referential integrity.
229 if (group == config.screen_density_groups.end()) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800230 context->GetDiagnostics()->Error(DiagMessage()
231 << "could not find referenced group '" << group_name << "'");
Shane Farmerefe45392017-08-21 14:39:28 -0700232 return {};
233 }
234
235 const std::vector<ConfigDescription>& densities = group->second;
Shane Farmer20ac0342017-11-29 16:55:05 -0800236 for (const auto& density_config : densities) {
Shane Farmerefe45392017-08-21 14:39:28 -0700237 splits.preferred_densities.push_back(density_config.density);
238 }
239 }
240
241 if (artifact.locale_group) {
242 const std::string& group_name = artifact.locale_group.value();
243 auto group = config.locale_groups.find(group_name);
244 // TODO: Remove validation when configuration parser ensures referential integrity.
245 if (group == config.locale_groups.end()) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800246 context->GetDiagnostics()->Error(DiagMessage()
247 << "could not find referenced group '" << group_name << "'");
Shane Farmerefe45392017-08-21 14:39:28 -0700248 return {};
249 }
250
251 const std::vector<ConfigDescription>& locales = group->second;
252 for (const auto& locale : locales) {
253 axis_filter.AddConfig(locale);
254 }
255 splits.config_filter = &axis_filter;
256 }
257
Shane Farmer20ac0342017-11-29 16:55:05 -0800258 Maybe<AndroidSdk> sdk = GetAndroidSdk(artifact, config, context->GetDiagnostics());
Shane Farmer3edd4722017-09-01 14:34:22 -0700259 if (sdk && sdk.value().min_sdk_version) {
Shane Farmer20ac0342017-11-29 16:55:05 -0800260 wrapped_context.SetMinSdkVersion(sdk.value().min_sdk_version.value());
Shane Farmerefe45392017-08-21 14:39:28 -0700261 }
262
263 std::unique_ptr<ResourceTable> table = old_table.Clone();
264
265 VersionCollapser collapser;
Shane Farmer20ac0342017-11-29 16:55:05 -0800266 if (!collapser.Consume(&wrapped_context, table.get())) {
267 context->GetDiagnostics()->Error(DiagMessage() << "Failed to strip versioned resources");
Shane Farmerefe45392017-08-21 14:39:28 -0700268 return {};
269 }
270
271 TableSplitter splitter{{}, splits};
272 splitter.SplitTable(table.get());
273 return table;
274}
275
Shane Farmer810fd182017-09-21 14:37:44 -0700276bool MultiApkGenerator::UpdateManifest(const Artifact& artifact,
277 const PostProcessingConfiguration& config,
278 std::unique_ptr<XmlResource>* updated_manifest,
279 IDiagnostics* diag) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700280 const xml::XmlResource* apk_manifest = apk_->GetManifest();
281 if (apk_manifest == nullptr) {
Shane Farmer810fd182017-09-21 14:37:44 -0700282 return false;
283 }
284
Adam Lesinski8780eb62017-10-31 17:44:39 -0700285 *updated_manifest = apk_manifest->Clone();
286 XmlResource* manifest = updated_manifest->get();
287
Shane Farmer810fd182017-09-21 14:37:44 -0700288 // Make sure the first element is <manifest> with package attribute.
289 xml::Element* manifest_el = manifest->root.get();
290 if (manifest_el == nullptr) {
291 return false;
292 }
293
294 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
295 diag->Error(DiagMessage(manifest->file.source) << "root tag must be <manifest>");
296 return false;
297 }
298
299 // Update the versionCode attribute.
300 xml::Attribute* versionCode = manifest_el->FindAttribute(kSchemaAndroid, "versionCode");
301 if (versionCode == nullptr) {
302 diag->Error(DiagMessage(manifest->file.source) << "manifest must have a versionCode attribute");
303 return false;
304 }
305
306 auto* compiled_version = ValueCast<BinaryPrimitive>(versionCode->compiled_value.get());
307 if (compiled_version == nullptr) {
308 diag->Error(DiagMessage(manifest->file.source) << "versionCode is invalid");
309 return false;
310 }
311
312 int new_version = compiled_version->value.data + artifact.version;
313 versionCode->compiled_value = ResourceUtils::TryParseInt(std::to_string(new_version));
314
315 // Check to see if the minSdkVersion needs to be updated.
316 Maybe<AndroidSdk> maybe_sdk = GetAndroidSdk(artifact, config, diag);
317 if (maybe_sdk) {
318 // TODO(safarmer): Handle the rest of the Android SDK.
319 const AndroidSdk& android_sdk = maybe_sdk.value();
320
321 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
322 if (xml::Attribute* min_sdk_attr =
323 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
324 // Populate with a pre-compiles attribute to we don't need to relink etc.
325 const std::string& min_sdk_str = std::to_string(android_sdk.min_sdk_version.value());
326 min_sdk_attr->compiled_value = ResourceUtils::TryParseInt(min_sdk_str);
327 } else {
328 // There was no minSdkVersion. This is strange since at this point we should have been
329 // through the manifest fixer which sets the default minSdkVersion.
330 diag->Error(DiagMessage(manifest->file.source) << "missing minSdkVersion from <uses-sdk>");
331 return false;
332 }
333 } else {
334 // No uses-sdk present. This is strange since at this point we should have been
335 // through the manifest fixer which should have added it.
336 diag->Error(DiagMessage(manifest->file.source) << "missing <uses-sdk> from <manifest>");
337 return false;
338 }
339 }
340
Shane Farmere1799352017-09-25 14:19:03 -0700341 if (artifact.screen_density_group) {
342 auto densities = config.screen_density_groups.find(artifact.screen_density_group.value());
343 CHECK(densities != config.screen_density_groups.end()) << "Missing density group";
344
345 xml::Element* screens_el = manifest_el->FindChild({}, "compatible-screens");
346 if (!screens_el) {
347 // create a new element.
348 std::unique_ptr<xml::Element> new_screens_el = util::make_unique<xml::Element>();
349 new_screens_el->name = "compatible-screens";
350 screens_el = new_screens_el.get();
351 manifest_el->InsertChild(0, std::move(new_screens_el));
352 } else {
353 // clear out the old element.
354 screens_el->GetChildElements().clear();
355 }
356
357 for (const auto& density : densities->second) {
358 std::unique_ptr<xml::Element> screen_el = util::make_unique<xml::Element>();
359 screen_el->name = "screen";
360 const char* density_str = density.toString().string();
361 screen_el->attributes.push_back(xml::Attribute{kSchemaAndroid, "screenDensity", density_str});
362 screens_el->AppendChild(std::move(screen_el));
363 }
364 }
Shane Farmer810fd182017-09-21 14:37:44 -0700365
366 return true;
367}
368
Shane Farmer0a5b2012017-06-22 12:24:12 -0700369} // namespace aapt