xref: /aosp_15_r20/external/protobuf/java/lite.md (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1# Protocol Buffers - Google's data interchange format
2
3Copyright 2008 Google Inc.
4
5https://developers.google.com/protocol-buffers/
6
7## Use the Protobuf Java Lite Runtime
8
9The Protobuf Java Lite runtime is separated from the main Java runtime because
10it's designed and implemented with different constraints. In particular, the Java
11Lite runtime is much smaller which makes it more suitable to be used on Android.
12
13In order to achieve maximum performance and code size, we do
14NOT guarantee API/ABI stability for Java Lite. If this is not acceptable
15for your use-case, use the full Java runtime instead. Note that
16the latest version of Java Lite is not compatible with the 3.0.0 version.
17
18You can generate Java Lite code for your .proto files:
19
20    $ protoc --java_out=lite:${OUTPUT_DIR} path/to/your/proto/file
21
22The "optimize_for = LITE_RUNTIME" option in the .proto file no longer has any
23effect on Java code.
24
25Include the generated Java files in your project and add a dependency on the
26protobuf Java Lite runtime. If you are using Maven, include the following:
27
28```xml
29<dependency>
30  <groupId>com.google.protobuf</groupId>
31  <artifactId>protobuf-javalite</artifactId>
32  <version>3.21.12</version>
33</dependency>
34```
35
36## R8 rule to make production app builds work
37
38The Lite runtime internally uses reflection to avoid generating hashCode/equals/(de)serialization methods.
39R8 by default obfuscates the field names, which makes the reflection fail causing exceptions of the form
40`java.lang.RuntimeException: Field {NAME}_ for {CLASS} not found. Known fields are [ {FIELDS} ]` in MessageSchema.java.
41
42There are open issues for this on the [protobuf Github project](https://github.com/protocolbuffers/protobuf/issues/6463) and [R8](https://issuetracker.google.com/issues/144631039).
43
44Until the issues is resolved you need to add the following line to your `proguard-rules.pro` file inside your project:
45
46```
47-keep class * extends com.google.protobuf.GeneratedMessageLite { *; }
48```
49
50## Older versions
51
52For the older version of Java Lite (v3.0.0), please refer to:
53
54    https://github.com/protocolbuffers/protobuf/blob/javalite/java/lite.md
55