blob: 5a05b86530645d6009d2d0c17683fbca88786ce9 [file] [log] [blame]
Wink Saville3df2fda2010-05-27 16:25:37 -07001Protocol Buffers - Google's data interchange format
2Copyright 2008 Google Inc.
3
Feng Xiaocd980d12014-11-19 15:58:54 -08004This directory contains the Java Protocol Buffers Nano runtime library.
Wink Saville3df2fda2010-05-27 16:25:37 -07005
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
Wink Saville3df2fda2010-05-27 16:25:37 -070043Installation - Without Maven
44============================
45
46If you would rather not install Maven to build the library, you may
47follow these instructions instead. Note that these instructions skip
48running unit tests.
49
501) Build the C++ code, or obtain a binary distribution of protoc. If
51 you install a binary distribution, make sure that it is the same
52 version as this package. If in doubt, run:
53
54 $ protoc --version
55
56 If you built the C++ code without installing, the compiler binary
57 should be located in ../src.
58
592) Invoke protoc to build DescriptorProtos.java:
60
61 $ protoc --java_out=src/main/java -I../src \
62 ../src/google/protobuf/descriptor.proto
Wink Saville5ab6e292010-05-29 13:00:38 -070063
Wink Saville3df2fda2010-05-27 16:25:37 -0700643) Compile the code in src/main/java using whatever means you prefer.
65
664) Install the classes wherever you prefer.
67
Ulas Kirazci23370232013-03-14 16:44:33 -070068Nano version
69============================
70
Max Caie005be62014-01-15 18:47:56 +000071Nano is a special code generator and runtime library designed specially
72for Android, and is very resource-friendly in both the amount of code
73and the runtime overhead. An overview of Nano features:
Max Cai1479c7a2013-09-24 17:40:37 +010074
Max Caie005be62014-01-15 18:47:56 +000075- No descriptors or message builders.
76- All messages are mutable; fields are public Java fields.
77- For optional fields only, encapsulation behind setter/getter/hazzer/
78 clearer functions is opt-in, which provide proper 'has' state support.
Max Caib337f252013-09-20 18:29:40 +010079- If not opted in, has state is not available. Serialization outputs
Max Caie005be62014-01-15 18:47:56 +000080 all fields not equal to their defaults (see important implications
81 below).
82- Required fields are always serialized.
83- Enum constants are integers; protection against invalid values only
84 when parsing from the wire.
Max Cai1479c7a2013-09-24 17:40:37 +010085- Enum constants can be generated into container interfaces bearing
86 the enum's name (so the referencing code is in Java style).
Max Caie005be62014-01-15 18:47:56 +000087- CodedInputByteBufferNano can only take byte[] (not InputStream).
88- Similarly CodedOutputByteBufferNano can only write to byte[].
89- Repeated fields are in arrays, not ArrayList or Vector. Null array
90 elements are allowed and silently ignored.
Max Caiadf24492013-11-13 18:21:28 +000091- Full support of serializing/deserializing repeated packed fields.
Max Caie005be62014-01-15 18:47:56 +000092- Support of extensions.
Ulas Kirazci23370232013-03-14 16:44:33 -070093- Unset messages/groups are null, not an immutable empty default
94 instance.
Ulas Kirazci23370232013-03-14 16:44:33 -070095- toByteArray(...) and mergeFrom(...) are now static functions of
96 MessageNano.
Max Caie005be62014-01-15 18:47:56 +000097- The 'bytes' type translates to the Java type byte[].
Ulas Kirazci23370232013-03-14 16:44:33 -070098
Dave Hawkey598087e2014-03-20 10:55:41 -060099The generated messages are not thread-safe for writes, but may be
100used simultaneously from multiple threads in a read-only manner.
101In other words, an appropriate synchronization mechanism (such as
102a ReadWriteLock) must be used to ensure that a message, its
103ancestors, and descendants are not accessed by any other threads
Juan Silveira79f19eb2014-06-17 15:01:22 +0100104while the message is being modified. Field reads, getter methods
105(but not getExtension(...)), toByteArray(...), writeTo(...),
106getCachedSize(), and getSerializedSize() are all considered read-only
107operations.
Dave Hawkey598087e2014-03-20 10:55:41 -0600108
Max Caib337f252013-09-20 18:29:40 +0100109IMPORTANT: If you have fields with defaults and opt out of accessors
Ulas Kirazci23370232013-03-14 16:44:33 -0700110
111How fields with defaults are serialized has changed. Because we don't
112keep "has" state, any field equal to its default is assumed to be not
113set and therefore is not serialized. Consider the situation where we
114change the default value of a field. Senders compiled against an older
115version of the proto continue to match against the old default, and
116don't send values to the receiver even though the receiver assumes the
117new default value. Therefore, think carefully about the implications
Max Caib337f252013-09-20 18:29:40 +0100118of changing the default value. Alternatively, turn on accessors and
119enjoy the benefit of the explicit has() checks.
Ulas Kirazci23370232013-03-14 16:44:33 -0700120
121IMPORTANT: If you have "bytes" fields with non-empty defaults
122
123Because the byte buffer is now of mutable type byte[], the default
124static final cannot be exposed through a public field. Each time a
125message's constructor or clear() function is called, the default value
126(kept in a private byte[]) is cloned. This causes a small memory
127penalty. This is not a problem if the field has no default or is an
128empty default.
129
Ulas Kirazcie83bbbb2013-04-01 11:29:43 -0700130Nano Generator options
131
Max Cai06eed372013-07-29 17:20:50 +0100132java_package -> <file-name>|<package-name>
133java_outer_classname -> <file-name>|<package-name>
134java_multiple_files -> true or false
Max Caib337f252013-09-20 18:29:40 +0100135java_nano_generate_has -> true or false [DEPRECATED]
136optional_field_style -> default or accessors
Max Cai8c65bb72013-10-10 16:09:48 +0100137enum_style -> c or java
Jie Daid9425a62014-04-21 14:29:03 -0700138ignore_services -> true or false
Jeff Davidsonec4b1ce2014-04-22 23:25:53 -0700139parcelable_messages -> true or false
Max Cai06eed372013-07-29 17:20:50 +0100140
Feng Xiaocd980d12014-11-19 15:58:54 -0800141java_package=<file-name>|<package-name> (no default)
142 This allows overriding the 'java_package' option value
143 for the given file from the command line. Use multiple
144 java_package options to override the option for multiple
145 files. The final Java package for each file is the value
146 of this command line option if present, or the value of
147 the same option defined in the file if present, or the
148 proto package if present, or the default Java package.
149
150java_outer_classname=<file-name>|<outer-classname> (no default)
151 This allows overriding the 'java_outer_classname' option
152 for the given file from the command line. Use multiple
153 java_outer_classname options to override the option for
154 multiple files. The final Java outer class name for each
155 file is the value of this command line option if present,
156 or the value of the same option defined in the file if
157 present, or the file name converted to CamelCase. This
158 outer class will nest all classes and integer constants
159 generated from file-scope messages and enums.
160
161java_multiple_files={true,false} (no default)
162 This allows overriding the 'java_multiple_files' option
163 in all source files and their imported files from the
164 command line. The final value of this option for each
165 file is the value defined in this command line option, or
166 the value of the same option defined in the file if
167 present, or false. This specifies whether to generate
168 package-level classes for the file-scope messages in the
169 same Java package as the outer class (instead of nested
170 classes in the outer class). File-scope enum constants
171 are still generated as integer constants in the outer
172 class. This affects the fully qualified references in the
173 Java code. NOTE: because the command line option
174 overrides the value for all files and their imported
175 files, using this option inconsistently may result in
176 incorrect references to the imported messages and enum
177 constants.
Max Cai06eed372013-07-29 17:20:50 +0100178
179java_nano_generate_has={true,false} (default: false)
Max Caib337f252013-09-20 18:29:40 +0100180 DEPRECATED. Use optional_field_style=accessors.
181
Ulas Kirazcie83bbbb2013-04-01 11:29:43 -0700182 If true, generates a public boolean variable has<fieldname>
Max Cai06eed372013-07-29 17:20:50 +0100183 accompanying each optional or required field (not present for
Ulas Kirazcie83bbbb2013-04-01 11:29:43 -0700184 repeated fields, groups or messages). It is set to false initially
185 and upon clear(). If parseFrom(...) reads the field from the wire,
186 it is set to true. This is a way for clients to inspect the "has"
187 value upon parse. If it is set to true, writeTo(...) will ALWAYS
188 output that field (even if field value is equal to its
189 default).
190
191 IMPORTANT: This option costs an extra 4 bytes per primitive field in
192 the message. Think carefully about whether you really need this. In
193 many cases reading the default works and determining whether the
194 field was received over the wire is irrelevant.
Ulas Kirazci23370232013-03-14 16:44:33 -0700195
Brian Duff10107cb2013-09-30 20:49:13 -0700196optional_field_style={default,accessors,reftypes} (default: default)
197 Defines the style of the generated code for fields.
198
199 * default *
200
Max Caib337f252013-09-20 18:29:40 +0100201 In the default style, optional fields translate into public mutable
202 Java fields, and the serialization process is as discussed in the
Brian Duff10107cb2013-09-30 20:49:13 -0700203 "IMPORTANT" section above.
Max Caib337f252013-09-20 18:29:40 +0100204
Brian Duff10107cb2013-09-30 20:49:13 -0700205 * accessors *
Max Caib337f252013-09-20 18:29:40 +0100206
Brian Duff10107cb2013-09-30 20:49:13 -0700207 When set to 'accessors', each optional field is encapsulated behind
208 4 accessors, namely get<fieldname>(), set<fieldname>(), has<fieldname>()
209 and clear<fieldname>() methods, with the standard semantics. The hazzer's
210 return value determines whether a field is serialized, so this style is
211 useful when you need to serialize a field with the default value, or check
212 if a field has been explicitly set to its default value from the wire.
Max Caib337f252013-09-20 18:29:40 +0100213
Andrew Flynnc997c132013-12-04 13:10:07 -0800214 In the 'accessors' style, required and nested message fields are still
215 translated to one public mutable Java field each, repeated fields are still
216 translated to arrays. No accessors are generated for them.
Brian Duff10107cb2013-09-30 20:49:13 -0700217
218 IMPORTANT: When using the 'accessors' style, ProGuard should always
Max Caib337f252013-09-20 18:29:40 +0100219 be enabled with optimization (don't use -dontoptimize) and allowing
220 access modification (use -allowaccessmodification). This removes the
221 unused accessors and maybe inline the rest at the call sites,
222 reducing the final code size.
223 TODO(maxtroy): find ProGuard config that would work the best.
224
Brian Duff10107cb2013-09-30 20:49:13 -0700225 * reftypes *
226
227 When set to 'reftypes', each proto field is generated as a public Java
228 field. For primitive types, these fields use the Java reference types
229 such as java.lang.Integer instead of primitive types such as int.
230
231 In the 'reftypes' style, fields are initialized to null (or empty
232 arrays for repeated fields), and their default values are not available.
233 They are serialized over the wire based on equality to null.
234
235 The 'reftypes' mode has some additional cost due to autoboxing and usage
236 of reference types. In practice, many boxed types are cached, and so don't
237 result in object creation. However, references do take slightly more memory
238 than primitives.
239
240 The 'reftypes' mode is useful when you want to be able to serialize fields
241 with default values, or check if a field has been explicitly set to the
242 default over the wire without paying the extra method cost of the
243 'accessors' mode.
244
245 Note that if you attempt to write null to a required field in the reftypes
246 mode, serialization of the proto will cause a NullPointerException. This is
247 an intentional indicator that you must set required fields.
248
Brian Duff10107cb2013-09-30 20:49:13 -0700249 NOTE
250 optional_field_style=accessors or reftypes cannot be used together with
251 java_nano_generate_has=true. If you need the 'has' flag for any
252 required field (you have no reason to), you can only use
253 java_nano_generate_has=true.
254
Max Cai8c65bb72013-10-10 16:09:48 +0100255enum_style={c,java} (default: c)
256 Defines where to put the int constants generated from enum members.
Brian Duff10107cb2013-09-30 20:49:13 -0700257
Max Cai8c65bb72013-10-10 16:09:48 +0100258 * c *
259
260 Use C-style, so the enum constants are available at the scope where
261 the enum is defined. A file-scope enum's members are referenced like
262 'FileOuterClass.ENUM_VALUE'; a message-scope enum's members are
263 referenced as 'Message.ENUM_VALUE'. The enum name is unavailable.
264 This complies with the Micro code generator's behavior.
265
266 * java *
267
268 Use Java-style, so the enum constants are available under the enum
269 name and referenced like 'EnumName.ENUM_VALUE' (they are still int
270 constants). The enum name becomes the name of a public interface, at
271 the scope where the enum is defined. If the enum is file-scope and
272 the java_multiple_files option is on, the interface will be defined
273 in its own file. To reduce code size, this interface should not be
274 implemented and ProGuard shrinking should be used, so after the Java
275 compiler inlines all referenced enum constants into the call sites,
276 the interface remains unused and can be removed by ProGuard.
277
Jie Daid9425a62014-04-21 14:29:03 -0700278ignore_services={true,false} (default: false)
279 Skips services definitions.
280
281 Nano doesn't support services. By default, if a service is defined
282 it will generate a compilation error. If this flag is set to true,
283 services will be silently ignored, instead.
284
Jeff Davidsonec4b1ce2014-04-22 23:25:53 -0700285parcelable_messages={true,false} (default: false)
286 Android-specific option to generate Parcelable messages.
287
Max Cai8c65bb72013-10-10 16:09:48 +0100288
289To use nano protobufs within the Android repo:
290
291- Set 'LOCAL_PROTOC_OPTIMIZE_TYPE := nano' in your local .mk file.
292 When building a Java library or an app (package) target, the build
293 system will add the Java nano runtime library to the
294 LOCAL_STATIC_JAVA_LIBRARIES variable, so you don't need to.
295- Set 'LOCAL_PROTO_JAVA_OUTPUT_PARAMS := ...' in your local .mk file
296 for any command-line options you need. Use commas to join multiple
Max Caibc8eec32014-01-14 14:54:48 +0000297 options. In the nano flavor only, whitespace surrounding the option
298 names and values are ignored, so you can use backslash-newline or
299 '+=' to structure your make files nicely.
Max Cai8c65bb72013-10-10 16:09:48 +0100300- The options will be applied to *all* proto files in LOCAL_SRC_FILES
301 when you build a Java library or package. In case different options
302 are needed for different proto files, build separate Java libraries
303 and reference them in your main target. Note: you should make sure
304 that, for each separate target, all proto files imported from any
305 proto file in LOCAL_SRC_FILES are included in LOCAL_SRC_FILES. This
306 is because the generator has to assume that the imported files are
307 built using the same options, and will generate code that reference
308 the fields and enums from the imported files using the same code
309 style.
310- Hint: 'include $(CLEAR_VARS)' resets all LOCAL_ variables, including
311 the two above.
312
313To use nano protobufs outside of Android repo:
Ulas Kirazci23370232013-03-14 16:44:33 -0700314
315- Link with the generated jar file
316 <protobuf-root>java/target/protobuf-java-2.3.0-nano.jar.
317- Invoke with --javanano_out, e.g.:
318
Max Cai06eed372013-07-29 17:20:50 +0100319./protoc '--javanano_out=\
Max Caibc8eec32014-01-14 14:54:48 +0000320 java_package=src/proto/simple-data.proto|my_package,\
321 java_outer_classname=src/proto/simple-data.proto|OuterName\
322 :.' src/proto/simple-data.proto
Max Cai8c65bb72013-10-10 16:09:48 +0100323
Ulas Kirazci39799d02013-07-19 11:56:43 -0700324Contributing to nano:
325
326Please add/edit tests in NanoTest.java.
327
328Please run the following steps to test:
329
330- cd external/protobuf
331- ./configure
332- Run "make -j12 check" and verify all tests pass.
333- cd java
334- Run "mvn test" and verify all tests pass.
335- cd ../../..
336- . build/envsetup.sh
337- lunch 1
Jeff Davidsonec4b1ce2014-04-22 23:25:53 -0700338- "make -j12 aprotoc libprotobuf-java-2.3.0-nano aprotoc-test-nano-params NanoAndroidTest" and
Ulas Kirazci2000cfd2013-07-24 14:35:42 -0700339 check for build errors.
Jeff Davidsonec4b1ce2014-04-22 23:25:53 -0700340- Plug in an Android device or start an emulator.
341- adb install -r out/target/product/generic/data/app/NanoAndroidTest.apk
342- Run:
343 "adb shell am instrument -w com.google.protobuf.nano.test/android.test.InstrumentationTestRunner"
344 and verify all tests pass.
Ulas Kirazci39799d02013-07-19 11:56:43 -0700345- repo sync -c -j256
346- "make -j12" and check for build errors
347
Wink Saville3df2fda2010-05-27 16:25:37 -0700348Usage
349=====
350
351The complete documentation for Protocol Buffers is available via the
352web at:
353
Feng Xiao9e65d4b2014-11-20 14:21:43 -0800354 https://developers.google.com/protocol-buffers/