xref: /aosp_15_r20/external/protobuf/java/README.md (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1*1b3f573fSAndroid Build Coastguard Worker# Protocol Buffers - Google's data interchange format
2*1b3f573fSAndroid Build Coastguard Worker
3*1b3f573fSAndroid Build Coastguard WorkerCopyright 2008 Google Inc.
4*1b3f573fSAndroid Build Coastguard Worker
5*1b3f573fSAndroid Build Coastguard Workerhttps://developers.google.com/protocol-buffers/
6*1b3f573fSAndroid Build Coastguard Worker
7*1b3f573fSAndroid Build Coastguard Worker## Use Java Protocol Buffers
8*1b3f573fSAndroid Build Coastguard Worker
9*1b3f573fSAndroid Build Coastguard WorkerTo use protobuf in Java, first obtain the protocol compiler (a.k.a., protoc,
10*1b3f573fSAndroid Build Coastguard Workersee instructions in the toplevel [README.md](../README.md)) and use it to
11*1b3f573fSAndroid Build Coastguard Workergenerate Java code for your .proto files:
12*1b3f573fSAndroid Build Coastguard Worker
13*1b3f573fSAndroid Build Coastguard Worker    $ protoc --java_out=${OUTPUT_DIR} path/to/your/proto/file
14*1b3f573fSAndroid Build Coastguard Worker
15*1b3f573fSAndroid Build Coastguard WorkerInclude the generated Java files in your project and add a dependency on the
16*1b3f573fSAndroid Build Coastguard Workerprotobuf Java runtime.
17*1b3f573fSAndroid Build Coastguard Worker
18*1b3f573fSAndroid Build Coastguard Worker### Maven
19*1b3f573fSAndroid Build Coastguard Worker
20*1b3f573fSAndroid Build Coastguard WorkerIf you are using Maven, use the following:
21*1b3f573fSAndroid Build Coastguard Worker
22*1b3f573fSAndroid Build Coastguard Worker```xml
23*1b3f573fSAndroid Build Coastguard Worker<dependency>
24*1b3f573fSAndroid Build Coastguard Worker  <groupId>com.google.protobuf</groupId>
25*1b3f573fSAndroid Build Coastguard Worker  <artifactId>protobuf-java</artifactId>
26*1b3f573fSAndroid Build Coastguard Worker  <version>3.21.12</version>
27*1b3f573fSAndroid Build Coastguard Worker</dependency>
28*1b3f573fSAndroid Build Coastguard Worker```
29*1b3f573fSAndroid Build Coastguard Worker
30*1b3f573fSAndroid Build Coastguard WorkerMake sure the version number of the runtime matches (or is newer than) the
31*1b3f573fSAndroid Build Coastguard Workerversion number of the protoc.
32*1b3f573fSAndroid Build Coastguard Worker
33*1b3f573fSAndroid Build Coastguard WorkerIf you want to use features like protobuf JsonFormat, add a dependency on the
34*1b3f573fSAndroid Build Coastguard Workerprotobuf-java-util package:
35*1b3f573fSAndroid Build Coastguard Worker
36*1b3f573fSAndroid Build Coastguard Worker```xml
37*1b3f573fSAndroid Build Coastguard Worker<dependency>
38*1b3f573fSAndroid Build Coastguard Worker  <groupId>com.google.protobuf</groupId>
39*1b3f573fSAndroid Build Coastguard Worker  <artifactId>protobuf-java-util</artifactId>
40*1b3f573fSAndroid Build Coastguard Worker  <version>3.21.12</version>
41*1b3f573fSAndroid Build Coastguard Worker</dependency>
42*1b3f573fSAndroid Build Coastguard Worker```
43*1b3f573fSAndroid Build Coastguard Worker
44*1b3f573fSAndroid Build Coastguard Worker### Gradle
45*1b3f573fSAndroid Build Coastguard Worker
46*1b3f573fSAndroid Build Coastguard WorkerIf you are using Gradle, add the following to your `build.gradle` file's dependencies:
47*1b3f573fSAndroid Build Coastguard Worker```
48*1b3f573fSAndroid Build Coastguard Worker    implementation 'com.google.protobuf:protobuf-java:3.21.12'
49*1b3f573fSAndroid Build Coastguard Worker```
50*1b3f573fSAndroid Build Coastguard WorkerAgain, be sure to check that the version number matches (or is newer than) the version number of protoc that you are using.
51*1b3f573fSAndroid Build Coastguard Worker
52*1b3f573fSAndroid Build Coastguard Worker### Use Java Protocol Buffers on Android
53*1b3f573fSAndroid Build Coastguard Worker
54*1b3f573fSAndroid Build Coastguard WorkerFor Android users, it's recommended to use protobuf Java Lite runtime because
55*1b3f573fSAndroid Build Coastguard Workerof its smaller code size. Java Lite runtime also works better with Proguard
56*1b3f573fSAndroid Build Coastguard Workerbecause it doesn't rely on Java reflection and is optimized to allow as much
57*1b3f573fSAndroid Build Coastguard Workercode stripping as possible. You can following these [instructions to use Java
58*1b3f573fSAndroid Build Coastguard WorkerLite runtime](lite.md).
59*1b3f573fSAndroid Build Coastguard Worker
60*1b3f573fSAndroid Build Coastguard Worker### Use Java Protocol Buffers with Bazel
61*1b3f573fSAndroid Build Coastguard Worker
62*1b3f573fSAndroid Build Coastguard WorkerBazel has native build rules to work with protobuf. For Java, you can use the
63*1b3f573fSAndroid Build Coastguard Worker`java_proto_library` rule for server and the `java_lite_proto_library` rule
64*1b3f573fSAndroid Build Coastguard Workerfor Android. Check out [our build files examples](../examples/BUILD) to learn
65*1b3f573fSAndroid Build Coastguard Workerhow to use them.
66*1b3f573fSAndroid Build Coastguard Worker
67*1b3f573fSAndroid Build Coastguard Worker## Build from Source
68*1b3f573fSAndroid Build Coastguard Worker
69*1b3f573fSAndroid Build Coastguard WorkerMost users should follow the instructions above to use protobuf Java runtime.
70*1b3f573fSAndroid Build Coastguard WorkerIf you are contributing code to protobuf or want to use a protobuf version
71*1b3f573fSAndroid Build Coastguard Workerthat hasn't been officially released yet, you can follow the instructions
72*1b3f573fSAndroid Build Coastguard Workerbelow to build protobuf from source code.
73*1b3f573fSAndroid Build Coastguard Worker
74*1b3f573fSAndroid Build Coastguard Worker### Build from Source - With Maven
75*1b3f573fSAndroid Build Coastguard Worker
76*1b3f573fSAndroid Build Coastguard Worker1) Install Apache Maven if you don't have it:
77*1b3f573fSAndroid Build Coastguard Worker
78*1b3f573fSAndroid Build Coastguard Worker     http://maven.apache.org/
79*1b3f573fSAndroid Build Coastguard Worker
80*1b3f573fSAndroid Build Coastguard Worker2) Build the C++ code, or obtain a binary distribution of protoc (see
81*1b3f573fSAndroid Build Coastguard Worker   the toplevel [README.md](../README.md)). If you install a binary
82*1b3f573fSAndroid Build Coastguard Worker   distribution, make sure that it is the same version as this package.
83*1b3f573fSAndroid Build Coastguard Worker   If in doubt, run:
84*1b3f573fSAndroid Build Coastguard Worker
85*1b3f573fSAndroid Build Coastguard Worker     $ protoc --version
86*1b3f573fSAndroid Build Coastguard Worker
87*1b3f573fSAndroid Build Coastguard Worker   You will need to place the protoc executable in ../src.  (If you
88*1b3f573fSAndroid Build Coastguard Worker   built it yourself, it should already be there.)
89*1b3f573fSAndroid Build Coastguard Worker
90*1b3f573fSAndroid Build Coastguard Worker3) Run the tests:
91*1b3f573fSAndroid Build Coastguard Worker
92*1b3f573fSAndroid Build Coastguard Worker     $ mvn test
93*1b3f573fSAndroid Build Coastguard Worker
94*1b3f573fSAndroid Build Coastguard Worker   If some tests fail, this library may not work correctly on your
95*1b3f573fSAndroid Build Coastguard Worker   system.  Continue at your own risk.
96*1b3f573fSAndroid Build Coastguard Worker
97*1b3f573fSAndroid Build Coastguard Worker4) Install the library into your Maven repository:
98*1b3f573fSAndroid Build Coastguard Worker
99*1b3f573fSAndroid Build Coastguard Worker     $ mvn install
100*1b3f573fSAndroid Build Coastguard Worker
101*1b3f573fSAndroid Build Coastguard Worker5) If you do not use Maven to manage your own build, you can build a
102*1b3f573fSAndroid Build Coastguard Worker   .jar file to use:
103*1b3f573fSAndroid Build Coastguard Worker
104*1b3f573fSAndroid Build Coastguard Worker     $ mvn package
105*1b3f573fSAndroid Build Coastguard Worker
106*1b3f573fSAndroid Build Coastguard Worker   The .jar will be placed in the "target" directory.
107*1b3f573fSAndroid Build Coastguard Worker
108*1b3f573fSAndroid Build Coastguard WorkerThe above instructions will install 2 maven artifacts:
109*1b3f573fSAndroid Build Coastguard Worker
110*1b3f573fSAndroid Build Coastguard Worker  * protobuf-java: The core Java Protocol Buffers library. Most users only
111*1b3f573fSAndroid Build Coastguard Worker                   need this artifact.
112*1b3f573fSAndroid Build Coastguard Worker  * protobuf-java-util: Utilities to work with protos. It contains JSON support
113*1b3f573fSAndroid Build Coastguard Worker                        as well as utilities to work with proto3 well-known
114*1b3f573fSAndroid Build Coastguard Worker                        types.
115*1b3f573fSAndroid Build Coastguard Worker
116*1b3f573fSAndroid Build Coastguard Worker### Build from Source - Without Maven
117*1b3f573fSAndroid Build Coastguard Worker
118*1b3f573fSAndroid Build Coastguard WorkerIf you would rather not install Maven to build the library, you may
119*1b3f573fSAndroid Build Coastguard Workerfollow these instructions instead.  Note that these instructions skip
120*1b3f573fSAndroid Build Coastguard Workerrunning unit tests and only describes how to install the core protobuf
121*1b3f573fSAndroid Build Coastguard Workerlibrary (without the util package).
122*1b3f573fSAndroid Build Coastguard Worker
123*1b3f573fSAndroid Build Coastguard Worker1) Build the C++ code, or obtain a binary distribution of protoc.  If
124*1b3f573fSAndroid Build Coastguard Worker   you install a binary distribution, make sure that it is the same
125*1b3f573fSAndroid Build Coastguard Worker   version as this package.  If in doubt, run:
126*1b3f573fSAndroid Build Coastguard Worker
127*1b3f573fSAndroid Build Coastguard Worker     $ protoc --version
128*1b3f573fSAndroid Build Coastguard Worker
129*1b3f573fSAndroid Build Coastguard Worker   If you built the C++ code without installing, the compiler binary
130*1b3f573fSAndroid Build Coastguard Worker   should be located in ../src.
131*1b3f573fSAndroid Build Coastguard Worker
132*1b3f573fSAndroid Build Coastguard Worker2) Invoke protoc to build DescriptorProtos.java:
133*1b3f573fSAndroid Build Coastguard Worker
134*1b3f573fSAndroid Build Coastguard Worker     $ protoc --java_out=core/src/main/java -I../src \
135*1b3f573fSAndroid Build Coastguard Worker         ../src/google/protobuf/descriptor.proto
136*1b3f573fSAndroid Build Coastguard Worker
137*1b3f573fSAndroid Build Coastguard Worker3) Compile the code in core/src/main/java using whatever means you prefer.
138*1b3f573fSAndroid Build Coastguard Worker
139*1b3f573fSAndroid Build Coastguard Worker4) Install the classes wherever you prefer.
140*1b3f573fSAndroid Build Coastguard Worker
141*1b3f573fSAndroid Build Coastguard Worker## Compatibility Notice
142*1b3f573fSAndroid Build Coastguard Worker
143*1b3f573fSAndroid Build Coastguard Worker* Protobuf minor version releases are backwards-compatible. If your code
144*1b3f573fSAndroid Build Coastguard Worker  can build/run against the old version, it's expected to build/run against
145*1b3f573fSAndroid Build Coastguard Worker  the new version as well. Both binary compatibility and source compatibility
146*1b3f573fSAndroid Build Coastguard Worker  are guaranteed for minor version releases if the user follows the guideline
147*1b3f573fSAndroid Build Coastguard Worker  described in this section.
148*1b3f573fSAndroid Build Coastguard Worker
149*1b3f573fSAndroid Build Coastguard Worker* Protobuf major version releases may also be backwards-compatible with the
150*1b3f573fSAndroid Build Coastguard Worker  last release of the previous major version. See the release notice for more
151*1b3f573fSAndroid Build Coastguard Worker  details.
152*1b3f573fSAndroid Build Coastguard Worker
153*1b3f573fSAndroid Build Coastguard Worker* APIs marked with the @ExperimentalApi annotation are subject to change. They
154*1b3f573fSAndroid Build Coastguard Worker  can be modified in any way, or even removed, at any time. Don't use them if
155*1b3f573fSAndroid Build Coastguard Worker  compatibility is needed. If your code is a library itself (i.e. it is used on
156*1b3f573fSAndroid Build Coastguard Worker  the CLASSPATH of users outside your own control), you should not use
157*1b3f573fSAndroid Build Coastguard Worker  experimental APIs, unless you repackage them (e.g. using ProGuard).
158*1b3f573fSAndroid Build Coastguard Worker
159*1b3f573fSAndroid Build Coastguard Worker* Deprecated non-experimental APIs will be removed two years after the release
160*1b3f573fSAndroid Build Coastguard Worker  in which they are first deprecated. You must fix your references before this
161*1b3f573fSAndroid Build Coastguard Worker  time. If you don't, any manner of breakage could result (you are not
162*1b3f573fSAndroid Build Coastguard Worker  guaranteed a compilation error).
163*1b3f573fSAndroid Build Coastguard Worker
164*1b3f573fSAndroid Build Coastguard Worker* Protobuf message interfaces/classes are designed to be subclassed by protobuf
165*1b3f573fSAndroid Build Coastguard Worker  generated code only. Do not subclass these message interfaces/classes
166*1b3f573fSAndroid Build Coastguard Worker  yourself. We may add new methods to the message interfaces/classes which will
167*1b3f573fSAndroid Build Coastguard Worker  break your own subclasses.
168*1b3f573fSAndroid Build Coastguard Worker
169*1b3f573fSAndroid Build Coastguard Worker* Don't use any method/class that is marked as "used by generated code only".
170*1b3f573fSAndroid Build Coastguard Worker  Such methods/classes are subject to change.
171*1b3f573fSAndroid Build Coastguard Worker
172*1b3f573fSAndroid Build Coastguard Worker* Protobuf LITE runtime APIs are not stable yet. They are subject to change even
173*1b3f573fSAndroid Build Coastguard Worker  in minor version releases.
174*1b3f573fSAndroid Build Coastguard Worker
175*1b3f573fSAndroid Build Coastguard Worker## Documentation
176*1b3f573fSAndroid Build Coastguard Worker
177*1b3f573fSAndroid Build Coastguard WorkerThe complete documentation for Protocol Buffers is available via the
178*1b3f573fSAndroid Build Coastguard Workerweb at:
179*1b3f573fSAndroid Build Coastguard Worker
180*1b3f573fSAndroid Build Coastguard Worker  https://developers.google.com/protocol-buffers/
181*1b3f573fSAndroid Build Coastguard Worker
182*1b3f573fSAndroid Build Coastguard Worker## Kotlin Protocol Buffers
183*1b3f573fSAndroid Build Coastguard Worker
184*1b3f573fSAndroid Build Coastguard WorkerCode to support more idiomatic Kotlin protocol buffers has been added to the
185*1b3f573fSAndroid Build Coastguard Workerrepository, and Kotlin support will be launched in the next numbered release.
186