在 zza.zza FirebaseAuthFallbackService 泄漏

Leak at zza.zza FirebaseAuthFallbackService

该应用程序运行完美,但在 LeakCanary 中,我每次关闭或暂停应用程序时都会出现泄漏。

我也尝试删除 authListener,但仍然存在泄漏。

我试过很多方法都没有成功,谁能帮帮我?

我的实现是:

implementation platform('com.google.firebase:firebase-bom:28.2.0')    
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.firebaseui:firebase-ui-auth:7.2.0'

我的截取代码是:

class MainActivity : AppCompatActivity() {

private lateinit var auth: FirebaseAuth
private lateinit var authListener: AuthStateListener

private val signInLauncher = registerForActivityResult(
    FirebaseAuthUIActivityResultContract()
) { res -> this.onSignInResult(res) }

override fun onCreate(savedInstanceState: Bundle?) {

    auth = Firebase.auth
    authListener = checkCurrentUser()
}

public override fun onStart() {
    super.onStart()
    auth.addAuthStateListener(authListener)
}

private fun checkCurrentUser() : AuthStateListener {

    return AuthStateListener {
        val user = it.currentUser

        if (user != null && !user.isAnonymous()) {
            // User is signed in

        } else {
            // No user is signed in
            signInAnonymously()
        }
    }
}

private fun signInAnonymously() {
    auth.signInAnonymously()
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                Log.d(AnonymousTAG, "signInAnonymously:success")
                //val user = auth.currentUser
            } else {
                // If sign in fails, display a message to the user.
                Log.w(AnonymousTAG, "signInAnonymously:failure", task.exception)
                
            }
        }
}

override fun onStop() {
    auth.removeAuthStateListener(authListener)
    super.onStop()
}

override fun onDestroy() {
    AppDatabase.distruggiInstance()
    super.onDestroy()
}

}

每当我关闭或暂停应用程序时,都会出现此漏洞:

┬───
│ GC Root: Global variable in native code
│
├─ com.google.firebase.auth.api.fallback.service.zza instance
│    Leaking: UNKNOWN
│    Retaining 1,9 kB in 10 objects
│    zza instance of com.google.firebase.auth.api.fallback.service.
│    FirebaseAuthFallbackService
│    ↓ zza.zza
│          ~~~
╰→ com.google.firebase.auth.api.fallback.service.FirebaseAuthFallbackService
​  instance
​     Leaking: YES (ObjectWatcher was watching this because com.google.firebase.
​     auth.api.fallback.service.FirebaseAuthFallbackService received
​     Service#onDestroy() callback and Service not held by ActivityThread)
​     Retaining 1,3 kB in 9 objects
​     key = b2e73ab0-d520-4fb8-b858-c9c7a9012f2d
​     watchDurationMillis = 5205
​     retainedDurationMillis = 202
​     mApplication instance of androidx.multidex.MultiDexApplication
​     mBase instance of android.app.ContextImpl

METADATA

Build.VERSION.SDK_INT: 30
Build.MANUFACTURER: Xiaomi
LeakCanary version: 2.7
App process name: it.xxxx.yyyyyyyyyy
Stats: LruCache[maxSize=3000,hits=2287,misses=64227,hitRate=3%]
RandomAccess[bytes=3141160,reads=64227,travel=29029887588,range=22210809,size=28
401420]
Heap dump reason: 1 retained objects, app is not visible
Analysis duration: 13350 ms```

编辑:2022 年 3 月 17 日

如 Github 问题 link 中所述(link 发布在下方),此问题已在 Firebase 身份验证库版本 21.0.2 中修复。


Firebase Android 客户端 SDK 导致内存泄漏,很多用户都遇到了这个问题。

这是 Github 存储库 firebase/firebase-android-sdk issue #2387 的内部错误。