Java Android protobuf nano文档
我正试图减少由Google proto-buf生成的方法的数量,替代方法之一是使用proto-buf-nano。但是,我没有找到关于如何使用它的文档。除了,我找不到任何关于如何使用nano从proto文件生成java文件的信息Java Android protobuf nano文档,java,android,protocol-buffers,Java,Android,Protocol Buffers,我正试图减少由Google proto-buf生成的方法的数量,替代方法之一是使用proto-buf-nano。但是,我没有找到关于如何使用它的文档。除了,我找不到任何关于如何使用nano从proto文件生成java文件的信息 所以问题很简单:如何使用google proto nano从proto文件生成java类,以及如何在项目中使用它们?查看主要protobuf编译器源代码: #include <google/protobuf/compiler/javanano/javanano_ge
所以问题很简单:如何使用google proto nano从proto文件生成java类,以及如何在项目中使用它们?查看主要protobuf编译器源代码:
#include <google/protobuf/compiler/javanano/javanano_generator.h>
....
int main(int argc, char* argv[]) {
google::protobuf::compiler::CommandLineInterface cli;
cli.AllowPlugins("protoc-");
...
// Proto2 JavaNano
google::protobuf::compiler::javanano::JavaNanoGenerator javanano_generator;
cli.RegisterGenerator("--javanano_out", &javanano_generator,
"Generate Java source file nano runtime.");
return cli.Run(argc, argv);
}
命令行
./protoc '--javanano_out=java_package=google/protobuf/unittest_simple_nano.proto|com.google.protobuf.nano,java_outer_classname=google/protobuf/unittest_simple_nano.proto|UnittestSimpleNano:target/generated-test-sources' google/protobuf/unittest_simple_nano.proto
从中提取的测试
在同一个类中有很多测试用例,看看这个项目,你可以找到一个maven-antrun插件
配置来生成测试资源类
<!-- java nano -->
<exec executable="../src/protoc">
<arg value="--javanano_out=java_package=google/protobuf/unittest_import_nano.proto|com.google.protobuf.nano,java_outer_classname=google/protobuf/unittest_import_nano.proto|UnittestImportNano:target/generated-test-sources" />
<arg value="--proto_path=../src" />
<arg value="--proto_path=src/test/java" />
<arg value="../src/google/protobuf/unittest_nano.proto" />
<arg value="../src/google/protobuf/unittest_simple_nano.proto" />
<arg value="../src/google/protobuf/unittest_stringutf8_nano.proto" />
<arg value="../src/google/protobuf/unittest_recursive_nano.proto" />
<arg value="../src/google/protobuf/unittest_import_nano.proto" />
<arg value="../src/google/protobuf/unittest_enum_multiplejava_nano.proto" />
<arg value="../src/google/protobuf/unittest_multiple_nano.proto" />
</exec>
希望这有帮助。非常感谢您的回答。。。我会稍后再试,然后回来我试着按照你建议的方式走,但我被卡住了。你能看一看吗?
public void testSimpleMessageNano() throws Exception {
SimpleMessageNano msg = new SimpleMessageNano();
assertEquals(123, msg.d);
assertEquals(null, msg.nestedMsg);
assertEquals(SimpleMessageNano.BAZ, msg.defaultNestedEnum);
msg.d = 456;
assertEquals(456, msg.d);
SimpleMessageNano.NestedMessage nestedMsg = new SimpleMessageNano.NestedMessage();
nestedMsg.bb = 2;
assertEquals(2, nestedMsg.bb);
msg.nestedMsg = nestedMsg;
assertEquals(2, msg.nestedMsg.bb);
msg.defaultNestedEnum = SimpleMessageNano.BAR;
assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum);
byte [] result = MessageNano.toByteArray(msg);
int msgSerializedSize = msg.getSerializedSize();
//System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
assertTrue(msgSerializedSize == 9);
assertEquals(result.length, msgSerializedSize);
SimpleMessageNano newMsg = SimpleMessageNano.parseFrom(result);
assertEquals(456, newMsg.d);
assertEquals(2, msg.nestedMsg.bb);
assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum);
}
<!-- java nano -->
<exec executable="../src/protoc">
<arg value="--javanano_out=java_package=google/protobuf/unittest_import_nano.proto|com.google.protobuf.nano,java_outer_classname=google/protobuf/unittest_import_nano.proto|UnittestImportNano:target/generated-test-sources" />
<arg value="--proto_path=../src" />
<arg value="--proto_path=src/test/java" />
<arg value="../src/google/protobuf/unittest_nano.proto" />
<arg value="../src/google/protobuf/unittest_simple_nano.proto" />
<arg value="../src/google/protobuf/unittest_stringutf8_nano.proto" />
<arg value="../src/google/protobuf/unittest_recursive_nano.proto" />
<arg value="../src/google/protobuf/unittest_import_nano.proto" />
<arg value="../src/google/protobuf/unittest_enum_multiplejava_nano.proto" />
<arg value="../src/google/protobuf/unittest_multiple_nano.proto" />
</exec>