Fix: [Android] Flipper Leakcanary plugin's OnHeapAnalyzedListener has been deprecated (#3923)
Summary: Flipper Leakcanary plugin's OnHeapAnalyzedListener has been deprecated. Flipper Leakcanary plugin's OnHeapAnalyzedListener has been deprecated change to EventListener implement. ## Changelog 1. add FlipperLeakEventListener.kt to implement EventListener. 2. update doc Pull Request resolved: https://github.com/facebook/flipper/pull/3923 Test Plan: Setup the Leakcanary plugin use FlipperLeakEventListener, test if it works. **Static Docs Preview: flipper** |[Full Site](https://our.intern.facebook.com/intern/staticdocs/eph/D39652084/V4/flipper/)| |**Modified Pages**| Reviewed By: antonk52 Differential Revision: D39652084 Pulled By: antonk52 fbshipit-source-id: 0afeb52dce6c1749a894a15dbb7921580c094ae6
This commit is contained in:
committed by
Facebook GitHub Bot
parent
67ff09563c
commit
5b794dc28f
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.flipper.plugins.leakcanary2
|
||||
|
||||
import com.facebook.flipper.android.AndroidFlipperClient
|
||||
import leakcanary.EventListener
|
||||
import shark.HeapAnalysis
|
||||
import shark.HeapAnalysisSuccess
|
||||
|
||||
class FlipperLeakEventListener : EventListener {
|
||||
private val leaks: MutableList<Leak> = mutableListOf()
|
||||
|
||||
override fun onEvent(event: EventListener.Event) {
|
||||
if (event is EventListener.Event.HeapAnalysisDone.HeapAnalysisSucceeded) {
|
||||
val heapAnalysis = event.heapAnalysis
|
||||
leaks.addAll(heapAnalysis.toLeakList())
|
||||
|
||||
AndroidFlipperClient.getInstanceIfInitialized()?.let { client ->
|
||||
(client.getPlugin(LeakCanary2FlipperPlugin.ID) as? LeakCanary2FlipperPlugin)?.reportLeaks(
|
||||
leaks)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun HeapAnalysis.toLeakList(): List<Leak> {
|
||||
return if (this is HeapAnalysisSuccess) {
|
||||
allLeaks
|
||||
.mapNotNull {
|
||||
if (it.leakTraces.isNotEmpty()) {
|
||||
it.leakTraces[0].toLeak(it.shortDescription)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
.toList()
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import leakcanary.OnHeapAnalyzedListener
|
||||
import shark.HeapAnalysis
|
||||
import shark.HeapAnalysisSuccess
|
||||
|
||||
@Deprecated("Use FlipperLeakEventListener add to LeakCanary.config.eventListeners instead")
|
||||
class FlipperLeakListener : OnHeapAnalyzedListener {
|
||||
private val leaks: MutableList<Leak> = mutableListOf()
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ dependencies {
|
||||
2. Update your the `onCreate` method in you `Application` to add the LeakCanary2 plugin to Flipper and the Flipper listener to LeakCanary:
|
||||
|
||||
```kt
|
||||
import com.facebook.flipper.plugins.leakcanary2.FlipperLeakListener
|
||||
import com.facebook.flipper.plugins.leakcanary2.FlipperLeakEventListener
|
||||
import com.facebook.flipper.plugins.leakcanary2.LeakCanary2FlipperPlugin
|
||||
|
||||
...
|
||||
@@ -27,9 +27,9 @@ import com.facebook.flipper.plugins.leakcanary2.LeakCanary2FlipperPlugin
|
||||
/*
|
||||
set the flipper listener in leak canary config
|
||||
*/
|
||||
LeakCanary.config = LeakCanary.config.copy(
|
||||
onHeapAnalyzedListener = FlipperLeakListener()
|
||||
)
|
||||
LeakCanary.config = LeakCanary.config.run {
|
||||
copy(eventListeners = eventListeners + FlipperLeakEventListener)
|
||||
}
|
||||
|
||||
SoLoader.init(this, false)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user