Merge "Writes the resource table proto out when writing a LoadedApk." into pi-dev

This commit is contained in:
Cory Smith
2018-03-18 22:59:27 +00:00
committed by Android (Google) Code Review
3 changed files with 27 additions and 12 deletions

View File

@@ -22,6 +22,7 @@
#include "format/binary/TableFlattener.h" #include "format/binary/TableFlattener.h"
#include "format/binary/XmlFlattener.h" #include "format/binary/XmlFlattener.h"
#include "format/proto/ProtoDeserialize.h" #include "format/proto/ProtoDeserialize.h"
#include "format/proto/ProtoSerialize.h"
#include "io/BigBufferStream.h" #include "io/BigBufferStream.h"
#include "io/Util.h" #include "io/Util.h"
#include "xml/XmlDom.h" #include "xml/XmlDom.h"
@@ -110,7 +111,7 @@ std::unique_ptr<LoadedApk> LoadedApk::LoadProtoApkFromFileCollection(
return {}; return {};
} }
return util::make_unique<LoadedApk>(source, std::move(collection), std::move(table), return util::make_unique<LoadedApk>(source, std::move(collection), std::move(table),
std::move(manifest)); std::move(manifest), ApkFormat::kProto);
} }
std::unique_ptr<LoadedApk> LoadedApk::LoadBinaryApkFromFileCollection( std::unique_ptr<LoadedApk> LoadedApk::LoadBinaryApkFromFileCollection(
@@ -153,7 +154,7 @@ std::unique_ptr<LoadedApk> LoadedApk::LoadBinaryApkFromFileCollection(
return {}; return {};
} }
return util::make_unique<LoadedApk>(source, std::move(collection), std::move(table), return util::make_unique<LoadedApk>(source, std::move(collection), std::move(table),
std::move(manifest)); std::move(manifest), ApkFormat::kBinary);
} }
bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options, bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
@@ -205,7 +206,7 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table
} }
// The resource table needs to be re-serialized since it might have changed. // The resource table needs to be re-serialized since it might have changed.
if (path == "resources.arsc") { if (format_ == ApkFormat::kBinary && path == kApkResourceTablePath) {
BigBuffer buffer(4096); BigBuffer buffer(4096);
// TODO(adamlesinski): How to determine if there were sparse entries (and if to encode // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode
// with sparse entries) b/35389232. // with sparse entries) b/35389232.
@@ -215,11 +216,22 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table
} }
io::BigBufferInputStream input_stream(&buffer); io::BigBufferInputStream input_stream(&buffer);
if (!io::CopyInputStreamToArchive(context, &input_stream, path, ArchiveEntry::kAlign, if (!io::CopyInputStreamToArchive(context,
&input_stream,
path,
ArchiveEntry::kAlign,
writer)) { writer)) {
return false; return false;
} }
} else if (format_ == ApkFormat::kProto && path == kProtoResourceTablePath) {
pb::ResourceTable pb_table;
SerializeTableToPb(*split_table, &pb_table);
if (!io::CopyProtoToArchive(context,
&pb_table,
path,
ArchiveEntry::kAlign, writer)) {
return false;
}
} else if (manifest != nullptr && path == "AndroidManifest.xml") { } else if (manifest != nullptr && path == "AndroidManifest.xml") {
BigBuffer buffer(8192); BigBuffer buffer(8192);
XmlFlattenerOptions xml_flattener_options; XmlFlattenerOptions xml_flattener_options;
@@ -246,9 +258,9 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table
} }
ApkFormat LoadedApk::DetermineApkFormat(io::IFileCollection* apk) { ApkFormat LoadedApk::DetermineApkFormat(io::IFileCollection* apk) {
if (apk->FindFile("resources.arsc") != nullptr) { if (apk->FindFile(kApkResourceTablePath) != nullptr) {
return ApkFormat::kBinary; return ApkFormat::kBinary;
} else if (apk->FindFile("resources.pb") != nullptr) { } else if (apk->FindFile(kProtoResourceTablePath) != nullptr) {
return ApkFormat::kProto; return ApkFormat::kProto;
} else { } else {
// If the resource table is not present, attempt to read the manifest. // If the resource table is not present, attempt to read the manifest.

View File

@@ -57,11 +57,13 @@ class LoadedApk {
const Source& source, std::unique_ptr<io::IFileCollection> collection, IDiagnostics* diag); const Source& source, std::unique_ptr<io::IFileCollection> collection, IDiagnostics* diag);
LoadedApk(const Source& source, std::unique_ptr<io::IFileCollection> apk, LoadedApk(const Source& source, std::unique_ptr<io::IFileCollection> apk,
std::unique_ptr<ResourceTable> table, std::unique_ptr<xml::XmlResource> manifest) std::unique_ptr<ResourceTable> table, std::unique_ptr<xml::XmlResource> manifest,
const ApkFormat& format)
: source_(source), : source_(source),
apk_(std::move(apk)), apk_(std::move(apk)),
table_(std::move(table)), table_(std::move(table)),
manifest_(std::move(manifest)) { manifest_(std::move(manifest)),
format_(format) {
} }
io::IFileCollection* GetFileCollection() { io::IFileCollection* GetFileCollection() {
@@ -112,6 +114,7 @@ class LoadedApk {
std::unique_ptr<io::IFileCollection> apk_; std::unique_ptr<io::IFileCollection> apk_;
std::unique_ptr<ResourceTable> table_; std::unique_ptr<ResourceTable> table_;
std::unique_ptr<xml::XmlResource> manifest_; std::unique_ptr<xml::XmlResource> manifest_;
ApkFormat format_;
static ApkFormat DetermineApkFormat(io::IFileCollection* apk); static ApkFormat DetermineApkFormat(io::IFileCollection* apk);
}; };

View File

@@ -104,7 +104,7 @@ class MultiApkGeneratorTest : public ::testing::Test {
TEST_F(MultiApkGeneratorTest, VersionFilterNewerVersion) { TEST_F(MultiApkGeneratorTest, VersionFilterNewerVersion) {
std::unique_ptr<ResourceTable> table = BuildTable(); std::unique_ptr<ResourceTable> table = BuildTable();
LoadedApk apk = {{"test.apk"}, {}, std::move(table), {}}; LoadedApk apk = {{"test.apk"}, {}, std::move(table), {}, kBinary};
std::unique_ptr<IAaptContext> ctx = test::ContextBuilder().SetMinSdkVersion(19).Build(); std::unique_ptr<IAaptContext> ctx = test::ContextBuilder().SetMinSdkVersion(19).Build();
FilterChain chain; FilterChain chain;
@@ -131,7 +131,7 @@ TEST_F(MultiApkGeneratorTest, VersionFilterNewerVersion) {
TEST_F(MultiApkGeneratorTest, VersionFilterOlderVersion) { TEST_F(MultiApkGeneratorTest, VersionFilterOlderVersion) {
std::unique_ptr<ResourceTable> table = BuildTable(); std::unique_ptr<ResourceTable> table = BuildTable();
LoadedApk apk = {{"test.apk"}, {}, std::move(table), {}}; LoadedApk apk = {{"test.apk"}, {}, std::move(table), {}, kBinary};
std::unique_ptr<IAaptContext> ctx = test::ContextBuilder().SetMinSdkVersion(1).Build(); std::unique_ptr<IAaptContext> ctx = test::ContextBuilder().SetMinSdkVersion(1).Build();
FilterChain chain; FilterChain chain;
@@ -156,7 +156,7 @@ TEST_F(MultiApkGeneratorTest, VersionFilterOlderVersion) {
TEST_F(MultiApkGeneratorTest, VersionFilterNoVersion) { TEST_F(MultiApkGeneratorTest, VersionFilterNoVersion) {
std::unique_ptr<ResourceTable> table = BuildTable(); std::unique_ptr<ResourceTable> table = BuildTable();
LoadedApk apk = {{"test.apk"}, {}, std::move(table), {}}; LoadedApk apk = {{"test.apk"}, {}, std::move(table), {}, kBinary};
std::unique_ptr<IAaptContext> ctx = test::ContextBuilder().SetMinSdkVersion(1).Build(); std::unique_ptr<IAaptContext> ctx = test::ContextBuilder().SetMinSdkVersion(1).Build();
FilterChain chain; FilterChain chain;