Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android protobuf nano usage

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.

like image 456
gunar Avatar asked Mar 28 '14 12:03

gunar


People also ask

What is Protobuf Nano?

Protocol Buffers are a way of encoding structured data in an efficient yet extensible format. JavaNano is a special code generator and runtime library designed specially for resource-restricted systems, like Android.

What is Android Protobuf?

Protocol Buffer or we can say Protobuf, is developed by Google in early 2001, release internally and in 2008 release publically. As the official definition says. Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data.

What is Protobuf Java?

Protocol Buffers is a library from Google. It provides efficient and language-independent ways to serialize the data. It supports serialization and deserialization from languages like Java, Python, Go, Dart, etc. It is one of the most popular serialization libraries used across industries by various companies.


1 Answers

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
like image 58
vzamanillo Avatar answered Oct 20 '22 08:10

vzamanillo