安卓protobuf的纳米用法 [英] Android protobuf nano usage
问题描述
我想从下面使用protobuf的纳米原文件生成Java文件。我上如何着手这太线程一些基本的指令。
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.
我有这个原文件, 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;
}
我试图从这里按照指示,更具体的 NANO版本的:
- 下载
protobuf-2.5.0.zip
和编译器protoc-2.5.0-win32.zip
从< A HREF =HTTPS://$c$c.google.com/p/protobuf/downloads/list>这里 - 解压缩的
protobuf-2.5.0.zip
到一个文件夹,并在里面的的src
子文件夹我解压缩protoc.exe
。 - 更改为
的Java
文件夹,并在里面发出:MVN清洁套装-P纳米
。该命令运行良好,并在目标
文件夹,我有的protobuf-java的2.5.0.jar
- Downloaded
protobuf-2.5.0.zip
and compilerprotoc-2.5.0-win32.zip
from here. - Unzipped
protobuf-2.5.0.zip
to a folder and in there insrc
subfolder I unzippedprotoc.exe
. - Changed to
java
folder and in there issued:mvn clean package -P nano
. That command ran fine and intarget
folder I haveprotobuf-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.
我不知道这意味着什么,如何连接?有一些参数 protoc.exe
指定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?
我试着发出以下命令: protoc --javanano_out = enum_style = --java_out = personal.proto
I tried to issue this command: protoc --javanano_out=enum_style=java --java_out=generated personal.proto
但我得到这个错误: - javanano_out:protoc-GEN-javanano:系统找不到指定文件
现在的问题是:什么我缺少/做错了上面?我想从上面的原始文件生成Java文件。
The question would be: what am I missing/doing wrong above? I am trying to generate java files from above proto file.
推荐答案
我觉得这个 protoc
未编译 javanano
支持。
I think this protoc
is not compiled with javanano
support.
在pre-编译Windows版本2.5.0不包括纳米的支持,看看源$ C $ C,在的src \谷歌\ protobuf的\编译
路径,包括Java产生而不是javanano发生器。在谷歌资料库最新源$ C $ C 包括javanano。
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.
您也可以下载最新的源$ C $ C < /一>,并尝试使用MinGW和MSYS或 Cygwin的编译,看看这个帖子<一个href="https://stackoverflow.com/questions/9243816/how-to-build-google-protocol-buffers-in-windows-for-mingw">How打造谷歌协议缓冲区在Windows的MinGW的?
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)
更新:
建成后的最终命令行 protoc.exe
其一原文件
protoc --javanano_out=store_unknown_fields=true:target/generated-sources personal.proto, target/generated-sources
对于多个原文件
protoc --javanano_out=store_unknown_fields=true:target/generated-sources --proto_path=inputpath input/*.proto
修改纳米发电机替换枚举成员的公共静态最终诠释
字段。这是一个问题,如果一个类有一个可选的枚举成员,因为该成员将被编译到原始的 INT
的价值和将采取的默认值零,这将是第一个元素的枚举。为了区分情况下,当没有设置一个枚举值,人们可以利用 optional_field_style
参数,将产生 java.lang.Integer中的
而不是一个原始的int。当原解析,调用者可以检查值是使用前值空。 NULL表示值未设置。
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
这篇关于安卓protobuf的纳米用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!