2018-04-12 17:58:55 -07:00
|
|
|
# Protocol Buffers - Google's data interchange format
|
|
|
|
|
|
|
|
|
|
Copyright 2008 Google Inc.
|
|
|
|
|
|
2026-04-15 11:08:21 -07:00
|
|
|
## The Protobuf Java Lite Runtime
|
2018-04-12 17:58:55 -07:00
|
|
|
|
2026-04-15 11:08:21 -07:00
|
|
|
The Protobuf Java Lite runtime is a separate runtime designed to be used on
|
|
|
|
|
mobile clients (especially on Android).
|
2018-04-12 17:58:55 -07:00
|
|
|
|
2026-04-15 11:08:21 -07:00
|
|
|
Lite runtime should not be used on server-side, as the tradeoffs it makes are
|
|
|
|
|
not suited for that environment. In server contexts, the Full runtime should
|
|
|
|
|
always be used instead.
|
2018-04-12 17:58:55 -07:00
|
|
|
|
2026-04-15 11:08:21 -07:00
|
|
|
The design goals differ from the Full runtime, focused on small binary size and
|
|
|
|
|
lower peak memory usage. To achieve this it takes a number of trade-offs,
|
|
|
|
|
including:
|
2018-04-12 17:58:55 -07:00
|
|
|
|
2026-04-15 11:08:21 -07:00
|
|
|
* Offers a subset of features (including no reflection, ProtoJSON or TextProto
|
|
|
|
|
support).
|
|
|
|
|
|
|
|
|
|
* It may have slower runtime performance characteristics.
|
|
|
|
|
|
|
|
|
|
* It may be less hardened against certain issues that could be a concern on
|
|
|
|
|
server-side if they wouldn't be a concern in the context of mobile clients.
|
2018-04-12 17:58:55 -07:00
|
|
|
|
2026-04-15 11:08:21 -07:00
|
|
|
* In order to achieve maximum performance and code size, we do NOT guarantee
|
|
|
|
|
API/ABI stability for Java Lite.
|
2018-04-12 17:58:55 -07:00
|
|
|
|
2026-05-22 12:47:00 -07:00
|
|
|
* It relies on `sun.misc.Unsafe` for performance. Since `sun.misc.Unsafe` is
|
|
|
|
|
terminally deprecated on standard JVMs, the Lite runtime **cannot be used**
|
|
|
|
|
in any JVM environment where `sun.misc.Unsafe` is unavailable.
|
|
|
|
|
|
2026-04-15 11:08:21 -07:00
|
|
|
If these trade-offs are not acceptable for your use-case, use the full Java
|
|
|
|
|
runtime instead.
|
|
|
|
|
|
|
|
|
|
You can generate Java Lite code for your .proto files:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ protoc --java_out=lite:${OUTPUT_DIR} path/to/your/proto/file
|
|
|
|
|
```
|
2018-04-12 17:58:55 -07:00
|
|
|
|
2019-05-08 17:05:58 -07:00
|
|
|
Include the generated Java files in your project and add a dependency on the
|
2022-03-10 13:19:07 -05:00
|
|
|
protobuf Java Lite runtime. If you are using Maven, include the following:
|
2019-05-08 17:05:58 -07:00
|
|
|
|
|
|
|
|
```xml
|
|
|
|
|
<dependency>
|
|
|
|
|
<groupId>com.google.protobuf</groupId>
|
|
|
|
|
<artifactId>protobuf-javalite</artifactId>
|
2024-02-22 13:56:47 -08:00
|
|
|
<version><!--version--></version>
|
2019-05-08 17:05:58 -07:00
|
|
|
</dependency>
|
|
|
|
|
```
|
2019-04-09 15:07:44 -07:00
|
|
|
|
2024-02-22 13:56:47 -08:00
|
|
|
And **replace `<!--version-->` with a version from the
|
|
|
|
|
[Maven Protocol Buffers \[Lite\] Repository](https://mvnrepository.com/artifact/com.google.protobuf/protobuf-javalite).**
|
|
|
|
|
For example, `3.25.3`.
|
|
|
|
|
|
2020-08-05 13:27:51 +02:00
|
|
|
## R8 rule to make production app builds work
|
|
|
|
|
|
2026-04-15 11:08:21 -07:00
|
|
|
The Lite runtime internally uses reflection to avoid generating
|
|
|
|
|
hashCode/equals/parse/serialize methods. R8 by default obfuscates the field
|
|
|
|
|
names, which makes the reflection fail causing exceptions of the form
|
|
|
|
|
`java.lang.RuntimeException: Field {NAME}_ for {CLASS} not found. Known fields
|
|
|
|
|
are [ {FIELDS} ]` in MessageSchema.java.
|
2020-08-05 13:27:51 +02:00
|
|
|
|
2026-04-15 11:08:21 -07:00
|
|
|
See previous discussion about this on the
|
|
|
|
|
[protobuf Github project](https://github.com/protocolbuffers/protobuf/issues/6463)
|
|
|
|
|
and [R8](https://issuetracker.google.com/issues/144631039).
|
2020-08-05 13:27:51 +02:00
|
|
|
|
2026-04-15 11:08:21 -07:00
|
|
|
In some cases, you may need to create a `proguard-rules.pro` file to mitigate
|
|
|
|
|
the obfuscation breaking the reflective codepaths, for example:
|
2020-08-05 13:27:51 +02:00
|
|
|
|
|
|
|
|
```
|
|
|
|
|
-keep class * extends com.google.protobuf.GeneratedMessageLite { *; }
|
|
|
|
|
```
|