Android protobuf nano usage

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

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:

  1. Downloaded protobuf-2.5.0.zip and compiler protoc-2.5.0-win32.zip from here.
  2. Unzipped protobuf-2.5.0.zip to a folder and in there in src subfolder I unzipped protoc.exe.
  3. 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   <protobuf-root>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.

回答1:

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!