blob: 563c46df26d1f91af22c097ff0c2398ed5a031d6 [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 Farmer0a5b2012017-06-22 12:24:12 -070026#include "configuration/ConfigurationParser.h"
27#include "filter/AbiFilter.h"
28#include "filter/Filter.h"
29#include "flatten/Archive.h"
Shane Farmer3edd4722017-09-01 14:34:22 -070030#include "flatten/XmlFlattener.h"
Shane Farmerefe45392017-08-21 14:39:28 -070031#include "optimize/VersionCollapser.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070032#include "process/IResourceTableConsumer.h"
33#include "split/TableSplitter.h"
34#include "util/Files.h"
Shane Farmer3edd4722017-09-01 14:34:22 -070035#include "xml/XmlDom.h"
Shane Farmer0a5b2012017-06-22 12:24:12 -070036
37namespace aapt {
38
Shane Farmerefe45392017-08-21 14:39:28 -070039using ::aapt::configuration::AndroidSdk;
Shane Farmer0a5b2012017-06-22 12:24:12 -070040using ::aapt::configuration::Artifact;
41using ::aapt::configuration::PostProcessingConfiguration;
Shane Farmer3edd4722017-09-01 14:34:22 -070042using ::aapt::xml::XmlResource;
Shane Farmer0a5b2012017-06-22 12:24:12 -070043using ::android::StringPiece;
44
Shane Farmer3edd4722017-09-01 14:34:22 -070045namespace {
46
47Maybe<AndroidSdk> GetAndroidSdk(const Artifact& artifact, const PostProcessingConfiguration& config,
48 IDiagnostics* diag) {
49 if (!artifact.android_sdk_group) {
50 return {};
51 }
52
53 const std::string& group_name = artifact.android_sdk_group.value();
54 auto group = config.android_sdk_groups.find(group_name);
55 // TODO: Remove validation when configuration parser ensures referential integrity.
56 if (group == config.android_sdk_groups.end()) {
57 diag->Error(DiagMessage() << "could not find referenced group '" << group_name << "'");
58 return {};
59 }
60
61 return group->second;
62}
63
64} // namespace
65
Shane Farmerefe45392017-08-21 14:39:28 -070066/**
67 * Context wrapper that allows the min Android SDK value to be overridden.
68 */
69class ContextWrapper : public IAaptContext {
70 public:
71 explicit ContextWrapper(IAaptContext* context)
72 : context_(context), min_sdk_(context_->GetMinSdkVersion()) {
73 }
74
75 PackageType GetPackageType() override {
76 return context_->GetPackageType();
77 }
78
79 SymbolTable* GetExternalSymbols() override {
80 return context_->GetExternalSymbols();
81 }
82
83 IDiagnostics* GetDiagnostics() override {
84 return context_->GetDiagnostics();
85 }
86
87 const std::string& GetCompilationPackage() override {
88 return context_->GetCompilationPackage();
89 }
90
91 uint8_t GetPackageId() override {
92 return context_->GetPackageId();
93 }
94
95 NameMangler* GetNameMangler() override {
96 return context_->GetNameMangler();
97 }
98
99 bool IsVerbose() override {
100 return context_->IsVerbose();
101 }
102
103 int GetMinSdkVersion() override {
104 return min_sdk_;
105 }
106
107 void SetMinSdkVersion(int min_sdk) {
108 min_sdk_ = min_sdk;
109 }
110
111 private:
112 IAaptContext* context_;
113
114 int min_sdk_ = -1;
115};
116
Shane Farmer0a5b2012017-06-22 12:24:12 -0700117MultiApkGenerator::MultiApkGenerator(LoadedApk* apk, IAaptContext* context)
118 : apk_(apk), context_(context) {
119}
120
Shane Farmerefe45392017-08-21 14:39:28 -0700121bool MultiApkGenerator::FromBaseApk(const MultiApkGeneratorOptions& options) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700122 // TODO(safarmer): Handle APK version codes for the generated APKs.
Shane Farmer9ecc0752017-08-24 15:55:36 -0700123 IDiagnostics* diag = context_->GetDiagnostics();
Shane Farmerefe45392017-08-21 14:39:28 -0700124 const PostProcessingConfiguration& config = options.config;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700125
Shane Farmer9ecc0752017-08-24 15:55:36 -0700126 const std::string& apk_name = file::GetFilename(apk_->GetSource().path).to_string();
127 const StringPiece ext = file::GetExtension(apk_name);
128 const std::string base_name = apk_name.substr(0, apk_name.rfind(ext.to_string()));
Shane Farmer0a5b2012017-06-22 12:24:12 -0700129
130 // For now, just write out the stripped APK since ABI splitting doesn't modify anything else.
131 for (const Artifact& artifact : config.artifacts) {
132 FilterChain filters;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700133
134 if (!artifact.name && !config.artifact_format) {
Shane Farmer9ecc0752017-08-24 15:55:36 -0700135 diag->Error(
Shane Farmer0a5b2012017-06-22 12:24:12 -0700136 DiagMessage() << "Artifact does not have a name and no global name template defined");
137 return false;
138 }
139
140 Maybe<std::string> artifact_name =
Shane Farmer9ecc0752017-08-24 15:55:36 -0700141 (artifact.name) ? artifact.Name(apk_name, diag)
142 : artifact.ToArtifactName(config.artifact_format.value(), apk_name, diag);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700143
144 if (!artifact_name) {
Shane Farmer9ecc0752017-08-24 15:55:36 -0700145 diag->Error(DiagMessage() << "Could not determine split APK artifact name");
Shane Farmer0a5b2012017-06-22 12:24:12 -0700146 return false;
147 }
148
Shane Farmerefe45392017-08-21 14:39:28 -0700149 std::unique_ptr<ResourceTable> table =
150 FilterTable(artifact, config, *apk_->GetResourceTable(), &filters);
151 if (!table) {
152 return false;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700153 }
154
Shane Farmer3edd4722017-09-01 14:34:22 -0700155 std::unique_ptr<XmlResource> manifest;
156
157 Maybe<AndroidSdk> maybe_sdk = GetAndroidSdk(artifact, config, diag);
158 if (maybe_sdk) {
159 // TODO(safarmer): Handle the rest of the Android SDK.
160 const AndroidSdk& android_sdk = maybe_sdk.value();
161
162 manifest = apk_->InflateManifest(context_);
163 if (!manifest) {
164 return false;
165 }
166
167 // Make sure the first element is <manifest> with package attribute.
168 xml::Element* manifest_el = manifest->root.get();
169 if (manifest_el == nullptr) {
170 return {};
171 }
172
173 if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
174 diag->Error(DiagMessage(manifest->file.source) << "root tag must be <manifest>");
175 return {};
176 }
177
178 if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
179 if (xml::Attribute* min_sdk_attr =
180 uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
181 if (min_sdk_attr == nullptr) {
182 diag->Error(DiagMessage(manifest->file.source.WithLine(uses_sdk_el->line_number))
183 << "missing android:minSdkVersion");
184 return {};
185 }
186 const std::string& min_sdk_str = std::to_string(android_sdk.min_sdk_version.value());
187 min_sdk_attr->compiled_value = ResourceUtils::TryParseInt(min_sdk_str);
188 }
189 }
190 }
191
Shane Farmerefe45392017-08-21 14:39:28 -0700192 std::string out = options.out_dir;
Shane Farmer3d66afe2017-08-24 11:04:35 -0700193 if (!file::mkdirs(out)) {
194 context_->GetDiagnostics()->Warn(DiagMessage() << "could not create out dir: " << out);
195 }
Shane Farmer0a5b2012017-06-22 12:24:12 -0700196 file::AppendPath(&out, artifact_name.value());
197
Shane Farmerefe45392017-08-21 14:39:28 -0700198 if (context_->IsVerbose()) {
199 context_->GetDiagnostics()->Note(DiagMessage() << "Generating split: " << out);
200 }
201
202 std::unique_ptr<IArchiveWriter> writer =
203 CreateZipFileArchiveWriter(context_->GetDiagnostics(), out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700204
205 if (context_->IsVerbose()) {
Shane Farmer9ecc0752017-08-24 15:55:36 -0700206 diag->Note(DiagMessage() << "Writing output: " << out);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700207 }
208
Shane Farmerefe45392017-08-21 14:39:28 -0700209 if (!apk_->WriteToArchive(context_, table.get(), options.table_flattener_options, &filters,
Shane Farmer3edd4722017-09-01 14:34:22 -0700210 writer.get(), manifest.get())) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700211 return false;
212 }
213 }
214
215 return true;
216}
217
Shane Farmerefe45392017-08-21 14:39:28 -0700218std::unique_ptr<ResourceTable> MultiApkGenerator::FilterTable(
219 const configuration::Artifact& artifact,
220 const configuration::PostProcessingConfiguration& config, const ResourceTable& old_table,
221 FilterChain* filters) {
222 TableSplitterOptions splits;
223 AxisConfigFilter axis_filter;
224 ContextWrapper wrappedContext{context_};
225
226 if (artifact.abi_group) {
227 const std::string& group_name = artifact.abi_group.value();
228
229 auto group = config.abi_groups.find(group_name);
230 // TODO: Remove validation when configuration parser ensures referential integrity.
231 if (group == config.abi_groups.end()) {
232 context_->GetDiagnostics()->Error(DiagMessage() << "could not find referenced ABI group '"
233 << group_name << "'");
234 return {};
235 }
236 filters->AddFilter(AbiFilter::FromAbiList(group->second));
237 }
238
239 if (artifact.screen_density_group) {
240 const std::string& group_name = artifact.screen_density_group.value();
241
242 auto group = config.screen_density_groups.find(group_name);
243 // TODO: Remove validation when configuration parser ensures referential integrity.
244 if (group == config.screen_density_groups.end()) {
245 context_->GetDiagnostics()->Error(DiagMessage() << "could not find referenced group '"
246 << group_name << "'");
247 return {};
248 }
249
250 const std::vector<ConfigDescription>& densities = group->second;
251 for(const auto& density_config : densities) {
252 splits.preferred_densities.push_back(density_config.density);
253 }
254 }
255
256 if (artifact.locale_group) {
257 const std::string& group_name = artifact.locale_group.value();
258 auto group = config.locale_groups.find(group_name);
259 // TODO: Remove validation when configuration parser ensures referential integrity.
260 if (group == config.locale_groups.end()) {
261 context_->GetDiagnostics()->Error(DiagMessage() << "could not find referenced group '"
262 << group_name << "'");
263 return {};
264 }
265
266 const std::vector<ConfigDescription>& locales = group->second;
267 for (const auto& locale : locales) {
268 axis_filter.AddConfig(locale);
269 }
270 splits.config_filter = &axis_filter;
271 }
272
Shane Farmer3edd4722017-09-01 14:34:22 -0700273 Maybe<AndroidSdk> sdk = GetAndroidSdk(artifact, config, context_->GetDiagnostics());
274 if (sdk && sdk.value().min_sdk_version) {
275 wrappedContext.SetMinSdkVersion(sdk.value().min_sdk_version.value());
Shane Farmerefe45392017-08-21 14:39:28 -0700276 }
277
278 std::unique_ptr<ResourceTable> table = old_table.Clone();
279
280 VersionCollapser collapser;
Shane Farmer3edd4722017-09-01 14:34:22 -0700281 if (!collapser.Consume(&wrappedContext, table.get())) {
Shane Farmerefe45392017-08-21 14:39:28 -0700282 context_->GetDiagnostics()->Error(DiagMessage() << "Failed to strip versioned resources");
283 return {};
284 }
285
286 TableSplitter splitter{{}, splits};
287 splitter.SplitTable(table.get());
288 return table;
289}
290
Shane Farmer0a5b2012017-06-22 12:24:12 -0700291} // namespace aapt