Firebase - No properties to serialize found on class

npk :

I have a class like this:

class dataModel {
    String id, name;
    Integer count;

    dataModel() {}
}

And I add data from Firebase.

mDatabase.addValueEventListener(mListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            dataSet.add(dataSnapshot.getValue(dataModel.class));
            //...
        }
});

When I run the app as debug, there is no problem. But after it is released, app crashes with the error:

com.google.firebase.database.DatabaseException: No properties to serialize found on class com.my.package.dataModel

I have minifyEnabled true

Alex Mamo :

To solve this, your class needs to implement the Serializable interface:

class dataModel implements Serializable {}

You also need to add a contructor with three arguments. Your class should look like this:

class dataModel implements Serializable {
    private String id, name;
    private Integer count;
    @ServerTimestamp
    private Date time;

    dataModel() {}

    dataModel(String id, String name, Integer count) {
        this.id = id;
        this.name = name;
        this.count = count;
    }
}

If the date is null, it will have the server-generated timestamp. So you don't need to do set the value for it. Please also see the annotation used to mark a Date field to be populated with a server timestamp.

Also very important, don't forget to add public getters.

and the other requirement will be to add -keepclassmembers class com.yourcompany.models.** { *; } in proguard-rules.pro.

As said here.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=471800&siteId=1