README.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. Protocol Buffers - Google's data interchange format
  2. Copyright 2008 Google Inc.
  3. This directory contains the Java Protocol Buffers 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 - 'Lite' Version - With Maven
  27. ==========================================
  28. Building the 'lite' version of the Java Protocol Buffers library is
  29. the same as building the full version, except that all commands are
  30. run using the 'lite' profile. (see
  31. http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
  32. E.g. to install the lite version of the jar, you would run:
  33. $ mvn install -P lite
  34. The resulting artifact has the 'lite' classifier. To reference it
  35. for dependency resolution, you would specify it as:
  36. <dependency>
  37. <groupId>com.google.protobuf</groupId>
  38. <artifactId>protobuf-java</artifactId>
  39. <version>${version}</version>
  40. <classifier>lite</classifier>
  41. </dependency>
  42. Installation - Without Maven
  43. ============================
  44. If you would rather not install Maven to build the library, you may
  45. follow these instructions instead. Note that these instructions skip
  46. running unit tests.
  47. 1) Build the C++ code, or obtain a binary distribution of protoc. If
  48. you install a binary distribution, make sure that it is the same
  49. version as this package. If in doubt, run:
  50. $ protoc --version
  51. If you built the C++ code without installing, the compiler binary
  52. should be located in ../src.
  53. 2) Invoke protoc to build DescriptorProtos.java:
  54. $ protoc --java_out=src/main/java -I../src \
  55. ../src/google/protobuf/descriptor.proto
  56. 3) Compile the code in src/main/java using whatever means you prefer.
  57. 4) Install the classes wherever you prefer.
  58. Micro version
  59. ============================
  60. The runtime and generated code for MICRO_RUNTIME is smaller
  61. because it does not include support for the descriptor and
  62. reflection, and enums are generated as integer constants in
  63. the parent message or the file's outer class, with no
  64. protection against invalid values set to enum fields. Also,
  65. not currently supported are packed repeated elements or
  66. extensions.
  67. To create a jar file for the runtime and run tests invoke
  68. "mvn package -P micro" from the <protobuf-root>/java
  69. directory. The generated jar file is
  70. <protobuf-root>java/target/protobuf-java-2.2.0-micro.jar.
  71. If you wish to compile the MICRO_RUNTIME your self, place
  72. the 7 files below, in <root>/com/google/protobuf and
  73. create a jar file for use with your code and the generated
  74. code:
  75. ByteStringMicro.java
  76. CodedInputStreamMicro.java
  77. CodedOutputStreamMicro.java
  78. InvalidProtocolBufferException.java
  79. MessageMicro.java
  80. WireFormatMicro.java
  81. If you wish to change on the code generator it is located
  82. in /src/google/protobuf/compiler/javamicro.
  83. To generate code for the MICRO_RUNTIME invoke protoc with
  84. --javamicro_out command line parameter. javamicro_out takes
  85. a series of optional sub-parameters separated by commas
  86. and a final parameter, with a colon separator, which defines
  87. the source directory. Sub-parameters begin with a name
  88. followed by an equal and if that sub-parameter has multiple
  89. parameters they are seperated by "|". The command line options
  90. are:
  91. opt -> speed or space
  92. java_use_vector -> true or false
  93. java_package -> <file-name>|<package-name>
  94. java_outer_classname -> <file-name>|<package-name>
  95. java_multiple_files -> true or false
  96. opt={speed,space} (default: space)
  97. This changes the code generation to optimize for speed or
  98. space. When opt=speed this changes the code generation
  99. for strings so that multiple conversions to Utf8 are
  100. eliminated.
  101. java_use_vector={true,false} (default: false)
  102. This specifies the collection class for repeated elements.
  103. If false, repeated elements use java.util.ArrayList<> and
  104. the code must be compiled with Java 1.5 or above. If true,
  105. repeated elements use java.util.Vector and the code can
  106. be compiled with Java 1.3 or above. The 'source'
  107. parameter of 'javac' may be used to control the version
  108. of the source: "javac -source 1.3". You can also change
  109. the <source> xml element for the maven-compiler-plugin.
  110. Below is for 1.5 sources:
  111. <plugin>
  112. <artifactId>maven-compiler-plugin</artifactId>
  113. <configuration>
  114. <source>1.5</source>
  115. <target>1.5</target>
  116. </configuration>
  117. </plugin>
  118. And below would be for 1.3 sources (note when changing
  119. to 1.3 you must also set java_use_vector=true):
  120. <plugin>
  121. <artifactId>maven-compiler-plugin</artifactId>
  122. <configuration>
  123. <source>1.3</source>
  124. <target>1.5</target>
  125. </configuration>
  126. </plugin>
  127. java_package=<file-name>|<package-name> (no default)
  128. This allows overriding the 'java_package' option value
  129. for the given file from the command line. Use multiple
  130. java_package options to override the option for multiple
  131. files. The final Java package for each file is the value
  132. of this command line option if present, or the value of
  133. the same option defined in the file if present, or the
  134. proto package if present, or the default Java package.
  135. java_outer_classname=<file-name>|<outer-classname> (no default)
  136. This allows overriding the 'java_outer_classname' option
  137. for the given file from the command line. Use multiple
  138. java_outer_classname options to override the option for
  139. multiple files. The final Java outer class name for each
  140. file is the value of this command line option if present,
  141. or the value of the same option defined in the file if
  142. present, or the file name converted to CamelCase. This
  143. outer class will nest all classes and integer constants
  144. generated from file-scope messages and enums.
  145. java_multiple_files={true,false} (no default)
  146. This allows overriding the 'java_multiple_files' option
  147. in all source files and their imported files from the
  148. command line. The final value of this option for each
  149. file is the value defined in this command line option, or
  150. the value of the same option defined in the file if
  151. present, or false. This specifies whether to generate
  152. package-level classes for the file-scope messages in the
  153. same Java package as the outer class (instead of nested
  154. classes in the outer class). File-scope enum constants
  155. are still generated as integer constants in the outer
  156. class. This affects the fully qualified references in the
  157. Java code. NOTE: because the command line option
  158. overrides the value for all files and their imported
  159. files, using this option inconsistently may result in
  160. incorrect references to the imported messages and enum
  161. constants.
  162. IMPORTANT: change of javamicro_out behavior:
  163. In previous versions, if the outer class name has not been
  164. given explicitly, javamicro_out would not infer the outer
  165. class name from the file name, and would skip the outer
  166. class generation. This makes the compilation succeed only
  167. if the source file contains a single message and no enums,
  168. and the generated class for that message is placed at the
  169. package level. To re-align with java_out, javamicro_out
  170. will now always generate the outer class, inferring its
  171. name from the file name if not given, as a container of the
  172. message classes and enum constants. To keep any existing
  173. single-message source file from causing the generation of
  174. an unwanted outer class, you can set the option
  175. java_multiple_files to true, either in the file or as a
  176. command line option.
  177. Below are a series of examples for clarification of the
  178. various parameters and options. Assuming this file:
  179. src/proto/simple-data-protos.proto:
  180. package testprotobuf;
  181. message SimpleData {
  182. optional fixed64 id = 1;
  183. optional string description = 2;
  184. optional bool ok = 3 [default = false];
  185. };
  186. and the compiled protoc in the current working directory,
  187. then a simple command line to compile this file would be:
  188. ./protoc --javamicro_out=. src/proto/simple-data-protos.proto
  189. This will create testprotobuf/SimpleDataProtos.java, which
  190. has the following content (extremely simplified):
  191. package testprotobuf;
  192. public final class SimpleDataProtos {
  193. public static final class SimpleData
  194. extends MessageMicro {
  195. ...
  196. }
  197. }
  198. The message SimpleData is compiled into the SimpleData
  199. class, nested in the file's outer class SimpleDataProtos,
  200. whose name is implicitly defined by the proto file name
  201. "simple-data-protos".
  202. The directory, aka Java package, testprotobuf is created
  203. because on line 1 of simple-data-protos.proto is
  204. "package testprotobuf;". If you wanted a different
  205. package name you could use the java_package option in the
  206. file:
  207. option java_package = "my_package";
  208. or in command line sub-parameter:
  209. ./protoc '--javamicro_out=\
  210. java_package=src/proto/simple-data-protos.proto|my_package:\
  211. .' src/proto/simple-data-protos.proto
  212. Here you see the new java_package sub-parameter which
  213. itself needs two parameters the file name and the
  214. package name, these are separated by "|". The value set
  215. in the command line overrides the value set in the file.
  216. Now you'll find SimpleDataProtos.java in the my_package/
  217. directory.
  218. If you wanted to also change the optimization for
  219. speed you'd add opt=speed with the comma seperator
  220. as follows:
  221. ./protoc '--javamicro_out=\
  222. opt=speed,\
  223. java_package=src/proto/simple-data-protos.proto|my_package:
  224. .' src/proto/simple-data-protos.proto
  225. If you also wanted a different outer class name you'd
  226. do the following:
  227. ./protoc '--javamicro_out=\
  228. opt=speed,\
  229. java_package=src/proto/simple-data-protos.proto|my_package,\
  230. java_outer_classname=src/proto/simple-data-protos.proto|OuterName:\
  231. .' src/proto/simple-data-protos.proto
  232. Now you'll find my_package/OuterName.java and the
  233. message class SimpleData nested in it.
  234. As mentioned java_package, java_outer_classname and
  235. java_multiple_files may also be specified in the file.
  236. In the example below we must define
  237. java_outer_classname because otherwise the outer class
  238. and one of the message classes will have the same name,
  239. which is forbidden to prevent name ambiguity:
  240. src/proto/sample-message.proto:
  241. package testmicroruntime;
  242. option java_package = "com.example";
  243. option java_outer_classname = "SampleMessageProtos";
  244. enum MessageType {
  245. SAMPLE = 1;
  246. EXAMPLE = 2;
  247. }
  248. message SampleMessage {
  249. required int32 id = 1;
  250. required MessageType type = 2;
  251. }
  252. message SampleMessageContainer {
  253. required SampleMessage message = 1;
  254. }
  255. This could be compiled using:
  256. ./protoc --javamicro_out=. src/proto/sample-message.proto
  257. and the output will be:
  258. com/example/SampleMessageProtos.java:
  259. package com.example;
  260. public final class SampleMessageProtos {
  261. public static final int SAMPLE = 1;
  262. public static final int EXAMPLE = 2;
  263. public static final class SampleMessage
  264. extends MessageMicro {
  265. ...
  266. }
  267. public static final class SampleMessageContainer
  268. extends MessageMicro {
  269. ...
  270. }
  271. }
  272. As you can see the file-scope enum MessageType is
  273. disassembled into two integer constants in the outer class.
  274. In javamicro_out, all enums are disassembled and compiled
  275. into integer constants in the parent scope (the containing
  276. message's class or the file's (i.e. outer) class).
  277. You may prefer the file-scope messages to be saved in
  278. separate files. You can do this by setting the option
  279. java_multiple_files to true, in either the file like this:
  280. option java_multiple_files = true;
  281. or the command line like this:
  282. ./protoc --javamicro_out=\
  283. java_multiple_files=true:\
  284. . src/proto/sample-message.proto
  285. The java_multiple_files option causes javamicro to use a
  286. separate file for each file-scope message, which resides
  287. directly in the Java package alongside the outer class:
  288. com/example/SampleMessageProtos.java:
  289. package com.example;
  290. public final class SampleMessageProtos {
  291. public static final int SAMPLE = 1;
  292. public static final int EXAMPLE = 2;
  293. }
  294. com/example/SampleMessage.java:
  295. package com.example;
  296. public final class SampleMessage
  297. extends MessageMicro {
  298. ...
  299. }
  300. com/example/SampleMessageContainer.java:
  301. package com.example;
  302. public final class SampleMessageContainer
  303. extends MessageMicro {
  304. ...
  305. }
  306. As you can see, the outer class now contains only the
  307. integer constants, generated from the file-scope enum
  308. "MessageType". Please note that message-scope enums are
  309. still generated as integer constants in the message class.
  310. Nano version
  311. ============================
  312. Nano is even smaller than micro, especially in the number of generated
  313. functions. It is like micro:
  314. - No support for descriptors and reflection;
  315. - Enum constants are integers with no protection against invalid
  316. values set to enum fields.
  317. Except:
  318. - Setter/getter/hazzer/clearer functions are opt-in.
  319. - If not opted in, has state is not available. Serialization outputs
  320. all fields not equal to their default. (See important implications
  321. below.)
  322. - Enum constants can be generated into container interfaces bearing
  323. the enum's name (so the referencing code is in Java style).
  324. - CodedInputStreamMicro is renamed to CodedInputByteBufferNano and can
  325. only take byte[] (not InputStream).
  326. - Similar rename from CodedOutputStreamMicro to
  327. CodedOutputByteBufferNano.
  328. - Repeated fields are in arrays, not ArrayList or Vector.
  329. - Unset messages/groups are null, not an immutable empty default
  330. instance.
  331. - Required fields are always serialized.
  332. - toByteArray(...) and mergeFrom(...) are now static functions of
  333. MessageNano.
  334. - "bytes" are of java type byte[].
  335. IMPORTANT: If you have fields with defaults and opt out of accessors
  336. How fields with defaults are serialized has changed. Because we don't
  337. keep "has" state, any field equal to its default is assumed to be not
  338. set and therefore is not serialized. Consider the situation where we
  339. change the default value of a field. Senders compiled against an older
  340. version of the proto continue to match against the old default, and
  341. don't send values to the receiver even though the receiver assumes the
  342. new default value. Therefore, think carefully about the implications
  343. of changing the default value. Alternatively, turn on accessors and
  344. enjoy the benefit of the explicit has() checks.
  345. IMPORTANT: If you have "bytes" fields with non-empty defaults
  346. Because the byte buffer is now of mutable type byte[], the default
  347. static final cannot be exposed through a public field. Each time a
  348. message's constructor or clear() function is called, the default value
  349. (kept in a private byte[]) is cloned. This causes a small memory
  350. penalty. This is not a problem if the field has no default or is an
  351. empty default.
  352. Nano Generator options
  353. java_package -> <file-name>|<package-name>
  354. java_outer_classname -> <file-name>|<package-name>
  355. java_multiple_files -> true or false
  356. java_nano_generate_has -> true or false [DEPRECATED]
  357. optional_field_style -> default or accessors
  358. enum_style -> c or java
  359. java_package:
  360. java_outer_classname:
  361. java_multiple_files:
  362. Same as Micro version.
  363. java_nano_generate_has={true,false} (default: false)
  364. DEPRECATED. Use optional_field_style=accessors.
  365. If true, generates a public boolean variable has<fieldname>
  366. accompanying each optional or required field (not present for
  367. repeated fields, groups or messages). It is set to false initially
  368. and upon clear(). If parseFrom(...) reads the field from the wire,
  369. it is set to true. This is a way for clients to inspect the "has"
  370. value upon parse. If it is set to true, writeTo(...) will ALWAYS
  371. output that field (even if field value is equal to its
  372. default).
  373. IMPORTANT: This option costs an extra 4 bytes per primitive field in
  374. the message. Think carefully about whether you really need this. In
  375. many cases reading the default works and determining whether the
  376. field was received over the wire is irrelevant.
  377. optional_field_style={default,accessors,reftypes} (default: default)
  378. Defines the style of the generated code for fields.
  379. * default *
  380. In the default style, optional fields translate into public mutable
  381. Java fields, and the serialization process is as discussed in the
  382. "IMPORTANT" section above.
  383. * accessors *
  384. When set to 'accessors', each optional field is encapsulated behind
  385. 4 accessors, namely get<fieldname>(), set<fieldname>(), has<fieldname>()
  386. and clear<fieldname>() methods, with the standard semantics. The hazzer's
  387. return value determines whether a field is serialized, so this style is
  388. useful when you need to serialize a field with the default value, or check
  389. if a field has been explicitly set to its default value from the wire.
  390. In the 'accessors' style, required fields are still translated to one
  391. public mutable Java field each, and repeated fields are still translated
  392. to arrays. No accessors are generated for them.
  393. IMPORTANT: When using the 'accessors' style, ProGuard should always
  394. be enabled with optimization (don't use -dontoptimize) and allowing
  395. access modification (use -allowaccessmodification). This removes the
  396. unused accessors and maybe inline the rest at the call sites,
  397. reducing the final code size.
  398. TODO(maxtroy): find ProGuard config that would work the best.
  399. * reftypes *
  400. When set to 'reftypes', each proto field is generated as a public Java
  401. field. For primitive types, these fields use the Java reference types
  402. such as java.lang.Integer instead of primitive types such as int.
  403. In the 'reftypes' style, fields are initialized to null (or empty
  404. arrays for repeated fields), and their default values are not available.
  405. They are serialized over the wire based on equality to null.
  406. The 'reftypes' mode has some additional cost due to autoboxing and usage
  407. of reference types. In practice, many boxed types are cached, and so don't
  408. result in object creation. However, references do take slightly more memory
  409. than primitives.
  410. The 'reftypes' mode is useful when you want to be able to serialize fields
  411. with default values, or check if a field has been explicitly set to the
  412. default over the wire without paying the extra method cost of the
  413. 'accessors' mode.
  414. Note that if you attempt to write null to a required field in the reftypes
  415. mode, serialization of the proto will cause a NullPointerException. This is
  416. an intentional indicator that you must set required fields.
  417. NOTE
  418. optional_field_style=accessors or reftypes cannot be used together with
  419. java_nano_generate_has=true. If you need the 'has' flag for any
  420. required field (you have no reason to), you can only use
  421. java_nano_generate_has=true.
  422. enum_style={c,java} (default: c)
  423. Defines where to put the int constants generated from enum members.
  424. * c *
  425. Use C-style, so the enum constants are available at the scope where
  426. the enum is defined. A file-scope enum's members are referenced like
  427. 'FileOuterClass.ENUM_VALUE'; a message-scope enum's members are
  428. referenced as 'Message.ENUM_VALUE'. The enum name is unavailable.
  429. This complies with the Micro code generator's behavior.
  430. * java *
  431. Use Java-style, so the enum constants are available under the enum
  432. name and referenced like 'EnumName.ENUM_VALUE' (they are still int
  433. constants). The enum name becomes the name of a public interface, at
  434. the scope where the enum is defined. If the enum is file-scope and
  435. the java_multiple_files option is on, the interface will be defined
  436. in its own file. To reduce code size, this interface should not be
  437. implemented and ProGuard shrinking should be used, so after the Java
  438. compiler inlines all referenced enum constants into the call sites,
  439. the interface remains unused and can be removed by ProGuard.
  440. To use nano protobufs within the Android repo:
  441. - Set 'LOCAL_PROTOC_OPTIMIZE_TYPE := nano' in your local .mk file.
  442. When building a Java library or an app (package) target, the build
  443. system will add the Java nano runtime library to the
  444. LOCAL_STATIC_JAVA_LIBRARIES variable, so you don't need to.
  445. - Set 'LOCAL_PROTO_JAVA_OUTPUT_PARAMS := ...' in your local .mk file
  446. for any command-line options you need. Use commas to join multiple
  447. options. Write all options on the same line; avoid backslash-newline
  448. or '+=', because they will introduce spaces in the middle of your
  449. options and the generator is not prepared to handle them.
  450. - The options will be applied to *all* proto files in LOCAL_SRC_FILES
  451. when you build a Java library or package. In case different options
  452. are needed for different proto files, build separate Java libraries
  453. and reference them in your main target. Note: you should make sure
  454. that, for each separate target, all proto files imported from any
  455. proto file in LOCAL_SRC_FILES are included in LOCAL_SRC_FILES. This
  456. is because the generator has to assume that the imported files are
  457. built using the same options, and will generate code that reference
  458. the fields and enums from the imported files using the same code
  459. style.
  460. - Hint: 'include $(CLEAR_VARS)' resets all LOCAL_ variables, including
  461. the two above.
  462. To use nano protobufs outside of Android repo:
  463. - Link with the generated jar file
  464. <protobuf-root>java/target/protobuf-java-2.3.0-nano.jar.
  465. - Invoke with --javanano_out, e.g.:
  466. ./protoc '--javanano_out=\
  467. java_package=src/proto/simple-data.proto|my_package,\
  468. java_outer_classname=src/proto/simple-data.proto|OuterName:\
  469. .' src/proto/simple-data.proto
  470. Contributing to nano:
  471. Please add/edit tests in NanoTest.java.
  472. Please run the following steps to test:
  473. - cd external/protobuf
  474. - ./configure
  475. - Run "make -j12 check" and verify all tests pass.
  476. - cd java
  477. - Run "mvn test" and verify all tests pass.
  478. - cd ../../..
  479. - . build/envsetup.sh
  480. - lunch 1
  481. - "make -j12 aprotoc libprotobuf-java-2.3.0-nano aprotoc-test-nano-params" and
  482. check for build errors.
  483. - repo sync -c -j256
  484. - "make -j12" and check for build errors
  485. Usage
  486. =====
  487. The complete documentation for Protocol Buffers is available via the
  488. web at:
  489. http://code.google.com/apis/protocolbuffers/