Using Android protobuf nano

I am trying to create java files from a proto file using protobuf nano. I got basic instructions on how to go into 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 instructions from here , specifically the NANO version:

  • Download protobuf-2.5.0.zip and the protobuf-2.5.0.zip compiler from here .
  • Unzip protobuf-2.5.0.zip to the folder and where the src subfolder is located, I will unzip protoc.exe .
  • The java folder has been changed and it displays: mvn clean package -P nano . This command went fine, and in the target folder I have protobuf-java-2.5.0.jar

From here I am not sure how to proceed, as in the original documentation I have this statement:

 - Link with the generated jar file <protobuf-root>java/target/protobuf-java-2.3.0-nano.jar. 

I'm not sure what that means, how to knit? Is there a parameter for protoc.exe that indicates 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 will be: what am I missing / doing wrong above? I am trying to create java files from a proto file.

+6
source share
1 answer

I think this protoc not compiled with javanano support.

Precompiled windows of version 2.5.0 do not include nano support, see the source code in the path " src\google\protobuf\compiler ", including the java generator, but not the javanano generator. The latest source code in google repositories includes javanano.

You can download the latest source code and try to compile it using MinGW and msys or CygWin , see this post. How to create google protocol buffers in Windows for mingw?

(later I will give details for the construction process)

UPDATE:

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 several proto files

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

The EDIT Nano generator replaces enumeration members with public static final int fields. This is a problem if the class has an optional enumeration member because this member will be compiled with a primitive int value and will take a default value of zero, which will be the first element of the enumeration. To distinguish cases when the enumeration value has not been set, you can use the optional_field_style parameter, which will generate java.lang.Integer instead of the primitive int. When the protocol is analyzed, the caller can check if the value is null before using the value. Null means the value has not been set.

The above script call could become:

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

All Articles