Android 版 Firebase 需要什么 ProGuard 配置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26273929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What ProGuard configuration do I need for Firebase on Android?
提问by caw
When using the FirebaseSDK for Android apps, I keep getting warnings and errors like these (in Eclipse):
在为 Android 应用程序使用FirebaseSDK 时,我不断收到如下警告和错误(在 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 成功编译版本?
回答by caw
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.** { *; }
if you are using custom objects in your Firebase, i.e. beans or POJOs.
无论如何,-keepnames class com.my.package.beans.** { *; }
如果您在 Firebase 中使用自定义对象,即 bean 或 POJO ,则必须添加。
Firebase SDK 1.0.18:
Firebase SDK 1.0.18:
-keepnames class com.firebase.** { *; }
-keepnames class com.shaded.fasterxml.Hymanson.** { *; }
-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.Hymanson.** { *; }
-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.Hymanson.** { *; }
-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.Hymanson.** { *; }
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.
欢迎任何官方指南。这些-dontwarn
指令显然是危险的,代码可能会在我没有测试过的地方中断。此外,上述规则非常宽松,其他规则可能会更好地优化您的 APK。
回答by MrAliB
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. 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:
在您的应用程序中使用 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.** {
*;
}
回答by user4989692
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
这不是真正的官方文档,但 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.Hymanson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.apache.**
-dontwarn org.w3c.dom.**
回答by Ugo Chirico
The configuration for firebase 2.5.2 seems changed. This is what is working for me:
firebase 2.5.2 的配置似乎发生了变化。这对我有用:
-keep class com.firebase.** { *; }
-keep class org.apache.** { *; }
-keepnames class com.shaded.fasterxml.** { *; }
-keepnames class com.fasterxml.Hymanson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.apache.**
-dontwarn org.w3c.dom.**
回答by Eliezer
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.**
回答by Peter
My working set for Firebase SDK 2.4.2:
我的Firebase SDK 2.4.2工作集:
-keep class com.firebase.** { *; }
-keepnames class com.fasterxml.Hymanson.** { *; }
-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
回答by Lee Hounshell
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.Hymanson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
回答by Masum
It solves my problem
它解决了我的问题
Add this to your proguard-rules file
将此添加到您的 proguard-rules 文件中
-optimizations !class/merging/*
回答by Kaiffi
If you are using Firebase Realtime Database, the model objects will be serialized and deserialized after obfuscationstrong text
如果您使用的是 Firebase 实时数据库,模型对象将在 obfuscationstrong 文本后进行序列化和反序列化
-keep class com.yourdevelopername.urappname.** { *; }
回答by Gastón Saillén
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.** {*;}