Android上的Firebase需要哪些ProGuard配置?

caw*_*caw 39 android proguard firebase

使用适用于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') ...
Run Code Online (Sandbox Code Playgroud)

很遗憾,Firebase没有任何关于其与ProGuard一起使用的官方文档.

在使用ProGuard进行模糊处理时,我的应用程序需要哪些指令才能使用Firebase成功编译版本?

caw*_*caw 45

根据我的个人测试,结果表明,Firebase增强的Android应用程序需要使用ProGuard进行编译.

在任何情况下,-keepnames class com.my.package.beans.** { *; }如果您在Firebase中使用自定义对象(即bean或POJO),则必须添加.

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.**
Run Code Online (Sandbox Code Playgroud)

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.**
Run Code Online (Sandbox Code Playgroud)

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.** { *; }
Run Code Online (Sandbox Code Playgroud)

最后一招:

-keep class !com.my.package.** { *; }
Run Code Online (Sandbox Code Playgroud)

笔记:

欢迎任何官方指南.这些-dontwarn指令显然是危险的,代码可能会在我未测试过的点上破坏.此外,上述规则非常宽松,其他规则可能更好地优化您的APK.

  • 我不得不添加`-keepattributes Signature`让jackson正常工作.参考:http://stackoverflow.com/questions/28433281/how-to-include-typereference-proguard-rule (2认同)

MrA*_*liB 9

我在Firebase文档中找到了这个:

在应用程序中使用Firebase实时数据库以及ProGuard时,您需要考虑模糊对象在混淆后如何序列化和反序列化.如果使用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.** {
*;
}
Run Code Online (Sandbox Code Playgroud)


use*_*692 8

这不是真正的官方文档,但Firebase确实在他们的一个Github存储库中显示了一些基本的proguard规则.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.**
Run Code Online (Sandbox Code Playgroud)


rea*_*dul 8

2021 答案

@Keep在数据类之前使用注释,以便 proguard 保留它们。它是JavaKotlin的 AndroidX 的一部分。适用于 Firebase、Jetpack Navigator 和 Retrofit。

@Keep
data class Listing(
    val id: String = "",
    val name: String = ""
)
Run Code Online (Sandbox Code Playgroud)

根据文档:

表示在构建时缩小代码时不应删除带注释的元素。这通常用于仅通过反射访问的方法和类,因此编译器可能认为代码未使用。

  • 哇!你是一个救生员,有一个非常优雅的解决方案!谢啦 :) (2认同)