1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MultiApkGenerator.h"
#include <algorithm>
#include <regex>
#include <string>
#include "androidfw/ConfigDescription.h"
#include "androidfw/StringPiece.h"
#include "LoadedApk.h"
#include "ResourceUtils.h"
#include "ValueVisitor.h"
#include "configuration/ConfigurationParser.h"
#include "cmd/Util.h"
#include "filter/AbiFilter.h"
#include "filter/Filter.h"
#include "format/Archive.h"
#include "format/binary/XmlFlattener.h"
#include "optimize/VersionCollapser.h"
#include "process/IResourceTableConsumer.h"
#include "split/TableSplitter.h"
#include "util/Files.h"
#include "xml/XmlDom.h"
#include "xml/XmlUtil.h"
namespace aapt {
using ::aapt::configuration::AndroidSdk;
using ::aapt::configuration::OutputArtifact;
using ::aapt::xml::kSchemaAndroid;
using ::aapt::xml::XmlResource;
using ::android::ConfigDescription;
using ::android::StringPiece;
/**
* Context wrapper that allows the min Android SDK value to be overridden.
*/
class ContextWrapper : public IAaptContext {
public:
explicit ContextWrapper(IAaptContext* context)
: context_(context), min_sdk_(context_->GetMinSdkVersion()) {
}
PackageType GetPackageType() override {
return context_->GetPackageType();
}
SymbolTable* GetExternalSymbols() override {
return context_->GetExternalSymbols();
}
IDiagnostics* GetDiagnostics() override {
if (source_diag_) {
return source_diag_.get();
}
return context_->GetDiagnostics();
}
const std::string& GetCompilationPackage() override {
return context_->GetCompilationPackage();
}
uint8_t GetPackageId() override {
return context_->GetPackageId();
}
NameMangler* GetNameMangler() override {
return context_->GetNameMangler();
}
bool IsVerbose() override {
return context_->IsVerbose();
}
int GetMinSdkVersion() override {
return min_sdk_;
}
void SetMinSdkVersion(int min_sdk) {
min_sdk_ = min_sdk;
}
void SetSource(const std::string& source) {
source_diag_ =
util::make_unique<SourcePathDiagnostics>(Source{source}, context_->GetDiagnostics());
}
private:
IAaptContext* context_;
std::unique_ptr<SourcePathDiagnostics> source_diag_;
int min_sdk_ = -1;
};
class SignatureFilter : public IPathFilter {
bool Keep(const std::string& path) override {
static std::regex signature_regex(R"regex(^META-INF/.*\.(RSA|DSA|EC|SF)$)regex");
if (std::regex_search(path, signature_regex)) {
return false;
}
return !(path == "META-INF/MANIFEST.MF");
}
};
MultiApkGenerator::MultiApkGenerator(LoadedApk* apk, IAaptContext* context)
: apk_(apk), context_(context) {
}
bool MultiApkGenerator::FromBaseApk(const MultiApkGeneratorOptions& options) {
std::unordered_set<std::string> artifacts_to_keep = options.kept_artifacts;
std::unordered_set<std::string> filtered_artifacts;
std::unordered_set<std::string> kept_artifacts;
// For now, just write out the stripped APK since ABI splitting doesn't modify anything else.
for (const OutputArtifact& artifact : options.apk_artifacts) {
FilterChain filters;
ContextWrapper wrapped_context{context_};
wrapped_context.SetSource(artifact.name);
if (!options.kept_artifacts.empty()) {
const auto& it = artifacts_to_keep.find(artifact.name);
if (it == artifacts_to_keep.end()) {
filtered_artifacts.insert(artifact.name);
if (context_->IsVerbose()) {
context_->GetDiagnostics()->Note(DiagMessage(artifact.name) << "skipping artifact");
}
continue;
} else {
artifacts_to_keep.erase(it);
kept_artifacts.insert(artifact.name);
}
}
std::unique_ptr<ResourceTable> table =
FilterTable(context_, artifact, *apk_->GetResourceTable(), &filters);
if (!table) {
return false;
}
IDiagnostics* diag = wrapped_context.GetDiagnostics();
std::unique_ptr<XmlResource> manifest;
if (!UpdateManifest(artifact, &manifest, diag)) {
diag->Error(DiagMessage() << "could not update AndroidManifest.xml for output artifact");
return false;
}
std::string out = options.out_dir;
if (!file::mkdirs(out)) {
diag->Warn(DiagMessage() << "could not create out dir: " << out);
}
file::AppendPath(&out, artifact.name);
if (context_->IsVerbose()) {
diag->Note(DiagMessage() << "Generating split: " << out);
}
std::unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(diag, out);
if (context_->IsVerbose()) {
diag->Note(DiagMessage() << "Writing output: " << out);
}
filters.AddFilter(util::make_unique<SignatureFilter>());
if (!apk_->WriteToArchive(&wrapped_context, table.get(), options.table_flattener_options,
&filters, writer.get(), manifest.get())) {
return false;
}
}
// Make sure all of the requested artifacts were valid. If there are any kept artifacts left,
// either the config or the command line was wrong.
if (!artifacts_to_keep.empty()) {
context_->GetDiagnostics()->Error(
DiagMessage() << "The configuration and command line to filter artifacts do not match");
context_->GetDiagnostics()->Error(DiagMessage() << kept_artifacts.size() << " kept:");
for (const auto& artifact : kept_artifacts) {
context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact);
}
context_->GetDiagnostics()->Error(DiagMessage() << filtered_artifacts.size() << " filtered:");
for (const auto& artifact : filtered_artifacts) {
context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact);
}
context_->GetDiagnostics()->Error(DiagMessage() << artifacts_to_keep.size() << " missing:");
for (const auto& artifact : artifacts_to_keep) {
context_->GetDiagnostics()->Error(DiagMessage() << " " << artifact);
}
return false;
}
return true;
}
std::unique_ptr<ResourceTable> MultiApkGenerator::FilterTable(IAaptContext* context,
const OutputArtifact& artifact,
const ResourceTable& old_table,
FilterChain* filters) {
TableSplitterOptions splits;
AxisConfigFilter axis_filter;
ContextWrapper wrapped_context{context};
wrapped_context.SetSource(artifact.name);
if (!artifact.abis.empty()) {
filters->AddFilter(AbiFilter::FromAbiList(artifact.abis));
}
if (!artifact.screen_densities.empty()) {
for (const auto& density_config : artifact.screen_densities) {
splits.preferred_densities.push_back(density_config.density);
}
}
if (!artifact.locales.empty()) {
for (const auto& locale : artifact.locales) {
axis_filter.AddConfig(locale);
}
splits.config_filter = &axis_filter;
}
if (artifact.android_sdk) {
wrapped_context.SetMinSdkVersion(artifact.android_sdk.value().min_sdk_version);
}
std::unique_ptr<ResourceTable> table = old_table.Clone();
VersionCollapser collapser;
if (!collapser.Consume(&wrapped_context, table.get())) {
context->GetDiagnostics()->Error(DiagMessage() << "Failed to strip versioned resources");
return {};
}
TableSplitter splitter{{}, splits};
splitter.SplitTable(table.get());
return table;
}
bool MultiApkGenerator::UpdateManifest(const OutputArtifact& artifact,
std::unique_ptr<XmlResource>* updated_manifest,
IDiagnostics* diag) {
const xml::XmlResource* apk_manifest = apk_->GetManifest();
if (apk_manifest == nullptr) {
return false;
}
*updated_manifest = apk_manifest->Clone();
XmlResource* manifest = updated_manifest->get();
// Make sure the first element is <manifest> with package attribute.
xml::Element* manifest_el = manifest->root.get();
if (!manifest_el) {
return false;
}
if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
diag->Error(DiagMessage(manifest->file.source) << "root tag must be <manifest>");
return false;
}
// Retrieve the versionCode attribute.
auto version_code = manifest_el->FindAttribute(kSchemaAndroid, "versionCode");
if (!version_code) {
diag->Error(DiagMessage(manifest->file.source) << "manifest must have a versionCode attribute");
return false;
}
auto version_code_value = ValueCast<BinaryPrimitive>(version_code->compiled_value.get());
if (!version_code_value) {
diag->Error(DiagMessage(manifest->file.source) << "versionCode is invalid");
return false;
}
// Retrieve the versionCodeMajor attribute.
auto version_code_major = manifest_el->FindAttribute(kSchemaAndroid, "versionCodeMajor");
BinaryPrimitive* version_code_major_value = nullptr;
if (version_code_major) {
version_code_major_value = ValueCast<BinaryPrimitive>(version_code_major->compiled_value.get());
if (!version_code_major_value) {
diag->Error(DiagMessage(manifest->file.source) << "versionCodeMajor is invalid");
return false;
}
}
// Calculate and set the updated version code
uint64_t major = (version_code_major_value)
? ((uint64_t) version_code_major_value->value.data) << 32 : 0;
uint64_t new_version = (major | version_code_value->value.data) + artifact.version;
SetLongVersionCode(manifest_el, new_version);
// Check to see if the minSdkVersion needs to be updated.
if (artifact.android_sdk) {
// TODO(safarmer): Handle the rest of the Android SDK.
const AndroidSdk& android_sdk = artifact.android_sdk.value();
if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
if (xml::Attribute* min_sdk_attr =
uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
// Populate with a pre-compiles attribute to we don't need to relink etc.
const std::string& min_sdk_str = std::to_string(android_sdk.min_sdk_version);
min_sdk_attr->compiled_value = ResourceUtils::TryParseInt(min_sdk_str);
} else {
// There was no minSdkVersion. This is strange since at this point we should have been
// through the manifest fixer which sets the default minSdkVersion.
diag->Error(DiagMessage(manifest->file.source) << "missing minSdkVersion from <uses-sdk>");
return false;
}
} else {
// No uses-sdk present. This is strange since at this point we should have been
// through the manifest fixer which should have added it.
diag->Error(DiagMessage(manifest->file.source) << "missing <uses-sdk> from <manifest>");
return false;
}
}
if (!artifact.screen_densities.empty()) {
xml::Element* screens_el = manifest_el->FindChild({}, "compatible-screens");
if (!screens_el) {
// create a new element.
std::unique_ptr<xml::Element> new_screens_el = util::make_unique<xml::Element>();
new_screens_el->name = "compatible-screens";
screens_el = new_screens_el.get();
manifest_el->AppendChild(std::move(new_screens_el));
} else {
// clear out the old element.
screens_el->GetChildElements().clear();
}
for (const auto& density : artifact.screen_densities) {
AddScreens(density, screens_el);
}
}
return true;
}
/**
* Adds a screen element with both screenSize and screenDensity set. Since we only know the density
* we add it for all screen sizes.
*
* This requires the resource IDs for the attributes from the framework library. Since these IDs are
* a part of the public API (and in public.xml) we hard code the values.
*
* The excert from the framework is as follows:
* <public type="attr" name="screenSize" id="0x010102ca" />
* <public type="attr" name="screenDensity" id="0x010102cb" />
*/
void MultiApkGenerator::AddScreens(const ConfigDescription& config, xml::Element* parent) {
// Hard coded integer representation of the supported screen sizes:
// small = 200
// normal = 300
// large = 400
// xlarge = 500
constexpr const uint32_t kScreenSizes[4] = {200, 300, 400, 500,};
constexpr const uint32_t kScreenSizeResourceId = 0x010102ca;
constexpr const uint32_t kScreenDensityResourceId = 0x010102cb;
for (uint32_t screen_size : kScreenSizes) {
std::unique_ptr<xml::Element> screen = util::make_unique<xml::Element>();
screen->name = "screen";
xml::Attribute* size = screen->FindOrCreateAttribute(kSchemaAndroid, "screenSize");
size->compiled_attribute = xml::AaptAttribute(Attribute(), {kScreenSizeResourceId});
size->compiled_value = ResourceUtils::MakeInt(screen_size);
xml::Attribute* density = screen->FindOrCreateAttribute(kSchemaAndroid, "screenDensity");
density->compiled_attribute = xml::AaptAttribute(Attribute(), {kScreenDensityResourceId});
density->compiled_value = ResourceUtils::MakeInt(config.density);
parent->AppendChild(std::move(screen));
}
}
} // namespace aapt
|