When I generate the signed apk, save the wrong data to Firebase Database

风格不统一 提交于 2020-01-24 20:12:54

问题


When I run the usb debugging app, it saves the data correctly when I register the user:

But when I generate the signed apk, doing the same process saves it this way in Firebase Database:

What is happening? (i use android studio)


回答1:


It's because proguard removes (obfuscation) unused code and renames classes and class members' (variables and methods) names to shorter names. There are two ways to keep them as you want

OPTION 1. Add annotation before every field and between parentheses put what name you want to be displayed in Firebase.

Method A) Add annotation to public fields

public class Datum {
    @PropertyName("name")
    public String name;
}

Methods B) Add annotation to public setter/getters if your fields are private

public class Datum {

    private String name;

    @PropertyName("name")
    public String getName() {
        return name;
    }

    @PropertyName("name")
    public void setName(String name) {
        this.name = name;
    }
}

OPTION 2. Configure proguard file to keep class, field and method names as is.

Method A) Do it as per Firebase docs. Add following lines to your proguard file. Below lines mean names of every class member (field, constructor and method) in models package and in sub-package of models direcotry will be kept as-is.

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

Method B) Adding classes one-by-one

If you want to keep name and class members of User class then add this.

-keep class com.josiah.app.data.models.User{ *;}

Methods C) Add all classes in a package

Let's say all of your model classes are inside models package and you want to keep names of all classes and class members intact in that package. Then you have to add following line into your proguard file.

-keep class com.josiah.app.data.models.** { *;}

NOTE:

  • * means everything inside class (fieds, methods and contructors)
  • ** after package means everything in this package (subpackages and classes)


来源:https://stackoverflow.com/questions/57937299/when-i-generate-the-signed-apk-save-the-wrong-data-to-firebase-database

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