Android Studio Prevent Proguard Stripped Unused Code

Firestore's DocumentSnapshot.toObject - RuntimeException: Could not deserialize object. Class does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped

I am using Firestore's DocumentSnapshot.toObject, which complaint of the above error in release build.

The reason is minifyEnabled true being used, which stripped away unused code/constructor which is required by DocumentSnapshot.toObject.

To solve this issue, use @Keepon single classes/methods/fields that you don’t want removed or renamed by ProGuard.

class UserQuoteHistory(@JvmField @PropertyName("last_quote") var lastQuote: LastQuote? = null) {    @Keep    class LastQuote(        @JvmField @PropertyName("quote_id") var quoteId: String? = null,        @JvmField @PropertyName("timestamp") var timestamp: Timestamp? = null    ) {        // alternative solution        // @Keep        // constructor(): this(null, null)    }}

NOTE: Proguard seems to target stripping of inner class (LastQuote) only at the moment, thus @Keep is not required for UserQuoteHistory.


❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.