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