1 # Protocol Buffers - Google's data interchange format
3 Copyright 2008 Google Inc.
5 https://developers.google.com/protocol-buffers/
7 ## Use Java Protocol Buffers
9 To use protobuf in Java, first obtain the protocol compiler (a.k.a., protoc,
10 see instructions in the toplevel [README.md](../README.md)) and use it to
11 generate Java code for your .proto files:
13 $ protoc --java_out=${OUTPUT_DIR} path/to/your/proto/file
15 Include the generated Java files in your project and add a dependency on the
16 protobuf Java runtime.
20 If you are using Maven, use the following:
24 <groupId>com.google.protobuf</groupId>
25 <artifactId>protobuf-java</artifactId>
26 <version>3.6.1</version>
30 Make sure the version number of the runtime matches (or is newer than) the
31 version number of the protoc.
33 If you want to use features like protobuf JsonFormat, add a dependency on the
34 protobuf-java-util package:
38 <groupId>com.google.protobuf</groupId>
39 <artifactId>protobuf-java-util</artifactId>
40 <version>3.6.1</version>
46 If you are using Gradle, add the following to your `build.gradle` file's dependencies:
48 compile 'com.google.protobuf:protobuf-java:3.6.1'
50 Again, be sure to check that the version number maches (or is newer than) the version number of protoc that you are using.
52 ### Use Java Protocol Buffers on Android
54 For Android users, it's recommended to use protobuf Java Lite runtime because
55 of its smaller code size. Java Lite runtime also works better with Proguard
56 because it doesn't rely on Java reflection and is optimized to allow as much
57 code stripping as possible. You can following these [instructions to use Java
58 Lite runtime](lite.md).
60 ### Use Java Protocol Buffers with Bazel
62 Bazel has native build rules to work with protobuf. For Java, you can use the
63 `java_proto_library` rule for server and the `java_lite_proto_library` rule
64 for Android. Check out [our build files examples](../examples/BUILD) to learn
69 Most users should follow the instructions above to use protobuf Java runtime.
70 If you are contributing code to protobuf or want to use a protobuf version
71 that hasn't been officially released yet, you can folllow the instructions
72 below to build protobuf from source code.
74 ### Build from Source - With Maven
76 1) Install Apache Maven if you don't have it:
78 http://maven.apache.org/
80 2) Build the C++ code, or obtain a binary distribution of protoc (see
81 the toplevel [README.md](../README.md)). If you install a binary
82 distribution, make sure that it is the same version as this package.
87 You will need to place the protoc executable in ../src. (If you
88 built it yourself, it should already be there.)
94 If some tests fail, this library may not work correctly on your
95 system. Continue at your own risk.
97 4) Install the library into your Maven repository:
101 5) If you do not use Maven to manage your own build, you can build a
106 The .jar will be placed in the "target" directory.
108 The above instructions will install 2 maven artifacts:
110 * protobuf-java: The core Java Protocol Buffers library. Most users only
112 * protobuf-java-util: Utilities to work with protos. It contains JSON support
113 as well as utilities to work with proto3 well-known
116 ### Build from Source - Without Maven
118 If you would rather not install Maven to build the library, you may
119 follow these instructions instead. Note that these instructions skip
120 running unit tests and only describes how to install the core protobuf
121 library (without the util package).
123 1) Build the C++ code, or obtain a binary distribution of protoc. If
124 you install a binary distribution, make sure that it is the same
125 version as this package. If in doubt, run:
129 If you built the C++ code without installing, the compiler binary
130 should be located in ../src.
132 2) Invoke protoc to build DescriptorProtos.java:
134 $ protoc --java_out=core/src/main/java -I../src \
135 ../src/google/protobuf/descriptor.proto
137 3) Compile the code in core/src/main/java using whatever means you prefer.
139 4) Install the classes wherever you prefer.
141 ## Compatibility Notice
143 * Protobuf minor version releases are backwards-compatible. If your code
144 can build/run against the old version, it's expected to build/run against
145 the new version as well. Both binary compatibility and source compatibility
146 are guaranteed for minor version releases if the user follows the guideline
147 described in this section.
149 * Protobuf major version releases may also be backwards-compatbile with the
150 last release of the previous major version. See the release notice for more
153 * APIs marked with the @ExperimentalApi annotation are subject to change. They
154 can be modified in any way, or even removed, at any time. Don't use them if
155 compatibility is needed. If your code is a library itself (i.e. it is used on
156 the CLASSPATH of users outside your own control), you should not use
157 experimental APIs, unless you repackage them (e.g. using ProGuard).
159 * Deprecated non-experimental APIs will be removed two years after the release
160 in which they are first deprecated. You must fix your references before this
161 time. If you don't, any manner of breakage could result (you are not
162 guaranteed a compilation error).
164 * Protobuf message interfaces/classes are designed to be subclassed by protobuf
165 generated code only. Do not subclass these message interfaces/classes
166 yourself. We may add new methods to the message interfaces/classes which will
167 break your own subclasses.
169 * Don't use any method/class that is marked as "used by generated code only".
170 Such methods/classes are subject to change.
172 * Protobuf LITE runtime APIs are not stable yet. They are subject to change even
173 in minor version releases.
177 The complete documentation for Protocol Buffers is available via the
180 https://developers.google.com/protocol-buffers/