What ProGuard configuration do I need for Firebase on 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.
Configure Proguard-rules with Firebase
You should -keep
for classes that will be serialized/deserialized by firebase, otherwise it won't be able to use reflection on those classes. -keepclassmembers
will only keep the members and obfuscate the name of the class, which is something you definitely don't want to do if your libs use reflection. Try replacing -keepclassmembers
with -keep
for those classes.
-keep class persistence.** {
*;
}
What are the ProGuard rules for Firebase?
Firebase uses the consumerProguardFiles
feature to automatically include the appropriate ProGuard if you're using Gradle, meaning you don't need to manually include anything.
If you need to manually apply the proguard rules, you can extract the proguard.txt
file from each AAR file.
Firebase + Proguard/R8
Finally got it fixed, next proguard rules did a trick for me:
# Firebase
-keep class com.google.android.gms.** { *; }
-keep class com.google.firebase.** { *; } // especially this one
Android minifyEnabled true for firebase
Possibly your models are obfuscate and that's why your code is not working properly. To fix it, you will need to keep the folder where you store all your FirebaseModels
in your proguard files.
# Firebase
-keep class com.myAppPackage.folderWhereYouSaveYourModels.** { *; }
Also you will need to add a few lines more, you can check the documentation:
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.** {
*;
}
Hope that it will help you!
Which proguard configuration is missing for firebase messaging to work on Xamarin.Android?
The linker will sometimes remove code that you want to preserve.
You could use the Android.Runtime.Preserve attribute.
public class Example
{
[Android.Runtime.Preserve]
public Example ()
{
}
}
For more details, please check the link below.
https://docs.microsoft.com/en-us/xamarin/android/deploy-test/linker
Proguard configuration for Firebase-UI library
Solved this by moving the ViewHolder
classes that are used by the FirebaseRecyclerAdapter
to a dedicated package (e.g. com.mypackage.myapp.viewholders) and adding a rule within the proguard configuration to prevent that classes within this package become obfuscated by proguard:
-keep class com.mypackage.myapp.viewholders.** { *; }
Related Topics
Install Shows Error in Console: Install Failed Conflicting Provider
Build and Install Unsigned APK on Device Without the Development Server
Creating Temporary Files in Android
How to Handle Cookies in Httpurlconnection Using Cookiemanager
Initializing Games Client in Android
Is There a List of Screen Resolutions for All Android Based Phones and Tablets
Android Appcompat 21 Elevation
Detecting When Screen Is Locked
Upgraded to Sdk 2.3 - Now No Emulators Have Connectivity
Lock Screen Orientation (Android)
Android Get Image from Gallery into Imageview
How to Create Edittext with Rounded Corners
How to Keep/Exclude a Particular Package Path When Using Proguard
How to Capture an Image and Store It with the Native Android Camera
Adjustpan Not Preventing Keyboard from Covering Edittext
Resolved Versions for App (22.0.0) and Test App (21.0.3) Differ