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