jvoaojvictor
jvoaojvictor

Reputation: 145

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

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

enter image description here

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

enter image description here

What is happening? (i use android studio)

Upvotes: 2

Views: 484

Answers (1)

Farid
Farid

Reputation: 2562

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)

Upvotes: 5

Related Questions