Firebase firestore variable name changed

瘦欲@ 提交于 2020-05-21 07:39:08

问题


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.

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);
            }
        });

回答1:


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
        proguardFiles getDefaultProguardFile('proguard-android.txt')
    }
}

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

Please check the docs for more informations:

  • https://firebase.google.com/docs/database/android/start#proguard



回答2:


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


来源:https://stackoverflow.com/questions/60719791/firebase-firestore-variable-name-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!