Even Better Protocol Buffers With Wire 2

The new Wire release can shrink your schema.

Written by Jesse Wilson.

We’ve been making steady progress on Wire, our implementation of compact, immutable protocol buffers for Android and Java. Today I’m happy to announce Wire 2.0, which further refines our take on what protocol buffers should be.

One of my favorite features of Wire 2 is our sophisticated schema pruning. Suppose you have an analytics API defined by event.proto:

message Event {
  optional int64 created_at = 1;
  optional string message = 2;
  optional string url = 3;
  optional Browser browser = 4;
}

message Browser {
  optional string user_agent = 1;
  repeated Feature features = 2;

  enum Feature {
    WEBGL = 1;
    LOCAL_STORAGE = 2;
    WEB_SQL = 3;
  }
}

The browser field on the event is neither useful nor necessary to the Android app. But since the Event type depends on it, the Browser class and Feature enum are both dragged into the generated code. Unused code slows down the build, enlarges the APK, and distracts from the useful code around it.

Wire 2 can prune unwanted garbage with includes and excludes compiler options. We can prune the Browser type everywhere it appears:

<plugin>
  <groupId>com.squareup.wire</groupId>
  <artifactId>wire-maven-plugin</artifactId>

  ...

    <excludes>
      <exclude>squareup.events.Browser</exclude>
    </excludes>

  ...

</plugin>

Or only this occurrence:

<plugin>
  <groupId>com.squareup.wire</groupId>
  <artifactId>wire-maven-plugin</artifactId>

  ...

    <excludes>
      <exclude>squareup.events.Event#browser</exclude>
    </excludes>

  ...

</plugin>

In either case, we’ll get an Event message that’s better suited to the client that consumes it. Provide whitelisted and blacklisted identifiers, and Wire 2 will retain the matched items and their transitive dependencies.

Use pruning to hide old, deprecated stuff from newer clients while services retain everything for backwards compatibility. Or perhaps hide the new, unstable fields from your fast-moving client teams!

Wire 2’s changelog lists all that’s new (it’s substantial)! Get Wire 2.0.0 on GitHub. Jesse Wilson *Android and jokes.*medium.com