README.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. Protocol Buffers - Google's data interchange format
  2. Copyright 2008 Google Inc.
  3. This directory contains the Java Protocol Buffers Nano runtime library.
  4. Installation - With Maven
  5. =========================
  6. The Protocol Buffers build is managed using Maven. If you would
  7. rather build without Maven, see below.
  8. 1) Install Apache Maven if you don't have it:
  9. http://maven.apache.org/
  10. 2) Build the C++ code, or obtain a binary distribution of protoc. If
  11. you install a binary distribution, make sure that it is the same
  12. version as this package. If in doubt, run:
  13. $ protoc --version
  14. You will need to place the protoc executable in ../src. (If you
  15. built it yourself, it should already be there.)
  16. 3) Run the tests:
  17. $ mvn test
  18. If some tests fail, this library may not work correctly on your
  19. system. Continue at your own risk.
  20. 4) Install the library into your Maven repository:
  21. $ mvn install
  22. 5) If you do not use Maven to manage your own build, you can build a
  23. .jar file to use:
  24. $ mvn package
  25. The .jar will be placed in the "target" directory.
  26. Installation - Without Maven
  27. ============================
  28. If you would rather not install Maven to build the library, you may
  29. follow these instructions instead. Note that these instructions skip
  30. running unit tests.
  31. 1) Build the C++ code, or obtain a binary distribution of protoc. If
  32. you install a binary distribution, make sure that it is the same
  33. version as this package. If in doubt, run:
  34. $ protoc --version
  35. If you built the C++ code without installing, the compiler binary
  36. should be located in ../src.
  37. 2) Invoke protoc to build DescriptorProtos.java:
  38. $ protoc --java_out=src/main/java -I../src \
  39. ../src/google/protobuf/descriptor.proto
  40. 3) Compile the code in src/main/java using whatever means you prefer.
  41. 4) Install the classes wherever you prefer.
  42. Nano version
  43. ============================
  44. JavaNano is a special code generator and runtime library designed specially for
  45. resource-restricted systems, like Android. It is very resource-friendly in both
  46. the amount of code and the runtime overhead. Here is an overview of JavaNano
  47. features compared with the official Java protobuf:
  48. - No descriptors or message builders.
  49. - All messages are mutable; fields are public Java fields.
  50. - For optional fields only, encapsulation behind setter/getter/hazzer/
  51. clearer functions is opt-in, which provide proper 'has' state support.
  52. - For proto2, if not opted in, has state (field presence) is not available.
  53. Serialization outputs all fields not equal to their defaults
  54. (see important implications below).
  55. The behavior is consistent with proto3 semantics.
  56. - Required fields (proto2 only) are always serialized.
  57. - Enum constants are integers; protection against invalid values only
  58. when parsing from the wire.
  59. - Enum constants can be generated into container interfaces bearing
  60. the enum's name (so the referencing code is in Java style).
  61. - CodedInputByteBufferNano can only take byte[] (not InputStream).
  62. - Similarly CodedOutputByteBufferNano can only write to byte[].
  63. - Repeated fields are in arrays, not ArrayList or Vector. Null array
  64. elements are allowed and silently ignored.
  65. - Full support for serializing/deserializing repeated packed fields.
  66. - Support extensions (in proto2).
  67. - Unset messages/groups are null, not an immutable empty default
  68. instance.
  69. - toByteArray(...) and mergeFrom(...) are now static functions of
  70. MessageNano.
  71. - The 'bytes' type translates to the Java type byte[].
  72. The generated messages are not thread-safe for writes, but may be
  73. used simultaneously from multiple threads in a read-only manner.
  74. In other words, an appropriate synchronization mechanism (such as
  75. a ReadWriteLock) must be used to ensure that a message, its
  76. ancestors, and descendants are not accessed by any other threads
  77. while the message is being modified. Field reads, getter methods
  78. (but not getExtension(...)), toByteArray(...), writeTo(...),
  79. getCachedSize(), and getSerializedSize() are all considered read-only
  80. operations.
  81. IMPORTANT: If you have fields with defaults and opt out of accessors
  82. How fields with defaults are serialized has changed. Because we don't
  83. keep "has" state, any field equal to its default is assumed to be not
  84. set and therefore is not serialized. Consider the situation where we
  85. change the default value of a field. Senders compiled against an older
  86. version of the proto continue to match against the old default, and
  87. don't send values to the receiver even though the receiver assumes the
  88. new default value. Therefore, think carefully about the implications
  89. of changing the default value. Alternatively, turn on accessors and
  90. enjoy the benefit of the explicit has() checks.
  91. IMPORTANT: If you have "bytes" fields with non-empty defaults
  92. Because the byte buffer is now of mutable type byte[], the default
  93. static final cannot be exposed through a public field. Each time a
  94. message's constructor or clear() function is called, the default value
  95. (kept in a private byte[]) is cloned. This causes a small memory
  96. penalty. This is not a problem if the field has no default or is an
  97. empty default.
  98. Nano Generator options
  99. java_package -> <file-name>|<package-name>
  100. java_outer_classname -> <file-name>|<package-name>
  101. java_multiple_files -> true or false
  102. java_nano_generate_has -> true or false [DEPRECATED]
  103. optional_field_style -> default or accessors
  104. enum_style -> c or java
  105. ignore_services -> true or false
  106. parcelable_messages -> true or false
  107. java_package=<file-name>|<package-name> (no default)
  108. This allows overriding the 'java_package' option value
  109. for the given file from the command line. Use multiple
  110. java_package options to override the option for multiple
  111. files. The final Java package for each file is the value
  112. of this command line option if present, or the value of
  113. the same option defined in the file if present, or the
  114. proto package if present, or the default Java package.
  115. java_outer_classname=<file-name>|<outer-classname> (no default)
  116. This allows overriding the 'java_outer_classname' option
  117. for the given file from the command line. Use multiple
  118. java_outer_classname options to override the option for
  119. multiple files. The final Java outer class name for each
  120. file is the value of this command line option if present,
  121. or the value of the same option defined in the file if
  122. present, or the file name converted to CamelCase. This
  123. outer class will nest all classes and integer constants
  124. generated from file-scope messages and enums.
  125. java_multiple_files={true,false} (no default)
  126. This allows overriding the 'java_multiple_files' option
  127. in all source files and their imported files from the
  128. command line. The final value of this option for each
  129. file is the value defined in this command line option, or
  130. the value of the same option defined in the file if
  131. present, or false. This specifies whether to generate
  132. package-level classes for the file-scope messages in the
  133. same Java package as the outer class (instead of nested
  134. classes in the outer class). File-scope enum constants
  135. are still generated as integer constants in the outer
  136. class. This affects the fully qualified references in the
  137. Java code. NOTE: because the command line option
  138. overrides the value for all files and their imported
  139. files, using this option inconsistently may result in
  140. incorrect references to the imported messages and enum
  141. constants.
  142. java_nano_generate_has={true,false} (default: false)
  143. DEPRECATED. Use optional_field_style=accessors.
  144. If true, generates a public boolean variable has<fieldname>
  145. accompanying each optional or required field (not present for
  146. repeated fields, groups or messages). It is set to false initially
  147. and upon clear(). If parseFrom(...) reads the field from the wire,
  148. it is set to true. This is a way for clients to inspect the "has"
  149. value upon parse. If it is set to true, writeTo(...) will ALWAYS
  150. output that field (even if field value is equal to its
  151. default).
  152. IMPORTANT: This option costs an extra 4 bytes per primitive field in
  153. the message. Think carefully about whether you really need this. In
  154. many cases reading the default works and determining whether the
  155. field was received over the wire is irrelevant.
  156. optional_field_style={default,accessors,reftypes} (default: default)
  157. Defines the style of the generated code for fields.
  158. * default *
  159. In the default style, optional fields translate into public mutable
  160. Java fields, and the serialization process is as discussed in the
  161. "IMPORTANT" section above.
  162. * accessors *
  163. When set to 'accessors', each optional field is encapsulated behind
  164. 4 accessors, namely get<fieldname>(), set<fieldname>(), has<fieldname>()
  165. and clear<fieldname>() methods, with the standard semantics. The hazzer's
  166. return value determines whether a field is serialized, so this style is
  167. useful when you need to serialize a field with the default value, or check
  168. if a field has been explicitly set to its default value from the wire.
  169. In the 'accessors' style, required and nested message fields are still
  170. translated to one public mutable Java field each, repeated fields are still
  171. translated to arrays. No accessors are generated for them.
  172. IMPORTANT: When using the 'accessors' style, ProGuard should always
  173. be enabled with optimization (don't use -dontoptimize) and allowing
  174. access modification (use -allowaccessmodification). This removes the
  175. unused accessors and maybe inline the rest at the call sites,
  176. reducing the final code size.
  177. TODO(maxtroy): find ProGuard config that would work the best.
  178. * reftypes *
  179. When set to 'reftypes', each proto field is generated as a public Java
  180. field. For primitive types, these fields use the Java reference types
  181. such as java.lang.Integer instead of primitive types such as int.
  182. In the 'reftypes' style, fields are initialized to null (or empty
  183. arrays for repeated fields), and their default values are not available.
  184. They are serialized over the wire based on equality to null.
  185. The 'reftypes' mode has some additional cost due to autoboxing and usage
  186. of reference types. In practice, many boxed types are cached, and so don't
  187. result in object creation. However, references do take slightly more memory
  188. than primitives.
  189. The 'reftypes' mode is useful when you want to be able to serialize fields
  190. with default values, or check if a field has been explicitly set to the
  191. default over the wire without paying the extra method cost of the
  192. 'accessors' mode.
  193. Note that if you attempt to write null to a required field in the reftypes
  194. mode, serialization of the proto will cause a NullPointerException. This is
  195. an intentional indicator that you must set required fields.
  196. NOTE
  197. optional_field_style=accessors or reftypes cannot be used together with
  198. java_nano_generate_has=true. If you need the 'has' flag for any
  199. required field (you have no reason to), you can only use
  200. java_nano_generate_has=true.
  201. enum_style={c,java} (default: c)
  202. Defines where to put the int constants generated from enum members.
  203. * c *
  204. Use C-style, so the enum constants are available at the scope where
  205. the enum is defined. A file-scope enum's members are referenced like
  206. 'FileOuterClass.ENUM_VALUE'; a message-scope enum's members are
  207. referenced as 'Message.ENUM_VALUE'. The enum name is unavailable.
  208. This complies with the Micro code generator's behavior.
  209. * java *
  210. Use Java-style, so the enum constants are available under the enum
  211. name and referenced like 'EnumName.ENUM_VALUE' (they are still int
  212. constants). The enum name becomes the name of a public interface, at
  213. the scope where the enum is defined. If the enum is file-scope and
  214. the java_multiple_files option is on, the interface will be defined
  215. in its own file. To reduce code size, this interface should not be
  216. implemented and ProGuard shrinking should be used, so after the Java
  217. compiler inlines all referenced enum constants into the call sites,
  218. the interface remains unused and can be removed by ProGuard.
  219. ignore_services={true,false} (default: false)
  220. Skips services definitions.
  221. Nano doesn't support services. By default, if a service is defined
  222. it will generate a compilation error. If this flag is set to true,
  223. services will be silently ignored, instead.
  224. parcelable_messages={true,false} (default: false)
  225. Android-specific option to generate Parcelable messages.
  226. To use nano protobufs within the Android repo:
  227. - Set 'LOCAL_PROTOC_OPTIMIZE_TYPE := nano' in your local .mk file.
  228. When building a Java library or an app (package) target, the build
  229. system will add the Java nano runtime library to the
  230. LOCAL_STATIC_JAVA_LIBRARIES variable, so you don't need to.
  231. - Set 'LOCAL_PROTO_JAVA_OUTPUT_PARAMS := ...' in your local .mk file
  232. for any command-line options you need. Use commas to join multiple
  233. options. In the nano flavor only, whitespace surrounding the option
  234. names and values are ignored, so you can use backslash-newline or
  235. '+=' to structure your make files nicely.
  236. - The options will be applied to *all* proto files in LOCAL_SRC_FILES
  237. when you build a Java library or package. In case different options
  238. are needed for different proto files, build separate Java libraries
  239. and reference them in your main target. Note: you should make sure
  240. that, for each separate target, all proto files imported from any
  241. proto file in LOCAL_SRC_FILES are included in LOCAL_SRC_FILES. This
  242. is because the generator has to assume that the imported files are
  243. built using the same options, and will generate code that reference
  244. the fields and enums from the imported files using the same code
  245. style.
  246. - Hint: 'include $(CLEAR_VARS)' resets all LOCAL_ variables, including
  247. the two above.
  248. To use nano protobufs outside of Android repo:
  249. - Link with the generated jar file
  250. <protobuf-root>java/target/protobuf-java-2.3.0-nano.jar.
  251. - Invoke with --javanano_out, e.g.:
  252. ./protoc '--javanano_out=\
  253. java_package=src/proto/simple-data.proto|my_package,\
  254. java_outer_classname=src/proto/simple-data.proto|OuterName\
  255. :.' src/proto/simple-data.proto
  256. Contributing to nano:
  257. Please add/edit tests in NanoTest.java.
  258. Please run the following steps to test:
  259. - cd external/protobuf
  260. - ./configure
  261. - Run "make -j12 check" and verify all tests pass.
  262. - cd java
  263. - Run "mvn test" and verify all tests pass.
  264. - cd ../../..
  265. - . build/envsetup.sh
  266. - lunch 1
  267. - "make -j12 aprotoc libprotobuf-java-2.3.0-nano aprotoc-test-nano-params NanoAndroidTest" and
  268. check for build errors.
  269. - Plug in an Android device or start an emulator.
  270. - adb install -r out/target/product/generic/data/app/NanoAndroidTest.apk
  271. - Run:
  272. "adb shell am instrument -w com.google.protobuf.nano.test/android.test.InstrumentationTestRunner"
  273. and verify all tests pass.
  274. - repo sync -c -j256
  275. - "make -j12" and check for build errors
  276. Usage
  277. =====
  278. The complete documentation for Protocol Buffers is available via the
  279. web at:
  280. https://developers.google.com/protocol-buffers/