当我生成签名的APK时,将错误的数据保存到Firebase数据库 [英] When I generate the signed apk, save the wrong data to Firebase Database

查看:71
本文介绍了当我生成签名的APK时,将错误的数据保存到Firebase数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行usb调试应用程序时,它会在我注册用户时正确保存数据:

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

但是当我生成签名的apk时,执行相同的过程会将其保存在Firebase数据库中:

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

发生了什么事? (我使用android studio)

What is happening? (i use android studio)

推荐答案

这是因为proguard会删除(混淆)未使用的代码,并将类和类成员(变量和方法)的名称重命名为较短的名称.有两种方法可以根据需要保留它们

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

选项1..在每个字段之前和括号之间添加注释,以将要显示的名称显示在Firebase中.

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

方法A) 将注释添加到公共字段

Method A) Add annotation to public fields

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

方法B) 如果您的字段是私有的,则将注释添加到公共setter/getters

public class Datum {

    private String name;

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

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

选项2. 配置proguard文件以保持类,字段和方法的名称不变.

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

方法A) 按照Firebase 文档.将以下行添加到您的proguard文件中.下面几行表示models程序包和models目录中的子程序包中每个类成员的名称(字段,构造函数和方法)将保持不变.

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

方法B) 一对一添加类

Method B) Adding classes one-by-one

如果要保留User类的名称和类成员,则添加它.

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

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

方法C) 将所有类添加到包中

Methods C) Add all classes in a package

假设所有模型类都在models包中,并且您希望在该包中完整保留所有类和类成员的名称.然后,您必须将以下行添加到您的proguard文件中.

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

注意:

  • *表示类中的所有内容(杂项,方法和构造函数)
  • 软件包后的
  • **表示此软件包中的所有内容(子软件包和类)
  • * means everything inside class (fieds, methods and contructors)
  • ** after package means everything in this package (subpackages and classes)

这篇关于当我生成签名的APK时,将错误的数据保存到Firebase数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆