blob: 48c3aeccb1f3c84ce81a1c598d9214d42f4ffde0 [file] [log] [blame] [view]
Feng Xiao7d550402018-04-12 17:58:55 -07001# Protocol Buffers - Google's data interchange format
jessecd04e9b2015-03-16 15:15:59 -07002
temporal40ee5512008-07-10 02:12:20 +00003Copyright 2008 Google Inc.
4
Feng Xiao7d550402018-04-12 17:58:55 -07005https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00006
Feng Xiao7d550402018-04-12 17:58:55 -07007## Use Java Protocol Buffers
temporalcc930432008-07-21 20:28:30 +00008
Feng Xiao7d550402018-04-12 17:58:55 -07009To use protobuf in Java, first obtain the protocol compiler (a.k.a., protoc,
10see instructions in the toplevel [README.md](../README.md)) and use it to
11generate Java code for your .proto files:
12
13 $ protoc --java_out=${OUTPUT_DIR} path/to/your/proto/file
14
15Include the generated Java files in your project and add a dependency on the
Kyle Krueger21ebeed2018-09-06 09:43:26 +020016protobuf Java runtime.
17
18### Maven
19
20If you are using Maven, use the following:
Feng Xiao7d550402018-04-12 17:58:55 -070021
22```xml
23<dependency>
24 <groupId>com.google.protobuf</groupId>
25 <artifactId>protobuf-java</artifactId>
Elliotte Rusty Haroldb6facc62021-03-01 10:01:35 -050026 <version>3.15.3</version>
Feng Xiao7d550402018-04-12 17:58:55 -070027</dependency>
28```
29
30Make sure the version number of the runtime matches (or is newer than) the
31version number of the protoc.
32
33If you want to use features like protobuf JsonFormat, add a dependency on the
34protobuf-java-util package:
35
36```xml
37<dependency>
38 <groupId>com.google.protobuf</groupId>
39 <artifactId>protobuf-java-util</artifactId>
Elliotte Rusty Haroldb6facc62021-03-01 10:01:35 -050040 <version>3.15.3</version>
Feng Xiao7d550402018-04-12 17:58:55 -070041</dependency>
42```
43
Kyle Krueger21ebeed2018-09-06 09:43:26 +020044### Gradle
45
46If you are using Gradle, add the following to your `build.gradle` file's dependencies:
47```
Tobias Liese1db2b662021-04-08 10:48:22 +020048 implementation 'com.google.protobuf:protobuf-java:3.15.3'
Kyle Krueger21ebeed2018-09-06 09:43:26 +020049```
Peter Newmanfc8e2e82020-07-18 04:12:54 +010050Again, be sure to check that the version number matches (or is newer than) the version number of protoc that you are using.
Kyle Krueger21ebeed2018-09-06 09:43:26 +020051
Feng Xiao7d550402018-04-12 17:58:55 -070052### Use Java Protocol Buffers on Android
53
54For Android users, it's recommended to use protobuf Java Lite runtime because
55of its smaller code size. Java Lite runtime also works better with Proguard
56because it doesn't rely on Java reflection and is optimized to allow as much
57code stripping as possible. You can following these [instructions to use Java
58Lite runtime](lite.md).
59
60### Use Java Protocol Buffers with Bazel
61
62Bazel 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
64for Android. Check out [our build files examples](../examples/BUILD) to learn
65how to use them.
66
67## Build from Source
68
69Most users should follow the instructions above to use protobuf Java runtime.
70If you are contributing code to protobuf or want to use a protobuf version
Peter Newmane2cc2de2020-08-10 19:08:25 +010071that hasn't been officially released yet, you can follow the instructions
Feng Xiao7d550402018-04-12 17:58:55 -070072below to build protobuf from source code.
73
74### Build from Source - With Maven
temporal40ee5512008-07-10 02:12:20 +000075
761) Install Apache Maven if you don't have it:
77
78 http://maven.apache.org/
79
Feng Xiao09732c92016-05-11 16:22:30 -0700802) 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.
83 If in doubt, run:
temporal40ee5512008-07-10 02:12:20 +000084
85 $ protoc --version
86
87 You will need to place the protoc executable in ../src. (If you
88 built it yourself, it should already be there.)
89
903) Run the tests:
91
92 $ mvn test
93
94 If some tests fail, this library may not work correctly on your
95 system. Continue at your own risk.
96
974) Install the library into your Maven repository:
98
99 $ mvn install
100
1015) If you do not use Maven to manage your own build, you can build a
102 .jar file to use:
103
104 $ mvn package
105
106 The .jar will be placed in the "target" directory.
kenton@google.com6c9f0022009-08-05 19:55:17 +0000107
Feng Xiao7d550402018-04-12 17:58:55 -0700108The above instructions will install 2 maven artifacts:
gk58855ea590d2009-08-04 23:43:27 +0000109
Feng Xiao09732c92016-05-11 16:22:30 -0700110 * protobuf-java: The core Java Protocol Buffers library. Most users only
111 need this artifact.
Feng Xiao09732c92016-05-11 16:22:30 -0700112 * protobuf-java-util: Utilities to work with protos. It contains JSON support
113 as well as utilities to work with proto3 well-known
114 types.
temporal40ee5512008-07-10 02:12:20 +0000115
Feng Xiao7d550402018-04-12 17:58:55 -0700116### Build from Source - Without Maven
temporalcc930432008-07-21 20:28:30 +0000117
118If you would rather not install Maven to build the library, you may
119follow these instructions instead. Note that these instructions skip
Feng Xiao09732c92016-05-11 16:22:30 -0700120running unit tests and only describes how to install the core protobuf
121library (without the util package).
temporalcc930432008-07-21 20:28:30 +0000122
1231) 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:
126
127 $ protoc --version
128
129 If you built the C++ code without installing, the compiler binary
130 should be located in ../src.
131
1322) Invoke protoc to build DescriptorProtos.java:
133
Feng Xiao09732c92016-05-11 16:22:30 -0700134 $ protoc --java_out=core/src/main/java -I../src \
temporalcc930432008-07-21 20:28:30 +0000135 ../src/google/protobuf/descriptor.proto
136
Feng Xiao09732c92016-05-11 16:22:30 -07001373) Compile the code in core/src/main/java using whatever means you prefer.
temporalcc930432008-07-21 20:28:30 +0000138
1394) Install the classes wherever you prefer.
140
Feng Xiao7d550402018-04-12 17:58:55 -0700141## Compatibility Notice
Feng Xiao09732c92016-05-11 16:22:30 -0700142
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
Otto Kekäläinen3808d092016-07-03 15:26:04 +0300145 the new version as well. Both binary compatibility and source compatibility
Feng Xiao09732c92016-05-11 16:22:30 -0700146 are guaranteed for minor version releases if the user follows the guideline
147 described in this section.
148
Brian Wignalla104dff2020-01-08 13:18:20 -0500149* Protobuf major version releases may also be backwards-compatible with the
Feng Xiao09732c92016-05-11 16:22:30 -0700150 last release of the previous major version. See the release notice for more
151 details.
152
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
Otto Kekäläinen3808d092016-07-03 15:26:04 +0300155 compatibility is needed. If your code is a library itself (i.e. it is used on
Feng Xiao09732c92016-05-11 16:22:30 -0700156 the CLASSPATH of users outside your own control), you should not use
157 experimental APIs, unless you repackage them (e.g. using ProGuard).
158
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).
163
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.
168
169* Don't use any method/class that is marked as "used by generated code only".
170 Such methods/classes are subject to change.
171
172* Protobuf LITE runtime APIs are not stable yet. They are subject to change even
173 in minor version releases.
174
Feng Xiao7d550402018-04-12 17:58:55 -0700175## Documentation
temporal40ee5512008-07-10 02:12:20 +0000176
177The complete documentation for Protocol Buffers is available via the
178web at:
179
Feng Xiaoe4288622014-10-01 16:26:23 -0700180 https://developers.google.com/protocol-buffers/
Deanna Garciaf6508072021-04-15 16:39:18 +0000181
182## Kotlin Protocol Buffers
183
184Code to support more idiomatic Kotlin protocol buffers has been added to the
185repository, and Kotlin support will be launched in the next numbered release.