Skip to content

Instantly share code, notes, and snippets.

@MikeFot
Created November 16, 2018 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeFot/735c9e7adbaede041d91b716a97864c8 to your computer and use it in GitHub Desktop.
Save MikeFot/735c9e7adbaede041d91b716a97864c8 to your computer and use it in GitHub Desktop.
import com.crashlytics.android.Crashlytics
import com.squareup.leakcanary.AnalysisResult
import com.squareup.leakcanary.DisplayLeakService
import com.squareup.leakcanary.HeapDump
import io.fabric.sdk.android.Fabric
import timber.log.Timber
class LeakUploadService : DisplayLeakService() {
override fun afterDefaultHandling(
heapDump: HeapDump,
result: AnalysisResult,
leakInfo: String
) {
if (!result.leakFound || result.excludedLeak) {
return
}
if (result.leakFound) {
uploadLeakToServer(result)
}
}
private fun uploadLeakToServer(result: AnalysisResult) {
if (Fabric.isInitialized()) {
val exception = result.leakTraceAsFakeException()
Timber.d("Logging leak exception to Crashlytics ${exception.hashCode()}")
Crashlytics.logException(exception)
}
}
}
@MikeFot
Copy link
Author

MikeFot commented Nov 16, 2018

fun init(app: Application) {
LeakCanary.refWatcher(app)
.maxStoredHeapDumps(10)
.listenerServiceClass(LeakUploadService::class.java)
.buildAndInstall()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment