Android Kotlin firebase.database.DatabaseException: No properties to serialize found on class

你说的曾经没有我的故事 提交于 2020-05-24 07:59:08

问题


I have followed the advice of other SO solution: 1. Make my class Serializeable 2. Have a default constructor for Firebase 3. Update my proguard rules (Not sure about this one)

And still I get the above error, any ideas?

package org.sherman.android.stub_firebase.Models

import java.io.Serializable

/**
 * Created by fyi2 on 2/21/18.
 */
class User(displayName:String, email:String, photoUrl:String, userId:String) : Serializable {
    constructor(): this("","","","")



}

New Class Definition:

class User :Serializable {
    private var displayName:String=""
    private var email:String=""
    private  var photoUrl:String=""
    private var userId:String=""

    constructor() {}

    constructor(displayName: String, email: String, photoUrl: String, userId: String) {
        this.displayName = displayName
        this.email = email
        this.photoUrl = photoUrl
        this.userId = userId
    }

}

Proguard Rule:

-keepclassmembers class org.sherman.android.stub_firebase.Models.** { *; }

Code where it is called:

fun registerUserToFirebase(email:String, password:String){
    mAuth?.createUserWithEmailAndPassword(email, password)?.addOnCompleteListener(this){ task ->
        if (task.isSuccessful){
            mProgressDialog?.dismiss()
            var currentUserFB = task.getResult().user as FirebaseUser
            var user = User("Bobby", currentUserFB!!.email!!, "", currentUserFB!!.uid)
            mUsersDB!!.child(currentUserFB.uid).setValue(user)
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        } else {
            Helper.showAlertDialog("Error!",task.exception?.message!!,this)

        }
    }
}

ERROR Log snippet:

com.google.firebase.database.DatabaseException: No properties to serialize found on class org.sherman.android.stub_firebase.Models.User
                                                                                       at com.google.android.gms.internal.zzelx.<init>(Unknown Source:799)
                                                                                       at com.google.android.gms.internal.zzelw.zzf(Unknown Source:12)
                                                                                       at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:259)
                                                                                       at com.google.android.gms.internal.zzelw.zzbw(Unknown Source:0)
                                                                                       at com.google.firebase.database.DatabaseReference.zza(Unknown Source:10)
                                                                                       at com.google.firebase.database.DatabaseReference.setValue(Unknown Source:7)
                                                                                       at org.sherman.android.stub_firebase.Activities.RegisterActivity$registerUserToFirebase$1.onComplete(RegisterActivity.kt:59)

回答1:


Solved, even in Kotlin it seems the variables had to be explicitly Public.

import java.io.Serializable

/**
 * Created by fyi2 on 2/21/18.
 */
class User :Serializable {
    public var displayName:String=""
    public var email:String=""
    public  var photoUrl:String=""
    public var userId:String=""

    constructor() {}

    constructor(displayName: String, email: String, photoUrl: String, userId: String) {
        this.displayName = displayName
        this.email = email
        this.photoUrl = photoUrl
        this.userId = userId
    }

}

Once that was modified, the rest worked as is.




回答2:


If you got the issue on release apk then Do like this in your proguard file

    -keepattributes Signature
    -keepclassmembers class com.yourcompany.models.** {
      *;
    }


来源:https://stackoverflow.com/questions/48911506/android-kotlin-firebase-database-databaseexception-no-properties-to-serialize-f

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