blob: 5ff890832371d9eeb6e77993ecd9f6e50f4c4882 [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"
Shane Farmerefe45392017-08-21 14:39:28 -070029#include "optimize/VersionCollapser.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070030#include "process/IResourceTableConsumer.h"
31#include "split/TableSplitter.h"
32#include "util/Files.h"
33
34namespace aapt {
35
Shane Farmerefe45392017-08-21 14:39:28 -070036using ::aapt::configuration::AndroidSdk;
Shane Farmer0a5b2012017-06-22 12:24:12 -070037using ::aapt::configuration::Artifact;
38using ::aapt::configuration::PostProcessingConfiguration;
39using ::android::StringPiece;
40
Shane Farmerefe45392017-08-21 14:39:28 -070041/**
42 * Context wrapper that allows the min Android SDK value to be overridden.
43 */
44class ContextWrapper : public IAaptContext {
45 public:
46 explicit ContextWrapper(IAaptContext* context)
47 : context_(context), min_sdk_(context_->GetMinSdkVersion()) {
48 }
49
50 PackageType GetPackageType() override {
51 return context_->GetPackageType();
52 }
53
54 SymbolTable* GetExternalSymbols() override {
55 return context_->GetExternalSymbols();
56 }
57
58 IDiagnostics* GetDiagnostics() override {
59 return context_->GetDiagnostics();
60 }
61
62 const std::string& GetCompilationPackage() override {
63 return context_->GetCompilationPackage();
64 }
65
66 uint8_t GetPackageId() override {
67 return context_->GetPackageId();
68 }
69
70 NameMangler* GetNameMangler() override {
71 return context_->GetNameMangler();
72 }
73
74 bool IsVerbose() override {
75 return context_->IsVerbose();
76 }
77
78 int GetMinSdkVersion() override {
79 return min_sdk_;
80 }
81
82 void SetMinSdkVersion(int min_sdk) {
83 min_sdk_ = min_sdk;
84 }
85
86 private:
87 IAaptContext* context_;
88
89 int min_sdk_ = -1;
90};
91
Shane Farmer0a5b2012017-06-22 12:24:12 -070092MultiApkGenerator::MultiApkGenerator(LoadedApk* apk, IAaptContext* context)
93 : apk_(apk), context_(context) {
94}
95
Shane Farmerefe45392017-08-21 14:39:28 -070096bool MultiApkGenerator::FromBaseApk(const MultiApkGeneratorOptions& options) {
Shane Farmer0a5b2012017-06-22 12:24:12 -070097 // TODO(safarmer): Handle APK version codes for the generated APKs.
Shane Farmer9ecc0752017-08-24 15:55:36 -070098 IDiagnostics* diag = context_->GetDiagnostics();
Shane Farmerefe45392017-08-21 14:39:28 -070099 const PostProcessingConfiguration& config = options.config;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700100
Shane Farmer9ecc0752017-08-24 15:55:36 -0700101 const std::string& apk_name = file::GetFilename(apk_->GetSource().path).to_string();
102 const StringPiece ext = file::GetExtension(apk_name);
103 const std::string base_name = apk_name.substr(0, apk_name.rfind(ext.to_string()));
Shane Farmer0a5b2012017-06-22 12:24:12 -0700104
105 // For now, just write out the stripped APK since ABI splitting doesn't modify anything else.
106 for (const Artifact& artifact : config.artifacts) {
107 FilterChain filters;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700108
109 if (!artifact.name && !config.artifact_format) {
Shane Farmer9ecc0752017-08-24 15:55:36 -0700110 diag->Error(
Shane Farmer0a5b2012017-06-22 12:24:12 -0700111 DiagMessage() << "Artifact does not have a name and no global name template defined");
112 return false;
113 }
114
115 Maybe<std::string> artifact_name =
Shane Farmer9ecc0752017-08-24 15:55:36 -0700116 (artifact.name) ? artifact.Name(apk_name, diag)
117 : artifact.ToArtifactName(config.artifact_format.value(), apk_name, diag);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700118
119 if (!artifact_name) {
Shane Farmer9ecc0752017-08-24 15:55:36 -0700120 diag->Error(DiagMessage() << "Could not determine split APK artifact name");
Shane Farmer0a5b2012017-06-22 12:24:12 -0700121 return false;
122 }
123
Shane Farmerefe45392017-08-21 14:39:28 -0700124 std::unique_ptr<ResourceTable> table =
125 FilterTable(artifact, config, *apk_->GetResourceTable(), &filters);
126 if (!table) {
127 return false;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700128 }
129
Shane Farmerefe45392017-08-21 14:39:28 -0700130 std::string out = options.out_dir;
Shane Farmer3d66afe2017-08-24 11:04:35 -0700131 if (!file::mkdirs(out)) {
132 context_->GetDiagnostics()->Warn(DiagMessage() << "could not create out dir: " << out);
133 }
Shane Farmer0a5b2012017-06-22 12:24:12 -0700134 file::AppendPath(&out, artifact_name.value());
135
Shane Farmerefe45392017-08-21 14:39:28 -0700136 if (context_->IsVerbose()) {
137 context_->GetDiagnostics()->Note(DiagMessage() << "Generating split: " << out);
138 }
139
140 std::unique_ptr<IArchiveWriter> writer =
141 CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700142
143 if (context_->IsVerbose()) {
Shane Farmer9ecc0752017-08-24 15:55:36 -0700144 diag->Note(DiagMessage() << "Writing output: " << out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700145 }
146
Shane Farmerefe45392017-08-21 14:39:28 -0700147 if (!apk_->WriteToArchive(context_, table.get(), options.table_flattener_options, &filters,
Shane Farmer0a5b2012017-06-22 12:24:12 -0700148 writer.get())) {
149 return false;
150 }
151 }
152
153 return true;
154}
155
Shane Farmerefe45392017-08-21 14:39:28 -0700156std::unique_ptr<ResourceTable> MultiApkGenerator::FilterTable(
157 const configuration::Artifact& artifact,
158 const configuration::PostProcessingConfiguration& config, const ResourceTable& old_table,
159 FilterChain* filters) {
160 TableSplitterOptions splits;
161 AxisConfigFilter axis_filter;
162 ContextWrapper wrappedContext{context_};
163
164 if (artifact.abi_group) {
165 const std::string& group_name = artifact.abi_group.value();
166
167 auto group = config.abi_groups.find(group_name);
168 // TODO: Remove validation when configuration parser ensures referential integrity.
169 if (group == config.abi_groups.end()) {
170 context_->GetDiagnostics()->Error(DiagMessage() << "could not find referenced ABI group '"
171 << group_name << "'");
172 return {};
173 }
174 filters->AddFilter(AbiFilter::FromAbiList(group->second));
175 }
176
177 if (artifact.screen_density_group) {
178 const std::string& group_name = artifact.screen_density_group.value();
179
180 auto group = config.screen_density_groups.find(group_name);
181 // TODO: Remove validation when configuration parser ensures referential integrity.
182 if (group == config.screen_density_groups.end()) {
183 context_->GetDiagnostics()->Error(DiagMessage() << "could not find referenced group '"
184 << group_name << "'");
185 return {};
186 }
187
188 const std::vector<ConfigDescription>& densities = group->second;
189 for(const auto& density_config : densities) {
190 splits.preferred_densities.push_back(density_config.density);
191 }
192 }
193
194 if (artifact.locale_group) {
195 const std::string& group_name = artifact.locale_group.value();
196 auto group = config.locale_groups.find(group_name);
197 // TODO: Remove validation when configuration parser ensures referential integrity.
198 if (group == config.locale_groups.end()) {
199 context_->GetDiagnostics()->Error(DiagMessage() << "could not find referenced group '"
200 << group_name << "'");
201 return {};
202 }
203
204 const std::vector<ConfigDescription>& locales = group->second;
205 for (const auto& locale : locales) {
206 axis_filter.AddConfig(locale);
207 }
208 splits.config_filter = &axis_filter;
209 }
210
211 if (artifact.android_sdk_group) {
212 const std::string& group_name = artifact.android_sdk_group.value();
213 auto group = config.android_sdk_groups.find(group_name);
214 // TODO: Remove validation when configuration parser ensures referential integrity.
215 if (group == config.android_sdk_groups.end()) {
216 context_->GetDiagnostics()->Error(DiagMessage() << "could not find referenced group '"
217 << group_name << "'");
218 return {};
219 }
220
221 const AndroidSdk& sdk = group->second;
222 if (!sdk.min_sdk_version) {
223 context_->GetDiagnostics()->Error(DiagMessage()
224 << "skipping SDK version. No min SDK: " << group_name);
225 return {};
226 }
227
228 ConfigDescription c;
229 const std::string& version = sdk.min_sdk_version.value();
230 if (!ConfigDescription::Parse(version, &c)) {
231 context_->GetDiagnostics()->Error(DiagMessage() << "could not parse min SDK: " << version);
232 return {};
233 }
234
235 wrappedContext.SetMinSdkVersion(c.sdkVersion);
236 }
237
238 std::unique_ptr<ResourceTable> table = old_table.Clone();
239
240 VersionCollapser collapser;
241 if (!collapser.Consume(context_, table.get())) {
242 context_->GetDiagnostics()->Error(DiagMessage() << "Failed to strip versioned resources");
243 return {};
244 }
245
246 TableSplitter splitter{{}, splits};
247 splitter.SplitTable(table.get());
248 return table;
249}
250
Shane Farmer0a5b2012017-06-22 12:24:12 -0700251} // namespace aapt