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