What ProGuard configuration do I need for Firebase on Android?

AndroidFirebaseProguard

Android Problem Overview


When using the Firebase SDK for Android apps, I keep getting warnings and errors like these (in Eclipse):

Warning ... can't find referenced class ...
Warning: there were ... unresolved references to classes or interfaces ...
You may need to specify additional library jars (using '-libraryjars') ...

Unfortunately, Firebase doesn't have any official documentation about its use with ProGuard.

What directives do I need for my apps to successfully compile releases with Firebase when obfuscated with ProGuard?

Android Solutions


Solution 1 - Android

Based on my personal tests, it turned out something along these lines is necessary for Firebase-enhanced Android apps to compile with ProGuard.

In any case, you have to add -keepnames class com.my.package.beans.** { *; } if you are using custom objects in your Firebase, i.e. beans or POJOs.

Firebase SDK 1.0.18:

-keepnames class com.firebase.** { *; }
-keepnames class com.shaded.fasterxml.jackson.** { *; }
-keepnames class org.shaded.apache.** { *; }
-keepnames class javax.servlet.** { *; }
-dontwarn org.w3c.dom.**
-dontwarn org.joda.time.**
-dontwarn org.shaded.apache.commons.logging.impl.**

Firebase SDK 1.1.1:

-keep class com.firebase.** { *; }
-keep class org.shaded.apache.** { *; }
-keepnames class com.shaded.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.w3c.dom.**
-dontwarn org.joda.time.**
-dontwarn org.shaded.apache.**
-dontwarn org.ietf.jgss.**

Firebase SDK 2.0.0:

-keep class com.firebase.** { *; }
-keep class org.apache.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.w3c.dom.**
-dontwarn org.joda.time.**
-dontwarn org.shaded.apache.**
-dontwarn org.ietf.jgss.**

# Only necessary if you downloaded the SDK jar directly instead of from maven.
-keep class com.shaded.fasterxml.jackson.** { *; }

Last resort:

-keep class !com.my.package.** { *; }

Notes:

Any official guideline would be welcome. The -dontwarn directives are obviously dangerous, code may break at points that I have not tested. Furthermore, the above rules are quite permissive and other rules may better optimize your APKs.

Solution 2 - Android

I found this in Firebase documentations:

When using Firebase Realtime Database in your app along with ProGuard you need to consider how your model objects will be serialized and deserialized after obfuscation. If you use DataSnapshot.getValue(Class) or DatabaseReference.setValue(Object) to read and write data you will need to add rules to the proguard-rules.pro file:

# Add this global rule    
-keepattributes Signature

# This rule will properly ProGuard all the model classes in 
# the package com.yourcompany.models. Modify to fit the structure
# of your app.
-keepclassmembers class com.yourcompany.models.** {
*;
}

Solution 3 - Android

2021 answer

Use @Keep annotation before your data classes so they're retained by proguard. It's a part of AndroidX for both Java and Kotlin. Works for Firebase, Jetpack Navigator and Retrofit.

@Keep
data class Listing(
    val id: String = "",
    val name: String = ""
)

According to documentation: > Denotes that the annotated element should not be removed when the code is minified at build time. This is typically used on methods and classes that are accessed only via reflection so a compiler may think that the code is unused.

Solution 4 - Android

It's not really official documentation, but Firebase did show some basic proguard rules in one of their Github repositories. https://github.com/firebase/AndroidChat/blob/master/app/proguard-rules.pro

# Basic ProGuard rules for Firebase Android SDK 2.0.0+
-keep class com.firebase.** { *; }
-keep class org.apache.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.apache.**
-dontwarn org.w3c.dom.**

Solution 5 - Android

Following up on the other answers, using Firebase 2.4.1 I only had to include the following in my proguard config (YMMV):

-keep class com.firebase.** { *; }
-dontwarn com.fasterxml.**

Solution 6 - Android

The configuration for firebase 2.5.2 seems changed. This is what is working for me:

-keep class com.firebase.** { *; }
-keep class org.apache.** { *; }
-keepnames class com.shaded.fasterxml.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.apache.**
-dontwarn org.w3c.dom.**

Solution 7 - Android

My working set for Firebase SDK 2.4.2:

-keep class com.firebase.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.w3c.dom.**
-dontwarn org.joda.time.**
-dontwarn org.shaded.apache.**
-dontwarn org.ietf.jgss.**
-dontwarn com.firebase.**
-dontnote com.firebase.client.core.GaePlatform

Solution 8 - Android

I also struggled with this. Thanks to user4989692 and Ugo for pointing me the right direction.

Here is what worked for me:

build.gradle

    buildTypes {
    debug {
        minifyEnabled false
        shrinkResources false
        useProguard false
        debuggable true
        signingConfig signingConfigs.debug
    }
    release {
        minifyEnabled true
        shrinkResources true
        useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.release
    }
}

proguard-rules.pro

-dontwarn org.w3c.dom.**
-dontwarn org.joda.time.**
-dontwarn org.shaded.apache.**
-dontwarn org.ietf.jgss.**
-dontwarn com.firebase.**
-dontnote com.firebase.client.core.GaePlatform

-keepattributes Signature
-keepattributes *Annotation*
-keepattributes InnerClasses,EnclosingMethod

-keep class com.YOUR-APP-DOMAIN.** { *; }

# Basic ProGuard rules for Firebase Android SDK 2.0.0+
-keep class com.firebase.** { *; }

-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }

Solution 9 - Android

If you are using Firebase Realtime Database, the model objects will be serialized and deserialized after obfuscationstrong text

-keep class com.yourdevelopername.urappname.** { *; }

Solution 10 - Android

This is why when you do clean architecture is easy to fix, look at this scenario, if I had multiple firebase requests from multiple files in my app it would be a mess to try to keep single classes for firebase to work, so, if we have a modularized code and we store all our requests and data model inside a data layer it would be so much easy to keep just the classes that uses firebase instead of the whole project, doing this will be better to shrink more the apk size also

enter image description here

-keep class com.mypackage.data.** {*;}

Solution 11 - Android

It solves my problem

Add this to your proguard-rules file

-optimizations !class/merging/*

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncawView Question on Stackoverflow
Solution 1 - AndroidcawView Answer on Stackoverflow
Solution 2 - AndroidMrAliBView Answer on Stackoverflow
Solution 3 - AndroidsecretshardulView Answer on Stackoverflow
Solution 4 - Androiduser4989692View Answer on Stackoverflow
Solution 5 - AndroidEliezerView Answer on Stackoverflow
Solution 6 - AndroidUgo ChiricoView Answer on Stackoverflow
Solution 7 - AndroidPeterView Answer on Stackoverflow
Solution 8 - AndroidLee HounshellView Answer on Stackoverflow
Solution 9 - AndroidAzmat AliView Answer on Stackoverflow
Solution 10 - AndroidGastón SaillénView Answer on Stackoverflow
Solution 11 - AndroidMasumView Answer on Stackoverflow