blob: e7a4f857852977ef40eeaf44a5b1209611b40b61 [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"
25#include "configuration/ConfigurationParser.h"
26#include "filter/AbiFilter.h"
27#include "filter/Filter.h"
28#include "flatten/Archive.h"
29#include "process/IResourceTableConsumer.h"
30#include "split/TableSplitter.h"
31#include "util/Files.h"
32
33namespace aapt {
34
35using ::aapt::configuration::Artifact;
36using ::aapt::configuration::PostProcessingConfiguration;
37using ::android::StringPiece;
38
39MultiApkGenerator::MultiApkGenerator(LoadedApk* apk, IAaptContext* context)
40 : apk_(apk), context_(context) {
41}
42
43bool MultiApkGenerator::FromBaseApk(const std::string& out_dir,
44 const PostProcessingConfiguration& config,
45 const TableFlattenerOptions& table_flattener_options) {
46 // TODO(safarmer): Handle APK version codes for the generated APKs.
Shane Farmer9ecc0752017-08-24 15:55:36 -070047 IDiagnostics* diag = context_->GetDiagnostics();
Shane Farmer0a5b2012017-06-22 12:24:12 -070048
Shane Farmer9ecc0752017-08-24 15:55:36 -070049 const std::string& apk_name = file::GetFilename(apk_->GetSource().path).to_string();
50 const StringPiece ext = file::GetExtension(apk_name);
51 const std::string base_name = apk_name.substr(0, apk_name.rfind(ext.to_string()));
Shane Farmer0a5b2012017-06-22 12:24:12 -070052
53 // For now, just write out the stripped APK since ABI splitting doesn't modify anything else.
54 for (const Artifact& artifact : config.artifacts) {
55 FilterChain filters;
56 TableSplitterOptions splits;
57 AxisConfigFilter axis_filter;
58
59 if (!artifact.name && !config.artifact_format) {
Shane Farmer9ecc0752017-08-24 15:55:36 -070060 diag->Error(
Shane Farmer0a5b2012017-06-22 12:24:12 -070061 DiagMessage() << "Artifact does not have a name and no global name template defined");
62 return false;
63 }
64
65 Maybe<std::string> artifact_name =
Shane Farmer9ecc0752017-08-24 15:55:36 -070066 (artifact.name) ? artifact.Name(apk_name, diag)
67 : artifact.ToArtifactName(config.artifact_format.value(), apk_name, diag);
Shane Farmer0a5b2012017-06-22 12:24:12 -070068
69 if (!artifact_name) {
Shane Farmer9ecc0752017-08-24 15:55:36 -070070 diag->Error(DiagMessage() << "Could not determine split APK artifact name");
Shane Farmer0a5b2012017-06-22 12:24:12 -070071 return false;
72 }
73
74 if (artifact.abi_group) {
75 const std::string& group_name = artifact.abi_group.value();
76
77 auto group = config.abi_groups.find(group_name);
78 // TODO: Remove validation when configuration parser ensures referential integrity.
79 if (group == config.abi_groups.end()) {
Shane Farmer9ecc0752017-08-24 15:55:36 -070080 diag->Error(DiagMessage() << "could not find referenced ABI group '" << group_name << "'");
Shane Farmer0a5b2012017-06-22 12:24:12 -070081 return false;
82 }
83 filters.AddFilter(AbiFilter::FromAbiList(group->second));
84 }
85
86 if (artifact.screen_density_group) {
87 const std::string& group_name = artifact.screen_density_group.value();
88
89 auto group = config.screen_density_groups.find(group_name);
90 // TODO: Remove validation when configuration parser ensures referential integrity.
91 if (group == config.screen_density_groups.end()) {
Shane Farmer9ecc0752017-08-24 15:55:36 -070092 diag->Error(DiagMessage() << "could not find referenced group '" << group_name << "'");
Shane Farmer0a5b2012017-06-22 12:24:12 -070093 return false;
94 }
95
96 const std::vector<ConfigDescription>& densities = group->second;
97 std::for_each(densities.begin(), densities.end(), [&](const ConfigDescription& c) {
98 splits.preferred_densities.push_back(c.density);
99 });
100 }
101
102 if (artifact.locale_group) {
103 const std::string& group_name = artifact.locale_group.value();
104 auto group = config.locale_groups.find(group_name);
105 // TODO: Remove validation when configuration parser ensures referential integrity.
106 if (group == config.locale_groups.end()) {
Shane Farmer9ecc0752017-08-24 15:55:36 -0700107 diag->Error(DiagMessage() << "could not find referenced group '" << group_name << "'");
Shane Farmer0a5b2012017-06-22 12:24:12 -0700108 return false;
109 }
110
111 const std::vector<ConfigDescription>& locales = group->second;
112 std::for_each(locales.begin(), locales.end(),
113 [&](const ConfigDescription& c) { axis_filter.AddConfig(c); });
114 splits.config_filter = &axis_filter;
115 }
116
117 std::unique_ptr<ResourceTable> table = apk_->GetResourceTable()->Clone();
118
Shane Farmer3d66afe2017-08-24 11:04:35 -0700119
Shane Farmer0a5b2012017-06-22 12:24:12 -0700120 TableSplitter splitter{{}, splits};
121 splitter.SplitTable(table.get());
122
123 std::string out = out_dir;
Shane Farmer3d66afe2017-08-24 11:04:35 -0700124 if (!file::mkdirs(out)) {
125 context_->GetDiagnostics()->Warn(DiagMessage() << "could not create out dir: " << out);
126 }
Shane Farmer0a5b2012017-06-22 12:24:12 -0700127 file::AppendPath(&out, artifact_name.value());
128
Shane Farmer9ecc0752017-08-24 15:55:36 -0700129 std::unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(diag, out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700130
131 if (context_->IsVerbose()) {
Shane Farmer9ecc0752017-08-24 15:55:36 -0700132 diag->Note(DiagMessage() << "Writing output: " << out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700133 }
134
135 if (!apk_->WriteToArchive(context_, table.get(), table_flattener_options, &filters,
136 writer.get())) {
137 return false;
138 }
139 }
140
141 return true;
142}
143
144} // namespace aapt