3

I am beginner in android and I have written code like this. If I enable minifyEnabled = true, that particular code is not triggering and I also don't know how to debug properly (I can only log) . How shall I do?

    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) {

        }
    });
Khant Thu Linn
  • 5,905
  • 7
  • 52
  • 120
  • may be you have to "keep" that class file or library using proguard-rule.pro did you refer the docs about Proguard – Jay Thummar Jun 04 '18 at 08:53
  • Can you elaborate a bit more on that? @JayThummar. I have written that in class "ChatHelper" – Khant Thu Linn Jun 04 '18 at 08:55
  • https://firebase.google.com/docs/database/android/start/ – IntelliJ Amiya Jun 04 '18 at 09:00
  • minifyEnabled = true is used for to enable proguard rule that is basically used to provide security to code and shrink the resources if you enable it . If you search for the file named as proguard-rules.pro this file is used for proguard-rule so when some library of class file not able to run properly because of this we have to keep that library from proguard . There are many answer of your question is given to keep firebase library check it and try again – Jay Thummar Jun 04 '18 at 09:03

3 Answers3

7

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!

Francisco Durdin Garcia
  • 12,540
  • 9
  • 53
  • 95
4

Open your proguard-rules.pro and Add

-keep class com.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.

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

For more details, see Proguard Rule.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

The documentation is not great in this regard. The only solution that worked for me was found here

You can "individually" exclude your models that firebase uses by adding the @keep annotation to those classes.

import androidx.annotation.Keep;

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

As an additional measure, I do the same for my Database calls as well. Not sure if that's necessary though.

Rami Alloush
  • 2,308
  • 2
  • 27
  • 33