Feng Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 1 | # Protocol Buffers - Google's data interchange format |
jesse | cd04e9b | 2015-03-16 15:15:59 -0700 | [diff] [blame] | 2 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3 | Copyright 2008 Google Inc. |
| 4 | |
Feng Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 5 | https://developers.google.com/protocol-buffers/ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 6 | |
Feng Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 7 | ## Use Java Protocol Buffers |
temporal | cc93043 | 2008-07-21 20:28:30 +0000 | [diff] [blame] | 8 | |
Feng Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 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: |
| 12 | |
| 13 | $ protoc --java_out=${OUTPUT_DIR} path/to/your/proto/file |
| 14 | |
| 15 | Include the generated Java files in your project and add a dependency on the |
Kyle Krueger | 21ebeed | 2018-09-06 09:43:26 +0200 | [diff] [blame] | 16 | protobuf Java runtime. |
| 17 | |
| 18 | ### Maven |
| 19 | |
| 20 | If you are using Maven, use the following: |
Feng Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 21 | |
| 22 | ```xml |
| 23 | <dependency> |
| 24 | <groupId>com.google.protobuf</groupId> |
| 25 | <artifactId>protobuf-java</artifactId> |
Elliotte Rusty Harold | b6facc6 | 2021-03-01 10:01:35 -0500 | [diff] [blame] | 26 | <version>3.15.3</version> |
Feng Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 27 | </dependency> |
| 28 | ``` |
| 29 | |
| 30 | Make sure the version number of the runtime matches (or is newer than) the |
| 31 | version number of the protoc. |
| 32 | |
| 33 | If you want to use features like protobuf JsonFormat, add a dependency on the |
| 34 | protobuf-java-util package: |
| 35 | |
| 36 | ```xml |
| 37 | <dependency> |
| 38 | <groupId>com.google.protobuf</groupId> |
| 39 | <artifactId>protobuf-java-util</artifactId> |
Elliotte Rusty Harold | b6facc6 | 2021-03-01 10:01:35 -0500 | [diff] [blame] | 40 | <version>3.15.3</version> |
Feng Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 41 | </dependency> |
| 42 | ``` |
| 43 | |
Kyle Krueger | 21ebeed | 2018-09-06 09:43:26 +0200 | [diff] [blame] | 44 | ### Gradle |
| 45 | |
| 46 | If you are using Gradle, add the following to your `build.gradle` file's dependencies: |
| 47 | ``` |
Tobias Liese | 1db2b66 | 2021-04-08 10:48:22 +0200 | [diff] [blame] | 48 | implementation 'com.google.protobuf:protobuf-java:3.15.3' |
Kyle Krueger | 21ebeed | 2018-09-06 09:43:26 +0200 | [diff] [blame] | 49 | ``` |
Peter Newman | fc8e2e8 | 2020-07-18 04:12:54 +0100 | [diff] [blame] | 50 | Again, be sure to check that the version number matches (or is newer than) the version number of protoc that you are using. |
Kyle Krueger | 21ebeed | 2018-09-06 09:43:26 +0200 | [diff] [blame] | 51 | |
Feng Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 52 | ### Use Java Protocol Buffers on Android |
| 53 | |
| 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). |
| 59 | |
| 60 | ### Use Java Protocol Buffers with Bazel |
| 61 | |
| 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 |
| 65 | how to use them. |
| 66 | |
| 67 | ## Build from Source |
| 68 | |
| 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 |
Peter Newman | e2cc2de | 2020-08-10 19:08:25 +0100 | [diff] [blame] | 71 | that hasn't been officially released yet, you can follow the instructions |
Feng Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 72 | below to build protobuf from source code. |
| 73 | |
| 74 | ### Build from Source - With Maven |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 75 | |
| 76 | 1) Install Apache Maven if you don't have it: |
| 77 | |
| 78 | http://maven.apache.org/ |
| 79 | |
Feng Xiao | 09732c9 | 2016-05-11 16:22:30 -0700 | [diff] [blame] | 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. |
| 83 | If in doubt, run: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 84 | |
| 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 | |
| 90 | 3) 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 | |
| 97 | 4) Install the library into your Maven repository: |
| 98 | |
| 99 | $ mvn install |
| 100 | |
| 101 | 5) 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.com | 6c9f002 | 2009-08-05 19:55:17 +0000 | [diff] [blame] | 107 | |
Feng Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 108 | The above instructions will install 2 maven artifacts: |
gk5885 | 5ea590d | 2009-08-04 23:43:27 +0000 | [diff] [blame] | 109 | |
Feng Xiao | 09732c9 | 2016-05-11 16:22:30 -0700 | [diff] [blame] | 110 | * protobuf-java: The core Java Protocol Buffers library. Most users only |
| 111 | need this artifact. |
Feng Xiao | 09732c9 | 2016-05-11 16:22:30 -0700 | [diff] [blame] | 112 | * 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 115 | |
Feng Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 116 | ### Build from Source - Without Maven |
temporal | cc93043 | 2008-07-21 20:28:30 +0000 | [diff] [blame] | 117 | |
| 118 | If you would rather not install Maven to build the library, you may |
| 119 | follow these instructions instead. Note that these instructions skip |
Feng Xiao | 09732c9 | 2016-05-11 16:22:30 -0700 | [diff] [blame] | 120 | running unit tests and only describes how to install the core protobuf |
| 121 | library (without the util package). |
temporal | cc93043 | 2008-07-21 20:28:30 +0000 | [diff] [blame] | 122 | |
| 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: |
| 126 | |
| 127 | $ protoc --version |
| 128 | |
| 129 | If you built the C++ code without installing, the compiler binary |
| 130 | should be located in ../src. |
| 131 | |
| 132 | 2) Invoke protoc to build DescriptorProtos.java: |
| 133 | |
Feng Xiao | 09732c9 | 2016-05-11 16:22:30 -0700 | [diff] [blame] | 134 | $ protoc --java_out=core/src/main/java -I../src \ |
temporal | cc93043 | 2008-07-21 20:28:30 +0000 | [diff] [blame] | 135 | ../src/google/protobuf/descriptor.proto |
| 136 | |
Feng Xiao | 09732c9 | 2016-05-11 16:22:30 -0700 | [diff] [blame] | 137 | 3) Compile the code in core/src/main/java using whatever means you prefer. |
temporal | cc93043 | 2008-07-21 20:28:30 +0000 | [diff] [blame] | 138 | |
| 139 | 4) Install the classes wherever you prefer. |
| 140 | |
Feng Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 141 | ## Compatibility Notice |
Feng Xiao | 09732c9 | 2016-05-11 16:22:30 -0700 | [diff] [blame] | 142 | |
| 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äinen | 3808d09 | 2016-07-03 15:26:04 +0300 | [diff] [blame] | 145 | the new version as well. Both binary compatibility and source compatibility |
Feng Xiao | 09732c9 | 2016-05-11 16:22:30 -0700 | [diff] [blame] | 146 | are guaranteed for minor version releases if the user follows the guideline |
| 147 | described in this section. |
| 148 | |
Brian Wignall | a104dff | 2020-01-08 13:18:20 -0500 | [diff] [blame] | 149 | * Protobuf major version releases may also be backwards-compatible with the |
Feng Xiao | 09732c9 | 2016-05-11 16:22:30 -0700 | [diff] [blame] | 150 | 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äinen | 3808d09 | 2016-07-03 15:26:04 +0300 | [diff] [blame] | 155 | compatibility is needed. If your code is a library itself (i.e. it is used on |
Feng Xiao | 09732c9 | 2016-05-11 16:22:30 -0700 | [diff] [blame] | 156 | 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 Xiao | 7d55040 | 2018-04-12 17:58:55 -0700 | [diff] [blame] | 175 | ## Documentation |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 176 | |
| 177 | The complete documentation for Protocol Buffers is available via the |
| 178 | web at: |
| 179 | |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 180 | https://developers.google.com/protocol-buffers/ |
Deanna Garcia | f650807 | 2021-04-15 16:39:18 +0000 | [diff] [blame] | 181 | |
| 182 | ## Kotlin Protocol Buffers |
| 183 | |
| 184 | Code to support more idiomatic Kotlin protocol buffers has been added to the |
| 185 | repository, and Kotlin support will be launched in the next numbered release. |