AAPT2: Move to proto3

We're going to be using the Any proto field in ConfigDescription,
so migrate to proto3.

Test: make aapt2_tests
Change-Id: I43b91f4dfc448251f4352e48586b3b15502c033d
This commit is contained in:
Adam Lesinski
2017-09-07 09:39:07 -07:00
parent 6833a07a07
commit 5bd4423710
5 changed files with 161 additions and 162 deletions

View File

@@ -14,77 +14,86 @@
* limitations under the License.
*/
// Keep proto2 syntax because we require the distinction between fields that
// are set and unset.
syntax = "proto2";
syntax = "proto3";
package aapt.pb;
option java_package = "com.android.aapt";
option optimize_for = LITE_RUNTIME;
package aapt.pb;
// A configuration description that wraps the binary form of the C++ class
// aapt::ConfigDescription, with an added product definition.
// TODO(adamlesinski): Flesh this out to be represented in proto.
message ConfigDescription {
optional bytes data = 1;
optional string product = 2;
bytes data = 1;
string product = 2;
}
// A string pool that wraps the binary form of the C++ class android::ResStringPool.
message StringPool {
optional bytes data = 1;
bytes data = 1;
}
// The position of a declared entity within a file.
message SourcePosition {
optional uint32 line_number = 1;
optional uint32 column_number = 2;
uint32 line_number = 1;
uint32 column_number = 2;
}
// Developer friendly source file information for an entity in the resource table.
message Source {
// The index of the string path within the source string pool of a ResourceTable.
optional uint32 path_idx = 1;
optional SourcePosition position = 2;
uint32 path_idx = 1;
SourcePosition position = 2;
}
// Top level message representing a resource table.
message ResourceTable {
// The string pool containing source paths referenced throughout the resource table. This does
// not end up in the final binary ARSC file.
optional StringPool source_pool = 1;
StringPool source_pool = 1;
// Resource definitions corresponding to an Android package.
repeated Package package = 2;
}
// A package ID in the range [0x00, 0xff].
message PackageId {
uint32 id = 1;
}
// Defines resources for an Android package.
message Package {
// The package ID of this package, in the range [0x00, 0xff].
// The ID 0x00 is reserved for shared libraries, or when the ID is assigned at run-time.
// The ID 0x01 is reserved for the 'android' package (framework).
// The ID range [0x02, 0x7f) is reserved for auto-assignment to shared libraries at run-time.
// The ID 0x7f is reserved for the application package.
// IDs > 0x7f are reserved for the application as well and are treated as feature splits.
optional uint32 package_id = 1;
// - ID 0x00 is reserved for shared libraries, or when the ID is assigned at run-time.
// - ID 0x01 is reserved for the 'android' package (framework).
// - ID range [0x02, 0x7f) is reserved for auto-assignment to shared libraries at run-time.
// - ID 0x7f is reserved for the application package.
// - IDs > 0x7f are reserved for the application as well and are treated as feature splits.
// This may not be set if no ID was assigned.
PackageId package_id = 1;
// The Java compatible Android package name of the app.
optional string package_name = 2;
string package_name = 2;
// The series of types defined by the package.
repeated Type type = 3;
}
// A type ID in the range [0x01, 0xff].
message TypeId {
uint32 id = 1;
}
// A set of resources grouped under a common type. Such types include string, layout, xml, dimen,
// attr, etc. This maps to the second part of a resource identifier in Java (R.type.entry).
message Type {
// The ID of the type. This may be 0, which indicates no ID is set.
optional uint32 id = 1;
// The ID of the type. This may not be set if no ID was assigned.
TypeId type_id = 1;
// The name of the type. This corresponds to the 'type' part of a full resource name of the form
// package:type/entry. The set of legal type names is listed in Resource.cpp.
optional string name = 2;
string name = 2;
// The entries defined for this type.
repeated Entry entry = 3;
@@ -112,17 +121,22 @@ message SymbolStatus {
PUBLIC = 2;
}
optional Visibility visibility = 1;
Visibility visibility = 1;
// The path at which this entry's visibility was defined (eg. public.xml).
optional Source source = 2;
Source source = 2;
// The comment associated with the <public> tag.
optional string comment = 3;
string comment = 3;
// Whether the symbol can be merged into another resource table without there being an existing
// definition to override. Used for overlays and set to true when <add-resource> is specified.
optional bool allow_new = 4;
bool allow_new = 4;
}
// An entry ID in the range [0x0000, 0xffff].
message EntryId {
uint32 id = 1;
}
// An entry declaration. An entry has a full resource ID that is the combination of package ID,
@@ -132,14 +146,15 @@ message Entry {
// The ID of this entry. Together with the package ID and type ID, this forms a full resource ID
// of the form 0xPPTTEEEE, where PP is the package ID, TT is the type ID, and EEEE is the entry
// ID.
optional uint32 id = 1;
// This may not be set if no ID was assigned.
EntryId entry_id = 1;
// The name of this entry. This corresponds to the 'entry' part of a full resource name of the
// form package:type/entry.
optional string name = 2;
string name = 2;
// The symbol status of this entry, which includes visibility information.
optional SymbolStatus symbol_status = 3;
SymbolStatus symbol_status = 3;
// The set of values defined for this entry, each corresponding to a different
// configuration/variant.
@@ -148,50 +163,54 @@ message Entry {
// A Configuration/Value pair.
message ConfigValue {
optional ConfigDescription config = 1;
optional Value value = 2;
ConfigDescription config = 1;
Value value = 2;
}
// The generic meta-data for every value in a resource table.
message Value {
// Where the value was defined.
optional Source source = 1;
Source source = 1;
// Any comment associated with the value.
optional string comment = 2;
string comment = 2;
// Whether the value can be overridden.
optional bool weak = 3;
bool weak = 3;
// If the value is an Item, this is set.
optional Item item = 4;
// If the value is a CompoundValue, this is set.
optional CompoundValue compound_value = 5;
// The value is either an Item or a CompoundValue.
oneof value {
Item item = 4;
CompoundValue compound_value = 5;
}
}
// An Item is an abstract type. It represents a value that can appear inline in many places, such
// as XML attribute values or on the right hand side of style attribute definitions. The concrete
// type is one of the types below. Only one can be set.
message Item {
optional Reference ref = 1;
optional String str = 2;
optional RawString raw_str = 3;
optional StyledString styled_str = 4;
optional FileReference file = 5;
optional Id id = 6;
optional Primitive prim = 7;
oneof value {
Reference ref = 1;
String str = 2;
RawString raw_str = 3;
StyledString styled_str = 4;
FileReference file = 5;
Id id = 6;
Primitive prim = 7;
}
}
// A CompoundValue is an abstract type. It represents a value that is a made of other values.
// These can only usually appear as top-level resources. The concrete type is one of the types
// below. Only one can be set.
message CompoundValue {
optional Attribute attr = 1;
optional Style style = 2;
optional Styleable styleable = 3;
optional Array array = 4;
optional Plural plural = 5;
oneof value {
Attribute attr = 1;
Style style = 2;
Styleable styleable = 3;
Array array = 4;
Plural plural = 5;
}
}
// A value that is a reference to another resource. This reference can be by name or resource ID.
@@ -204,16 +223,16 @@ message Reference {
ATTRIBUTE = 1;
}
optional Type type = 1;
Type type = 1;
// The resource ID (0xPPTTEEEE) of the resource being referred.
optional uint32 id = 2;
// The resource ID (0xPPTTEEEE) of the resource being referred. This is optional.
uint32 id = 2;
// The optional resource name.
optional string name = 3;
// The name of the resource being referred. This is optional if the resource ID is set.
string name = 3;
// Whether this reference is referencing a private resource (@*package:type/entry).
optional bool private = 4;
bool private = 4;
}
// A value that represents an ID. This is just a placeholder, as ID values are used to occupy a
@@ -223,32 +242,32 @@ message Id {
// A value that is a string.
message String {
optional string value = 1;
string value = 1;
}
// A value that is a raw string, which is unescaped/uninterpreted. This is typically used to
// represent the value of a style attribute before the attribute is compiled and the set of
// allowed values is known.
message RawString {
optional string value = 1;
string value = 1;
}
// A string with styling information, like html tags that specify boldness, italics, etc.
message StyledString {
// The raw text of the string.
optional string value = 1;
string value = 1;
// A Span marks a region of the string text that is styled.
message Span {
// The name of the tag, and its attributes, encoded as follows:
// tag_name;attr1=value1;attr2=value2;[...]
optional string tag = 1;
string tag = 1;
// The first character position this span applies to, in UTF-16 offset.
optional uint32 first_char = 2;
uint32 first_char = 2;
// The last character position this span applies to, in UTF-16 offset.
optional uint32 last_char = 3;
uint32 last_char = 3;
}
repeated Span span = 2;
@@ -257,14 +276,14 @@ message StyledString {
// A value that is a reference to an external entity, like an XML file or a PNG.
message FileReference {
// Path to a file within the APK (typically res/type-config/entry.ext).
optional string path = 1;
string path = 1;
}
// A value that represents a primitive data type (float, int, boolean, etc.).
// Corresponds to the fields (type/data) of the C struct android::Res_value.
message Primitive {
optional uint32 type = 1;
optional uint32 data = 2;
uint32 type = 1;
uint32 data = 2;
}
// A value that represents an XML attribute and what values it accepts.
@@ -272,21 +291,22 @@ message Attribute {
// A Symbol used to represent an enum or a flag.
message Symbol {
// Where the enum/flag item was defined.
optional Source source = 1;
Source source = 1;
// Any comments associated with the enum or flag.
optional string comment = 2;
string comment = 2;
// The name of the enum/flag as a reference. Enums/flag items are generated as ID resource
// values.
optional Reference name = 3;
Reference name = 3;
// The value of the enum/flag.
optional uint32 value = 4;
uint32 value = 4;
}
// Bitmask of formats allowed for an attribute.
enum FormatFlags {
NONE = 0x0; // Proto3 requires a default of 0.
ANY = 0x0000ffff; // Allows any type except ENUM and FLAGS.
REFERENCE = 0x01; // Allows Reference values.
STRING = 0x02; // Allows String/StyledString values.
@@ -304,15 +324,15 @@ message Attribute {
// A bitmask of types that this XML attribute accepts. Corresponds to the flags in the
// enum FormatFlags.
optional uint32 format_flags = 1;
uint32 format_flags = 1;
// The smallest integer allowed for this XML attribute. Only makes sense if the format includes
// FormatFlags::INTEGER.
optional int32 min_int = 2;
int32 min_int = 2;
// The largest integer allowed for this XML attribute. Only makes sense if the format includes
// FormatFlags::INTEGER.
optional int32 max_int = 3;
int32 max_int = 3;
// The set of enums/flags defined in this attribute. Only makes sense if the format includes
// either FormatFlags::ENUM or FormatFlags::FLAGS. Having both is an error.
@@ -324,23 +344,23 @@ message Style {
// An XML attribute/value pair defined in the style.
message Entry {
// Where the entry was defined.
optional Source source = 1;
Source source = 1;
// Any comments associated with the entry.
optional string comment = 2;
string comment = 2;
// A reference to the XML attribute.
optional Reference key = 3;
Reference key = 3;
// The Item defined for this XML attribute.
optional Item item = 4;
Item item = 4;
}
// The optinal style from which this style inherits attributes.
optional Reference parent = 1;
Reference parent = 1;
// The source file information of the parent inheritance declaration.
optional Source parent_source = 2;
Source parent_source = 2;
// The set of XML attribute/value pairs for this style.
repeated Entry entry = 3;
@@ -352,13 +372,13 @@ message Styleable {
// An attribute defined for this styleable.
message Entry {
// Where the attribute was defined within the <declare-styleable> block.
optional Source source = 1;
Source source = 1;
// Any comments associated with the declaration.
optional string comment = 2;
string comment = 2;
// The reference to the attribute.
optional Reference attr = 3;
Reference attr = 3;
}
// The set of attribute declarations.
@@ -370,13 +390,13 @@ message Array {
// A single element of the array.
message Element {
// Where the element was defined.
optional Source source = 1;
Source source = 1;
// Any comments associated with the element.
optional string comment = 2;
string comment = 2;
// The value assigned to this element.
optional Item item = 3;
Item item = 3;
}
// The list of array elements.
@@ -398,16 +418,16 @@ message Plural {
// The plural value for a given arity.
message Entry {
// Where the plural was defined.
optional Source source = 1;
Source source = 1;
// Any comments associated with the plural.
optional string comment = 2;
string comment = 2;
// The arity of the plural.
optional Arity arity = 3;
Arity arity = 3;
// The value assigned to this plural.
optional Item item = 4;
Item item = 4;
}
// The set of arity/plural mappings.
@@ -417,14 +437,13 @@ message Plural {
// Defines an abstract XmlNode that must be either an XmlElement, or
// a text node represented by a string.
message XmlNode {
// If set, this node is an element/tag.
optional XmlElement element = 1;
// If set, this node is a chunk of text.
optional string text = 2;
oneof node {
XmlElement element = 1;
string text = 2;
}
// Source line and column info.
optional SourcePosition source = 3;
SourcePosition source = 3;
}
// An <element> in an XML document.
@@ -433,10 +452,10 @@ message XmlElement {
repeated XmlNamespace namespace_declaration = 1;
// The namespace URI of this element.
optional string namespace_uri = 2;
string namespace_uri = 2;
// The name of this element.
optional string name = 3;
string name = 3;
// The attributes of this element.
repeated XmlAttribute attribute = 4;
@@ -447,25 +466,25 @@ message XmlElement {
// A namespace declaration on an XmlElement (xmlns:android="http://...").
message XmlNamespace {
optional string prefix = 1;
optional string uri = 2;
string prefix = 1;
string uri = 2;
// Source line and column info.
optional SourcePosition source = 3;
SourcePosition source = 3;
}
// An attribute defined on an XmlElement (android:text="...").
message XmlAttribute {
optional string namespace_uri = 1;
optional string name = 2;
optional string value = 3;
string namespace_uri = 1;
string name = 2;
string value = 3;
// Source line and column info.
optional SourcePosition source = 4;
SourcePosition source = 4;
// The resource ID (0xPPTTEEEE) of the attribute.
optional uint32 resource_id = 5;
// The optional resource ID (0xPPTTEEEE) of the attribute.
uint32 resource_id = 5;
// The interpreted/compiled version of the `value` string.
optional Item compiled_item = 6;
// The optional interpreted/compiled version of the `value` string.
Item compiled_item = 6;
}

View File

@@ -14,39 +14,39 @@
* limitations under the License.
*/
syntax = "proto2";
option java_package = "android.aapt.pb.internal";
option optimize_for = LITE_RUNTIME;
syntax = "proto3";
import "frameworks/base/tools/aapt2/Resources.proto";
package aapt.pb.internal;
option java_package = "android.aapt.pb.internal";
option optimize_for = LITE_RUNTIME;
// The top level message representing an external resource file (layout XML, PNG, etc).
// This is used to represent a compiled file before it is linked. Only useful to aapt2.
message CompiledFile {
message Symbol {
// The name of the symbol (in the form package:type/name).
optional string resource_name = 1;
string resource_name = 1;
// The position in the file at which this symbol is defined. For debug use.
optional aapt.pb.SourcePosition source = 2;
aapt.pb.SourcePosition source = 2;
}
// The name of the resource (in the form package:type/name).
optional string resource_name = 1;
string resource_name = 1;
// The configuration for which the resource is defined.
optional aapt.pb.ConfigDescription config = 2;
aapt.pb.ConfigDescription config = 2;
// The filesystem path to where the source file originated.
// Mainly used to display helpful error messages.
optional string source_path = 3;
string source_path = 3;
// Any symbols this file auto-generates/exports (eg. @+id/foo in an XML file).
repeated Symbol exported_symbol = 4;
// If this is a compiled XML file, this is the root node.
optional aapt.pb.XmlNode xml_root = 5;
aapt.pb.XmlNode xml_root = 5;
}

View File

@@ -42,13 +42,8 @@ void SerializeSourceToPb(const Source& source, StringPool* src_pool, pb::Source*
void DeserializeSourceFromPb(const pb::Source& pb_source, const android::ResStringPool& src_pool,
Source* out_source) {
if (pb_source.has_path_idx()) {
out_source->path = util::GetString(src_pool, pb_source.path_idx());
}
if (pb_source.has_position()) {
out_source->line = static_cast<size_t>(pb_source.position().line_number());
}
out_source->path = util::GetString(src_pool, pb_source.path_idx());
out_source->line = static_cast<size_t>(pb_source.position().line_number());
}
pb::SymbolStatus_Visibility SerializeVisibilityToPb(SymbolState state) {
@@ -84,7 +79,8 @@ void SerializeConfig(const ConfigDescription& config, pb::ConfigDescription* out
bool DeserializeConfigDescriptionFromPb(const pb::ConfigDescription& pb_config,
ConfigDescription* out_config) {
if (!pb_config.has_data()) {
// a ConfigDescription must be at least 4 bytes to store the size.
if (pb_config.data().size() < 4) {
return false;
}

View File

@@ -64,7 +64,7 @@ class PackagePbDeserializer {
bool DeserializeFromPb(const pb::Package& pb_package, ResourceTable* table) {
Maybe<uint8_t> id;
if (pb_package.has_package_id()) {
id = static_cast<uint8_t>(pb_package.package_id());
id = static_cast<uint8_t>(pb_package.package_id().id());
}
std::map<ResourceId, ResourceNameRef> id_index;
@@ -90,10 +90,7 @@ class PackagePbDeserializer {
&entry->symbol_status.source);
}
if (pb_status.has_comment()) {
entry->symbol_status.comment = pb_status.comment();
}
entry->symbol_status.comment = pb_status.comment();
entry->symbol_status.allow_new = pb_status.allow_new();
SymbolState visibility = DeserializeVisibilityFromPb(pb_status.visibility());
@@ -101,15 +98,15 @@ class PackagePbDeserializer {
if (visibility == SymbolState::kPublic) {
// This is a public symbol, we must encode the ID now if there is one.
if (pb_entry.has_id()) {
entry->id = static_cast<uint16_t>(pb_entry.id());
if (pb_entry.has_entry_id()) {
entry->id = static_cast<uint16_t>(pb_entry.entry_id().id());
}
if (type->symbol_status.state != SymbolState::kPublic) {
// If the type has not been made public, do so now.
type->symbol_status.state = SymbolState::kPublic;
if (pb_type.has_id()) {
type->id = static_cast<uint8_t>(pb_type.id());
if (pb_type.has_type_id()) {
type->id = static_cast<uint8_t>(pb_type.type_id().id());
}
}
} else if (visibility == SymbolState::kPrivate) {
@@ -119,7 +116,8 @@ class PackagePbDeserializer {
}
}
ResourceId resid(pb_package.package_id(), pb_type.id(), pb_entry.id());
ResourceId resid(pb_package.package_id().id(), pb_type.type_id().id(),
pb_entry.entry_id().id());
if (resid.is_valid()) {
id_index[resid] = ResourceNameRef(pkg->name, type->type, entry->name);
}
@@ -321,11 +319,11 @@ class PackagePbDeserializer {
out_ref->reference_type = DeserializeReferenceTypeFromPb(pb_ref.type());
out_ref->private_reference = pb_ref.private_();
if (pb_ref.has_id()) {
if (pb_ref.id() != 0) {
out_ref->id = ResourceId(pb_ref.id());
}
if (pb_ref.has_name()) {
if (!pb_ref.name().empty()) {
ResourceNameRef name_ref;
if (!ResourceUtils::ParseResourceName(pb_ref.name(), &name_ref, nullptr)) {
diag_->Error(DiagMessage(source_) << "invalid reference name '" << pb_ref.name() << "'");
@@ -344,10 +342,7 @@ class PackagePbDeserializer {
DeserializeSourceFromPb(pb_item.source(), *source_pool_, &source);
out_value->SetSource(std::move(source));
}
if (pb_item.has_comment()) {
out_value->SetComment(pb_item.comment());
}
out_value->SetComment(pb_item.comment());
}
private:

View File

@@ -179,15 +179,11 @@ class PbSerializerVisitor : public RawValueVisitor {
template <typename T>
void SerializeItemCommonToPb(const Item& item, T* pb_item) {
SerializeSourceToPb(item.GetSource(), source_pool_, pb_item->mutable_source());
if (!item.GetComment().empty()) {
pb_item->set_comment(item.GetComment());
}
pb_item->set_comment(item.GetComment());
}
void SerializeReferenceToPb(const Reference& ref, pb::Reference* pb_ref) {
if (ref.id) {
pb_ref->set_id(ref.id.value().id);
}
pb_ref->set_id(ref.id.value_or_default(ResourceId(0x0)).id);
if (ref.name) {
pb_ref->set_name(ref.name.value().ToString());
@@ -221,21 +217,21 @@ std::unique_ptr<pb::ResourceTable> SerializeTableToPb(ResourceTable* table) {
for (auto& package : table->packages) {
pb::Package* pb_package = pb_table->add_package();
if (package->id) {
pb_package->set_package_id(package->id.value());
pb_package->mutable_package_id()->set_id(package->id.value());
}
pb_package->set_package_name(package->name);
for (auto& type : package->types) {
pb::Type* pb_type = pb_package->add_type();
if (type->id) {
pb_type->set_id(type->id.value());
pb_type->mutable_type_id()->set_id(type->id.value());
}
pb_type->set_name(ToString(type->type).to_string());
for (auto& entry : type->entries) {
pb::Entry* pb_entry = pb_type->add_entry();
if (entry->id) {
pb_entry->set_id(entry->id.value());
pb_entry->mutable_entry_id()->set_id(entry->id.value());
}
pb_entry->set_name(entry->name);
@@ -249,20 +245,13 @@ std::unique_ptr<pb::ResourceTable> SerializeTableToPb(ResourceTable* table) {
for (auto& config_value : entry->values) {
pb::ConfigValue* pb_config_value = pb_entry->add_config_value();
SerializeConfig(config_value->config, pb_config_value->mutable_config());
if (!config_value->product.empty()) {
pb_config_value->mutable_config()->set_product(config_value->product);
}
pb_config_value->mutable_config()->set_product(config_value->product);
pb::Value* pb_value = pb_config_value->mutable_value();
SerializeSourceToPb(config_value->value->GetSource(), &source_pool,
pb_value->mutable_source());
if (!config_value->value->GetComment().empty()) {
pb_value->set_comment(config_value->value->GetComment());
}
if (config_value->value->IsWeak()) {
pb_value->set_weak(true);
}
pb_value->set_comment(config_value->value->GetComment());
pb_value->set_weak(config_value->value->IsWeak());
PbSerializerVisitor visitor(&source_pool, pb_value);
config_value->value->Accept(&visitor);