From 543817295e05bcf226628ec26706e575741a0722 Mon Sep 17 00:00:00 2001 From: Mathijs Vogelzang Date: Wed, 5 Aug 2020 13:27:51 +0200 Subject: [PATCH] Add R8 rule to documentation Using default production Android build and protobuf-lite leads to RuntimeExceptions. The workarounds are documented in issues and I took those discussions and summarized them in the lite.md file. --- java/lite.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/java/lite.md b/java/lite.md index 5cbfbb1d5f..f248ff6c3e 100644 --- a/java/lite.md +++ b/java/lite.md @@ -34,6 +34,22 @@ protobuf Java runtime. If you are using Maven, use the following: ``` +## R8 rule to make production app builds work + +The Lite runtime internally uses reflection to avoid generating hashCode/equals/(de)serialization 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. + +There 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). + +Until the issues is resolved you need to add the following line to your `proguard-rules.pro` file inside your project: + +``` +-keep class * extends com.google.protobuf.GeneratedMessageLite { *; } +``` + +## Older versions + For the older version of Java Lite (v3.0.0), please refer to: https://github.com/protocolbuffers/protobuf/blob/javalite/java/lite.md