xref: /aosp_15_r20/external/protobuf/javanano/README.md (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1*1b3f573fSAndroid Build Coastguard WorkerProtocol Buffers - Google's data interchange format
2*1b3f573fSAndroid Build Coastguard Worker===================================================
3*1b3f573fSAndroid Build Coastguard Worker
4*1b3f573fSAndroid Build Coastguard Worker[![Build Status](https://travis-ci.org/google/protobuf.svg?branch=master)](https://travis-ci.org/google/protobuf)
5*1b3f573fSAndroid Build Coastguard Worker
6*1b3f573fSAndroid Build Coastguard WorkerCopyright 2008 Google Inc.
7*1b3f573fSAndroid Build Coastguard Worker
8*1b3f573fSAndroid Build Coastguard WorkerThis directory contains the Java Protocol Buffers Nano runtime library.
9*1b3f573fSAndroid Build Coastguard Worker
10*1b3f573fSAndroid Build Coastguard Worker**Nano is no longer supported by protobuf team. We recommend Android users to
11*1b3f573fSAndroid Build Coastguard Workeruse protobuf lite runtime instead.**
12*1b3f573fSAndroid Build Coastguard Worker
13*1b3f573fSAndroid Build Coastguard WorkerInstallation - With Maven
14*1b3f573fSAndroid Build Coastguard Worker-------------------------
15*1b3f573fSAndroid Build Coastguard Worker
16*1b3f573fSAndroid Build Coastguard WorkerThe Protocol Buffers build is managed using Maven.  If you would
17*1b3f573fSAndroid Build Coastguard Workerrather build without Maven, see below.
18*1b3f573fSAndroid Build Coastguard Worker
19*1b3f573fSAndroid Build Coastguard Worker1) Install Apache Maven if you don't have it:
20*1b3f573fSAndroid Build Coastguard Worker
21*1b3f573fSAndroid Build Coastguard Worker     http://maven.apache.org/
22*1b3f573fSAndroid Build Coastguard Worker
23*1b3f573fSAndroid Build Coastguard Worker2) Build the C++ code, or obtain a binary distribution of protoc.  If
24*1b3f573fSAndroid Build Coastguard Worker   you install a binary distribution, make sure that it is the same
25*1b3f573fSAndroid Build Coastguard Worker   version as this package.  If in doubt, run:
26*1b3f573fSAndroid Build Coastguard Worker
27*1b3f573fSAndroid Build Coastguard Worker     $ protoc --version
28*1b3f573fSAndroid Build Coastguard Worker
29*1b3f573fSAndroid Build Coastguard Worker   You will need to place the protoc executable in ../src.  (If you
30*1b3f573fSAndroid Build Coastguard Worker   built it yourself, it should already be there.)
31*1b3f573fSAndroid Build Coastguard Worker
32*1b3f573fSAndroid Build Coastguard Worker3) Run the tests:
33*1b3f573fSAndroid Build Coastguard Worker
34*1b3f573fSAndroid Build Coastguard Worker     $ mvn test
35*1b3f573fSAndroid Build Coastguard Worker
36*1b3f573fSAndroid Build Coastguard Worker   If some tests fail, this library may not work correctly on your
37*1b3f573fSAndroid Build Coastguard Worker   system.  Continue at your own risk.
38*1b3f573fSAndroid Build Coastguard Worker
39*1b3f573fSAndroid Build Coastguard Worker4) Install the library into your Maven repository:
40*1b3f573fSAndroid Build Coastguard Worker
41*1b3f573fSAndroid Build Coastguard Worker     $ mvn install
42*1b3f573fSAndroid Build Coastguard Worker
43*1b3f573fSAndroid Build Coastguard Worker5) If you do not use Maven to manage your own build, you can build a
44*1b3f573fSAndroid Build Coastguard Worker   .jar file to use:
45*1b3f573fSAndroid Build Coastguard Worker
46*1b3f573fSAndroid Build Coastguard Worker     $ mvn package
47*1b3f573fSAndroid Build Coastguard Worker
48*1b3f573fSAndroid Build Coastguard Worker   The .jar will be placed in the "target" directory.
49*1b3f573fSAndroid Build Coastguard Worker
50*1b3f573fSAndroid Build Coastguard WorkerInstallation - Without Maven
51*1b3f573fSAndroid Build Coastguard Worker----------------------------
52*1b3f573fSAndroid Build Coastguard Worker
53*1b3f573fSAndroid Build Coastguard WorkerIf you would rather not install Maven to build the library, you may
54*1b3f573fSAndroid Build Coastguard Workerfollow these instructions instead.  Note that these instructions skip
55*1b3f573fSAndroid Build Coastguard Workerrunning unit tests.
56*1b3f573fSAndroid Build Coastguard Worker
57*1b3f573fSAndroid Build Coastguard Worker1) Build the C++ code, or obtain a binary distribution of protoc.  If
58*1b3f573fSAndroid Build Coastguard Worker   you install a binary distribution, make sure that it is the same
59*1b3f573fSAndroid Build Coastguard Worker   version as this package.  If in doubt, run:
60*1b3f573fSAndroid Build Coastguard Worker
61*1b3f573fSAndroid Build Coastguard Worker     $ protoc --version
62*1b3f573fSAndroid Build Coastguard Worker
63*1b3f573fSAndroid Build Coastguard Worker   If you built the C++ code without installing, the compiler binary
64*1b3f573fSAndroid Build Coastguard Worker   should be located in ../src.
65*1b3f573fSAndroid Build Coastguard Worker
66*1b3f573fSAndroid Build Coastguard Worker2) Invoke protoc to build DescriptorProtos.java:
67*1b3f573fSAndroid Build Coastguard Worker
68*1b3f573fSAndroid Build Coastguard Worker     $ protoc --java_out=src/main/java -I../src \
69*1b3f573fSAndroid Build Coastguard Worker         ../src/google/protobuf/descriptor.proto
70*1b3f573fSAndroid Build Coastguard Worker
71*1b3f573fSAndroid Build Coastguard Worker3) Compile the code in src/main/java using whatever means you prefer.
72*1b3f573fSAndroid Build Coastguard Worker
73*1b3f573fSAndroid Build Coastguard Worker4) Install the classes wherever you prefer.
74*1b3f573fSAndroid Build Coastguard Worker
75*1b3f573fSAndroid Build Coastguard WorkerNano version
76*1b3f573fSAndroid Build Coastguard Worker------------
77*1b3f573fSAndroid Build Coastguard Worker
78*1b3f573fSAndroid Build Coastguard WorkerJavaNano is a special code generator and runtime library designed specially for
79*1b3f573fSAndroid Build Coastguard Workerresource-restricted systems, like Android. It is very resource-friendly in both
80*1b3f573fSAndroid Build Coastguard Workerthe amount of code and the runtime overhead. Here is an overview of JavaNano
81*1b3f573fSAndroid Build Coastguard Workerfeatures compared with the official Java protobuf:
82*1b3f573fSAndroid Build Coastguard Worker
83*1b3f573fSAndroid Build Coastguard Worker- No descriptors or message builders.
84*1b3f573fSAndroid Build Coastguard Worker- All messages are mutable; fields are public Java fields.
85*1b3f573fSAndroid Build Coastguard Worker- For optional fields only, encapsulation behind setter/getter/hazzer/
86*1b3f573fSAndroid Build Coastguard Worker  clearer functions is opt-in, which provide proper 'has' state support.
87*1b3f573fSAndroid Build Coastguard Worker- For proto2, if not opted in, has state (field presence) is not available.
88*1b3f573fSAndroid Build Coastguard Worker  Serialization outputs all fields not equal to their defaults
89*1b3f573fSAndroid Build Coastguard Worker  (see important implications below).
90*1b3f573fSAndroid Build Coastguard Worker  The behavior is consistent with proto3 semantics.
91*1b3f573fSAndroid Build Coastguard Worker- Required fields (proto2 only) are always serialized.
92*1b3f573fSAndroid Build Coastguard Worker- Enum constants are integers; protection against invalid values only
93*1b3f573fSAndroid Build Coastguard Worker  when parsing from the wire.
94*1b3f573fSAndroid Build Coastguard Worker- Enum constants can be generated into container interfaces bearing
95*1b3f573fSAndroid Build Coastguard Worker  the enum's name (so the referencing code is in Java style).
96*1b3f573fSAndroid Build Coastguard Worker- CodedInputByteBufferNano can only take byte[] (not InputStream).
97*1b3f573fSAndroid Build Coastguard Worker- Similarly CodedOutputByteBufferNano can only write to byte[].
98*1b3f573fSAndroid Build Coastguard Worker- Repeated fields are in arrays, not ArrayList or Vector. Null array
99*1b3f573fSAndroid Build Coastguard Worker  elements are allowed and silently ignored.
100*1b3f573fSAndroid Build Coastguard Worker- Full support for serializing/deserializing repeated packed fields.
101*1b3f573fSAndroid Build Coastguard Worker- Support  extensions (in proto2).
102*1b3f573fSAndroid Build Coastguard Worker- Unset messages/groups are null, not an immutable empty default
103*1b3f573fSAndroid Build Coastguard Worker  instance.
104*1b3f573fSAndroid Build Coastguard Worker- toByteArray(...) and mergeFrom(...) are now static functions of
105*1b3f573fSAndroid Build Coastguard Worker  MessageNano.
106*1b3f573fSAndroid Build Coastguard Worker- The 'bytes' type translates to the Java type byte[].
107*1b3f573fSAndroid Build Coastguard Worker
108*1b3f573fSAndroid Build Coastguard WorkerThe generated messages are not thread-safe for writes, but may be
109*1b3f573fSAndroid Build Coastguard Workerused simultaneously from multiple threads in a read-only manner.
110*1b3f573fSAndroid Build Coastguard WorkerIn other words, an appropriate synchronization mechanism (such as
111*1b3f573fSAndroid Build Coastguard Workera ReadWriteLock) must be used to ensure that a message, its
112*1b3f573fSAndroid Build Coastguard Workerancestors, and descendants are not accessed by any other threads
113*1b3f573fSAndroid Build Coastguard Workerwhile the message is being modified. Field reads, getter methods
114*1b3f573fSAndroid Build Coastguard Worker(but not getExtension(...)), toByteArray(...), writeTo(...),
115*1b3f573fSAndroid Build Coastguard WorkergetCachedSize(), and getSerializedSize() are all considered read-only
116*1b3f573fSAndroid Build Coastguard Workeroperations.
117*1b3f573fSAndroid Build Coastguard Worker
118*1b3f573fSAndroid Build Coastguard WorkerIMPORTANT: If you have fields with defaults and opt out of accessors
119*1b3f573fSAndroid Build Coastguard Worker
120*1b3f573fSAndroid Build Coastguard WorkerHow fields with defaults are serialized has changed. Because we don't
121*1b3f573fSAndroid Build Coastguard Workerkeep "has" state, any field equal to its default is assumed to be not
122*1b3f573fSAndroid Build Coastguard Workerset and therefore is not serialized. Consider the situation where we
123*1b3f573fSAndroid Build Coastguard Workerchange the default value of a field. Senders compiled against an older
124*1b3f573fSAndroid Build Coastguard Workerversion of the proto continue to match against the old default, and
125*1b3f573fSAndroid Build Coastguard Workerdon't send values to the receiver even though the receiver assumes the
126*1b3f573fSAndroid Build Coastguard Workernew default value. Therefore, think carefully about the implications
127*1b3f573fSAndroid Build Coastguard Workerof changing the default value. Alternatively, turn on accessors and
128*1b3f573fSAndroid Build Coastguard Workerenjoy the benefit of the explicit has() checks.
129*1b3f573fSAndroid Build Coastguard Worker
130*1b3f573fSAndroid Build Coastguard WorkerIMPORTANT: If you have "bytes" fields with non-empty defaults
131*1b3f573fSAndroid Build Coastguard Worker
132*1b3f573fSAndroid Build Coastguard WorkerBecause the byte buffer is now of mutable type byte[], the default
133*1b3f573fSAndroid Build Coastguard Workerstatic final cannot be exposed through a public field. Each time a
134*1b3f573fSAndroid Build Coastguard Workermessage's constructor or clear() function is called, the default value
135*1b3f573fSAndroid Build Coastguard Worker(kept in a private byte[]) is cloned. This causes a small memory
136*1b3f573fSAndroid Build Coastguard Workerpenalty. This is not a problem if the field has no default or is an
137*1b3f573fSAndroid Build Coastguard Workerempty default.
138*1b3f573fSAndroid Build Coastguard Worker
139*1b3f573fSAndroid Build Coastguard WorkerNano Generator options
140*1b3f573fSAndroid Build Coastguard Worker----------------------
141*1b3f573fSAndroid Build Coastguard Worker
142*1b3f573fSAndroid Build Coastguard Worker```
143*1b3f573fSAndroid Build Coastguard Workerjava_package           -> <file-name>|<package-name>
144*1b3f573fSAndroid Build Coastguard Workerjava_outer_classname   -> <file-name>|<package-name>
145*1b3f573fSAndroid Build Coastguard Workerjava_multiple_files    -> true or false
146*1b3f573fSAndroid Build Coastguard Workerjava_nano_generate_has -> true or false [DEPRECATED]
147*1b3f573fSAndroid Build Coastguard Workeroptional_field_style   -> default or accessors
148*1b3f573fSAndroid Build Coastguard Workerenum_style             -> c or java
149*1b3f573fSAndroid Build Coastguard Workerignore_services        -> true or false
150*1b3f573fSAndroid Build Coastguard Workerparcelable_messages    -> true or false
151*1b3f573fSAndroid Build Coastguard Workergenerate_intdefs       -> true or false
152*1b3f573fSAndroid Build Coastguard Worker```
153*1b3f573fSAndroid Build Coastguard Worker
154*1b3f573fSAndroid Build Coastguard Worker**java_package=\<file-name\>|\<package-name\>** (no default)
155*1b3f573fSAndroid Build Coastguard Worker
156*1b3f573fSAndroid Build Coastguard Worker  This allows overriding the 'java_package' option value
157*1b3f573fSAndroid Build Coastguard Worker  for the given file from the command line. Use multiple
158*1b3f573fSAndroid Build Coastguard Worker  java_package options to override the option for multiple
159*1b3f573fSAndroid Build Coastguard Worker  files. The final Java package for each file is the value
160*1b3f573fSAndroid Build Coastguard Worker  of this command line option if present, or the value of
161*1b3f573fSAndroid Build Coastguard Worker  the same option defined in the file if present, or the
162*1b3f573fSAndroid Build Coastguard Worker  proto package if present, or the default Java package.
163*1b3f573fSAndroid Build Coastguard Worker
164*1b3f573fSAndroid Build Coastguard Worker**java_outer_classname=\<file-name\>|\<outer-classname\>** (no default)
165*1b3f573fSAndroid Build Coastguard Worker
166*1b3f573fSAndroid Build Coastguard Worker  This allows overriding the 'java_outer_classname' option
167*1b3f573fSAndroid Build Coastguard Worker  for the given file from the command line. Use multiple
168*1b3f573fSAndroid Build Coastguard Worker  java_outer_classname options to override the option for
169*1b3f573fSAndroid Build Coastguard Worker  multiple files. The final Java outer class name for each
170*1b3f573fSAndroid Build Coastguard Worker  file is the value of this command line option if present,
171*1b3f573fSAndroid Build Coastguard Worker  or the value of the same option defined in the file if
172*1b3f573fSAndroid Build Coastguard Worker  present, or the file name converted to CamelCase. This
173*1b3f573fSAndroid Build Coastguard Worker  outer class will nest all classes and integer constants
174*1b3f573fSAndroid Build Coastguard Worker  generated from file-scope messages and enums.
175*1b3f573fSAndroid Build Coastguard Worker
176*1b3f573fSAndroid Build Coastguard Worker**java_multiple_files={true,false}** (no default)
177*1b3f573fSAndroid Build Coastguard Worker
178*1b3f573fSAndroid Build Coastguard Worker  This allows overriding the 'java_multiple_files' option
179*1b3f573fSAndroid Build Coastguard Worker  in all source files and their imported files from the
180*1b3f573fSAndroid Build Coastguard Worker  command line. The final value of this option for each
181*1b3f573fSAndroid Build Coastguard Worker  file is the value defined in this command line option, or
182*1b3f573fSAndroid Build Coastguard Worker  the value of the same option defined in the file if
183*1b3f573fSAndroid Build Coastguard Worker  present, or false. This specifies whether to generate
184*1b3f573fSAndroid Build Coastguard Worker  package-level classes for the file-scope messages in the
185*1b3f573fSAndroid Build Coastguard Worker  same Java package as the outer class (instead of nested
186*1b3f573fSAndroid Build Coastguard Worker  classes in the outer class). File-scope enum constants
187*1b3f573fSAndroid Build Coastguard Worker  are still generated as integer constants in the outer
188*1b3f573fSAndroid Build Coastguard Worker  class. This affects the fully qualified references in the
189*1b3f573fSAndroid Build Coastguard Worker  Java code. NOTE: because the command line option
190*1b3f573fSAndroid Build Coastguard Worker  overrides the value for all files and their imported
191*1b3f573fSAndroid Build Coastguard Worker  files, using this option inconsistently may result in
192*1b3f573fSAndroid Build Coastguard Worker  incorrect references to the imported messages and enum
193*1b3f573fSAndroid Build Coastguard Worker  constants.
194*1b3f573fSAndroid Build Coastguard Worker
195*1b3f573fSAndroid Build Coastguard Worker**java_nano_generate_has={true,false}** (default: false)
196*1b3f573fSAndroid Build Coastguard Worker
197*1b3f573fSAndroid Build Coastguard Worker  DEPRECATED. Use optional_field_style=accessors.
198*1b3f573fSAndroid Build Coastguard Worker
199*1b3f573fSAndroid Build Coastguard Worker  If true, generates a public boolean variable has\<fieldname\>
200*1b3f573fSAndroid Build Coastguard Worker  accompanying each optional or required field (not present for
201*1b3f573fSAndroid Build Coastguard Worker  repeated fields, groups or messages). It is set to false initially
202*1b3f573fSAndroid Build Coastguard Worker  and upon clear(). If parseFrom(...) reads the field from the wire,
203*1b3f573fSAndroid Build Coastguard Worker  it is set to true. This is a way for clients to inspect the "has"
204*1b3f573fSAndroid Build Coastguard Worker  value upon parse. If it is set to true, writeTo(...) will ALWAYS
205*1b3f573fSAndroid Build Coastguard Worker  output that field (even if field value is equal to its
206*1b3f573fSAndroid Build Coastguard Worker  default).
207*1b3f573fSAndroid Build Coastguard Worker
208*1b3f573fSAndroid Build Coastguard Worker  IMPORTANT: This option costs an extra 4 bytes per primitive field in
209*1b3f573fSAndroid Build Coastguard Worker  the message. Think carefully about whether you really need this. In
210*1b3f573fSAndroid Build Coastguard Worker  many cases reading the default works and determining whether the
211*1b3f573fSAndroid Build Coastguard Worker  field was received over the wire is irrelevant.
212*1b3f573fSAndroid Build Coastguard Worker
213*1b3f573fSAndroid Build Coastguard Worker**optional_field_style={default,accessors,reftypes}** (default: default)
214*1b3f573fSAndroid Build Coastguard Worker
215*1b3f573fSAndroid Build Coastguard Worker  Defines the style of the generated code for fields.
216*1b3f573fSAndroid Build Coastguard Worker
217*1b3f573fSAndroid Build Coastguard Worker  * default
218*1b3f573fSAndroid Build Coastguard Worker
219*1b3f573fSAndroid Build Coastguard Worker  In the default style, optional fields translate into public mutable
220*1b3f573fSAndroid Build Coastguard Worker  Java fields, and the serialization process is as discussed in the
221*1b3f573fSAndroid Build Coastguard Worker  "IMPORTANT" section above.
222*1b3f573fSAndroid Build Coastguard Worker
223*1b3f573fSAndroid Build Coastguard Worker  * accessors
224*1b3f573fSAndroid Build Coastguard Worker
225*1b3f573fSAndroid Build Coastguard Worker  When set to 'accessors', each optional field is encapsulated behind
226*1b3f573fSAndroid Build Coastguard Worker  4 accessors, namely get\<fieldname\>(), set\<fieldname\>(), has\<fieldname\>()
227*1b3f573fSAndroid Build Coastguard Worker  and clear\<fieldname\>() methods, with the standard semantics. The hazzer's
228*1b3f573fSAndroid Build Coastguard Worker  return value determines whether a field is serialized, so this style is
229*1b3f573fSAndroid Build Coastguard Worker  useful when you need to serialize a field with the default value, or check
230*1b3f573fSAndroid Build Coastguard Worker  if a field has been explicitly set to its default value from the wire.
231*1b3f573fSAndroid Build Coastguard Worker
232*1b3f573fSAndroid Build Coastguard Worker  In the 'accessors' style, required and nested message fields are still
233*1b3f573fSAndroid Build Coastguard Worker  translated to one public mutable Java field each, repeated fields are still
234*1b3f573fSAndroid Build Coastguard Worker  translated to arrays. No accessors are generated for them.
235*1b3f573fSAndroid Build Coastguard Worker
236*1b3f573fSAndroid Build Coastguard Worker  IMPORTANT: When using the 'accessors' style, ProGuard should always
237*1b3f573fSAndroid Build Coastguard Worker  be enabled with optimization (don't use -dontoptimize) and allowing
238*1b3f573fSAndroid Build Coastguard Worker  access modification (use -allowaccessmodification). This removes the
239*1b3f573fSAndroid Build Coastguard Worker  unused accessors and maybe inline the rest at the call sites,
240*1b3f573fSAndroid Build Coastguard Worker  reducing the final code size.
241*1b3f573fSAndroid Build Coastguard Worker  TODO(maxtroy): find ProGuard config that would work the best.
242*1b3f573fSAndroid Build Coastguard Worker
243*1b3f573fSAndroid Build Coastguard Worker  * reftypes
244*1b3f573fSAndroid Build Coastguard Worker
245*1b3f573fSAndroid Build Coastguard Worker  When set to 'reftypes', each proto field is generated as a public Java
246*1b3f573fSAndroid Build Coastguard Worker  field. For primitive types, these fields use the Java reference types
247*1b3f573fSAndroid Build Coastguard Worker  such as java.lang.Integer instead of primitive types such as int.
248*1b3f573fSAndroid Build Coastguard Worker
249*1b3f573fSAndroid Build Coastguard Worker  In the 'reftypes' style, fields are initialized to null (or empty
250*1b3f573fSAndroid Build Coastguard Worker  arrays for repeated fields), and their default values are not available.
251*1b3f573fSAndroid Build Coastguard Worker  They are serialized over the wire based on equality to null.
252*1b3f573fSAndroid Build Coastguard Worker
253*1b3f573fSAndroid Build Coastguard Worker  The 'reftypes' mode has some additional cost due to autoboxing and usage
254*1b3f573fSAndroid Build Coastguard Worker  of reference types. In practice, many boxed types are cached, and so don't
255*1b3f573fSAndroid Build Coastguard Worker  result in object creation. However, references do take slightly more memory
256*1b3f573fSAndroid Build Coastguard Worker  than primitives.
257*1b3f573fSAndroid Build Coastguard Worker
258*1b3f573fSAndroid Build Coastguard Worker  The 'reftypes' mode is useful when you want to be able to serialize fields
259*1b3f573fSAndroid Build Coastguard Worker  with default values, or check if a field has been explicitly set to the
260*1b3f573fSAndroid Build Coastguard Worker  default over the wire without paying the extra method cost of the
261*1b3f573fSAndroid Build Coastguard Worker  'accessors' mode.
262*1b3f573fSAndroid Build Coastguard Worker
263*1b3f573fSAndroid Build Coastguard Worker  Note that if you attempt to write null to a required field in the reftypes
264*1b3f573fSAndroid Build Coastguard Worker  mode, serialization of the proto will cause a NullPointerException. This is
265*1b3f573fSAndroid Build Coastguard Worker  an intentional indicator that you must set required fields.
266*1b3f573fSAndroid Build Coastguard Worker
267*1b3f573fSAndroid Build Coastguard Worker  NOTE
268*1b3f573fSAndroid Build Coastguard Worker  optional_field_style=accessors or reftypes cannot be used together with
269*1b3f573fSAndroid Build Coastguard Worker  java_nano_generate_has=true. If you need the 'has' flag for any
270*1b3f573fSAndroid Build Coastguard Worker  required field (you have no reason to), you can only use
271*1b3f573fSAndroid Build Coastguard Worker  java_nano_generate_has=true.
272*1b3f573fSAndroid Build Coastguard Worker
273*1b3f573fSAndroid Build Coastguard Worker**enum_style={c,java}** (default: c)
274*1b3f573fSAndroid Build Coastguard Worker
275*1b3f573fSAndroid Build Coastguard Worker  Defines where to put the int constants generated from enum members.
276*1b3f573fSAndroid Build Coastguard Worker
277*1b3f573fSAndroid Build Coastguard Worker  * c
278*1b3f573fSAndroid Build Coastguard Worker
279*1b3f573fSAndroid Build Coastguard Worker  Use C-style, so the enum constants are available at the scope where
280*1b3f573fSAndroid Build Coastguard Worker  the enum is defined. A file-scope enum's members are referenced like
281*1b3f573fSAndroid Build Coastguard Worker  'FileOuterClass.ENUM_VALUE'; a message-scope enum's members are
282*1b3f573fSAndroid Build Coastguard Worker  referenced as 'Message.ENUM_VALUE'. The enum name is unavailable.
283*1b3f573fSAndroid Build Coastguard Worker  This complies with the Micro code generator's behavior.
284*1b3f573fSAndroid Build Coastguard Worker
285*1b3f573fSAndroid Build Coastguard Worker  * java
286*1b3f573fSAndroid Build Coastguard Worker
287*1b3f573fSAndroid Build Coastguard Worker  Use Java-style, so the enum constants are available under the enum
288*1b3f573fSAndroid Build Coastguard Worker  name and referenced like 'EnumName.ENUM_VALUE' (they are still int
289*1b3f573fSAndroid Build Coastguard Worker  constants). The enum name becomes the name of a public interface, at
290*1b3f573fSAndroid Build Coastguard Worker  the scope where the enum is defined. If the enum is file-scope and
291*1b3f573fSAndroid Build Coastguard Worker  the java_multiple_files option is on, the interface will be defined
292*1b3f573fSAndroid Build Coastguard Worker  in its own file. To reduce code size, this interface should not be
293*1b3f573fSAndroid Build Coastguard Worker  implemented and ProGuard shrinking should be used, so after the Java
294*1b3f573fSAndroid Build Coastguard Worker  compiler inlines all referenced enum constants into the call sites,
295*1b3f573fSAndroid Build Coastguard Worker  the interface remains unused and can be removed by ProGuard.
296*1b3f573fSAndroid Build Coastguard Worker
297*1b3f573fSAndroid Build Coastguard Worker**ignore_services={true,false}** (default: false)
298*1b3f573fSAndroid Build Coastguard Worker
299*1b3f573fSAndroid Build Coastguard Worker  Skips services definitions.
300*1b3f573fSAndroid Build Coastguard Worker
301*1b3f573fSAndroid Build Coastguard Worker  Nano doesn't support services. By default, if a service is defined
302*1b3f573fSAndroid Build Coastguard Worker  it will generate a compilation error. If this flag is set to true,
303*1b3f573fSAndroid Build Coastguard Worker  services will be silently ignored, instead.
304*1b3f573fSAndroid Build Coastguard Worker
305*1b3f573fSAndroid Build Coastguard Worker**parcelable_messages={true,false}** (default: false)
306*1b3f573fSAndroid Build Coastguard Worker
307*1b3f573fSAndroid Build Coastguard Worker  Android-specific option to generate Parcelable messages.
308*1b3f573fSAndroid Build Coastguard Worker
309*1b3f573fSAndroid Build Coastguard Worker**generate_intdefs={true,false}** (default: false)
310*1b3f573fSAndroid Build Coastguard Worker  Android-specific option to generate @IntDef annotations for enums.
311*1b3f573fSAndroid Build Coastguard Worker
312*1b3f573fSAndroid Build Coastguard Worker  If turned on, an '@IntDef' annotation (a public @interface) will be
313*1b3f573fSAndroid Build Coastguard Worker  generated for each enum, and every integer parameter and return
314*1b3f573fSAndroid Build Coastguard Worker  value in the generated code meant for this enum will be annotated
315*1b3f573fSAndroid Build Coastguard Worker  with it. This interface is generated with the same name and at the
316*1b3f573fSAndroid Build Coastguard Worker  same place as the enum members' container interfaces described
317*1b3f573fSAndroid Build Coastguard Worker  above under 'enum_style=java', regardless of the enum_style option
318*1b3f573fSAndroid Build Coastguard Worker  used. When this is combined with enum_style=java, the interface
319*1b3f573fSAndroid Build Coastguard Worker  will be both the '@IntDef' annotation and the container of the enum
320*1b3f573fSAndroid Build Coastguard Worker  members; otherwise the interface has an empty body.
321*1b3f573fSAndroid Build Coastguard Worker
322*1b3f573fSAndroid Build Coastguard Worker  Your app must declare a compile-time dependency on the
323*1b3f573fSAndroid Build Coastguard Worker  android-support-annotations library.
324*1b3f573fSAndroid Build Coastguard Worker
325*1b3f573fSAndroid Build Coastguard Worker  For more information on how these @IntDef annotations help with
326*1b3f573fSAndroid Build Coastguard Worker  compile-time type safety, see:
327*1b3f573fSAndroid Build Coastguard Worker  https://sites.google.com/a/android.com/tools/tech-docs/support-annotations
328*1b3f573fSAndroid Build Coastguard Worker  and
329*1b3f573fSAndroid Build Coastguard Worker  https://developer.android.com/reference/android/support/annotation/IntDef.html
330*1b3f573fSAndroid Build Coastguard Worker
331*1b3f573fSAndroid Build Coastguard Worker
332*1b3f573fSAndroid Build Coastguard WorkerTo use nano protobufs within the Android repo:
333*1b3f573fSAndroid Build Coastguard Worker----------------------------------------------
334*1b3f573fSAndroid Build Coastguard Worker
335*1b3f573fSAndroid Build Coastguard Worker- Set 'LOCAL_PROTOC_OPTIMIZE_TYPE := nano' in your local .mk file.
336*1b3f573fSAndroid Build Coastguard Worker  When building a Java library or an app (package) target, the build
337*1b3f573fSAndroid Build Coastguard Worker  system will add the Java nano runtime library to the
338*1b3f573fSAndroid Build Coastguard Worker  LOCAL_STATIC_JAVA_LIBRARIES variable, so you don't need to.
339*1b3f573fSAndroid Build Coastguard Worker- Set 'LOCAL_PROTO_JAVA_OUTPUT_PARAMS := ...' in your local .mk file
340*1b3f573fSAndroid Build Coastguard Worker  for any command-line options you need. Use commas to join multiple
341*1b3f573fSAndroid Build Coastguard Worker  options. In the nano flavor only, whitespace surrounding the option
342*1b3f573fSAndroid Build Coastguard Worker  names and values are ignored, so you can use backslash-newline or
343*1b3f573fSAndroid Build Coastguard Worker  '+=' to structure your make files nicely.
344*1b3f573fSAndroid Build Coastguard Worker- The options will be applied to *all* proto files in LOCAL_SRC_FILES
345*1b3f573fSAndroid Build Coastguard Worker  when you build a Java library or package. In case different options
346*1b3f573fSAndroid Build Coastguard Worker  are needed for different proto files, build separate Java libraries
347*1b3f573fSAndroid Build Coastguard Worker  and reference them in your main target. Note: you should make sure
348*1b3f573fSAndroid Build Coastguard Worker  that, for each separate target, all proto files imported from any
349*1b3f573fSAndroid Build Coastguard Worker  proto file in LOCAL_SRC_FILES are included in LOCAL_SRC_FILES. This
350*1b3f573fSAndroid Build Coastguard Worker  is because the generator has to assume that the imported files are
351*1b3f573fSAndroid Build Coastguard Worker  built using the same options, and will generate code that reference
352*1b3f573fSAndroid Build Coastguard Worker  the fields and enums from the imported files using the same code
353*1b3f573fSAndroid Build Coastguard Worker  style.
354*1b3f573fSAndroid Build Coastguard Worker- Hint: 'include $(CLEAR_VARS)' resets all LOCAL_ variables, including
355*1b3f573fSAndroid Build Coastguard Worker  the two above.
356*1b3f573fSAndroid Build Coastguard Worker
357*1b3f573fSAndroid Build Coastguard WorkerTo use nano protobufs outside of Android repo:
358*1b3f573fSAndroid Build Coastguard Worker----------------------------------------------
359*1b3f573fSAndroid Build Coastguard Worker
360*1b3f573fSAndroid Build Coastguard Worker- Link with the generated jar file
361*1b3f573fSAndroid Build Coastguard Worker  \<protobuf-root\>java/target/protobuf-java-2.3.0-nano.jar.
362*1b3f573fSAndroid Build Coastguard Worker- Invoke with --javanano_out, e.g.:
363*1b3f573fSAndroid Build Coastguard Worker```
364*1b3f573fSAndroid Build Coastguard Worker./protoc '--javanano_out=\
365*1b3f573fSAndroid Build Coastguard Worker    java_package=src/proto/simple-data.proto|my_package,\
366*1b3f573fSAndroid Build Coastguard Worker    java_outer_classname=src/proto/simple-data.proto|OuterName\
367*1b3f573fSAndroid Build Coastguard Worker  :.' src/proto/simple-data.proto
368*1b3f573fSAndroid Build Coastguard Worker```
369*1b3f573fSAndroid Build Coastguard Worker
370*1b3f573fSAndroid Build Coastguard WorkerContributing to nano:
371*1b3f573fSAndroid Build Coastguard Worker---------------------
372*1b3f573fSAndroid Build Coastguard Worker
373*1b3f573fSAndroid Build Coastguard WorkerPlease add/edit tests in NanoTest.java.
374*1b3f573fSAndroid Build Coastguard Worker
375*1b3f573fSAndroid Build Coastguard WorkerPlease run the following steps to test:
376*1b3f573fSAndroid Build Coastguard Worker
377*1b3f573fSAndroid Build Coastguard Worker- cd external/protobuf
378*1b3f573fSAndroid Build Coastguard Worker- ./configure
379*1b3f573fSAndroid Build Coastguard Worker- Run "make -j12 check" and verify all tests pass.
380*1b3f573fSAndroid Build Coastguard Worker- cd java
381*1b3f573fSAndroid Build Coastguard Worker- Run "mvn test" and verify all tests pass.
382*1b3f573fSAndroid Build Coastguard Worker- cd ../../..
383*1b3f573fSAndroid Build Coastguard Worker- . build/envsetup.sh
384*1b3f573fSAndroid Build Coastguard Worker- lunch 1
385*1b3f573fSAndroid Build Coastguard Worker- "make -j12 aprotoc libprotobuf-java-2.3.0-nano aprotoc-test-nano-params NanoAndroidTest" and
386*1b3f573fSAndroid Build Coastguard Worker  check for build errors.
387*1b3f573fSAndroid Build Coastguard Worker- Plug in an Android device or start an emulator.
388*1b3f573fSAndroid Build Coastguard Worker- adb install -r out/target/product/generic/data/app/NanoAndroidTest.apk
389*1b3f573fSAndroid Build Coastguard Worker- Run:
390*1b3f573fSAndroid Build Coastguard Worker  "adb shell am instrument -w com.google.protobuf.nano.test/android.test.InstrumentationTestRunner"
391*1b3f573fSAndroid Build Coastguard Worker  and verify all tests pass.
392*1b3f573fSAndroid Build Coastguard Worker- repo sync -c -j256
393*1b3f573fSAndroid Build Coastguard Worker- "make -j12" and check for build errors
394*1b3f573fSAndroid Build Coastguard Worker
395*1b3f573fSAndroid Build Coastguard WorkerUsage
396*1b3f573fSAndroid Build Coastguard Worker-----
397*1b3f573fSAndroid Build Coastguard Worker
398*1b3f573fSAndroid Build Coastguard WorkerThe complete documentation for Protocol Buffers is available via the
399*1b3f573fSAndroid Build Coastguard Workerweb at:
400*1b3f573fSAndroid Build Coastguard Worker
401*1b3f573fSAndroid Build Coastguard Worker    https://developers.google.com/protocol-buffers/
402