【问题标题】:Android minifyEnabled true for firebaseAndroid minifyEnabled true for firebase
【发布时间】:2018-11-13 13:27:50
【问题描述】:

我是 android 的初学者,我写过这样的代码。如果我启用 minifyEnabled = true,则该特定代码不会触发,我也不知道如何正确调试(我只能记录)。我该怎么办?

    DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("ConversationUser").child(FirebaseAuth.getInstance().getUid());
    database.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            ConversationUser conversationUser = dataSnapshot.getValue(ConversationUser.class);


            Log.e("Chat", conversationUser.toString());
            Log.e("Chat Status", conversationUser.getStatus());
            String status = conversationUser.getStatus();

            if (status != null && status.toLowerCase().equals("active")) {
                //TODO: this never trigger if minifyEnabled = true
                retrieveConversation(conversationUser.getId());
                EventBus.getDefault().post(new ChatDetailAdapter.ReceiveMessage());
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            Log.e("ChatHelper", "onChildChanged");

            ConversationUser conversationUser = dataSnapshot.getValue(ConversationUser.class);
            if (conversationUser.getStatus().toLowerCase().equals("delete")) {

                for(Iterator<Map.Entry<String, Conversation>> it = mainMessage.getConversations().entrySet().iterator(); it.hasNext(); ) {
                    Map.Entry<String, Conversation> entry = it.next();
                    if(entry.getKey().equals(conversationUser.getId())) {
                        it.remove();
                    }
                }

                sortConversation();
                EventBus.getDefault().post(new ChatDetailAdapter.ReceiveMessage());
            }
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            EventBus.getDefault().post(new ChatDetailAdapter.RemoveMessage());
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            EventBus.getDefault().post(new ChatDetailAdapter.ReceiveMessage());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

【问题讨论】:

  • 可能是您必须使用 proguard-rule.pro 来“保留”该类文件或库,您是否参考了有关 Proguard 的文档
  • 您能详细说明一下吗? @JayThummar。我在“ChatHelper”课上写过
  • minifyEnabled = true 用于启用 proguard 规则,该规则基本上用于为代码提供安全性并在启用时缩减资源。如果您搜索名为 proguard-rules.pro 的文件,该文件将用于 proguard-rule 因此当某些类文件库因此无法正常运行时,我们必须将该库从 proguard 中保留下来。您的问题有很多答案,以保持 firebase 库检查并重试

标签: android firebase


【解决方案1】:

您的模型可能被混淆了,这就是您的代码无法正常工作的原因。要修复它,您需要在 proguard 文件中保留存储所有 FirebaseModels 的文件夹。

# Firebase
-keep class com.myAppPackage.folderWhereYouSaveYourModels.** { *; }

另外你还需要添加几行,你可以查看文档:

在您的应用中使用 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.** {
  *;
}

希望对你有帮助!

【讨论】:

  • 好的,我更新了它,为您存储 FirebaseModels 的特定文件夹添加了一个额外的 keep
  • 这看起来是最准确的答案!
【解决方案2】:

打开你的proguard-rules.pro并添加

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

在您的应用中使用 Firebase 实时数据库和 ProGuard 时 你需要考虑你的模型对象将如何被序列化和 混淆后反序列化。

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

更多详情请见Proguard Rule

【讨论】:

  • @KhantThuLinn 好的。
  • 谢谢。现在好了。
【解决方案3】:

在这方面的文档不是很好。找到了唯一对我有用的解决方案here

您可以通过将 @keep 注释添加到这些类来“单独”排除 firebase 使用的 models

import androidx.annotation.Keep;

@Keep
public class User {
    public static String userId;
    ....
}

作为一项附加措施,我也对数据库调用执行相同的操作。不过不确定是否有必要。

【讨论】:

    猜你喜欢
    • 2017-10-16
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 2015-05-09
    相关资源
    最近更新 更多