简体   繁体   English

Android 版 Firebase 需要什么 ProGuard 配置?

[英]What ProGuard configuration do I need for Firebase on Android?

When using the Firebase SDK for Android apps, I keep getting warnings and errors like these (in Eclipse):在为 Android 应用程序使用Firebase SDK 时,我不断收到如下警告和错误(在 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.不幸的是,Firebase 没有关于它与 ProGuard 一起使用的任何官方文档。

What directives do I need for my apps to successfully compile releases with Firebase when obfuscated with ProGuard?当使用 ProGuard 进行混淆时,我的应用程序需要哪些指令才能使用 Firebase 成功编译版本?

Based on my personal tests, it turned out something along these lines is necessary for Firebase-enhanced Android apps to compile with ProGuard.根据我的个人测试,事实证明,Firebase 增强的 Android 应用程序需要使用 ProGuard 进行编译。

In any case, you have to add -keepnames class com.my.package.beans.** { *; }无论如何,您必须添加-keepnames class com.my.package.beans.** { *; } -keepnames class com.my.package.beans.** { *; } if you are using custom objects in your Firebase, ie beans or POJOs. -keepnames class com.my.package.beans.** { *; }如果您使用的是你的火力地堡,即豆类或POJO的自定义对象。

Firebase SDK 1.0.18: 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: 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: 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. -dontwarn指令显然是危险的,代码可能会在我没有测试过的地方中断。 Furthermore, the above rules are quite permissive and other rules may better optimize your APKs.此外,上述规则非常宽松,其他规则可能会更好地优化您的 APK。

I found this in Firebase documentations:我在 Firebase 文档中找到了这个:

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.在您的应用程序中使用 Firebase 实时数据库和 ProGuard 时,您需要考虑在混淆后如何序列化和反序列化模型对象。 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:如果您使用 DataSnapshot.getValue(Class) 或 DatabaseReference.setValue(Object) 读取和写入数据,则需要向 proguard-rules.pro 文件添加规则:

# 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.** {
*;
}

It's not really official documentation, but Firebase did show some basic proguard rules in one of their Github repositories.这不是真正的官方文档,但 Firebase 确实在其 Github 存储库之一中显示了一些基本的 proguard 规则。 https://github.com/firebase/AndroidChat/blob/master/app/proguard-rules.pro 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.**

2021 answer 2021 答案

Use @Keep annotation before your data classes so they're retained by proguard.在数据类之前使用@Keep注释,以便 proguard 保留它们。 It's a part of AndroidX for both Java and Kotlin .它是JavaKotlin的 AndroidX 的一部分。 Works for Firebase, Jetpack Navigator and Retrofit.适用于 Firebase、Jetpack Navigator 和 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.这通常用于仅通过反射访问的方法和类,因此编译器可能认为代码未使用。

The configuration for firebase 2.5.2 seems changed. firebase 2.5.2 的配置似乎发生了变化。 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.**

Following up on the other answers, using Firebase 2.4.1 I only had to include the following in my proguard config (YMMV):跟进其他答案,使用 Firebase 2.4.1 我只需要在我的 proguard 配置(YMMV)中包含以下内容:

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

My working set for Firebase SDK 2.4.2 :我的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

I also struggled with this.我也为此苦苦挣扎。 Thanks to user4989692 and Ugo for pointing me the right direction.感谢 user4989692 和 Ugo 为我指明了正确的方向。

Here is what worked for me:这是对我有用的:

build.gradle构建.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 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.** { *; }

It solves my problem它解决了我的问题

Add this to your proguard-rules file将此添加到您的 proguard-rules 文件中

-optimizations !class/merging/*

If you are using Firebase Realtime Database, the model objects will be serialized and deserialized after obfuscation. 如果您使用的是Firebase Realtime数据库,则混淆后将对模型对象进行序列化和反序列化。 So you need to have this in your proguard-rules.pro : 因此,您需要在proguard-rules.pro

# 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.** {
  *;
}

More informations here : https://firebase.google.com/docs/database/android/start 此处提供更多信息: https : //firebase.google.com/docs/database/android/start

如果您使用的是 Firebase 实时数据库,模型对象将在 obfuscationstrong 文本后进行序列化和反序列化

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

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这就是为什么当你做干净的架构很容易修复时,看看这个场景,如果我的应用程序中有来自多个文件的多个 firebase 请求,那么尝试保持单个类让 firebase 工作将是一团糟,所以,如果我们有一个模块化的代码,我们将所有的请求和数据模型存储在一个数据层中,只保留使用 firebase 的类而不是整个项目会非常容易,这样做会更好地缩小 apk 的大小

在此处输入图片说明

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM