com.anatawa12.lightweight-protobuf:compiler

A lightweight protocol buffer implementation for java.


License
MIT

Documentation

lightweight protobuf

A lightweight protocol buffer implementation for java.

maven-central a12 maintenance: Active

Why not Google's official protobuf for java nor wire by Square?

Because those are a little heavy, I made this.

protobuf-javalite-3.15.8.jar is 694 kilo-bytes and uses reflection.

wire-runtime-3.7.0.jar is 189 kilo-bytes, uses reflection, and needs okio-2.8.0.jar, 243 kilo-bytes jar so in total 432 kilo-bytes.

I want to make protobuf implementation in less than 100 kilo-bytes.

There's comparison of fat-jar with shadow plugin at file-size-test/fat-sizes.txt

Limitations

Compared with Google's protobuf, there are various limitations.

  • This doesn't support extensions.
  • Currently, to make compiler easily, all fields are used as proto3's normal field. This means,
    • No support for requires
    • No hasField() for optionals
    • No support for proto3's optionals
  • No support for descriptors of anything like FileDescriptor.

How To Use

You can get runtime library from maven central. Here's example configuration for gradle.

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.anatawa12.lightweight-protobuf:lightweight-protobuf:$lightWeightProtobufVersion")
}

You can also get jar version of protoc plugin from maven central. You can generate java files using protobuf-gradle-plugin.

protobuf {
    plugins {
        create("lw-java") {
            artifact = "com.anatawa12.lightweight-protobuf:compiler:$lightWeightProtobufVersion:all@jar"
        }
    }
    generateProtoTasks {
        // use 'all().each {' for groovy
        all().forEach {
            it.plugins {
                create("lw-java")
            }
        }
    }
}

If you want to generate java files using protoc, you need to write wrapper script for each platform like this.

#!/bin/sh
exec java -jar /path/to/compiler-plugin.jar
@ECHO OFF
java -jar \path\to\compiler-plugin.jar

Status

  • implement data struct compiler
  • implement parser
  • implement writer
  • full support for proto2 and proto3
    • proto3: packed by default
    • proto3's optional
    • proto2's optional
    • proto2's required