Wink Saville | 3df2fda | 2010-05-27 16:25:37 -0700 | [diff] [blame] | 1 | Protocol Buffers - Google's data interchange format |
| 2 | Copyright 2008 Google Inc. |
| 3 | |
Feng Xiao | cd980d1 | 2014-11-19 15:58:54 -0800 | [diff] [blame] | 4 | This directory contains the Java Protocol Buffers Nano runtime library. |
Wink Saville | 3df2fda | 2010-05-27 16:25:37 -0700 | [diff] [blame] | 5 | |
| 6 | Installation - With Maven |
| 7 | ========================= |
| 8 | |
| 9 | The Protocol Buffers build is managed using Maven. If you would |
| 10 | rather build without Maven, see below. |
| 11 | |
| 12 | 1) Install Apache Maven if you don't have it: |
| 13 | |
| 14 | http://maven.apache.org/ |
| 15 | |
| 16 | 2) 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 | |
| 25 | 3) 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 | |
| 32 | 4) Install the library into your Maven repository: |
| 33 | |
| 34 | $ mvn install |
| 35 | |
| 36 | 5) 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 Saville | 3df2fda | 2010-05-27 16:25:37 -0700 | [diff] [blame] | 43 | Installation - Without Maven |
| 44 | ============================ |
| 45 | |
| 46 | If you would rather not install Maven to build the library, you may |
| 47 | follow these instructions instead. Note that these instructions skip |
| 48 | running unit tests. |
| 49 | |
| 50 | 1) 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 | |
| 59 | 2) Invoke protoc to build DescriptorProtos.java: |
| 60 | |
| 61 | $ protoc --java_out=src/main/java -I../src \ |
| 62 | ../src/google/protobuf/descriptor.proto |
Wink Saville | 5ab6e29 | 2010-05-29 13:00:38 -0700 | [diff] [blame] | 63 | |
Wink Saville | 3df2fda | 2010-05-27 16:25:37 -0700 | [diff] [blame] | 64 | 3) Compile the code in src/main/java using whatever means you prefer. |
| 65 | |
| 66 | 4) Install the classes wherever you prefer. |
| 67 | |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 68 | Nano version |
| 69 | ============================ |
| 70 | |
Max Cai | e005be6 | 2014-01-15 18:47:56 +0000 | [diff] [blame] | 71 | Nano is a special code generator and runtime library designed specially |
| 72 | for Android, and is very resource-friendly in both the amount of code |
| 73 | and the runtime overhead. An overview of Nano features: |
Max Cai | 1479c7a | 2013-09-24 17:40:37 +0100 | [diff] [blame] | 74 | |
Max Cai | e005be6 | 2014-01-15 18:47:56 +0000 | [diff] [blame] | 75 | - 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 Cai | b337f25 | 2013-09-20 18:29:40 +0100 | [diff] [blame] | 79 | - If not opted in, has state is not available. Serialization outputs |
Max Cai | e005be6 | 2014-01-15 18:47:56 +0000 | [diff] [blame] | 80 | 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 Cai | 1479c7a | 2013-09-24 17:40:37 +0100 | [diff] [blame] | 85 | - Enum constants can be generated into container interfaces bearing |
| 86 | the enum's name (so the referencing code is in Java style). |
Max Cai | e005be6 | 2014-01-15 18:47:56 +0000 | [diff] [blame] | 87 | - 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 Cai | adf2449 | 2013-11-13 18:21:28 +0000 | [diff] [blame] | 91 | - Full support of serializing/deserializing repeated packed fields. |
Max Cai | e005be6 | 2014-01-15 18:47:56 +0000 | [diff] [blame] | 92 | - Support of extensions. |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 93 | - Unset messages/groups are null, not an immutable empty default |
| 94 | instance. |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 95 | - toByteArray(...) and mergeFrom(...) are now static functions of |
| 96 | MessageNano. |
Max Cai | e005be6 | 2014-01-15 18:47:56 +0000 | [diff] [blame] | 97 | - The 'bytes' type translates to the Java type byte[]. |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 98 | |
Dave Hawkey | 598087e | 2014-03-20 10:55:41 -0600 | [diff] [blame] | 99 | The generated messages are not thread-safe for writes, but may be |
| 100 | used simultaneously from multiple threads in a read-only manner. |
| 101 | In other words, an appropriate synchronization mechanism (such as |
| 102 | a ReadWriteLock) must be used to ensure that a message, its |
| 103 | ancestors, and descendants are not accessed by any other threads |
Juan Silveira | 79f19eb | 2014-06-17 15:01:22 +0100 | [diff] [blame] | 104 | while the message is being modified. Field reads, getter methods |
| 105 | (but not getExtension(...)), toByteArray(...), writeTo(...), |
| 106 | getCachedSize(), and getSerializedSize() are all considered read-only |
| 107 | operations. |
Dave Hawkey | 598087e | 2014-03-20 10:55:41 -0600 | [diff] [blame] | 108 | |
Max Cai | b337f25 | 2013-09-20 18:29:40 +0100 | [diff] [blame] | 109 | IMPORTANT: If you have fields with defaults and opt out of accessors |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 110 | |
| 111 | How fields with defaults are serialized has changed. Because we don't |
| 112 | keep "has" state, any field equal to its default is assumed to be not |
| 113 | set and therefore is not serialized. Consider the situation where we |
| 114 | change the default value of a field. Senders compiled against an older |
| 115 | version of the proto continue to match against the old default, and |
| 116 | don't send values to the receiver even though the receiver assumes the |
| 117 | new default value. Therefore, think carefully about the implications |
Max Cai | b337f25 | 2013-09-20 18:29:40 +0100 | [diff] [blame] | 118 | of changing the default value. Alternatively, turn on accessors and |
| 119 | enjoy the benefit of the explicit has() checks. |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 120 | |
| 121 | IMPORTANT: If you have "bytes" fields with non-empty defaults |
| 122 | |
| 123 | Because the byte buffer is now of mutable type byte[], the default |
| 124 | static final cannot be exposed through a public field. Each time a |
| 125 | message's constructor or clear() function is called, the default value |
| 126 | (kept in a private byte[]) is cloned. This causes a small memory |
| 127 | penalty. This is not a problem if the field has no default or is an |
| 128 | empty default. |
| 129 | |
Ulas Kirazci | e83bbbb | 2013-04-01 11:29:43 -0700 | [diff] [blame] | 130 | Nano Generator options |
| 131 | |
Max Cai | 06eed37 | 2013-07-29 17:20:50 +0100 | [diff] [blame] | 132 | java_package -> <file-name>|<package-name> |
| 133 | java_outer_classname -> <file-name>|<package-name> |
| 134 | java_multiple_files -> true or false |
Max Cai | b337f25 | 2013-09-20 18:29:40 +0100 | [diff] [blame] | 135 | java_nano_generate_has -> true or false [DEPRECATED] |
| 136 | optional_field_style -> default or accessors |
Max Cai | 8c65bb7 | 2013-10-10 16:09:48 +0100 | [diff] [blame] | 137 | enum_style -> c or java |
Jie Dai | d9425a6 | 2014-04-21 14:29:03 -0700 | [diff] [blame] | 138 | ignore_services -> true or false |
Jeff Davidson | ec4b1ce | 2014-04-22 23:25:53 -0700 | [diff] [blame] | 139 | parcelable_messages -> true or false |
Max Cai | 06eed37 | 2013-07-29 17:20:50 +0100 | [diff] [blame] | 140 | |
Feng Xiao | cd980d1 | 2014-11-19 15:58:54 -0800 | [diff] [blame] | 141 | java_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 | |
| 150 | java_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 | |
| 161 | java_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 Cai | 06eed37 | 2013-07-29 17:20:50 +0100 | [diff] [blame] | 178 | |
| 179 | java_nano_generate_has={true,false} (default: false) |
Max Cai | b337f25 | 2013-09-20 18:29:40 +0100 | [diff] [blame] | 180 | DEPRECATED. Use optional_field_style=accessors. |
| 181 | |
Ulas Kirazci | e83bbbb | 2013-04-01 11:29:43 -0700 | [diff] [blame] | 182 | If true, generates a public boolean variable has<fieldname> |
Max Cai | 06eed37 | 2013-07-29 17:20:50 +0100 | [diff] [blame] | 183 | accompanying each optional or required field (not present for |
Ulas Kirazci | e83bbbb | 2013-04-01 11:29:43 -0700 | [diff] [blame] | 184 | 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 Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 195 | |
Brian Duff | 10107cb | 2013-09-30 20:49:13 -0700 | [diff] [blame] | 196 | optional_field_style={default,accessors,reftypes} (default: default) |
| 197 | Defines the style of the generated code for fields. |
| 198 | |
| 199 | * default * |
| 200 | |
Max Cai | b337f25 | 2013-09-20 18:29:40 +0100 | [diff] [blame] | 201 | In the default style, optional fields translate into public mutable |
| 202 | Java fields, and the serialization process is as discussed in the |
Brian Duff | 10107cb | 2013-09-30 20:49:13 -0700 | [diff] [blame] | 203 | "IMPORTANT" section above. |
Max Cai | b337f25 | 2013-09-20 18:29:40 +0100 | [diff] [blame] | 204 | |
Brian Duff | 10107cb | 2013-09-30 20:49:13 -0700 | [diff] [blame] | 205 | * accessors * |
Max Cai | b337f25 | 2013-09-20 18:29:40 +0100 | [diff] [blame] | 206 | |
Brian Duff | 10107cb | 2013-09-30 20:49:13 -0700 | [diff] [blame] | 207 | 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 Cai | b337f25 | 2013-09-20 18:29:40 +0100 | [diff] [blame] | 213 | |
Andrew Flynn | c997c13 | 2013-12-04 13:10:07 -0800 | [diff] [blame] | 214 | 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 Duff | 10107cb | 2013-09-30 20:49:13 -0700 | [diff] [blame] | 217 | |
| 218 | IMPORTANT: When using the 'accessors' style, ProGuard should always |
Max Cai | b337f25 | 2013-09-20 18:29:40 +0100 | [diff] [blame] | 219 | 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 Duff | 10107cb | 2013-09-30 20:49:13 -0700 | [diff] [blame] | 225 | * 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 Duff | 10107cb | 2013-09-30 20:49:13 -0700 | [diff] [blame] | 249 | 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 Cai | 8c65bb7 | 2013-10-10 16:09:48 +0100 | [diff] [blame] | 255 | enum_style={c,java} (default: c) |
| 256 | Defines where to put the int constants generated from enum members. |
Brian Duff | 10107cb | 2013-09-30 20:49:13 -0700 | [diff] [blame] | 257 | |
Max Cai | 8c65bb7 | 2013-10-10 16:09:48 +0100 | [diff] [blame] | 258 | * 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 Dai | d9425a6 | 2014-04-21 14:29:03 -0700 | [diff] [blame] | 278 | ignore_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 Davidson | ec4b1ce | 2014-04-22 23:25:53 -0700 | [diff] [blame] | 285 | parcelable_messages={true,false} (default: false) |
| 286 | Android-specific option to generate Parcelable messages. |
| 287 | |
Max Cai | 8c65bb7 | 2013-10-10 16:09:48 +0100 | [diff] [blame] | 288 | |
| 289 | To 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 Cai | bc8eec3 | 2014-01-14 14:54:48 +0000 | [diff] [blame] | 297 | 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 Cai | 8c65bb7 | 2013-10-10 16:09:48 +0100 | [diff] [blame] | 300 | - 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 | |
| 313 | To use nano protobufs outside of Android repo: |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 314 | |
| 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 Cai | 06eed37 | 2013-07-29 17:20:50 +0100 | [diff] [blame] | 319 | ./protoc '--javanano_out=\ |
Max Cai | bc8eec3 | 2014-01-14 14:54:48 +0000 | [diff] [blame] | 320 | 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 Cai | 8c65bb7 | 2013-10-10 16:09:48 +0100 | [diff] [blame] | 323 | |
Ulas Kirazci | 39799d0 | 2013-07-19 11:56:43 -0700 | [diff] [blame] | 324 | Contributing to nano: |
| 325 | |
| 326 | Please add/edit tests in NanoTest.java. |
| 327 | |
| 328 | Please 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 Davidson | ec4b1ce | 2014-04-22 23:25:53 -0700 | [diff] [blame] | 338 | - "make -j12 aprotoc libprotobuf-java-2.3.0-nano aprotoc-test-nano-params NanoAndroidTest" and |
Ulas Kirazci | 2000cfd | 2013-07-24 14:35:42 -0700 | [diff] [blame] | 339 | check for build errors. |
Jeff Davidson | ec4b1ce | 2014-04-22 23:25:53 -0700 | [diff] [blame] | 340 | - 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 Kirazci | 39799d0 | 2013-07-19 11:56:43 -0700 | [diff] [blame] | 345 | - repo sync -c -j256 |
| 346 | - "make -j12" and check for build errors |
| 347 | |
Wink Saville | 3df2fda | 2010-05-27 16:25:37 -0700 | [diff] [blame] | 348 | Usage |
| 349 | ===== |
| 350 | |
| 351 | The complete documentation for Protocol Buffers is available via the |
| 352 | web at: |
| 353 | |
Feng Xiao | 9e65d4b | 2014-11-20 14:21:43 -0800 | [diff] [blame] | 354 | https://developers.google.com/protocol-buffers/ |