1Protocol Buffers - Google's data interchange format
2Copyright 2008 Google Inc.
3
4This directory contains the Java Protocol Buffers runtime library.
5
6Installation - With Maven
7=========================
8
9The Protocol Buffers build is managed using Maven.  If you would
10rather build without Maven, see below.
11
121) Install Apache Maven if you don't have it:
13
14     http://maven.apache.org/
15
162) Build the C++ code, or obtain a binary distribution of protoc.  If
17   you install a binary distribution, make sure that it is the same
18   version as this package.  If in doubt, run:
19
20     $ protoc --version
21
22   You will need to place the protoc executable in ../src.  (If you
23   built it yourself, it should already be there.)
24
253) Run the tests:
26
27     $ mvn test
28
29   If some tests fail, this library may not work correctly on your
30   system.  Continue at your own risk.
31
324) Install the library into your Maven repository:
33
34     $ mvn install
35
365) If you do not use Maven to manage your own build, you can build a
37   .jar file to use:
38
39     $ mvn package
40
41   The .jar will be placed in the "target" directory.
42
43Installation - 'Lite' Version - With Maven
44==========================================
45
46Building the 'lite' version of the Java Protocol Buffers library is
47the same as building the full version, except that all commands are
48run using the 'lite' profile.  (see
49http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
50
51E.g. to install the lite version of the jar, you would run:
52
53  $ mvn install -P lite
54
55The resulting artifact has the 'lite' classifier.  To reference it
56for dependency resolution, you would specify it as:
57
58  <dependency>
59    <groupId>com.google.protobuf</groupId>
60    <artifactId>protobuf-java</artifactId>
61    <version>${version}</version>
62    <classifier>lite</classifier>
63  </dependency>
64
65Installation - Without Maven
66============================
67
68If you would rather not install Maven to build the library, you may
69follow these instructions instead.  Note that these instructions skip
70running unit tests.
71
721) Build the C++ code, or obtain a binary distribution of protoc.  If
73   you install a binary distribution, make sure that it is the same
74   version as this package.  If in doubt, run:
75
76     $ protoc --version
77
78   If you built the C++ code without installing, the compiler binary
79   should be located in ../src.
80
812) Invoke protoc to build DescriptorProtos.java:
82
83     $ protoc --java_out=src/main/java -I../src \
84         ../src/google/protobuf/descriptor.proto
85
863) Compile the code in src/main/java using whatever means you prefer.
87
884) Install the classes wherever you prefer.
89
90Micro version
91============================
92
93The runtime and generated code for MICRO_RUNTIME is smaller
94because it does not include support for the descriptor and
95reflection, and enums are generated as integer constants in
96the parent message or the file's outer class, with no
97protection against invalid values set to enum fields. Also,
98not currently supported are packed repeated elements or
99extensions.
100
101To create a jar file for the runtime and run tests invoke
102"mvn package -P micro" from the <protobuf-root>/java
103directory. The generated jar file is
104<protobuf-root>java/target/protobuf-java-2.6.0-micro.jar.
105
106If you wish to compile the MICRO_RUNTIME your self, place
107the 7 files below, in <root>/com/google/protobuf and
108create a jar file for use with your code and the generated
109code:
110
111ByteStringMicro.java
112CodedInputStreamMicro.java
113CodedOutputStreamMicro.java
114InvalidProtocolBufferException.java
115MessageMicro.java
116WireFormatMicro.java
117
118If you wish to change on the code generator it is located
119in /src/google/protobuf/compiler/javamicro.
120
121To generate code for the MICRO_RUNTIME invoke protoc with
122--javamicro_out command line parameter. javamicro_out takes
123a series of optional sub-parameters separated by commas
124and a final parameter, with a colon separator, which defines
125the source directory. Sub-parameters begin with a name
126followed by an equal and if that sub-parameter has multiple
127parameters they are seperated by "|". The command line options
128are:
129
130opt                  -> speed or space
131java_use_vector      -> true or false
132java_package         -> <file-name>|<package-name>
133java_outer_classname -> <file-name>|<package-name>
134java_multiple_files  -> true or false
135
136opt={speed,space} (default: space)
137  This changes the code generation to optimize for speed or
138  space. When opt=speed this changes the code generation
139  for strings so that multiple conversions to Utf8 are
140  eliminated.
141
142java_use_vector={true,false} (default: false)
143  This specifies the collection class for repeated elements.
144  If false, repeated elements use java.util.ArrayList<> and
145  the code must be compiled with Java 1.5 or above. If true,
146  repeated elements use java.util.Vector and the code can
147  be compiled with Java 1.3 or above. The 'source'
148  parameter of 'javac' may be used to control the version
149  of the source: "javac -source 1.3". You can also change
150  the <source> xml element for the maven-compiler-plugin.
151  Below is for 1.5 sources:
152
153      <plugin>
154        <artifactId>maven-compiler-plugin</artifactId>
155        <configuration>
156          <source>1.5</source>
157          <target>1.5</target>
158        </configuration>
159      </plugin>
160
161  And below would be for 1.3 sources (note when changing
162  to 1.3 you must also set java_use_vector=true):
163
164      <plugin>
165        <artifactId>maven-compiler-plugin</artifactId>
166        <configuration>
167          <source>1.3</source>
168          <target>1.5</target>
169        </configuration>
170      </plugin>
171
172java_package=<file-name>|<package-name> (no default)
173  This allows overriding the 'java_package' option value
174  for the given file from the command line. Use multiple
175  java_package options to override the option for multiple
176  files. The final Java package for each file is the value
177  of this command line option if present, or the value of
178  the same option defined in the file if present, or the
179  proto package if present, or the default Java package.
180
181java_outer_classname=<file-name>|<outer-classname> (no default)
182  This allows overriding the 'java_outer_classname' option
183  for the given file from the command line. Use multiple
184  java_outer_classname options to override the option for
185  multiple files. The final Java outer class name for each
186  file is the value of this command line option if present,
187  or the value of the same option defined in the file if
188  present, or the file name converted to CamelCase. This
189  outer class will nest all classes and integer constants
190  generated from file-scope messages and enums.
191
192java_multiple_files={true,false} (no default)
193  This allows overriding the 'java_multiple_files' option
194  in all source files and their imported files from the
195  command line. The final value of this option for each
196  file is the value defined in this command line option, or
197  the value of the same option defined in the file if
198  present, or false. This specifies whether to generate
199  package-level classes for the file-scope messages in the
200  same Java package as the outer class (instead of nested
201  classes in the outer class). File-scope enum constants
202  are still generated as integer constants in the outer
203  class. This affects the fully qualified references in the
204  Java code. NOTE: because the command line option
205  overrides the value for all files and their imported
206  files, using this option inconsistently may result in
207  incorrect references to the imported messages and enum
208  constants.
209
210
211IMPORTANT: change of javamicro_out behavior:
212
213In previous versions, if the outer class name has not been
214given explicitly, javamicro_out would not infer the outer
215class name from the file name, and would skip the outer
216class generation. This makes the compilation succeed only
217if the source file contains a single message and no enums,
218and the generated class for that message is placed at the
219package level. To re-align with java_out, javamicro_out
220will now always generate the outer class, inferring its
221name from the file name if not given, as a container of the
222message classes and enum constants. To keep any existing
223single-message source file from causing the generation of
224an unwanted outer class, you can set the option
225java_multiple_files to true, either in the file or as a
226command line option.
227
228
229Below are a series of examples for clarification of the
230various parameters and options. Assuming this file:
231
232src/proto/simple-data-protos.proto:
233
234    package testprotobuf;
235
236    message SimpleData {
237      optional fixed64 id = 1;
238      optional string description = 2;
239      optional bool ok = 3 [default = false];
240    };
241
242and the compiled protoc in the current working directory,
243then a simple command line to compile this file would be:
244
245./protoc --javamicro_out=. src/proto/simple-data-protos.proto
246
247This will create testprotobuf/SimpleDataProtos.java, which
248has the following content (extremely simplified):
249
250    package testprotobuf;
251
252    public final class SimpleDataProtos {
253      public static final class SimpleData
254          extends MessageMicro {
255        ...
256      }
257    }
258
259The message SimpleData is compiled into the SimpleData
260class, nested in the file's outer class SimpleDataProtos,
261whose name is implicitly defined by the proto file name
262"simple-data-protos".
263
264The directory, aka Java package, testprotobuf is created
265because on line 1 of simple-data-protos.proto is
266"package testprotobuf;". If you wanted a different
267package name you could use the java_package option in the
268file:
269
270    option java_package = "my_package";
271
272or in command line sub-parameter:
273
274./protoc '--javamicro_out=\
275java_package=src/proto/simple-data-protos.proto|my_package:\
276.' src/proto/simple-data-protos.proto
277
278Here you see the new java_package sub-parameter which
279itself needs two parameters the file name and the
280package name, these are separated by "|". The value set
281in the command line overrides the value set in the file.
282Now you'll find SimpleDataProtos.java in the my_package/
283directory.
284
285If you wanted to also change the optimization for
286speed you'd add opt=speed with the comma seperator
287as follows:
288
289./protoc '--javamicro_out=\
290opt=speed,\
291java_package=src/proto/simple-data-protos.proto|my_package:
292.' src/proto/simple-data-protos.proto
293
294If you also wanted a different outer class name you'd
295do the following:
296
297./protoc '--javamicro_out=\
298opt=speed,\
299java_package=src/proto/simple-data-protos.proto|my_package,\
300java_outer_classname=src/proto/simple-data-protos.proto|OuterName:\
301.' src/proto/simple-data-protos.proto
302
303Now you'll find my_package/OuterName.java and the
304message class SimpleData nested in it.
305
306As mentioned java_package, java_outer_classname and
307java_multiple_files may also be specified in the file.
308In the example below we must define
309java_outer_classname because otherwise the outer class
310and one of the message classes will have the same name,
311which is forbidden to prevent name ambiguity:
312
313src/proto/sample-message.proto:
314
315    package testmicroruntime;
316
317    option java_package = "com.example";
318    option java_outer_classname = "SampleMessageProtos";
319
320    enum MessageType {
321      SAMPLE = 1;
322      EXAMPLE = 2;
323    }
324
325    message SampleMessage {
326      required int32 id = 1;
327      required MessageType type = 2;
328    }
329
330    message SampleMessageContainer {
331      required SampleMessage message = 1;
332    }
333
334This could be compiled using:
335
336./protoc --javamicro_out=. src/proto/sample-message.proto
337
338and the output will be:
339
340com/example/SampleMessageProtos.java:
341
342    package com.example;
343
344    public final class SampleMessageProtos {
345      public static final int SAMPLE = 1;
346      public static final int EXAMPLE = 2;
347      public static final class SampleMessage
348          extends MessageMicro {
349        ...
350      }
351      public static final class SampleMessageContainer
352          extends MessageMicro {
353        ...
354      }
355    }
356
357As you can see the file-scope enum MessageType is
358disassembled into two integer constants in the outer class.
359In javamicro_out, all enums are disassembled and compiled
360into integer constants in the parent scope (the containing
361message's class or the file's (i.e. outer) class).
362
363You may prefer the file-scope messages to be saved in
364separate files. You can do this by setting the option
365java_multiple_files to true, in either the file like this:
366
367    option java_multiple_files = true;
368
369or the command line like this:
370
371./protoc --javamicro_out=\
372java_multiple_files=true:\
373. src/proto/sample-message.proto
374
375The java_multiple_files option causes javamicro to use a
376separate file for each file-scope message, which resides
377directly in the Java package alongside the outer class:
378
379com/example/SampleMessageProtos.java:
380
381    package com.example;
382    public final class SampleMessageProtos {
383      public static final int SAMPLE = 1;
384      public static final int EXAMPLE = 2;
385    }
386
387com/example/SampleMessage.java:
388
389    package com.example;
390    public final class SampleMessage
391        extends MessageMicro {
392      ...
393    }
394
395com/example/SampleMessageContainer.java:
396
397    package com.example;
398    public final class SampleMessageContainer
399        extends MessageMicro {
400      ...
401    }
402
403As you can see, the outer class now contains only the
404integer constants, generated from the file-scope enum
405"MessageType". Please note that message-scope enums are
406still generated as integer constants in the message class.
407
408
409Nano version
410============================
411
412Nano is a special code generator and runtime library designed specially
413for Android, and is very resource-friendly in both the amount of code
414and the runtime overhead. An overview of Nano features:
415
416- No descriptors or message builders.
417- All messages are mutable; fields are public Java fields.
418- For optional fields only, encapsulation behind setter/getter/hazzer/
419  clearer functions is opt-in, which provide proper 'has' state support.
420- If not opted in, has state is not available. Serialization outputs
421  all fields not equal to their defaults (see important implications
422  below).
423- Required fields are always serialized.
424- Enum constants are integers; protection against invalid values only
425  when parsing from the wire.
426- Enum constants can be generated into container interfaces bearing
427  the enum's name (so the referencing code is in Java style).
428- CodedInputByteBufferNano can only take byte[] (not InputStream).
429- Similarly CodedOutputByteBufferNano can only write to byte[].
430- Repeated fields are in arrays, not ArrayList or Vector. Null array
431  elements are allowed and silently ignored.
432- Full support of serializing/deserializing repeated packed fields.
433- Support of extensions.
434- Unset messages/groups are null, not an immutable empty default
435  instance.
436- toByteArray(...) and mergeFrom(...) are now static functions of
437  MessageNano.
438- The 'bytes' type translates to the Java type byte[].
439
440The generated messages are not thread-safe for writes, but may be
441used simultaneously from multiple threads in a read-only manner.
442In other words, an appropriate synchronization mechanism (such as
443a ReadWriteLock) must be used to ensure that a message, its
444ancestors, and descendants are not accessed by any other threads
445while the message is being modified. Field reads, getter methods
446(but not getExtension(...)), toByteArray(...), writeTo(...),
447getCachedSize(), and getSerializedSize() are all considered read-only
448operations.
449
450IMPORTANT: If you have fields with defaults and opt out of accessors
451
452How fields with defaults are serialized has changed. Because we don't
453keep "has" state, any field equal to its default is assumed to be not
454set and therefore is not serialized. Consider the situation where we
455change the default value of a field. Senders compiled against an older
456version of the proto continue to match against the old default, and
457don't send values to the receiver even though the receiver assumes the
458new default value. Therefore, think carefully about the implications
459of changing the default value. Alternatively, turn on accessors and
460enjoy the benefit of the explicit has() checks.
461
462IMPORTANT: If you have "bytes" fields with non-empty defaults
463
464Because the byte buffer is now of mutable type byte[], the default
465static final cannot be exposed through a public field. Each time a
466message's constructor or clear() function is called, the default value
467(kept in a private byte[]) is cloned. This causes a small memory
468penalty. This is not a problem if the field has no default or is an
469empty default.
470
471Nano Generator options
472
473java_package           -> <file-name>|<package-name>
474java_outer_classname   -> <file-name>|<package-name>
475java_multiple_files    -> true or false
476java_nano_generate_has -> true or false [DEPRECATED]
477optional_field_style   -> default or accessors
478enum_style             -> c or java
479ignore_services        -> true or false
480parcelable_messages    -> true or false
481
482java_package:
483java_outer_classname:
484java_multiple_files:
485  Same as Micro version.
486
487java_nano_generate_has={true,false} (default: false)
488  DEPRECATED. Use optional_field_style=accessors.
489
490  If true, generates a public boolean variable has<fieldname>
491  accompanying each optional or required field (not present for
492  repeated fields, groups or messages). It is set to false initially
493  and upon clear(). If parseFrom(...) reads the field from the wire,
494  it is set to true. This is a way for clients to inspect the "has"
495  value upon parse. If it is set to true, writeTo(...) will ALWAYS
496  output that field (even if field value is equal to its
497  default).
498
499  IMPORTANT: This option costs an extra 4 bytes per primitive field in
500  the message. Think carefully about whether you really need this. In
501  many cases reading the default works and determining whether the
502  field was received over the wire is irrelevant.
503
504optional_field_style={default,accessors,reftypes} (default: default)
505  Defines the style of the generated code for fields.
506
507  * default *
508
509  In the default style, optional fields translate into public mutable
510  Java fields, and the serialization process is as discussed in the
511  "IMPORTANT" section above.
512
513  * accessors *
514
515  When set to 'accessors', each optional field is encapsulated behind
516  4 accessors, namely get<fieldname>(), set<fieldname>(), has<fieldname>()
517  and clear<fieldname>() methods, with the standard semantics. The hazzer's
518  return value determines whether a field is serialized, so this style is
519  useful when you need to serialize a field with the default value, or check
520  if a field has been explicitly set to its default value from the wire.
521
522  In the 'accessors' style, required and nested message fields are still
523  translated to one public mutable Java field each, repeated fields are still
524  translated to arrays. No accessors are generated for them.
525
526  IMPORTANT: When using the 'accessors' style, ProGuard should always
527  be enabled with optimization (don't use -dontoptimize) and allowing
528  access modification (use -allowaccessmodification). This removes the
529  unused accessors and maybe inline the rest at the call sites,
530  reducing the final code size.
531  TODO(maxtroy): find ProGuard config that would work the best.
532
533  * reftypes *
534
535  When set to 'reftypes', each proto field is generated as a public Java
536  field. For primitive types, these fields use the Java reference types
537  such as java.lang.Integer instead of primitive types such as int.
538
539  In the 'reftypes' style, fields are initialized to null (or empty
540  arrays for repeated fields), and their default values are not available.
541  They are serialized over the wire based on equality to null.
542
543  The 'reftypes' mode has some additional cost due to autoboxing and usage
544  of reference types. In practice, many boxed types are cached, and so don't
545  result in object creation. However, references do take slightly more memory
546  than primitives.
547
548  The 'reftypes' mode is useful when you want to be able to serialize fields
549  with default values, or check if a field has been explicitly set to the
550  default over the wire without paying the extra method cost of the
551  'accessors' mode.
552
553  Note that if you attempt to write null to a required field in the reftypes
554  mode, serialization of the proto will cause a NullPointerException. This is
555  an intentional indicator that you must set required fields.
556
557  NOTE
558  optional_field_style=accessors or reftypes cannot be used together with
559  java_nano_generate_has=true. If you need the 'has' flag for any
560  required field (you have no reason to), you can only use
561  java_nano_generate_has=true.
562
563enum_style={c,java} (default: c)
564  Defines where to put the int constants generated from enum members.
565
566  * c *
567
568  Use C-style, so the enum constants are available at the scope where
569  the enum is defined. A file-scope enum's members are referenced like
570  'FileOuterClass.ENUM_VALUE'; a message-scope enum's members are
571  referenced as 'Message.ENUM_VALUE'. The enum name is unavailable.
572  This complies with the Micro code generator's behavior.
573
574  * java *
575
576  Use Java-style, so the enum constants are available under the enum
577  name and referenced like 'EnumName.ENUM_VALUE' (they are still int
578  constants). The enum name becomes the name of a public interface, at
579  the scope where the enum is defined. If the enum is file-scope and
580  the java_multiple_files option is on, the interface will be defined
581  in its own file. To reduce code size, this interface should not be
582  implemented and ProGuard shrinking should be used, so after the Java
583  compiler inlines all referenced enum constants into the call sites,
584  the interface remains unused and can be removed by ProGuard.
585
586ignore_services={true,false} (default: false)
587  Skips services definitions.
588
589  Nano doesn't support services. By default, if a service is defined
590  it will generate a compilation error. If this flag is set to true,
591  services will be silently ignored, instead.
592
593parcelable_messages={true,false} (default: false)
594  Android-specific option to generate Parcelable messages.
595
596generate_intdefs={true,false} (default: false)
597  Android-specific option to generate @IntDef annotations for enums.
598
599  If turned on, an @IntDef annotation (a public @interface) will be
600  generated for each enum, and every integer parameter and return
601  value in the generated code meant for this enum will be annotated
602  with it. This interface is generated with the same name and at the
603  same place as the enum members' container interfaces described
604  above under enum_style=java, regardless of the enum_style option
605  used. When this is combined with enum_style=java, the interface
606  will be both the @IntDef annotation and the container of the enum
607  members; otherwise the interface has an empty body.
608
609  Your app must declare a compile-time dependency on the
610  android-support-annotations library.
611
612  For more information on how these @IntDef annotations help with
613  compile-time type safety, see:
614  https://sites.google.com/a/android.com/tools/tech-docs/support-annotations
615  and
616  https://developer.android.com/reference/android/support/annotation/IntDef.html
617
618
619To use nano protobufs within the Android repo:
620
621- Set 'LOCAL_PROTOC_OPTIMIZE_TYPE := nano' in your local .mk file.
622  When building a Java library or an app (package) target, the build
623  system will add the Java nano runtime library to the
624  LOCAL_STATIC_JAVA_LIBRARIES variable, so you don't need to.
625- Set 'LOCAL_PROTO_JAVA_OUTPUT_PARAMS := ...' in your local .mk file
626  for any command-line options you need. Use commas to join multiple
627  options. In the nano flavor only, whitespace surrounding the option
628  names and values are ignored, so you can use backslash-newline or
629  '+=' to structure your make files nicely.
630- The options will be applied to *all* proto files in LOCAL_SRC_FILES
631  when you build a Java library or package. In case different options
632  are needed for different proto files, build separate Java libraries
633  and reference them in your main target. Note: you should make sure
634  that, for each separate target, all proto files imported from any
635  proto file in LOCAL_SRC_FILES are included in LOCAL_SRC_FILES. This
636  is because the generator has to assume that the imported files are
637  built using the same options, and will generate code that reference
638  the fields and enums from the imported files using the same code
639  style.
640- Hint: 'include $(CLEAR_VARS)' resets all LOCAL_ variables, including
641  the two above.
642
643To use nano protobufs outside of Android repo:
644
645- Link with the generated jar file
646  <protobuf-root>java/target/protobuf-java-2.6.0-nano.jar.
647- Invoke with --javanano_out, e.g.:
648
649./protoc '--javanano_out=\
650    java_package=src/proto/simple-data.proto|my_package,\
651    java_outer_classname=src/proto/simple-data.proto|OuterName\
652  :.' src/proto/simple-data.proto
653
654Contributing to nano:
655
656Please add/edit tests in NanoTest.java.
657
658Please run the following steps to test:
659
660- cd external/protobuf
661- ./configure
662- Run "make -j12 check" and verify all tests pass.
663- cd java
664- Run "mvn test" and verify all tests pass.
665- cd ../../..
666- . build/envsetup.sh
667- lunch 1
668- "make -j12 aprotoc libprotobuf-java-nano aprotoc-test-nano-params NanoAndroidTest" and
669  check for build errors.
670- Plug in an Android device or start an emulator.
671- adb install -r out/target/product/generic/data/app/NanoAndroidTest/NanoAndroidTest.apk
672- Run:
673  "adb shell am instrument -w com.google.protobuf.nano.test/android.test.InstrumentationTestRunner"
674  and verify all tests pass.
675- repo sync -c -j256
676- "make -j12" and check for build errors
677
678
679Usage
680=====
681
682The complete documentation for Protocol Buffers is available via the
683web at:
684
685  https://developers.google.com/protocol-buffers/
686