protobuf安卓 java通信,安卓protobuf的纳米用法

I am trying to generate java files from below proto file using protobuf nano. I got some basic instruction on how to proceed in this SO thread.

I have this proto file, personal.proto:

package tutorial;

option java_package = "com.example.tutorial";

option java_outer_classname = "AddressBookProtos";

message Person {

required string name = 1;

required int32 id = 2;

optional string email = 3;

enum PhoneType {

MOBILE = 0;

HOME = 1;

WORK = 2;

}

message PhoneNumber {

required string number = 1;

optional PhoneType type = 2 [default = HOME];

}

repeated PhoneNumber phone = 4;

}

message AddressBook {

repeated Person person = 1;

}

I tried to follow the instruction from here, more specifically NANO version:

Downloaded protobuf-2.5.0.zip and compiler protoc-2.5.0-win32.zip from here.

Unzipped protobuf-2.5.0.zip to a folder and in there in src subfolder I unzipped protoc.exe.

Changed to java folder and in there issued: mvn clean package -P nano. That command ran fine and in target folder I have protobuf-java-2.5.0.jar

From here I am not sure how to proceed since in the initial documentation I have this statement:

- Link with the generated jar file

java/target/protobuf-java-2.3.0-nano.jar.

I am not sure what that means, how to link? Is there some parameter for protoc.exe that specifies the jar file to use?

I tried to issue this command: protoc --javanano_out=enum_style=java --java_out=generated personal.proto

but I get this error: --javanano_out: protoc-gen-javanano: The system cannot find the file specified.

The question would be: what am I missing/doing wrong above? I am trying to generate java files from above proto file.

解决方案

I think this protoc is not compiled with javanano support.

The pre-compiled windows version 2.5.0 does not include nano support, take a look at the source code, in the "src\google\protobuf\compiler" path, includes the java generator but not the javanano generator. The latest source code at google repositories includes javanano.

You can download the latest source code and try to compile it using MinGW and msys or CygWin, take a look at this post How to build google protocol buffers in Windows for mingw?

(I will post details for the building process later)

UPDATE:

The final command line after building protoc.exe

For one proto file

protoc --javanano_out=store_unknown_fields=true:target/generated-sources personal.proto, target/generated-sources

For multiple proto files

protoc --javanano_out=store_unknown_fields=true:target/generated-sources --proto_path=inputpath input/*.proto

EDIT Nano generator replaces enum members with public static final int fields. This is a problem if a class has an optional enum member because that member will be compiled to a primitive int value and will take the default value of zero, which will be the first element from enum. To distinguish the cases when an enum value was not set, one can take advantage of optional_field_style parameter that will generate java.lang.Integer instead of a primitive int. When the proto is parsed, the caller can check if the value is null before using the value. Null means the value was not set.

The above call script can become:

protoc --javanano_out=store_unknown_fields=true,optional_field_style=reftypes:target/generated-sources --proto_path=input input/*.proto

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值