2

My firestore variables are saved by other names in cloud firestore. I think this is related to proguard, but I am not getting it.

Following are my username.

public String id;
public String name;
public String number;
public String profilePic;
public String shopPic;

and following is the screenshot, what is saved on firestore. sc

Here is some related code, which is very simple

        FirestoreUrls.get().getAccountsCollection()
                .document().set(binding.getUser()).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                hideProgressBar();
                handleFirebaseException(getClass(), task, true);
            }
        });
0

3 Answers 3

8

You are experiencing this problem because you are using Proguard for security, which shuffles the code so others cannot see it right after you create the app's APK. This is also available in the case of Firebase. To solve this, please add the following lines of code in your build.gradle file:

buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

Then add the following rules in your project's Proguard file to prevent that behavior by replacing the package name with your own package name:

-keepattributes Signature //Global rule

-keepclassmembers class com.example.YourModelClass.** {
  *;
}

Please check the docs for more information:

10
  • I checked this configuration, it is still same Mar 17, 2020 at 10:16
  • Maybe this can help you a little bit.
    – Alex Mamo
    Mar 17, 2020 at 10:21
  • Not yet Alex Mamo Mar 18, 2020 at 10:12
  • Hi @AlexMamo I'm facing the same problem and This solve my problem (Thanks) but the version of my app on google play still changing names. Should recreate a new APK and upload it to google play to solve the problem?
    – Amr Jyniat
    Jun 15, 2020 at 9:33
  • 1
    @CodeREDInnovations try to remove the point after YourModalClass
    – Amr Jyniat
    Jun 16, 2020 at 4:53
3

proguard renames fields and class names to shorter ones. To prevent this you can exclude this class from proguard optimizations with "-keep class com.package.MyClass" line in proguard-android.txt file.

But IMHO instead of this you should somewhere in your code map your variables to proper strings and then send strings to cloud firestore. Because for now any refactoring in your class (renaming fields for example) may broke your firestore name matching.

UPDATE:

It looks like you can map object fileds to proper strings this way:

User user = binding.getUser()

Map<String, Object> docData = new HashMap<>();
docData.put("id", user.id);
docData.put("name", user.name);
docData.put("number", user.number);
docData.put("profilePic", user.profilePic);
docData.put("shopPic", user.shopPic);

FirestoreUrls.get().getAccountsCollection()
                .document().set(docData)...
2
  • But proguard works for release version, I am currently working on debug apk. Mar 17, 2020 at 10:19
  • Yes, this is a way Mar 18, 2020 at 10:04
1
  • Ok, so I had the exact same problem and this is what I did to solve it.

Solution:

  • Go inside you your proguard-rules.pro file and paste this:
# Add this global rule
    -keepattributes Signature
-keep class <Your class package here>.** { *; }

  • The <Your class package here> can be found inside your class file:
package com.elliottsoftware.calftracker.domain.models.fireBase


//TODO THE GETTERS AND SETTERS USED BY FIREBASE ARE CASE SENSITIVE
data class FireBaseCalf(var calftag: String? = null,
                        var cowtag:String? = null,
                        var ccianumber: String? = null,
                        var sex:String? = null,
                        var details:String?=null,
                        var date: Date? = null,
                        var birthweight:String? = null,
                        var id: String? = null,

                        ) {
    // Null default values create a no-argument default constructor, which is needed
    // for deserialization from a DataSnapshot.
}

  • notice the com.elliottsoftware.calftracker.domain.models.fireBase, that is what you are going to replace with <Your class package here>

  • so in the end your proguard-rules.pro file will look like this:

# Add this global rule
    -keepattributes Signature

    -keep class com.elliottsoftware.calftracker.domain.models.fireBase.** { *; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.