简体   繁体   English

Firebase firestore 变量名称已更改

[英]Firebase firestore variable name changed

My firestore variables are saved by other names in cloud firestore.我的 firestore 变量以其他名称保存在 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.以下是屏幕截图,保存在 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);
            }
        });

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.您遇到此问题是因为您使用 Proguard 来确保安全性,它会打乱代码,因此在您创建应用程序的 APK 后其他人无法立即看到它。 This is also available in the case of Firebase.这也适用于 Firebase。 To solve this, please add the following lines of code in your build.gradle file:要解决此问题,请在build.gradle文件中添加以下代码行:

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:然后在项目的 Proguard 文件中添加以下规则,以通过将包名称替换为您自己的包名称来防止该行为:

-keepattributes Signature //Global rule

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

Please check the docs for more information:请查看文档以获取更多信息:

proguard renames fields and class names to shorter ones. proguard将字段和类名重命名为更短的名称。 To prevent this you can exclude this class from proguard optimizations with "-keep class com.package.MyClass" line in proguard-android.txt file.为了防止这种情况,您可以使用proguard-android.txt文件中的"-keep class com.package.MyClass"行将此类从 proguard 优化中排除。

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.因为现在您的类中的任何重构(例如重命名字段)都可能会破坏您的 Firestore 名称匹配。

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)...
  • 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: Go 在你的 proguard-rules.pro 文件中并粘贴:
# Add this global rule
    -keepattributes Signature
-keep class <Your class package here>.** { *; }

  • The <Your class package here> can be found inside your class file: <Your class package here>可以在 class 文件中找到:
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>注意com.elliottsoftware.calftracker.domain.models.fireBase ,这就是您要替换为<Your class package here>

  • so in the end your proguard-rules.pro file will look like this:所以最后你的 proguard-rules.pro 文件将如下所示:

# Add this global rule
    -keepattributes Signature

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM