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