安卓protobuf的纳米文档 [英] Android protobuf nano documentation

查看:453
本文介绍了安卓protobuf的纳米文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,以减少由谷歌原BUF生成的,而且替​​代品之一是使用原BUF纳米方法的数量。但是我发现如何使用它没有文档。除<一href="https://github.com/android/platform_external_protobuf/tree/master/java/src/main/java/com/google/protobuf/nano"相对=nofollow>封装环节,我无法找到如何从采用纳米原文件,java文件什么。

I am trying to reduce the number of methods that are generated by Google proto-buf and one of the alternatives is to use proto-buf nano. However I found no documentation on how to use it. Except the package link, I can't find anything on how to generate java files from proto files using nano.

所以,问题是直接的:?如何使用谷歌原纳米,以便产生从原文件中的Java类,以及如何使用它们在项目

So the question is straight-forward: how to use google proto nano in order to generate java classes from proto files and how to use them in a project?

推荐答案

看主要protobuf的编译器的源$ C ​​$ C:

Looking at the main protobuf compiler source code:

#include <google/protobuf/compiler/javanano/javanano_generator.h>
.... 

int main(int argc, char* argv[]) {

    google::protobuf::compiler::CommandLineInterface cli;
    cli.AllowPlugins("protoc-");
    ...
    // Proto2 JavaNano
    google::protobuf::compiler::javanano::JavaNanoGenerator javanano_generator;
    cli.RegisterGenerator("--javanano_out", &javanano_generator,
    "Generate Java source file nano runtime.");

    return cli.Run(argc, argv);
}

看着的protobuf的自述

纳米发电机选项

      
  • java_package - &GT; &LT;文件名称&gt; |&LT;包名称&gt;
  •   
  • java_outer_classname - &GT; &LT;文件名称&gt; |&LT;包名称&gt;
  •   
  • java_multiple_files - &GT; true或false
  •   
  • java_nano_generate_has - &GT;真的还是假的[DE preCATED]
  •   
  • optional_field_style - &GT;默认或访问
  •   
  • enum_style - &GT;的C或Java
  •   
  • java_package -> <file-name>|<package-name>
  • java_outer_classname -> <file-name>|<package-name>
  • java_multiple_files -> true or false
  • java_nano_generate_has -> true or false [DEPRECATED]
  • optional_field_style -> default or accessors
  • enum_style -> c or java

要使用Android的回购之外纳米protobufs:

      
  • 链接与生成jar文件&LT; protobuf的根&GT;的Java /目标/ protobuf的-java的-2.3.0-nano.jar
  •   
  • 在调用与 - javanano_out ,例如:
  •   
  • Link with the generated jar file <protobuf-root>java/target/protobuf-java-2.3.0-nano.jar.
  • Invoke with --javanano_out, e.g.:

./ protoc '--javanano_out=java_package=src/proto/simple-data.proto|my_package,java_outer_classname=src/proto/simple-data.proto|OuterName:.' SRC /原/简单data.proto

到了Android回购中使用的纳米protobufs:

      
  • 设置 LOCAL_PROTOC_OPTIMIZE_TYPE:=纳米在本地的.mk文件。当建立一个Java库或应用程序(包)的目标,构建
      系统将在Java运行时的纳米库添加到
       LOCAL_STATIC_JAVA_LIBRARIES 变量,所以你并不需要。
  •   在你需要的任何命令行选项当地的.mk文件:'= ... LOCAL_PROTO_JAVA_OUTPUT_PARAMS'
  • 设置。使用逗号连接多个
      选项​​。在只有纳米味道,周围的空白选择
      名称和值会被忽略,所以你可以用反斜杠换行或
      '+ ='很好地组织你的make文件。
  •   
  • 的选项将被应用到的所有的原型在 LOCAL_SRC_FILES 当你建立一个Java库或包文件。如果不同的选择
      需要针对不同的原文件,建立独立的Java库
      并引用他们的主要目标。注意:你应该确保
      即,对于每一个单独的目标,所有的原始文件从任何
    进口   在 LOCAL_SRC_FILES 原文件都包含在 LOCAL_SRC_FILES 。这
      是因为发电机必须假定导入的文件
      用同样的选项建成,将产生code引用
      从导入的文件使用相同的code
    领域和枚举   风格。
  •   
  • 提示:包括$(CLEAR_VARS)重置所有 LOCAL _ 变量,包括上述两种   
  • Set 'LOCAL_PROTOC_OPTIMIZE_TYPE := nano' in your local .mk file. When building a Java library or an app (package) target, the build
    system will add the Java nano runtime library to the
    LOCAL_STATIC_JAVA_LIBRARIES variable, so you don't need to.
  • Set 'LOCAL_PROTO_JAVA_OUTPUT_PARAMS := ...' in your local .mk file for any command-line options you need. Use commas to join multiple
    options. In the nano flavor only, whitespace surrounding the option
    names and values are ignored, so you can use backslash-newline or
    '+=' to structure your make files nicely.
  • The options will be applied to all proto files in LOCAL_SRC_FILES when you build a Java library or package. In case different options
    are needed for different proto files, build separate Java libraries
    and reference them in your main target. Note: you should make sure
    that, for each separate target, all proto files imported from any
    proto file in LOCAL_SRC_FILES are included in LOCAL_SRC_FILES. This
    is because the generator has to assume that the imported files are
    built using the same options, and will generate code that reference
    the fields and enums from the imported files using the same code
    style.
  • Hint: 'include $(CLEAR_VARS)' resets all LOCAL_ variables, including the two above.

简单的纳米例如<一href="https://android.googlesource.com/platform/external/protobuf/+/master/src/google/protobuf/">https://android.googlesource.com/platform/external/protobuf/+/master/src/google/protobuf/.

unittest_simple_nano.proto

package protobuf_unittest_import;

option java_package = "com.google.protobuf.nano";
// Explicit outer classname to suppress legacy info.
option java_outer_classname = "UnittestSimpleNano";

message SimpleMessageNano {
  message NestedMessage {
    optional int32 bb = 1;
  }

  enum NestedEnum {
    FOO = 1;
    BAR = 2;
    BAZ = 3;
  }

  optional int32 d = 1 [default = 123];
  optional NestedMessage nested_msg = 2;
  optional NestedEnum default_nested_enum = 3 [default = BAZ];
}

命令行

./protoc '--javanano_out=java_package=google/protobuf/unittest_simple_nano.proto|com.google.protobuf.nano,java_outer_classname=google/protobuf/unittest_simple_nano.proto|UnittestSimpleNano:target/generated-test-sources' google/protobuf/unittest_simple_nano.proto

测试从<一个提取href="https://github.com/android/platform_external_protobuf/blob/master/java/src/test/java/com/google/protobuf/NanoTest.java">NanoTest.java

Test extracted from NanoTest.java

  public void testSimpleMessageNano() throws Exception {
    SimpleMessageNano msg = new SimpleMessageNano();
    assertEquals(123, msg.d);
    assertEquals(null, msg.nestedMsg);
    assertEquals(SimpleMessageNano.BAZ, msg.defaultNestedEnum);

    msg.d = 456;
    assertEquals(456, msg.d);

    SimpleMessageNano.NestedMessage nestedMsg = new SimpleMessageNano.NestedMessage();
    nestedMsg.bb = 2;
    assertEquals(2, nestedMsg.bb);
    msg.nestedMsg = nestedMsg;
    assertEquals(2, msg.nestedMsg.bb);

    msg.defaultNestedEnum = SimpleMessageNano.BAR;
    assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum);

    byte [] result = MessageNano.toByteArray(msg);
    int msgSerializedSize = msg.getSerializedSize();
    //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
    assertTrue(msgSerializedSize == 9);
    assertEquals(result.length, msgSerializedSize);

    SimpleMessageNano newMsg = SimpleMessageNano.parseFrom(result);
    assertEquals(456, newMsg.d);
    assertEquals(2, msg.nestedMsg.bb);
    assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum);
  }

有很多在同一类的测试用例,并期待在项目<一href="https://android.googlesource.com/platform/external/protobuf/+/35d9fd84ff1d9ecdb91156b757cc9fdcc3f25249/java/pom.xml">pom你可以找到一个的maven-antrun-插件配置生成一个测试资源类

There are a lot of test cases in the same class, and looking at the project pom you can find a maven-antrun-plugin configuration to generate that test resource classes

<!-- java nano -->
<exec executable="../src/protoc">
  <arg value="--javanano_out=java_package=google/protobuf/unittest_import_nano.proto|com.google.protobuf.nano,java_outer_classname=google/protobuf/unittest_import_nano.proto|UnittestImportNano:target/generated-test-sources" />
  <arg value="--proto_path=../src" />
  <arg value="--proto_path=src/test/java" />
  <arg value="../src/google/protobuf/unittest_nano.proto" />
  <arg value="../src/google/protobuf/unittest_simple_nano.proto" />
  <arg value="../src/google/protobuf/unittest_stringutf8_nano.proto" />
  <arg value="../src/google/protobuf/unittest_recursive_nano.proto" />
  <arg value="../src/google/protobuf/unittest_import_nano.proto" />
  <arg value="../src/google/protobuf/unittest_enum_multiplejava_nano.proto" />
  <arg value="../src/google/protobuf/unittest_multiple_nano.proto" />
</exec>

希望这有助于。

Hope this helps.

这篇关于安卓protobuf的纳米文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆