What ProGuard configuration do I need for Firebase on Android?
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 = ""
)
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.**
caw
Updated on April 07, 2021Comments
-
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 over 9 years@JennyTong: Thanks! Any explanation for the changes? I can't find any
org.apache.**
orcom.fasterxml.jackson.**
classes in the SDK, only theshaded
versions. -
Riccardo Casatta over 9 yearsI had to add
-keepattributes Signature
to let jackson work properly. Ref: stackoverflow.com/questions/28433281/… -
caw over 9 years@RiccardoCasatta You're right, this is required. I just forgot to mention it because I have this attribute, anyway.
-
caw almost 9 yearsThanks for this source! The differences to my configuration (see other answer) seems to be that they dropped the
dontwarn
rules fororg.joda.time.**
,org.shaded.apache.**
andorg.ietf.jgss.**
and instead included one fororg.apache.**
. -
user4989692 almost 9 yearsYou'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 over 8 yearsSo it's just the last four entries that are new, right? Maybe a
-dontwarn com.firebase.**
is sufficient here? -
Frank van Puffelen over 8 yearsFrom our Slack channel: "you will need to add the following line if you're using FirebaseUI:
-dontwarn com.firebase.ui.auth.**
" -
domenukk almost 8 yearswon't keep com.YOUR-APP_DOMAIN simply disable all of Proguard? This seems really wrong.
-
Tino almost 8 yearsAccording to firebase you need instead: -keepclassmembers class com.yourcompany.models.** { *; } just the models you are using together with the real time database!
-
SalicBlu3 over 7 yearsCould you provide a link to the documentation?
-
Billyjoker over 5 yearsYou 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 about 5 yearsIf 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/String;)J] -
Nasib over 4 yearsi 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 about 4 years@SalicBlu3, here is the documentation: firebase.google.com/docs/database/android/start#proguard.
-
AdamHurwitz about 4 yearsDid the approach in the documentation not work for you @Kaiffi?
-
AdamHurwitz about 4 yearsUnfortunately, this did not resolve the Firebase issue for the Coinverse app.
-
AdamHurwitz about 4 yearsUnfortunately, this did not resolve the Firebase issue for the Coinverse app. You may see the full code here.
-
AdamHurwitz about 4 yearsUnfortunately, this did not resolve the Firebase issue for the Coinverse app. You may see the full code here.
-
AdamHurwitz about 4 yearsUnfortunately, this did not resolve the Firebase issue for the Coinverse app. You may see the full code here.
-
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 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 over 3 yearsWoW! you are a life saver with a very elegant solution! Thanks man :)
-
secretshardul over 3 yearsGoogle could do better by updating its documentation.
-
Pedro Paulo Amorim over 3 yearsYou 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 over 3 yearsHave tested on Samsung devices, didn't face any issue. We don't have this Proguard rule.
-
Mostafa Onaizan over 2 yearsthis is what we all looking for.