← Protobuf
Protobuf·Format guide
All tools

Protocol Buffers in apps

Protobuf is Google's binary serialization format. Apps use it for gRPC and REST APIs, for cached and stored data (DataStore on Android, SDK caches, Firebase), and for build artefacts: Android App Bundles keep their manifest, resources and BundleConfig.pb in protobuf. The tool decodes it with or without a schema, in your browser.

Contents

  1. The wire format
  2. Decoding without a schema
  3. Decoding with a schema
  4. Input
  5. App Bundles
  6. The protobuf library

1The wire format

A message is a sequence of fields. Each field starts with a varint tag: the field number shifted left by three, plus a wire type.

Wire typeEncodingUsed for
0 varint7 bits per byte, little-endian groupsint32/64, uint32/64, bool, enum; sint32/64 as zigzag
1 i648 bytes, little-endianfixed64, sfixed64, double
2 lena varint length, then that many bytesstring, bytes, nested messages, packed repeated numbers
5 i324 bytes, little-endianfixed32, sfixed32, float
3 / 4group start / enddeprecated groups

Field names and types aren't stored: only the schema knows them.

2Decoding without a schema

Field numbers and wire types are certain; the rest is inferred:

Click a field to see its bytes in the hex panel. Copy as JSON copies the decoded tree.

3Decoding with a schema

Open a FileDescriptorSet (protoc --descriptor_set_out=x.desc --include_imports …, often called .desc or .protoset) together with the data. Fields then get their names and declared types (enums by name, sint/fixed/float decoded correctly, packed fields expanded). The tool picks the message type that fits the data best, and Decode as chooses another. Apps built with full protobuf (not protobuf-lite) embed their descriptors; you can recover them by searching the app for .proto file names.

4Input

Compressed gRPC frames (gzip) aren't inflated.

5App Bundles

In an .aab, base/manifest/AndroidManifest.xml and the XML under res/ are aapt2 protobuf (XmlNode) rather than binary AXML. The AXML tool recognises them and shows them as XML; other .pb files open here.

6The protobuf library

lib/protobuf.js is a JavaScript module with no dependencies and no user interface; it works in pages, workers and jsc. It exports Protobuf, a class of static methods.

<script type="module">
	import { Protobuf } from "../lib/protobuf.js";

	const message = Protobuf.decode(bytes);                        // without a schema
	for (const field of message.fields) {
		console.log(field.number, field.wireType, field.interpretation || Protobuf.readings(field));
	}
	const schema = Protobuf.schemaFromDescriptorSet(descriptorBytes);
	const typed = Protobuf.decode(bytes, { schema, type: Protobuf.bestType(schema, bytes) });
	console.log(Protobuf.toJSON(typed));
</script>
MethodWhat it does
Protobuf.decode(bytes, { schema, type })A message { fields, offset, end, type }, or null when the bytes aren't one. Each field has number, wireType, offset, end, and value (varints, as a BigInt) or bytes; length-delimited fields get an interpretation (message, string, packed, bytes or empty); with a schema, name, fieldType, repeated, typedValue.
Protobuf.readings(field)Every reading of a scalar: [{ kind, value }] for uint, int64, sint, bool, fixed64, double, sfixed64, fixed32, float, sfixed32.
Protobuf.interpret(bytes, depth)The best guess for length-delimited bytes.
Protobuf.parseMessage(bytes, start, end)The fields alone, without interpretations, or null.
Protobuf.readVarint(bytes, position, end)[value, next position].
Protobuf.unframe(bytes), Protobuf.delimited(bytes)gRPC frames [{ offset, bytes, compressed }], or varint-delimited messages; null when the data isn't framed that way.
Protobuf.schemaFromDescriptorSet(bytes)A schema { types, enums, files } from a FileDescriptorSet. Schemas can also be written by hand: types["pkg.Msg"].fields[number] = { name, type, label, typeName }.
Protobuf.bestType(schema, bytes)The message type whose fields fit the bytes best.
Protobuf.applySchema(message, schema, type)Adds names and types to a parsed message.
Protobuf.toJSON(message)Plain values keyed by field name or number; repeated fields become arrays.
Protobuf.SCALAR_TYPESDescriptor type numbers to type names (1 double … 18 sint64).
Protobuf.parseBytesText(text)Bytes from hex, Base64, or escaped text (\x0a, protoc's \012), or null.