What ProGuard configuration do I need for Firebase on Android?

46,124

Solution 1

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

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

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

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

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.**
Share:
46,124
caw
Author by

caw

Updated on April 07, 2021

Comments

  • caw
    caw about 3 years

    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?

  • caw
    caw over 9 years
    @JennyTong: Thanks! Any explanation for the changes? I can't find any org.apache.** or com.fasterxml.jackson.** classes in the SDK, only the shaded versions.
  • Riccardo Casatta
    Riccardo Casatta over 9 years
    I had to add -keepattributes Signature to let jackson work properly. Ref: stackoverflow.com/questions/28433281/…
  • caw
    caw over 9 years
    @RiccardoCasatta You're right, this is required. I just forgot to mention it because I have this attribute, anyway.
  • caw
    caw almost 9 years
    Thanks for this source! The differences to my configuration (see other answer) seems to be that they dropped the dontwarn rules for org.joda.time.**, org.shaded.apache.** and org.ietf.jgss.** and instead included one for org.apache.**.
  • user4989692
    user4989692 almost 9 years
    You're welcome. Just wanted to add it since it was from Firebase itself and also since it was slightly different from your personal tests.
  • caw
    caw over 8 years
    So it's just the last four entries that are new, right? Maybe a -dontwarn com.firebase.** is sufficient here?
  • Frank van Puffelen
    Frank van Puffelen over 8 years
    From our Slack channel: "you will need to add the following line if you're using FirebaseUI: -dontwarn com.firebase.ui.auth.**"
  • domenukk
    domenukk almost 8 years
    won't keep com.YOUR-APP_DOMAIN simply disable all of Proguard? This seems really wrong.
  • Tino
    Tino almost 8 years
    According to firebase you need instead: -keepclassmembers class com.yourcompany.models.** { *; } just the models you are using together with the real time database!
  • SalicBlu3
    SalicBlu3 over 7 years
    Could you provide a link to the documentation?
  • Billyjoker
    Billyjoker over 5 years
    You saved my day with "if you are using custom objects in your Firebase, i.e. beans or POJOs". My app was crashing on production cause not having this rule. Thanks!!
  • Kirill Karmazin
    Kirill Karmazin about 5 years
    If you use AdMob add -keep class com.google.android.gms.internal.** { *; } this will fix IllegalArgumentException: Stack size becomes negative after instruction [23] invokestatic #46 in [com/google/android/gms/internal/ads/zzaq.zzf(Ljava/lang/Str‌​ing;)J]
  • Nasib
    Nasib over 4 years
    i didn't know what BEAN/POJOS mean, lol googled it, Beans/Pojos are called model classes in java, i.e for encapsulating data. thought it might be helpful for others like me :D
  • AdamHurwitz
    AdamHurwitz about 4 years
    @SalicBlu3, here is the documentation: firebase.google.com/docs/database/android/start#proguard.
  • AdamHurwitz
    AdamHurwitz about 4 years
    Did the approach in the documentation not work for you @Kaiffi?
  • AdamHurwitz
    AdamHurwitz about 4 years
    Unfortunately, this did not resolve the Firebase issue for the Coinverse app.
  • AdamHurwitz
    AdamHurwitz about 4 years
    Unfortunately, this did not resolve the Firebase issue for the Coinverse app. You may see the full code here.
  • AdamHurwitz
    AdamHurwitz about 4 years
    Unfortunately, this did not resolve the Firebase issue for the Coinverse app. You may see the full code here.
  • AdamHurwitz
    AdamHurwitz about 4 years
    Unfortunately, this did not resolve the Firebase issue for the Coinverse app. You may see the full code here.
  • secretshardul
    secretshardul almost 4 years
    @Keep annotation can also be used to preserve data classes. Tested and works for Firebase: developer.android.com/guide/navigation/…
  • secretshardul
    secretshardul almost 4 years
    @Keep annotation can also be used to preserve data classes. Tested and works for Firebase: developer.android.com/guide/navigation/…
  • Rami Alloush
    Rami Alloush over 3 years
    WoW! you are a life saver with a very elegant solution! Thanks man :)
  • secretshardul
    secretshardul over 3 years
    Google could do better by updating its documentation.
  • Pedro Paulo Amorim
    Pedro Paulo Amorim over 3 years
    You still have to add -keep class com.google.firebase.** { *; } otherwise your app will not work on Samsung devices, please refer: stackoverflow.com/a/59883426/2430555
  • secretshardul
    secretshardul over 3 years
    Have tested on Samsung devices, didn't face any issue. We don't have this Proguard rule.
  • Mostafa Onaizan
    Mostafa Onaizan over 2 years
    this is what we all looking for.